[PATCH] D61222: [clang-format] Fix a bug in AlignConsecutiveDeclarations

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 27 17:08:36 PDT 2019


owenpan updated this revision to Diff 196994.
owenpan retitled this revision from "[clang-format] Fix bug in determineTokenType() for TT_StartOfName" to "[clang-format] Fix a bug in AlignConsecutiveDeclarations".
owenpan added a comment.
This revision is now accepted and ready to land.

Fix it in `WhitespaceManager::alignConsecutiveDeclarations()` instead.


Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D61222/new/

https://reviews.llvm.org/D61222

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
@@ -10575,6 +10575,16 @@
                "  unsigned c;\n"
                "}",
                Alignment);
+
+  // See PR37175
+  FormatStyle Style = getMozillaStyle();
+  Style.AlignConsecutiveDeclarations = true;
+  EXPECT_EQ("DECOR1 /**/ DECOR2 /**/ int8_t /**/\n"
+            "foo(int a);",
+            format("DECOR1 /**/ DECOR2 /**/ int8_t /**/ foo (int a);", Style));
+  EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
+            "bar(int a);",
+            format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ bar (int a);", Style));
 }
 
 TEST_F(FormatTest, LinuxBraceBreaking) {
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -463,9 +463,21 @@
       [](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);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61222.196994.patch
Type: text/x-patch
Size: 1957 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190428/17e4b172/attachment.bin>


More information about the cfe-commits mailing list