[PATCH] D79388: [clang-format] Fix AlignConsecutive on PP blocks

Jake Merdich via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 4 19:55:49 PDT 2020


JakeMerdichAMD created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.

Possible alternatives might be hashing preprocessor state to detect if
two lines come from the same block (but the way that ifdefs are
sometimes deferred makes that tricky) or showing the preprocessor
statements on all passes instead of just the first one (seems harder).

Fixes #25167,#31281


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79388

Files:
  clang/lib/Format/FormatToken.h
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/unittests/Format/FormatTest.cpp
  clang/unittests/Format/FormatTestComments.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79388.261992.patch
Type: text/x-patch
Size: 4559 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200505/925e7dcb/attachment.bin>


More information about the cfe-commits mailing list