Author: d0k
Date: Wed May 18 08:32:38 2016
New Revision: 269923

URL: http://llvm.org/viewvc/llvm-project?rev=269923&view=rev
Log:
[include-fixer] Don't insert #includes if a fatal error occurred.

This typically happens when the user didn't setup include paths correctly
and the fixer starts adding garbage includes. Avoid that. Disable the error
limit though, as we might hit that easily with missing includes and still
want to fix those cases.

Added:
    clang-tools-extra/trunk/test/include-fixer/exit_on_fatal.cpp
Modified:
    clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
    clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.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=269923&r1=269922&r2=269923&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp Wed May 18 08:32:38 
2016
@@ -390,6 +390,10 @@ bool IncludeFixerActionFactory::runInvoc
                              /*ShouldOwnClient=*/true);
   Compiler.createSourceManager(*Files);
 
+  // We abort on fatal errors so don't let a large number of errors become
+  // fatal. A missing #include can cause thousands of errors.
+  Compiler.getDiagnostics().setErrorLimit(0);
+
   // Run the parser, gather missing includes.
   auto ScopedToolAction =
       llvm::make_unique<Action>(SymbolIndexMgr, MinimizeIncludePaths);
@@ -401,8 +405,9 @@ bool IncludeFixerActionFactory::runInvoc
                             Replacements);
 
   // Technically this should only return true if we're sure that we have a
-  // parseable file. We don't know that though.
-  return true;
+  // parseable file. We don't know that though. Only inform users of fatal
+  // errors.
+  return !Compiler.getDiagnostics().hasFatalErrorOccurred();
 }
 
 } // namespace include_fixer

Modified: clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp?rev=269923&r1=269922&r2=269923&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp Wed May 18 
08:32:38 2016
@@ -108,7 +108,11 @@ int includeFixerMain(int argc, const cha
   include_fixer::IncludeFixerActionFactory Factory(
       *SymbolIndexMgr, Replacements, MinimizeIncludePaths);
 
-  tool.run(&Factory); // Always succeeds.
+  if (tool.run(&Factory) != 0) {
+    llvm::errs()
+        << "Clang died with a fatal error! (incorrect include paths?)\n";
+    return 1;
+  }
 
   if (!Quiet)
     for (const tooling::Replacement &Replacement : Replacements)

Added: clang-tools-extra/trunk/test/include-fixer/exit_on_fatal.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/include-fixer/exit_on_fatal.cpp?rev=269923&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/include-fixer/exit_on_fatal.cpp (added)
+++ clang-tools-extra/trunk/test/include-fixer/exit_on_fatal.cpp Wed May 18 
08:32:38 2016
@@ -0,0 +1,11 @@
+// REQUIRES: shell
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: not clang-include-fixer -db=fixed -input='foo= "foo.h"' %t.cpp --
+// RUN: FileCheck %s -input-file=%t.cpp
+
+// CHECK-NOT: #include
+// CHECK: #include "doesnotexist.h"
+// CHECK-NEXT: foo f;
+
+#include "doesnotexist.h"
+foo f;


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to