[all-commits] [llvm/llvm-project] b2eb43: [clang-format] Fix AlignConsecutive on PP blocks

MyDeveloperDay via All-commits all-commits at lists.llvm.org
Wed May 13 10:32:21 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: b2eb439317576ce718193763c12bff9fccdfc166
      https://github.com/llvm/llvm-project/commit/b2eb439317576ce718193763c12bff9fccdfc166
  Author: mydeveloperday <mydeveloperday at gmail.com>
  Date:   2020-05-13 (Wed, 13 May 2020)

  Changed paths:
    M clang/lib/Format/FormatToken.h
    M clang/lib/Format/UnwrappedLineParser.cpp
    M clang/lib/Format/WhitespaceManager.cpp
    M clang/unittests/Format/FormatTest.cpp
    M clang/unittests/Format/FormatTestComments.cpp

  Log Message:
  -----------
  [clang-format] Fix AlignConsecutive on PP blocks

Summary:
Currently the 'AlignConsecutive*' options incorrectly align across
elif and else statements, even if they are very far away and across
unrelated preprocessor macros.

This failed since on preprocessor run 2+, there is not enough context
about the #ifdefs to actually differentiate one block from another,
causing them to align across different blocks or even large sections of
the file.

Eg, with AlignConsecutiveAssignments:

```
\#if FOO      // Run 1
\#else        // Run 1
int a   = 1;  // Run 2, wrong
\#endif       // Run 1

\#if FOO      // Run 1
\#else        // Run 1
int bar = 1;  // Run 2
\#endif       // Run 1
```

is read as

```
int a   = 1;  // Run 2, wrong
int bar = 1;  // Run 2
```

The approach taken to fix this was to add a new flag to Token that
forces breaking alignment across groups of lines (MustBreakAlignBefore)
in a similar manner to the existing flag that forces a line break
(MustBreakBefore). This flag is set for the first Token after a
preprocessor statement or diff conflict marker.

Fixes #25167,#31281

Patch By: JakeMerdichAMD

Reviewed By: MyDeveloperDay

Tags: #clang, #clang-format

Differential Revision: https://reviews.llvm.org/D79388




More information about the All-commits mailing list