[cfe-commits] r93750 - in /cfe/trunk: lib/Frontend/TextDiagnosticPrinter.cpp test/Misc/tabstop.c

Douglas Gregor dgregor at apple.com
Mon Jan 18 11:28:02 PST 2010


Author: dgregor
Date: Mon Jan 18 13:28:01 2010
New Revision: 93750

URL: http://llvm.org/viewvc/llvm-project?rev=93750&view=rev
Log:
Print fix-it hints properly around tabs, from Christian Adåker!

Modified:
    cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp
    cfe/trunk/test/Misc/tabstop.c

Modified: cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp?rev=93750&r1=93749&r2=93750&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp Mon Jan 18 13:28:01 2010
@@ -428,6 +428,42 @@
         }
       }
     }
+    // Now that we have the entire fixit line, expand the tabs in it.
+    // Since we don't want to insert spaces in the middle of a word,
+    // find each word and the column it should line up with and insert
+    // spaces until they match.
+    if (!FixItInsertionLine.empty()) {
+      unsigned FixItPos = 0;
+      unsigned LinePos = 0;
+      unsigned TabExpandedCol = 0;
+      unsigned LineLength = LineEnd - LineStart;
+
+      while (FixItPos < FixItInsertionLine.size() && LinePos < LineLength) {
+        // Find the next word in the FixIt line.
+        while (FixItPos < FixItInsertionLine.size() &&
+               FixItInsertionLine[FixItPos] == ' ')
+          ++FixItPos;
+        unsigned CharDistance = FixItPos - TabExpandedCol;
+
+        // Walk forward in the source line, keeping track of
+        // the tab-expanded column.
+        for (unsigned I = 0; I < CharDistance; ++I, ++LinePos)
+          if (LinePos >= LineLength || LineStart[LinePos] != '\t')
+            ++TabExpandedCol;
+          else
+            TabExpandedCol =
+              (TabExpandedCol/DiagOpts->TabStop + 1) * DiagOpts->TabStop;
+
+        // Adjust the fixit line to match this column.
+        FixItInsertionLine.insert(FixItPos, TabExpandedCol-FixItPos, ' ');
+        FixItPos = TabExpandedCol;
+
+        // Walk to the end of the word.
+        while (FixItPos < FixItInsertionLine.size() &&
+               FixItInsertionLine[FixItPos] != ' ')
+          ++FixItPos;
+      }
+    }
   }
 
   // If the source line is too long for our terminal, select only the

Modified: cfe/trunk/test/Misc/tabstop.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/tabstop.c?rev=93750&r1=93749&r2=93750&view=diff

==============================================================================
--- cfe/trunk/test/Misc/tabstop.c (original)
+++ cfe/trunk/test/Misc/tabstop.c Mon Jan 18 13:28:01 2010
@@ -28,3 +28,23 @@
 //CHECK-5: {{^          void\* b = 1;}}
 //CHECK-5: {{^     void\* c = 1;}}
 //CHECK-5: {{^void\* d = 1;}}
+
+// Test code modification hints
+
+void f(void)
+{
+	if (0	& 1	== 1)
+	{}
+}
+
+// CHECK-3: {{^   }}if (0 & 1   == 1)
+// CHECK-3: {{^   }}        (       )
+// CHECK-3: {{^   }}    (    )
+
+// CHECK-4: {{^    }}if (0   & 1 == 1)
+// CHECK-4: {{^    }}          (     )
+// CHECK-4: {{^    }}    (      )
+
+// CHECK-5: {{^     }}if (0     & 1  == 1)
+// CHECK-5: {{^     }}            (      )
+// CHECK-5: {{^     }}    (        )





More information about the cfe-commits mailing list