r338578 - [Format] Fix for bug 35641

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 1 08:32:57 PDT 2018


Author: ibiryukov
Date: Wed Aug  1 08:32:56 2018
New Revision: 338578

URL: http://llvm.org/viewvc/llvm-project?rev=338578&view=rev
Log:
[Format] Fix for bug 35641

Summary:
Bug was caused due to comments at the start of scope. For a code like:
```
int func() { //
  int b;
  int c;
}
```
the comment at the first line gets IndentAndNestingLevel (1,1) whereas
the following declarations get only (0,1) which prevents them from insertion
of a new scope. So, I changed the AlignTokenSequence to look at previous
*non-comment* token when deciding whether to introduce a new scope into
stack or not.

Patch by Kadir Cetinkaya!

Reviewers: rsmith, djasper

Reviewed By: djasper

Subscribers: lebedev.ri, cfe-commits, klimek

Tags: #clang

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

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=338578&r1=338577&r2=338578&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Wed Aug  1 08:32:56 2018
@@ -255,8 +255,14 @@ AlignTokenSequence(unsigned Start, unsig
             Changes[ScopeStack.back()].indentAndNestingLevel())
       ScopeStack.pop_back();
 
+    // Compare current token to previous non-comment token to ensure whether
+    // it is in a deeper scope or not.
+    unsigned PreviousNonComment = i - 1;
+    while (PreviousNonComment > Start &&
+           Changes[PreviousNonComment].Tok->is(tok::comment))
+      PreviousNonComment--;
     if (i != Start && Changes[i].indentAndNestingLevel() >
-                          Changes[i - 1].indentAndNestingLevel())
+                          Changes[PreviousNonComment].indentAndNestingLevel())
       ScopeStack.push_back(i);
 
     bool InsideNestedScope = ScopeStack.size() != 0;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=338578&r1=338577&r2=338578&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Aug  1 08:32:56 2018
@@ -9804,6 +9804,14 @@ TEST_F(FormatTest, AlignConsecutiveDecla
       "});\n",
       Alignment);
   Alignment.PointerAlignment = FormatStyle::PAS_Right;
+
+  // See llvm.org/PR35641
+  Alignment.AlignConsecutiveDeclarations = true;
+  verifyFormat("int func() { //\n"
+               "  int      b;\n"
+               "  unsigned c;\n"
+               "}",
+               Alignment);
 }
 
 TEST_F(FormatTest, LinuxBraceBreaking) {




More information about the cfe-commits mailing list