[PATCH] D59332: [clang-format] AlignConsecutiveDeclarations fails with attributes

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 13 14:12:54 PDT 2019


MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: rolandschulz, reuk, djasper, klimek.
MyDeveloperDay added a project: clang-tools-extra.

Addresses http://llvm.org/PR40418

When using `AlignConsecutiveDeclarations: true`

variables with Attributes would not be aligned correctly

  void f(int  extremlylongparameternamexxxxxxxx1,
         long extremlylongparameternamexxxxxxxx2,
         int[[clang::nodiscard]] extremlylongparameternamexxxxxxxx3,
         int __attribute__((clang::nodiscard)) extremlylongparameternamexxxxxxxx4) {
    int           a;
    unsigned long b;
    int[[clang::nodiscard]] c;
    int __attribute__((clang::nodiscard)) d;
  }

following this change, both parameters and variables with attributes will be aligned (space permitting)

  void f(int              extremlylongparameternamexxxxxxxx1,
         long             extremlylongparameternamexxxxxxxx2,
         int              [[clang::nodiscard]] extremlylongparameternamexxxxxxxx3,
         int __attribute__((clang::nodiscard)) extremlylongparameternamexxxxxxxx4) {
    int              a;
    unsigned long    b;
    int              [[clang::nodiscard]] c;
    int __attribute__((clang::nodiscard)) d;
  }




https://reviews.llvm.org/D59332

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
@@ -10135,6 +10135,47 @@
                "  unsigned c;\n"
                "}",
                Alignment);
+
+  // See llvm.org/PR40418
+  verifyFormat(
+      "void f(int              extremlylongparameternamexxxxxxxx1,\n"
+      "       long             extremlylongparameternamexxxxxxxx2,\n"
+      "       int              [[a::b]] extremlylongparameternamexxxxxxxx3,\n"
+      "       int __attribute__((a::b)) extremlylongparameternamexxxxxxxx4) "
+      "{}\n",
+      Alignment);
+
+  verifyFormat("void f(int              extremlylongparameternamexxxxxxxx1,\n"
+               "       long             extremlylongparameternamexxxxxxxx2,\n"
+               "       int              [[deprecated]] "
+               "extremlylongparameternamexxxxxxxx3,\n"
+               "       int __attribute__((deprecated)) "
+               "extremlylongparameternamexxxxxxxx4) "
+               "{}\n",
+               Alignment);
+
+  verifyFormat("int              a;\n"
+               "unsigned long    b;\n"
+               "int __attribute__((a::b)) d;\n"
+               "int              [[a::b]] c;\n"
+               "int __attribute__((a::b)) e;\n"
+               "int __attribute__((depreciated)) f;\n"
+               "int __attribute__((aligned(16))) g;\n",
+               Alignment);
+
+  verifyFormat("int              a;\n"
+               "unsigned long    b;\n"
+               "int __attribute__((a::b)) d;\n"
+               "int              [[deprecated]] c;\n"
+               "int __attribute__((a::b)) e;\n",
+               Alignment);
+
+  verifyFormat("int              a;\n"
+               "unsigned long    b;\n"
+               "int __attribute__((deprecated)) de;\n"
+               "int              [[deprecated]] c;\n"
+               "int __attribute__((a::b)) e;\n",
+               Alignment);
 }
 
 TEST_F(FormatTest, LinuxBraceBreaking) {
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -17,8 +17,8 @@
 namespace clang {
 namespace format {
 
-bool WhitespaceManager::Change::IsBeforeInFile::
-operator()(const Change &C1, const Change &C2) const {
+bool WhitespaceManager::Change::IsBeforeInFile::operator()(
+    const Change &C1, const Change &C2) const {
   return SourceMgr.isBeforeInTranslationUnit(
       C1.OriginalWhitespaceRange.getBegin(),
       C2.OriginalWhitespaceRange.getBegin());
@@ -465,6 +465,19 @@
         // definitions.
         return C.Tok->is(TT_StartOfName) ||
                C.Tok->is(TT_FunctionDeclarationName) ||
+               C.Tok->startsSequence(
+                   tok::l_paren, tok::l_paren, tok::identifier, tok::coloncolon,
+                   tok::identifier, tok::r_paren, tok::r_paren) ||
+               C.Tok->startsSequence(tok::l_square, tok::l_square,
+                                     tok::identifier, tok::coloncolon,
+                                     tok::identifier, tok::r_square,
+                                     tok::r_square) ||
+               C.Tok->startsSequence(tok::l_square, tok::l_square,
+                                     tok::identifier, tok::r_square,
+                                     tok::r_square) ||
+               C.Tok->startsSequence(tok::l_paren, tok::l_paren,
+                                     tok::identifier, tok::r_paren,
+                                     tok::r_paren) ||
                C.Tok->is(tok::kw_operator);
       },
       Changes, /*StartAt=*/0);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59332.190505.patch
Type: text/x-patch
Size: 3768 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190313/65a67ac1/attachment-0001.bin>


More information about the cfe-commits mailing list