]> granicus.if.org Git - clang/commitdiff
[clang-format] Fix a bug in AlignConsecutiveDeclarations.
authorOwen Pan <owenpiano@gmail.com>
Wed, 1 May 2019 18:23:44 +0000 (18:23 +0000)
committerOwen Pan <owenpiano@gmail.com>
Wed, 1 May 2019 18:23:44 +0000 (18:23 +0000)
Fixes PR37175

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@359711 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/WhitespaceManager.cpp
unittests/Format/FormatTest.cpp

index 9f1ea7022f0de5e8ad6c565a4791fb90cf1ea263..5383addd7eec2e47f850381f2886beea22e61fb7 100644 (file)
@@ -463,9 +463,21 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
       [](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);
 }
index fd61470d656b0cc22260488ad4f375378ab7f577..ade52c753c43ea12e329afa1211f0cccc2501fc7 100644 (file)
@@ -10581,6 +10581,13 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
                "  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) {