r354183 - clang-format with UseTab: Always sometimes doesn't insert the right amount of tabs.

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 15 15:07:43 PST 2019


Author: alexfh
Date: Fri Feb 15 15:07:43 2019
New Revision: 354183

URL: http://llvm.org/viewvc/llvm-project?rev=354183&view=rev
Log:
clang-format with UseTab: Always sometimes doesn't insert the right amount of tabs.

Trailing comments are not always aligned properly when UseTab is set to Always.

Consider:

int a;        // x
int bbbbbbbb; // x
With .clang-format:

---
Language:        Cpp
BasedOnStyle: LLVM
UseTab: Always
...
The trailing comments of this code block should be aligned, but aren't

To align the first trailing comment it needs to insert 8 spaces. This should be
one tab plus six spaces. It skips the logic of the first partial tab in
FirstTabWidth (=2) + Style.TabWidth (=8) <= Spaces (=8) and only inserts one
tab. Proposed fix and test is attached.

Patch by Hylke Kleve.

Differential revision: https://reviews.llvm.org/D57655

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=354183&r1=354182&r2=354183&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Fri Feb 15 15:07:43 2019
@@ -679,11 +679,15 @@ void WhitespaceManager::appendIndentText
   case FormatStyle::UT_Always: {
     unsigned FirstTabWidth =
         Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
-    // Indent with tabs only when there's at least one full tab.
-    if (FirstTabWidth + Style.TabWidth <= Spaces) {
-      Spaces -= FirstTabWidth;
-      Text.append("\t");
+    // Insert only spaces when we want to end up before the next tab.
+    if (Spaces < FirstTabWidth || Spaces == 1) {
+      Text.append(Spaces, ' ');
+      break;
     }
+    // Align to the next tab.
+    Spaces -= FirstTabWidth;
+    Text.append("\t");
+
     Text.append(Spaces / Style.TabWidth, '\t');
     Text.append(Spaces % Style.TabWidth, ' ');
     break;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=354183&r1=354182&r2=354183&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Feb 15 15:07:43 2019
@@ -8751,6 +8751,9 @@ TEST_F(FormatTest, ConfigurableUseOfTab)
                "\t\t    parameter2); \\\n"
                "\t}",
                Tab);
+  verifyFormat("int a;\t      // x\n"
+               "int bbbbbbbb; // x\n",
+               Tab);
 
   Tab.TabWidth = 4;
   Tab.IndentWidth = 8;




More information about the cfe-commits mailing list