[clang-tools-extra] r292405 - [include-fixer] Don't return a correction if the header insertion failed.

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 18 08:22:58 PST 2017


Author: d0k
Date: Wed Jan 18 10:22:58 2017
New Revision: 292405

URL: http://llvm.org/viewvc/llvm-project?rev=292405&view=rev
Log:
[include-fixer] Don't return a correction if the header insertion failed.

This is could happen in cases involving macros and we don't want to
return an invalid fixit for it or a confusing error message with no
fixit.

Modified:
    clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp

Modified: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp?rev=292405&r1=292404&r2=292405&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp Wed Jan 18 10:22:58 2017
@@ -118,14 +118,14 @@ bool IncludeFixerActionFactory::runInvoc
   return !Compiler.getDiagnostics().hasFatalErrorOccurred();
 }
 
-static void addDiagnosticsForContext(TypoCorrection &Correction,
+static bool addDiagnosticsForContext(TypoCorrection &Correction,
                                      const IncludeFixerContext &Context,
                                      StringRef Code, SourceLocation StartOfFile,
                                      ASTContext &Ctx) {
   auto Reps = createIncludeFixerReplacements(
       Code, Context, format::getLLVMStyle(), /*AddQualifiers=*/false);
-  if (!Reps)
-    return;
+  if (!Reps || Reps->size() != 1)
+    return false;
 
   unsigned DiagID = Ctx.getDiagnostics().getCustomDiagID(
       DiagnosticsEngine::Note, "Add '#include %0' to provide the missing "
@@ -133,7 +133,6 @@ static void addDiagnosticsForContext(Typ
 
   // FIXME: Currently we only generate a diagnostic for the first header. Give
   // the user choices.
-  assert(Reps->size() == 1 && "Expected exactly one replacement");
   const tooling::Replacement &Placed = *Reps->begin();
 
   auto Begin = StartOfFile.getLocWithOffset(Placed.getOffset());
@@ -143,6 +142,7 @@ static void addDiagnosticsForContext(Typ
      << FixItHint::CreateReplacement(CharSourceRange::getCharRange(Begin, End),
                                      Placed.getReplacementText());
   Correction.addExtraDiagnostic(std::move(PD));
+  return true;
 }
 
 /// Callback for incomplete types. If we encounter a forward declaration we
@@ -286,12 +286,12 @@ clang::TypoCorrection IncludeFixerSemaSo
     FileID FID = SM.getFileID(Typo.getLoc());
     StringRef Code = SM.getBufferData(FID);
     SourceLocation StartOfFile = SM.getLocForStartOfFile(FID);
-    addDiagnosticsForContext(
-        Correction,
-        getIncludeFixerContext(SM, CI->getPreprocessor().getHeaderSearchInfo(),
-                               MatchedSymbols),
-        Code, StartOfFile, CI->getASTContext());
-    return Correction;
+    if (addDiagnosticsForContext(
+            Correction, getIncludeFixerContext(
+                            SM, CI->getPreprocessor().getHeaderSearchInfo(),
+                            MatchedSymbols),
+            Code, StartOfFile, CI->getASTContext()))
+      return Correction;
   }
   return TypoCorrection();
 }




More information about the cfe-commits mailing list