[PATCH] D97137: Bug fix of https://bugs.llvm.org/show_bug.cgi?id=49175

Darwin Xu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 20 23:48:31 PST 2021


darwin created this revision.
darwin added a reviewer: clang-format.
darwin added a project: clang-format.
darwin requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The AlignConsecutiveDeclarations option doesn't handle pointer properly:

The expected code format:

  unsigned int*       a;
  int*                b;
  unsigned int Const* c;

The actual code after formatting:

  unsigned int* a;
  int*          b;
  unsigned int Const* c;

>From the code of clang-format, it seems the WhitespaceManager miss treated `Const` as one of the token and which leads to the faulty behavior. So I added an extra check that if the next token is a pointer or a reference, then the current token is not the aligned token (the `matcher` lambda returns false).

Unit test passed:

  darwin at Darwins-iMac build % cmake --build . -j24 -t check-clang-unit
  ...
  [100%] Running lit suite /Volumes/silo/Projects/llvm-project/clang/test/Unit
  
  Testing Time: 270.81s
    Passed: 13848
  [100%] Built target check-clang-unit


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97137

Files:
  clang/lib/Format/WhitespaceManager.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -13945,6 +13945,10 @@
   verifyFormat("int      oneTwoThree{0}; // comment\n"
                "unsigned oneTwo;         // comment",
                Alignment);
+  verifyFormat("unsigned int *      a;\n"
+               "int *               b;\n"
+               "unsigned int Const *c;",
+               Alignment);
   EXPECT_EQ("float const a = 5;\n"
             "\n"
             "int oneTwoThree = 123;",
@@ -14249,6 +14253,12 @@
   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
             "foo(int a);",
             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
+
+  Alignment.PointerAlignment = FormatStyle::PAS_Left;
+  verifyFormat("unsigned int*       a;\n"
+               "int*                b;\n"
+               "unsigned int Const* c;",
+               Alignment);
 }
 
 TEST_F(FormatTest, LinuxBraceBreaking) {
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -709,6 +709,8 @@
         for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
           if (Next->is(tok::comment))
             continue;
+          if (Next->is(TT_PointerOrReference))
+            return false;
           if (!Next->Tok.getIdentifierInfo())
             break;
           if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97137.325277.patch
Type: text/x-patch
Size: 1628 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210221/5d1782f4/attachment.bin>


More information about the cfe-commits mailing list