[include-cleaner] Bailout on invalid code for the command-line tool
authorHaojian Wu <hokein.wu@gmail.com>
Mon, 19 Jun 2023 12:57:09 +0000 (14:57 +0200)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 20 Jun 2023 07:07:27 +0000 (09:07 +0200)
The binary tool only works on working source code, if the source code is
not compilable, don't perform any analysis and edits.

Differential Revision: https://reviews.llvm.org/D153271

clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp

index 918f7c9..008da47 100644 (file)
@@ -108,10 +108,18 @@ class Action : public clang::ASTFrontendAction {
   }
 
   void EndSourceFile() override {
+    const auto &SM = getCompilerInstance().getSourceManager();
+    if (SM.getDiagnostics().hasUncompilableErrorOccurred()) {
+      llvm::errs()
+          << "Skipping file " << getCurrentFile()
+          << " due to compiler errors. clang-include-cleaner expects to "
+             "work on compilable source code.\n";
+      return;
+    }
+
     if (!HTMLReportPath.empty())
       writeHTML();
 
-    const auto &SM = getCompilerInstance().getSourceManager();
     auto &HS = getCompilerInstance().getPreprocessor().getHeaderSearchInfo();
     llvm::StringRef Path =
         SM.getFileEntryForID(SM.getMainFileID())->tryGetRealPathName();