Commit c43afcf
committed
[clang-format] Align within the level with Cpp11BracedListStyle disabled
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.1 parent d0c0986 commit c43afcf
File tree
3 files changed
+34
-25
lines changed- clang
- lib/Format
- unittests/Format
3 files changed
+34
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6486 | 6486 | | |
6487 | 6487 | | |
6488 | 6488 | | |
6489 | | - | |
| 6489 | + | |
6490 | 6490 | | |
6491 | 6491 | | |
6492 | 6492 | | |
6493 | 6493 | | |
6494 | 6494 | | |
6495 | | - | |
| 6495 | + | |
| 6496 | + | |
6496 | 6497 | | |
6497 | 6498 | | |
6498 | 6499 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
279 | 280 | | |
280 | 281 | | |
281 | 282 | | |
282 | | - | |
| 283 | + | |
283 | 284 | | |
284 | 285 | | |
285 | | - | |
286 | 286 | | |
287 | 287 | | |
288 | | - | |
| 288 | + | |
| 289 | + | |
289 | 290 | | |
290 | | - | |
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
295 | | - | |
| 295 | + | |
296 | 296 | | |
297 | 297 | | |
298 | 298 | | |
| |||
314 | 314 | | |
315 | 315 | | |
316 | 316 | | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
317 | 320 | | |
318 | 321 | | |
319 | 322 | | |
| |||
340 | 343 | | |
341 | 344 | | |
342 | 345 | | |
343 | | - | |
344 | 346 | | |
345 | 347 | | |
346 | 348 | | |
347 | 349 | | |
348 | 350 | | |
349 | | - | |
350 | | - | |
| 351 | + | |
351 | 352 | | |
352 | 353 | | |
353 | 354 | | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | 355 | | |
362 | 356 | | |
363 | 357 | | |
| |||
532 | 526 | | |
533 | 527 | | |
534 | 528 | | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | | - | |
540 | | - | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
541 | 537 | | |
542 | 538 | | |
543 | 539 | | |
| |||
550 | 546 | | |
551 | 547 | | |
552 | 548 | | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
553 | 552 | | |
554 | 553 | | |
555 | 554 | | |
| |||
578 | 577 | | |
579 | 578 | | |
580 | 579 | | |
581 | | - | |
| 580 | + | |
582 | 581 | | |
583 | 582 | | |
584 | 583 | | |
585 | 584 | | |
586 | 585 | | |
587 | 586 | | |
588 | 587 | | |
| 588 | + | |
589 | 589 | | |
590 | 590 | | |
591 | 591 | | |
| |||
637 | 637 | | |
638 | 638 | | |
639 | 639 | | |
640 | | - | |
| 640 | + | |
| 641 | + | |
641 | 642 | | |
| 643 | + | |
642 | 644 | | |
643 | 645 | | |
644 | 646 | | |
| |||
684 | 686 | | |
685 | 687 | | |
686 | 688 | | |
| 689 | + | |
687 | 690 | | |
688 | 691 | | |
689 | 692 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20312 | 20312 | | |
20313 | 20313 | | |
20314 | 20314 | | |
| 20315 | + | |
| 20316 | + | |
| 20317 | + | |
| 20318 | + | |
| 20319 | + | |
20315 | 20320 | | |
20316 | 20321 | | |
20317 | 20322 | | |
| |||
0 commit comments