3749 Commits

Author SHA1 Message Date
Emilia Kond
fa6025e25b
[clang-format] Don't confuse initializer equal signs in for loops (#77712)
clang-format has logic to align declarations of multiple variables of
the same type, aligning them at the equals sign. This logic is applied
in for loops as well. However, this alignment logic also erroneously
affected the equals signs of designated initializers.

This patch forbids alignment if the token 2 tokens back from the equals
sign is a designated initializer period.

Fixes https://github.com/llvm/llvm-project/issues/73902
2024-01-22 14:57:37 +02:00
Hirofumi Nakamura
df4ba00c7b
[clang-format] Support of TableGen statements in unwrapped line parser (#78846)
Make TableGen's statements to be parsed considering their structure.
- Avoid to parse label
- Avoid class from being parsed as c++'s class
- Support if statement of the form `if <cond> then { ... }` 
- Support defset statement of the form `defset <type> <name> {}`

---------

Co-authored-by: Björn Schäpers <github@hazardy.de>
2024-01-22 21:35:01 +09:00
XDeme
a464e05109
[clang-format] Handle templated elaborated type specifier in function… (#77013)
… return type.

The behavior now is consistent with the non template version.
Enabled and updated old test.
2024-01-20 14:47:58 -08:00
XDeme
2fc2ee136c
[clang-format] Fix poor spacing in AlignArrayOfStructures: Left (#77868)
Fixes llvm/llvm-project#62904

`AlignArrayOfStructures: Left` combined with `SpacesInParentheses: true`
causes the first cell of every row to have 1 additional space.
We were only setting the first cell of the first row to be against the
left brace, now every row will be against the left brace.
2024-01-20 13:34:37 -08:00
Hirofumi Nakamura
fcb6737f82
[clang-format] Support of TableGen identifiers beginning with a number. (#78571)
TableGen allows the identifiers beginning with a number.
This patch add the support of the recognition of such identifiers.
2024-01-20 21:15:58 +09:00
Owen Pan
a7d7da6e45
[clang-format] Add SkipMacroDefinitionBody option (#78682)
Closes #67991.

See also: #70338

Co-authored-by: @tomekpaszek
2024-01-20 00:08:23 -08:00
Hirofumi Nakamura
e3702f6225
[clang-format] TableGen multi line string support. (#78032)
Support the handling of TableGen's multiline string (code) literal.
That has the form, 
[{ this is the string possibly with multi line... }]
2024-01-17 21:20:35 +09:00
Ilya Biryukov
5723fce088
[Format] Fix isStartOfName to recognize attributes (#76804)
This addresses a problem with formatting attributes. Some context:
- eaff083035c8 changed `isStartOfName` to fix problems inside
`#pragma`s, but this behavior changed formatting of attribute macros in
an undesirable way.
- efeb546865c233dfa7706ee0316c676de9f69897 changed Google format style
to fix some widely used attributes.

Instead of changing the format style, this commit specializes behavior
introduced in eaff083035c8 to `#pragma`s. This seems to work well in
both cases.

Also update the test with two `GUARDED_BY` directives. While the
formatting after efeb546865c233dfa7706ee0316c676de9f69897 seems better,
this case is rare enough to not warrant the extra complexity. We are
reverting it back to the state it had before
efeb546865c233dfa7706ee0316c676de9f69897.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-01-15 14:57:05 +01:00
sstwcw
e3acfbc471
[clang-format] Stop aligning the to continuation lines (#76378)
Some unwrapped lines are marked as continuations of the previous lines,
for example the ports in a Verilog module header. Previously, if the
first line following the ports line was changed, and git-clang-format
was run, the changed line would be indented by an extra continuation
indentation.
2024-01-15 03:04:42 +00:00
rmarker
60a9874f54
[clang-format] Add PenaltyBreakScopeResolution option. (#78015)
Resolves #78014
2024-01-14 20:55:44 +01:00
XDeme
97a9dbb649
[clang-format] Handle possible crash in getCells (#77723)
Done as requested in llvm/llvm-project#77045

I have changed the test a bit, because since the root problem was fixed,
the original test would possibly never crash.
2024-01-12 10:20:44 -08:00
MyDeveloperDay
c65b939fb7
[clang-format] SpacesInSquareBrackets not working for Java (#77833)
spaces in [] needs to be handled the same in Java the same as C#.

Co-authored-by: paul_hoad <paul_hoad@amat.com>
2024-01-12 08:30:53 +00:00
Emilia Kond
791637e782
[clang-format] Don't allow casts in front of ampamp (#77704)
clang-format performs a heuristic to see if a right parenthesis is the
parenthesis of a cast expression. This check never looked ahead to see
if the next token is an ampamp && token. This resulted in the paren
being set as an CastRParen, and the following ampamp got annotated as an
UnaryOperator!

Since && can never be a unary operator is standard C++, this patch
forbids the right paren from ever becoming a cast.

Fixes https://github.com/llvm/llvm-project/issues/77680
2024-01-12 03:47:37 +02:00
Hirofumi Nakamura
0cc31579e0
[clang-format] TableGen keywords support. (#77477)
Add TableGen keywords to the additional keyword list of the formatter.

This pull request is the splited part from
https://github.com/llvm/llvm-project/pull/76059 .
2024-01-11 20:07:49 +01:00
r4nt
b7770befee
[ClangFormat] Fix formatting bugs. (#76245)
1. There are multiple calls to addFakeParenthesis; move the guard to not
   assign fake parenthesis into the function to make sure we cover all
   calls.
2. MustBreakBefore can be set on a token in two cases: either during
   unwrapped line parsing, or later, during token annotation. We must
   keep the latter, but reset the former.
3. Added a test to document that the intended behavior of preferring not
   to break between a return type and a function identifier.
   For example, with MOCK_METHOD(r, n, a)=r n a, the code
   MOCK_METHOD(void, f, (int a, int b)) should prefer the same breaks as
   the expanded void f(int a, int b).
2024-01-11 13:28:45 +01:00
XDeme
093e6bdd4b
[clang-format] Fix crash involving array designators (#77045)
Fixes llvm/llvm-project#76716
Fixes parsing of `[0]{}`. Before this patch it was begin parsed as a
lambda, now it is correctly parsed as a designator initializer.
2024-01-10 19:46:11 -08:00
Gedare Bloom
b2c0c6f3f2
[clang-format]: Split alignment of declarations around assignment (#69340)
Function pointers are detected as a type of declaration using
FunctionTypeLParen. They are aligned based on rules for
AlignConsecutiveDeclarations. When a function pointer is on the
right-hand side of an assignment, the alignment of the function pointer
can result in excessive whitespace padding due to the ordering of
alignment, as the alignment processes a line from left-to-right and
first aligns the declarations before and after the assignment operator,
and then aligns the assignment operator. Injection of whitespace by
alignment of declarations after the equal sign followed by alignment of
the equal sign results in the excessive whitespace.

Fixes #68079.
2024-01-10 19:35:03 -08:00
James Grant
cc77e33271
[clang-format] Don't apply severe penalty if no possible column formats (#76675)
If there are possible column formats, but they weren't selected because
they don't fit within remaining characters for the current path then
applying severe penalty to induce column layout by selection of a
different path seems fair.

But if due to style configuration or what the input code is, there are
no possible column formats, different paths aren't going to have column
layouts. Seems wrong to apply the severe penalty to induce column
layouts if there are none available.

It just causes selection of sub-optimal paths, e.g. get bad formatting
when brace initializers are used inside lambda bodies.

Fixes #56350
2024-01-10 19:32:14 -08:00
Owen Pan
c69ec700ad [clang-format][NFC] Don't use clang-format style in config files
The current CI doesn't use the latest clang-format and fails most
clang-format patches on the code formatting check. This patch
temporarily removes the clang-format style from the .clang-format
files.
2024-01-10 00:58:35 -08:00
kadir çetinkaya
27f547968c
[clang-format] Break after string literals with trailing line breaks (#76795)
This restores a subset of functionality that was forego in
d68826dfbd987377ef6771d40c1d984f09ee3b9e.

Streaming multiple string literals is rare enough in practice, hence
that change makes sense in general. But it seems people were
incidentally relying on this for having line breaks after string
literals that ended with `\n`.

This patch tries to restore that behavior to prevent regressions in the
upcoming LLVM release, until we can implement some configuration based
approach as proposed in https://github.com/llvm/llvm-project/pull/69859.
2024-01-08 11:11:02 +01:00
Owen Pan
ca8441d6db [clang-format][NFC] Fix a typo. 2023-12-29 19:47:08 -08:00
XDeme
41ef6fc54f
[clang-format] Fix bad indentation with attribute and templated type (#76336)
Fixes llvm/llvm-project#76314
2023-12-29 21:27:53 +01:00
Owen Pan
8f9803b5ab
[clang-format] Add an fnmatch-like function for .clang-format-ignore (#76021)
This is needed because Windows doesn't have anything equivalent to the
POSIX fnmatch() function.
2023-12-24 01:05:10 -08:00
XDeme
8097a5d37b
[clang-format] Fix operator overload inconsistency in BreakAfterAttributes: Always (#74943)
Fixes llvm/llvm-project#74901
2023-12-22 23:02:47 -08:00
Owen Pan
f8f8926054
[clang-format] Fix a bug in annotating function declaration names (#76206)
Annotates function declaration names having unnamed parameters.
2023-12-22 22:51:00 -08:00
Ilya Biryukov
efeb546865
[clang-format] Add common attribute macros to Google style (#76239)
We have found that 199fc973ced20016b04ba540cf63a1d4914fa513 regresses
formatting of our codebases because we do not properly configure the
names of attribute macros.

`GUARDED_BY` and `ABSL_GUARDED_BY` are very commoon in Google codebases
so it is reasonable to include them by default to avoid the need for
extra configuration in every Google repository.
2023-12-22 15:07:43 +01:00
Ilya Biryukov
d03beb9419
[clang-format] Do not break on JS fields like on goto labels (#76233)
This regressions was introduced in
70d7ea0cebcf363cd0ddcfb76375fb5fada87dd5.
The commit moved some code and correctly picked up an explicit check for
not running on Verilog.
However, the moved code also never ran for JavaScript and after the
commit we run it there and
this causes the wrong formatting of:

```js
export type Params = Config&{
  columns: Column[];
};
```
into
```js
export type Params = Config&{
columns:
  Column[];
};
```
2023-12-22 14:41:38 +01:00
Owen Pan
401f0396c3
[clang-format] Fix a bug in IndentExternBlock: NoIndent (#75731)
Fixes #36620.
Fixes #75719.
2023-12-17 15:07:11 -08:00
XDeme
9512d6d213
[clang-format] Fix parsing of operator<() {} (#75144)
Fixes #74876.

During the parsing of `operator<(Foo&) {}`, there was no handling for
the operator<, so it called `consumeToken()` again, causing the
`AnnotationParser::Scopes` to have one additional left brace each time
it tried to parse it, leaving it unbalanced.
Because of this, in the following code:
```cpp
class Foo {
  void operator<(Foo&) {}
  Foo& f;
};
```
The `&` in the reference member, was being interpreted as
`TT_BinaryOperator` instead of `TT_PointerOrReference`.
2023-12-13 11:57:56 -08:00
Kazu Hirata
f3dcc2351c
[clang] Use StringRef::{starts,ends}_with (NFC) (#75149)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-13 08:54:13 -08:00
Owen Pan
3791b3fca6
[clang-format][NFC] Clean up the driver and getStyle() in Format.cpp (#74794) 2023-12-08 15:23:01 -08:00
Owen Pan
b683709ea6 [clang-format] Fix a possible crash in AlignAfterOpenBracket: BlockIndent 2023-12-06 17:40:41 -08:00
Owen Pan
e1a4b0032f
[clang-format] Handle merging functions containing only a block comment (#74651)
Fixed #41854.
2023-12-06 16:56:22 -08:00
Owen Pan
1241b5b05b
[clang-format][NFC] Refactor getting first/last non-comment of line (#74570) 2023-12-06 11:45:41 -08:00
Owen Pan
924f6ca1bd
[clang-format] Remove duplicates in @property using std::set (#74235)
Re-implement ObjCPropertyAttributeOrder using std::set for sorting and
removing duplicates. (We can't use llvm::SmallSet because it's
unordered.)
2023-12-04 16:33:20 -08:00
sstwcw
b3e80d8ed2
[clang-format] Add space in Verilog tagged unions (#71354)
In a tagged union expression, there should be a space between the field
name and the data. Previously, the tag could be recognized as part of a
dotted identifier or a struct literal, and the space would be omitted.
2023-12-02 19:26:07 +00:00
Jared Grubb
c45a66ecd4 [clang-format] ObjCPropertyAttributeOrder to sort ObjC property attributes
Add a style option to specify the order that property attributes should
appear in ObjC property declarations (property attributes are things like
`nonatomic, strong, nullable`).

Closes #71323.

Differential Revision: https://reviews.llvm.org/D150083
2023-12-01 17:41:30 -08:00
Owen Pan
5c60e2ce78 [clang-format][NFC] Reformat source code with clang-format style 2023-11-30 20:19:30 -08:00
Emilia Kond
a112921d88
[clang-format] Don't skip stringizing when determining brace kind (#73886)
PR #69473 introduced skipping PP directives when determining the brace
kind of an lbrace. However, it did so by skipping to the end of the line
when encountering a hash character. This means it also skipped to the
end of line when encountering a macro stringizing operator, which,
unlike PP directives, don't have effect until the end of line.

This led to cases where the rbrace could be completely skipped if it was
on the same line as a stringizing operator.

This patch skips hash characters if we're already in a PP directive, as
you can't define a macro inside of a macro

Fixes https://github.com/llvm/llvm-project/issues/72662
2023-11-30 21:26:39 +02:00
Owen Pan
bbae59ae71
[clang-format] Finalize children after formatting them (#73753)
This would also fix the overlapping replacements below:
```
$ clang-format
 a(
 #else
 #endif
) = []() {      
)}
The new replacement overlaps with an existing replacement.
New replacement: <stdin>: 38:+7:"
"
Existing replacement: <stdin>: 38:+7:" "
```
Fixed #73487.
2023-11-29 12:56:05 -08:00
Owen Pan
4c17452076
[clang-format][NFC] Extend isProto() to also cover LK_TextProto (#73582) 2023-11-29 12:52:01 -08:00
sstwcw
9fa2d74be4
[clang-format] Indent Verilog case statements with comments (#71353)
If a line contains a comment outside of (fake) parentheses, the part
following it is indented according to `CurrentState.Indent`. A Verilog
case label and the statement that follows are broken with
mustBreakBefore. So the part that follows the case label needs some
special handling. Previously, that variable was left out. So the
indentation was wrong when there was a comment.

old:

```Verilog
case (data)
  16'd0:
    result = //
        10'b0111111111;
endcase
case (data)
  16'd0:
    //

  //
  result = //
  10'b0111111111;
endcase
```

new:

```Verilog
case (data)
  16'd0:
    result = //
        10'b0111111111;
endcase
case (data)
  16'd0:
    //

    //
    result = //
        10'b0111111111;
endcase
```
2023-11-29 15:19:13 +00:00
sstwcw
3af82b3962
[clang-format] Add spaces around the Verilog implication operator (#71352)
The Verilog implication operator `->` is a binary operator meaning
either the left hand side is false or the right hand side is true.
Previously it was treated as the C++ struct member operator.

I didn't even know it existed when I added the operator formatting part.
And I didn't check all the tests for all the operators I added. That is
how the bad test got in.
2023-11-29 15:17:59 +00:00
Owen Pan
39faf13dde
[clang-format] Add BreakAdjacentStringLiterals option (#73432)
Closes #70451.
2023-11-27 13:01:16 -08:00
Owen Pan
659e4017b7 [clang-format][NFC] Improve an if conditional in the annotator 2023-11-26 22:54:44 -08:00
Owen Pan
a369a5946f
[clang-format] Fix a bug in formating #define A x: (#73220)
Fixed #70789.
2023-11-26 16:20:19 -08:00
Owen Pan
dcab84fd5e Reland [clang-format][NFC] Remove a redundant isLiteral() call 2023-11-21 04:17:03 +00:00
Gulfem Savrun Yeniceri
3e6d629ccf Revert "[clang-format][NFC] Remove a redundant isLiteral() call"
This reverts commit f6033699646b7650123a273c043a93e5eeaac6d8.
This change is labeled as NFC, but introduces a functional change
without a test, and caused a breakage as reported in
https://reviews.llvm.org/rGf6033699646b7650123a273c043a93e5eeaac6d8.
2023-11-20 22:15:07 +00:00
Owen Pan
edad025d1e
[clang-format] Correctly annotate braces of empty functions (#72733)
Also fixed some existing test cases.

Fixed #57305.
Fixed #58251.
2023-11-19 15:10:27 -08:00
Owen Pan
cb3a605c5d
[clang-format] Fix a bug in isStartOfName() on macro definitions (#72768)
Fixed #72751.
2023-11-19 15:08:54 -08:00