r359711 - [clang-format] Fix a bug in AlignConsecutiveDeclarations.

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Wed May 1 11:23:45 PDT 2019


Author: owenpan
Date: Wed May  1 11:23:44 2019
New Revision: 359711

URL: http://llvm.org/viewvc/llvm-project?rev=359711&view=rev
Log:
[clang-format] Fix a bug in AlignConsecutiveDeclarations.

Fixes PR37175

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

Modified:
    cfe/trunk/lib/Format/WhitespaceManager.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/WhitespaceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.cpp?rev=359711&r1=359710&r2=359711&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Wed May  1 11:23:44 2019
@@ -463,9 +463,21 @@ void WhitespaceManager::alignConsecutive
       [](Change const &C) {
         // tok::kw_operator is necessary for aligning operator overload
         // definitions.
-        return C.Tok->is(TT_StartOfName) ||
-               C.Tok->is(TT_FunctionDeclarationName) ||
-               C.Tok->is(tok::kw_operator);
+        if (C.Tok->isOneOf(TT_FunctionDeclarationName, tok::kw_operator))
+          return true;
+        if (C.Tok->isNot(TT_StartOfName))
+          return false;
+        // Check if there is a subsequent name that starts the same declaration.
+        for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
+          if (Next->is(tok::comment))
+            continue;
+          if (!Next->Tok.getIdentifierInfo())
+            break;
+          if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
+                            tok::kw_operator))
+            return false;
+        }
+        return true;
       },
       Changes, /*StartAt=*/0);
 }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=359711&r1=359710&r2=359711&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed May  1 11:23:44 2019
@@ -10581,6 +10581,13 @@ TEST_F(FormatTest, AlignConsecutiveDecla
                "  unsigned c;\n"
                "}",
                Alignment);
+
+  // See PR37175
+  FormatStyle Style = getMozillaStyle();
+  Style.AlignConsecutiveDeclarations = true;
+  EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
+            "foo(int a);",
+            format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
 }
 
 TEST_F(FormatTest, LinuxBraceBreaking) {




More information about the cfe-commits mailing list