]> granicus.if.org Git - clang/commitdiff
Print fix-it hints properly around tabs, from Christian Adåker!
authorDouglas Gregor <dgregor@apple.com>
Mon, 18 Jan 2010 19:28:01 +0000 (19:28 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 18 Jan 2010 19:28:01 +0000 (19:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93750 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Frontend/TextDiagnosticPrinter.cpp
test/Misc/tabstop.c

index fcefd4e358224cbf617499192bb4a204762e0e37..83b4542caa255b3e05fe39e21b88b6e3c36cf5f4 100644 (file)
@@ -428,6 +428,42 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
         }
       }
     }
+    // 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
index 3fabda76eb8ca276c249b4892f282311b2b0a221..66685c62d15a94ac301d3c5db1772661f3c7b8ad 100644 (file)
@@ -28,3 +28,23 @@ void* d =    1;
 //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: {{^     }}    (        )