Commit Graph

196 Commits

Author SHA1 Message Date
owenca
836919bb34 [clang-format] Fix a crash in AlignArrayOfStructures (#167099)
Fixes #157405
2025-11-09 17:35:54 -08:00
Björn Schäpers
bcb1b773f6 [clang-format] Add option to separate comment alignment between ... (#165033)
normal lines and PP directives.

Handling PP directives differently can be desired, like in #161848.
Changing the default is not an option, there are tests for exactly the
current behaviour.
2025-11-07 15:12:30 +00:00
Björn Schäpers
ca69a8d2f4 [clang-format] Fix ColumnLimit violation while aligning (#165627)
It did compute the length only on the first line, and thus the following
lines could be (and in the test example were) moved over the column
limit, when the = was aligned.
2025-11-03 20:19:34 +01:00
sstwcw
f8a0599d76 [clang-format] Align comments following continued aligned lines (#164687)
new

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};  //
auto b                     = [] { //
  return;                         //
};

auto aaaaaaaaaaaaaaaaaaaaa = {};  //
auto b                     = [] { //
  return aaaaaaaaaaaaaaaaaaaaa;   //
};
```

old

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};  //
auto b                     = [] { //
  return;     //
};

auto aaaaaaaaaaaaaaaaaaaaa = {};                    //
auto b                     = [] {                   //
  return aaaaaaaaaaaaaaaaaaaaa; //
};
```

Aligning a line to another line involves keeping track of the tokens'
positions. Previously the shift was incorrectly added to some tokens
that did not move. Then the comments would end up in the wrong places.
2025-10-27 18:41:09 +00:00
Björn Schäpers
c318f82a4a [clang-format] Respect ColumnLimit while aligning multiline expressions (#163863)
Before the patch the added test case would indent the function and
moving its second line beyond the column limit.

Fixes #68122.
2025-10-20 22:11:11 +02:00
Björn Schäpers
8774128fcb [clang-format][NFC] Simplify AlignMacroMatches (#164122)
Just return early based on the SpacedRequiredBefore.
2025-10-20 08:43:53 +00:00
Björn Schäpers
dd6a6bac70 [clang-format] Reuse AlignTokens for aligning macros (#164120)
Fixes #52985.

This leaves aligning short case statements with its own logic. But that
is harder to port, because it aligns even with no content of the right
hand side of the :.
2025-10-20 00:04:42 +02:00
Björn Schäpers
cf28a476fb [clang-format] Remove special handling of comments after brace/paren (#71672)
Fixes http://llvm.org/PR55487 (#55487)

The code did not match the documentation about Cpp11BracedListStyle.
Changed handling of comments after opening braces, which are supposedly
function call like to behave exactly like their parenthesis counter
part.
2025-10-18 22:25:28 +00:00
sstwcw
a0b8261610 [clang-format] Continue aligned lines better (#161903)
Fixes #53497.

Fixes #56078.

after with config `{AlignConsecutiveAssignments: true}`

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = [] {
  x = {.one_foooooooooooooooo = 2, //
       .two_fooooooooooooo    = 3, //
       .three_fooooooooooooo  = 4};
};

A B          = {"Hello "
                "World"};
BYTE payload = 2;

float i2 = 0;
auto v   = type{i2 = 1, //
                i  = 3};
```

before

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = [] {
  x = {.one_foooooooooooooooo = 2, //
                           .two_fooooooooooooo    = 3, //
                           .three_fooooooooooooo  = 4};
};

A B          = {"Hello "
                         "World"};
BYTE payload = 2;

float i2 = 0;
auto v   = type{i2 = 1, //
              i  = 3};
```

When a line gets aligned, the following lines may need to move with it.
This patch replaces the old algorithm with a simpler one.  It uses the
`IsAligned` attribute.  It makes most of the scope stack irrelevant.
Now the stack only needs to keep track of 2 levels.

The old algorithm had problems like these.

- Whether lines inside a braced list moved depended on whether there was
  a type at the start.  It should depend on whether the inside was
  aligned to the brace.  The first case that came up with a type name at
  the start happened to have a comma at the end of the list so the
  inside was not aligned to the brace.

- Excluding lines inside closures did not always work.

- A continued string could move twice as much as it should.

The following problems are not fixed yet.

A token that opens a scope is needed.  Operator precedence is not
enough.

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = 0 + //
         0;
```

The algorithm has trouble when things that should not move and things
that should move are nested.  This is related to the `IsAligned`
attribute being a boolean.  It also affects how spaces and tabs are
selected.

```C++
auto aaaaaaaaaaaaaaaaaaaaa = {};
auto b                     = {.a = {
              .a = 0,
          }};
```
2025-10-15 15:18:11 +00:00
Björn Schäpers
847e1e1890 [clang-format][NFC] Introduce isNoneOf (#161021)
And apply throughout the code base.
2025-10-02 20:52:45 +02:00
sstwcw
a0c75a9009 [clang-format] Align within the level with Cpp11BracedListStyle disabled (#159140)
When the style is
`{AlignConsecutiveDeclarations: true, Cpp11BracedListStyle: false}`, the
program would sometimes align the lambda body with the outside. Like
this.

```C++
const volatile auto result{ []() {
  const auto        something = 1;
  return 2;
} };
```

This patch stops it.  Now the output looks like this.

```C++
const volatile auto result{ []() {
  const auto something = 1;
  return 2;
} };
```

Fixes #53699.

The problem was with how the `AlignTokenSequence` function tracked
levels. The stack was pushed once when a token was more nested than the
previous one. It was popped once when a token was less nested than the
previous one. With the option `Cpp11BracedListStyle` disabled, the `[`
token was deeper than the previous token by 2 levels. Both its
indentation level and nesting level were more than that of the previous
one. But the stack was only pushed once. The following tokens popped the
2 levels separately. The function treated the body of the lambda
expression as on the same level as the outside.

The problem is fixed this way. The function `AlignTokens` which calls
the function `AlignTokenSequence` already uses a simpler and more robust
way to track levels. It remembers the outermost level it should align.
It compares the token's level with the outermost level. It does not need
a stack. So it is immune to the problem. This patch makes the function
`AlignTokenSequence` take as a parameter the indices of the tokens
matched by the function `AlignTokens`. This way it does not have to
contain the logic again.

Now the function `AlignTokenSequence` only aligns tokens already matched
by the function `AlignTokens`. It is easy to see that the assertion on
line 453 holds. The workaround on line 354 is not needed any more. The
test on line 20310 added at the same time as the assertion ensures that
the assertion hold.

The stack in the function `AlignTokenSequence` is kept for now. It is
still used to figure out when things inside a level should move along
with the outer level. Since the stack has the problem, the developer
plans to move the logic into token annotator. It already uses a stack.
2025-09-25 16:46:43 +00:00
Owen Pan
5ccbea9f48 [clang-format][NFC] Replace size() with empty() (#147164) 2025-07-06 14:19:30 -07:00
Owen Pan
5f11d64cfb [clang-format] Fix a bug in AlignConsecutiveDeclarations (#135516)
Fix #109768
2025-04-13 09:01:37 -07:00
Owen Pan
786db636b9 [clang-format] Add KeepFormFeed option (#113268)
Closes #113170.
2024-10-23 19:55:32 -07:00
Brad House
f0bd62d870 [clang-format] Add AlignFunctionDeclarations to AlignConsecutiveDeclarations (#108241)
Enabling AlignConsecutiveDeclarations also aligns function prototypes
or declarations.  This is often unexpected as typically function
prototypes, especially in public headers, don't use any padding.

Setting AlignFunctionDeclarations to false will skip this alignment.
It is by default set to true to keep compatibility with prior
versions to not make unexpected changes.

Fixes #74320
2024-10-06 17:46:43 -07:00
Owen Pan
656d5aa958 [clang-format] Fix misalignments of pointers in angle brackets (#106013)
Fixes #105898.
2024-08-27 19:13:27 -07:00
Owen Pan
dcf6b7a8ea [clang-format] Fix a bug in TCAS_Leave using tabs for indentation (#98427)
Fixes #92530.
2024-07-11 19:36:42 -07:00
Owen Pan
dba2aa265c [clang-format] Add LeftWithLastLine to AlignEscapedNewlines option (#93402)
Closes #92999.
2024-05-27 15:20:58 -07:00
Owen Pan
236b3e1aad [clang-format] Handle Java switch expressions (#91112)
Also adds AllowShortCaseExpressionOnASingleLine option and
AlignCaseArrows suboption of AlignConsecutiveShortCaseStatements.

Fixes #55903.
2024-05-06 19:55:55 -07:00
Owen Pan
aa596fa4d9 [clang-format] Set Change.TokenLength to ColumnWidth (#90378)
Fixes #37705.
Fixes #47333.
Fixes #47624.
Fixes #58850.
Fixes #75929.
Fixes #87885.
Fixes #89916.
2024-04-28 14:06:12 -07:00
Owen Pan
b4af01bada [clang-format][NFC] Don't repeat Changes[i]/Changes[i - 1] 2024-04-27 15:14:20 -07:00
Owen Pan
684f27d37a [clang-format][NFC] Use is instead of getType() == 2024-04-06 01:51:45 -07:00
rayroudc
dd06b8e679 [clang-format] Fix anonymous reference parameter with default value (#86254)
When enabling alignment of consecutive declarations and reference right
alignment, the needed space between `& ` and ` = ` is removed in the
following use case.

Problem (does not compile)
```
int    a(const Test    &= Test());
double b();
```

Expected:
```
int    a(const Test & = Test());
double b();
```

Test command:

```
echo "int    a(const Test& = Test()); double b();" | clang-format -style="{AlignConsecutiveDeclarations: true, ReferenceAlignment: Right}"
```
2024-03-27 19:59:31 +01:00
Owen Pan
cceedc939a [clang-format] Fix a crash with AlignArrayOfStructures option (#86420)
Fixes #86109.
2024-03-24 15:22:40 -07:00
Hirofumi Nakamura
e54af60816 [clang-format] Added AlignConsecutiveTableGenBreakingDAGArgColons option. (#86150)
The option to specify the style of alignment of the colons inside TableGen's DAGArg.
2024-03-22 23:11:36 +09:00
Owen Pan
b2082a9817 Revert "[clang-format][NFC] Delete 100+ redundant #include lines in .cpp files"
This reverts commit b92d6dd704. See
github.com/llvm/llvm-project/commit/b92d6dd704d7#commitcomment-139992444

We should use a tool like Visual Studio to clean up the headers.
2024-03-19 21:28:22 -07:00
Owen Pan
b92d6dd704 [clang-format][NFC] Delete 100+ redundant #include lines in .cpp files 2024-03-16 22:24:11 -07:00
Hirofumi Nakamura
19cec9ca12 [clang-format] Add AlignConsecutiveTableGenDefinitions option. (#83008)
To align TableGen consecutive definitions.
2024-02-27 22:31:23 +09:00
Hirofumi Nakamura
046682ef88 [clang-format] Add AlignConsecutiveTableGenCondOperatorColons option. (#82878)
To align colons inside TableGen !cond operators.
2024-02-26 22:50:51 +09:00
Owen Pan
179ade6a6d [clang-format] Fix an issue reported by static analyzer
Fixes #79685.
2024-01-27 21:21:01 -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
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
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
Owen Pan
5679f5515b [clang-format] Fix crashes in AlignArrayOfStructures (#72520)
Fixed #54815.
Fixed #55269.
Fixed #55493.
Fixed #68431.
2023-11-17 14:35:30 -08:00
Owen Pan
fff993b7cf [clang-format] Fix a bug in aligning comments in vector of structs (#72099)
Fixed #71825.
2023-11-13 13:41:01 -08:00
Owen Pan
cc75e52016 [clang-format][NFC] Refactor isPointerOrReference 2023-11-10 01:23:05 -08:00
Björn Schäpers
5efa84cf6f [clang-format] Don't align comments over scopes
We now stop aligning trailing comments on all closing braces, for
classes etc. we even check for the semicolon between the comment and the
brace.
    
Fixes #67906.
2023-10-25 12:50:15 +02:00
Owen Pan
7bc1031c47 Revert "[clang-format] Fix align consecutive declarations over function pointers"
This reverts commit a84e0b4bdc.

Fixes #68079.
2023-10-24 02:15:44 -07:00
Björn Schäpers
b6f29191ad [clang-format][NFC] AlignTokenSequence: Skip loop iteration
When Shift is 0 there does nothing happen in the remainder of the loop,
express that directly.
2023-10-04 22:20:33 +02:00
Björn Schäpers
7539bcf994 [clang-format][NFC] AlignTokenSequence: Rename Changes[i] to CurrentC…
…hange

To improve debugging experience.
2023-10-04 21:19:56 +02:00
Björn Schäpers
b4a076a12d [clang-format][NFC] AlignTokens: Rename Changes[i] to CurrentChange (#68152)
To improve debugging experience.
2023-10-04 21:19:21 +02:00
Owen Pan
7e856d1894 Reland "[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (#67955)"
Reland 6a621ed8e4 which failed on Windows but not macOS.

The failures were caused by moving the annotation of the children from the
top to the bottom of TokenAnnotator::annotate(), resulting in invalid lines
including incomplete ones being skipped.
2023-10-03 22:26:48 -07:00
Owen Pan
d08fcc817e Revert "[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (#67955)"
This reverts commit 6a621ed8e4 as it caused
buildbots to fail.
2023-10-03 18:19:23 -07:00
Owen Pan
6a621ed8e4 [clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (#67955)
After annotating constructors/destructors as FunctionDeclarationName in
commit 0863051208, we have seen several issues because ctors/dtors had
been treated differently than functions in aligning, wrapping, and
indenting.

This patch annotates ctors/dtors as CtorDtorDeclName instead and would
effectively revert commit 0468fa07f8, which is obsolete now.

Fixed #67903.
Fixed #67907.
2023-10-03 18:02:09 -07:00
Björn Schäpers
1f01269028 [clang-format] Fix alignment in presence of template functions (#68029)
Fixes #68102.
2023-10-03 15:35:30 +02:00
Owen Pan
0468fa07f8 [clang-format] Don't align ctors and dtors with other functions (#67618)
Fixed #67604.
2023-09-29 02:09:36 -07:00
Owen Pan
d3f8c88abe [clang-format] Fix a bug in aligning trailing comments (#67221)
Fixes #67116.
2023-09-24 20:07:23 -07:00
Owen Pan
ff7e854022 [clang-format] Fix an assertion failure in Whitespace Manager 2023-09-24 17:53:05 -07:00
Owen Pan
d1c643a016 [clang-format][NFC] Clean up alignTrailingComments() (#67218) 2023-09-24 15:00:57 -07:00
sstwcw
ddc80637cc [clang-format] Break long string literals in C#, etc.
Now strings that are too long for one line in C#, Java, JavaScript, and
Verilog get broken into several lines.  C# and JavaScript interpolated
strings are not broken.

A new subclass BreakableStringLiteralUsingOperators is used to handle
the logic for adding plus signs and commas.  The updateAfterBroken
method was added because now parentheses or braces may be required after
the parentheses or commas are added.  In order to decide whether the
added plus sign should be unindented in the BreakableToken object, the
logic for it is taken out into a separate function
shouldUnindentNextOperator.

The logic for finding the continuation indentation when the option
AlignAfterOpenBracket is set to DontAlign is not implemented yet.  So in
that case the new line may have the wrong indentation, and the parts may
have the wrong length if the string needs to be broken more than once
because finding where to break the string depends on where the string
starts.

The preambles for the C# and Java unit tests are changed to the newer
style in order to allow the 3-argument verifyFormat macro.  Some cases
are changed from verifyFormat to verifyImcompleteFormat because those
use incomplete code and the new verifyFormat function checks that the
code is complete.

The line in the doc was changed to being indented by 4 spaces, that is,
the default continuation indentation.  It has always been the case.  It
was probably a mistake that the doc showed 2 spaces previously.

This commit was fist committed as 16ccba5107.  The tests caused
assertion failures.  Then it was reverted in 547bce3613.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D154093
2023-09-05 03:19:49 +00:00