[cfe-commits] [PATCH] Add -fmachine-readable-fixits.

Douglas Gregor dgregor at apple.com
Wed Aug 18 15:58:28 PDT 2010


On Aug 18, 2010, at 1:30 PM, Eelis van der Weegen wrote:

> Attached is my first stab at adding an option to print machine readable
> fix-its. The output format and option name are a bit awkward, but I
> couldn't come up with anything better..
> 
> Please review. :)

How about -fdiagnostics-parseable-fixits?

--- a/lib/Frontend/TextDiagnosticPrinter.cpp
+++ b/lib/Frontend/TextDiagnosticPrinter.cpp
@@ -537,6 +537,37 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
     if (DiagOpts->ShowColors)
       OS.resetColor();
   }
+
+  if (DiagOpts->ShowMachineReadableFixits) {
+    for (const FixItHint *Hint = Hints; Hint != Hints + NumHints; ++Hint) {
+
+      const CharSourceRange & R = Hint->RemoveRange;
+
+      SourceLocation B = R.getBegin();
+      SourceLocation E = R.getEnd();
+
+      // We follow FixItRewriter's example in not (yet) handling
+      // fix-its in macros.
+      if (R.isInvalid() || B.isMacroID() || E.isMacroID())
+        continue;

If any of the hints contains a macro ID, we don't want to emit any of the fix-its. Otherwise, we could end up printing a partial fix.

+
+      std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
+      std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
+
+      // Adjust for token ranges.
+      if (R.isTokenRange())
+        EInfo.second += Lexer::MeasureTokenLength(E, SM, *LangOpts);
+
+      // We specifically do not do word-wrapping or tab-expansion,
+      // because this is supposed to be machine readable.
+      OS << " fix: " << SM.getPresumedLoc(B).getFilename()

I have a slight preference for "fix-it:". Plus, the file name should be enclosed in quotes and escaped.

+         << ":{" << SM.getLineNumber(BInfo.first, BInfo.second)
+         << ':' << SM.getColumnNumber(BInfo.first, BInfo.second)
+         << '-' << SM.getLineNumber(EInfo.first, EInfo.second)
+         << ':' << SM.getColumnNumber(EInfo.first, EInfo.second)
+         << "}: " << Hint->CodeToInsert << '\n';

CodeToInsert should be enclosed in quotes and use escaping, since we may end up with an empty string, a string involving newlines, etc.

  - Doug



More information about the cfe-commits mailing list