]> granicus.if.org Git - clang/commitdiff
Simplify FixItHint by eliminated the unnecessary InsertionLoc
authorDouglas Gregor <dgregor@apple.com>
Wed, 18 Aug 2010 14:24:02 +0000 (14:24 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 18 Aug 2010 14:24:02 +0000 (14:24 +0000)
location. Patch by Eelis van der Weegen!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111362 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/Diagnostic.h
lib/Basic/Diagnostic.cpp
lib/Frontend/TextDiagnosticPrinter.cpp
lib/Rewrite/FixItRewriter.cpp
tools/libclang/CIndexDiagnostic.cpp

index 4371bdda0a8602bf0dd421856d19418b9faebcb3..6eafa03b97a3cf92ae481d2565c88808d5c98a02 100644 (file)
@@ -95,23 +95,20 @@ namespace clang {
 /// compilation.
 class FixItHint {
 public:
-  /// \brief Code that should be removed to correct the error.
+  /// \brief Code that should be replaced to correct the error. Empty for an
+  /// insertion hint.
   CharSourceRange RemoveRange;
 
-  /// \brief The location at which we should insert code to correct
-  /// the error.
-  SourceLocation InsertionLoc;
-
   /// \brief The actual code to insert at the insertion location, as a
   /// string.
   std::string CodeToInsert;
 
   /// \brief Empty code modification hint, indicating that no code
   /// modification is known.
-  FixItHint() : RemoveRange(), InsertionLoc() { }
+  FixItHint() : RemoveRange() { }
 
   bool isNull() const {
-    return !RemoveRange.isValid() && !InsertionLoc.isValid();
+    return !RemoveRange.isValid();
   }
   
   /// \brief Create a code modification hint that inserts the given
@@ -119,7 +116,8 @@ public:
   static FixItHint CreateInsertion(SourceLocation InsertionLoc,
                                    llvm::StringRef Code) {
     FixItHint Hint;
-    Hint.InsertionLoc = InsertionLoc;
+    Hint.RemoveRange =
+      CharSourceRange(SourceRange(InsertionLoc, InsertionLoc), false);
     Hint.CodeToInsert = Code;
     return Hint;
   }
@@ -141,7 +139,6 @@ public:
                                      llvm::StringRef Code) {
     FixItHint Hint;
     Hint.RemoveRange = RemoveRange;
-    Hint.InsertionLoc = RemoveRange.getBegin();
     Hint.CodeToInsert = Code;
     return Hint;
   }
index d4c7aa9521711e3dd846d0d5fb7b4d570024df4e..d8095f4f6d543a1fca52b77f61f340cd1ddb4650 100644 (file)
@@ -1152,11 +1152,6 @@ void StoredDiagnostic::Serialize(llvm::raw_ostream &OS) const {
       break;
     }
 
-    if (F->InsertionLoc.isValid() && F->InsertionLoc.isMacroID()) {
-      NumFixIts = 0;
-      break;
-    }
-
     ++NumFixIts;
   }
 
@@ -1166,7 +1161,6 @@ void StoredDiagnostic::Serialize(llvm::raw_ostream &OS) const {
     WriteSourceLocation(OS, SM, F->RemoveRange.getBegin());
     WriteSourceLocation(OS, SM, F->RemoveRange.getEnd());
     WriteUnsigned(OS, F->RemoveRange.isTokenRange());
-    WriteSourceLocation(OS, SM, F->InsertionLoc);
     WriteString(OS, F->CodeToInsert);
   }
 }
@@ -1294,12 +1288,11 @@ StoredDiagnostic::Deserialize(FileManager &FM, SourceManager &SM,
   if (ReadUnsigned(Memory, MemoryEnd, NumFixIts))
     return Diag;
   for (unsigned I = 0; I != NumFixIts; ++I) {
-    SourceLocation RemoveBegin, RemoveEnd, InsertionLoc;
+    SourceLocation RemoveBegin, RemoveEnd;
     unsigned InsertLen = 0, RemoveIsTokenRange;
     if (ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveBegin) ||
         ReadSourceLocation(FM, SM, Memory, MemoryEnd, RemoveEnd) ||
         ReadUnsigned(Memory, MemoryEnd, RemoveIsTokenRange) ||
-        ReadSourceLocation(FM, SM, Memory, MemoryEnd, InsertionLoc) ||
         ReadUnsigned(Memory, MemoryEnd, InsertLen) ||
         Memory + InsertLen > MemoryEnd) {
       Diag.FixIts.clear();
@@ -1309,7 +1302,6 @@ StoredDiagnostic::Deserialize(FileManager &FM, SourceManager &SM,
     FixItHint Hint;
     Hint.RemoveRange = CharSourceRange(SourceRange(RemoveBegin, RemoveEnd),
                                        RemoveIsTokenRange);
-    Hint.InsertionLoc = InsertionLoc;
     Hint.CodeToInsert.assign(Memory, Memory + InsertLen);
     Memory += InsertLen;
     Diag.FixIts.push_back(Hint);
index 1b5b7e2ea863fd1778ae2d32171a5619e19b3f5c..bc1b50475bce0f796e793a3778a62931a52960e2 100644 (file)
@@ -447,11 +447,11 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
   if (NumHints && DiagOpts->ShowFixits) {
     for (const FixItHint *Hint = Hints, *LastHint = Hints + NumHints;
          Hint != LastHint; ++Hint) {
-      if (Hint->InsertionLoc.isValid()) {
+      if (!Hint->CodeToInsert.empty()) {
         // We have an insertion hint. Determine whether the inserted
         // code is on the same line as the caret.
         std::pair<FileID, unsigned> HintLocInfo
-          = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc);
+          = SM.getDecomposedInstantiationLoc(Hint->RemoveRange.getBegin());
         if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) ==
               SM.getLineNumber(FID, FileOffset)) {
           // Insert the new code into the line just below the code
index df7b60547a4ebbecf4bef4056ef8de0fa3b613d1..3fdec07459d6bde792c5fc181e6592e66ceade94 100644 (file)
@@ -96,12 +96,6 @@ void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
       CanRewrite = false;
       break;
     }
-
-    if (Hint.InsertionLoc.isValid() &&
-        !Rewrite.isRewritable(Hint.InsertionLoc)) {
-      CanRewrite = false;
-      break;
-    }
   }
 
   if (!CanRewrite) {
@@ -120,12 +114,6 @@ void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
   for (unsigned Idx = 0, Last = Info.getNumFixItHints();
        Idx < Last; ++Idx) {
     const FixItHint &Hint = Info.getFixItHint(Idx);
-    if (!Hint.RemoveRange.isValid()) {
-      // We're adding code.
-      if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
-        Failed = true;
-      continue;
-    }
 
     if (Hint.CodeToInsert.empty()) {
       // We're removing code.
index 3db37b97da145fc21146c0c94a4ee8acd3b8e182..531992efebcec35c77ff5cc82c3f878be931a736 100644 (file)
@@ -204,26 +204,13 @@ CXString clang_getDiagnosticFixIt(CXDiagnostic Diagnostic, unsigned FixIt,
 
   const FixItHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt];
   if (ReplacementRange) {
-    if (Hint.RemoveRange.isInvalid())  {
-      // Create an empty range that refers to a single source
-      // location (which is the insertion point).
-      CXSourceRange Range = { 
-        { (void *)&StoredDiag->Diag.getLocation().getManager(), 
-          (void *)&StoredDiag->LangOpts },
-        Hint.InsertionLoc.getRawEncoding(),
-        Hint.InsertionLoc.getRawEncoding() 
-      };
-
-      *ReplacementRange = Range;
-    } else {
-      // Create a range that covers the entire replacement (or
-      // removal) range, adjusting the end of the range to point to
-      // the end of the token.
-      *ReplacementRange
-          = translateSourceRange(StoredDiag->Diag.getLocation().getManager(),
-                                 StoredDiag->LangOpts,
-                                 Hint.RemoveRange);
-    }
+    // Create a range that covers the entire replacement (or
+    // removal) range, adjusting the end of the range to point to
+    // the end of the token.
+    *ReplacementRange
+        = translateSourceRange(StoredDiag->Diag.getLocation().getManager(),
+                                StoredDiag->LangOpts,
+                                Hint.RemoveRange);
   }
 
   return createCXString(Hint.CodeToInsert);