[PATCH] D43303: [Format] Fix for bug 35641

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 14 10:54:35 PST 2018


kadircet created this revision.
kadircet added reviewers: rsmith, djasper.
kadircet added a project: clang.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rC Clang

https://reviews.llvm.org/D43303

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


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -9449,6 +9449,14 @@
       "});\n",
       Alignment);
   Alignment.PointerAlignment = FormatStyle::PAS_Right;
+
+  // Bug 35641
+  Alignment.AlignConsecutiveDeclarations = true;
+  verifyFormat("int func() { //\n"
+               "  int b;\n"
+               "  int c;\n"
+               "}",
+               Alignment);
 }
 
 TEST_F(FormatTest, LinuxBraceBreaking) {
Index: lib/Format/WhitespaceManager.cpp
===================================================================
--- lib/Format/WhitespaceManager.cpp
+++ lib/Format/WhitespaceManager.cpp
@@ -255,8 +255,14 @@
             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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43303.134264.patch
Type: text/x-patch
Size: 1440 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180214/0fc4b04b/attachment.bin>


More information about the cfe-commits mailing list