[cfe-commits] r68322 - in /cfe/trunk/lib/Frontend: CMakeLists.txt FixItRewriter.cpp

Douglas Gregor dgregor at apple.com
Thu Apr 2 09:34:42 PDT 2009


Author: dgregor
Date: Thu Apr  2 11:34:42 2009
New Revision: 68322

URL: http://llvm.org/viewvc/llvm-project?rev=68322&view=rev
Log:
Some cleanups to the fix-it rewriter. Thanks, Chris

Modified:
    cfe/trunk/lib/Frontend/CMakeLists.txt
    cfe/trunk/lib/Frontend/FixItRewriter.cpp

Modified: cfe/trunk/lib/Frontend/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CMakeLists.txt?rev=68322&r1=68321&r2=68322&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/CMakeLists.txt (original)
+++ cfe/trunk/lib/Frontend/CMakeLists.txt Thu Apr  2 11:34:42 2009
@@ -1,6 +1,7 @@
 set(LLVM_NO_RTTI 1)
 
 add_clang_library(clangFrontend
+  FixItRewriter.cpp
   HTMLDiagnostics.cpp
   InitHeaderSearch.cpp
   TextDiagnosticBuffer.cpp

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

==============================================================================
--- cfe/trunk/lib/Frontend/FixItRewriter.cpp (original)
+++ cfe/trunk/lib/Frontend/FixItRewriter.cpp Thu Apr  2 11:34:42 2009
@@ -12,8 +12,9 @@
 // then forwards any diagnostics to the adapted diagnostic client.
 //
 //===----------------------------------------------------------------------===//
-#include "clang/Basic/SourceManager.h"
+
 #include "clang/Frontend/FixItRewriter.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Rewrite/Rewriter.h"
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/Support/Streams.h"
@@ -37,16 +38,14 @@
   if (NumFailures > 0) {
     // FIXME: Use diagnostic machinery!
     std::fprintf(stderr, 
-                 "%d fix-it failures detected; code will not be modified",
+                 "%d fix-it failures detected; code will not be modified\n",
                  NumFailures);
     return true;
   }
 
   llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
   llvm::raw_ostream *OutFile;
-  if (OutFileName == "-") {
-    OutFile = &llvm::outs();
-  } else if (!OutFileName.empty()) {
+  if (!OutFileName.empty()) {
     std::string Err;
     OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), 
                                        // set binary mode (critical for Windoze)
@@ -81,7 +80,7 @@
 }
 
 bool FixItRewriter::IncludeInDiagnosticCounts() const {
-  return Client? Client->IncludeInDiagnosticCounts() : false;
+  return Client? Client->IncludeInDiagnosticCounts() : true;
 }
 
 void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
@@ -91,13 +90,12 @@
 
   // Make sure that we can perform all of the modifications we
   // in this diagnostic.
-  bool CanRewrite = true;
-  for (unsigned Idx = 0; Idx < Info.getNumCodeModificationHints(); ++Idx) {
+  bool CanRewrite = Info.getNumCodeModificationHints() > 0;
+  for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
+       Idx < Last; ++Idx) {
     const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
     if (Hint.RemoveRange.isValid() &&
-        (!Rewrite->isRewritable(Hint.RemoveRange.getBegin()) ||
-         !Rewrite->isRewritable(Hint.RemoveRange.getEnd()) ||
-         Rewrite->getRangeSize(Hint.RemoveRange) == -1)) {
+        Rewrite->getRangeSize(Hint.RemoveRange) == -1) {
       CanRewrite = false;
       break;
     }
@@ -109,33 +107,49 @@
     }
   }
 
-  if (!CanRewrite) // FIXME: warn the user that this rewrite couldn't be done
+  if (!CanRewrite) { 
+    if (Info.getNumCodeModificationHints() > 0) {
+      // FIXME: warn the user that this rewrite couldn't be done
+    }
+
+    // If this was an error, refuse to perform any rewriting.
+    if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
+      if (++NumFailures == 1) {
+        // FIXME: use diagnostic machinery to print this.
+        std::fprintf(stderr, "error without fix-it advice detected; "
+                     "fix-it will produce no output\n");
+      }
+    }
     return;
+  }
 
   bool Failed = false;
-  for (unsigned Idx = 0; Idx < Info.getNumCodeModificationHints(); ++Idx) {
+  for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints(); 
+       Idx < Last; ++Idx) {
     const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
-    if (Hint.RemoveRange.isValid()) {
-      if (Hint.CodeToInsert.empty()) {
-        // We're removing code.
-        if (Rewrite->RemoveText(Hint.RemoveRange.getBegin(),
-                                Rewrite->getRangeSize(Hint.RemoveRange)))
-          Failed = true;
-      } else {
-        // We're replacing code.
-        if (Rewrite->ReplaceText(Hint.RemoveRange.getBegin(),
-                                 Rewrite->getRangeSize(Hint.RemoveRange),
-                                 Hint.CodeToInsert.c_str(),
-                                 Hint.CodeToInsert.size()))
-          Failed = true;
-      }
-    } else {
+    if (!Hint.RemoveRange.isValid()) {
       // We're adding code.
       if (Rewrite->InsertStrBefore(Hint.InsertionLoc, Hint.CodeToInsert))
         Failed = true;
+      continue;
     }
+    
+    if (Hint.CodeToInsert.empty()) {
+      // We're removing code.
+      if (Rewrite->RemoveText(Hint.RemoveRange.getBegin(),
+                              Rewrite->getRangeSize(Hint.RemoveRange)))
+        Failed = true;
+      continue;
+    } 
+      
+    // We're replacing code.
+    if (Rewrite->ReplaceText(Hint.RemoveRange.getBegin(),
+                             Rewrite->getRangeSize(Hint.RemoveRange),
+                             Hint.CodeToInsert.c_str(),
+                             Hint.CodeToInsert.size()))
+      Failed = true;
   }
 
-  if (Failed)
+  if (Failed) // FIXME: notify the user that the rewrite failed.
     ++NumFailures;
 }





More information about the cfe-commits mailing list