llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Jan Svoboda (jansvoboda11)

<details>
<summary>Changes</summary>

Calling `CompilerInstance::hasDiagnostics()` after 
`CompilerInstance::createDiagnostics()` is pointless, since the creation step 
cannot fail. This removes such calls.

---
Full diff: https://github.com/llvm/llvm-project/pull/172705.diff


6 Files Affected:

- (modified) clang/include/clang/DependencyScanning/DependencyScannerImpl.h 
(+1-1) 
- (modified) clang/lib/DependencyScanning/DependencyScannerImpl.cpp (+6-12) 
- (modified) clang/lib/Tooling/Tooling.cpp (+2-7) 
- (modified) clang/unittests/Frontend/CodeGenActionTest.cpp (-2) 
- (modified) clang/unittests/Frontend/CompilerInstanceTest.cpp (+3-3) 
- (modified) clang/unittests/Tooling/DependencyScannerTest.cpp (-4) 


``````````diff
diff --git a/clang/include/clang/DependencyScanning/DependencyScannerImpl.h 
b/clang/include/clang/DependencyScanning/DependencyScannerImpl.h
index f43c7f70183fd..f155e344b48ed 100644
--- a/clang/include/clang/DependencyScanning/DependencyScannerImpl.h
+++ b/clang/include/clang/DependencyScanning/DependencyScannerImpl.h
@@ -111,7 +111,7 @@ 
initVFSForByNameScanning(IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
                          ArrayRef<std::string> CommandLine,
                          StringRef WorkingDirectory, StringRef ModuleName);
 
-bool initializeScanCompilerInstance(
+void initializeScanCompilerInstance(
     CompilerInstance &ScanInstance,
     IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
     DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
diff --git a/clang/lib/DependencyScanning/DependencyScannerImpl.cpp 
b/clang/lib/DependencyScanning/DependencyScannerImpl.cpp
index 201230d7d6a8e..06901b28b6f87 100644
--- a/clang/lib/DependencyScanning/DependencyScannerImpl.cpp
+++ b/clang/lib/DependencyScanning/DependencyScannerImpl.cpp
@@ -485,7 +485,7 @@ dependencies::initVFSForByNameScanning(
   return std::make_pair(OverlayFS, ModifiedCommandLine);
 }
 
-bool dependencies::initializeScanCompilerInstance(
+void dependencies::initializeScanCompilerInstance(
     CompilerInstance &ScanInstance,
     IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
     DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
@@ -497,8 +497,6 @@ bool dependencies::initializeScanCompilerInstance(
   // Create the compiler's actual diagnostics engine.
   sanitizeDiagOpts(ScanInstance.getDiagnosticOpts());
   ScanInstance.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
-  if (!ScanInstance.hasDiagnostics())
-    return false;
 
   ScanInstance.getPreprocessorOpts().AllowPCHWithDifferentModulesCachePath =
       true;
@@ -553,8 +551,6 @@ bool dependencies::initializeScanCompilerInstance(
 
   // Avoid some checks and module map parsing when loading PCM files.
   ScanInstance.getPreprocessorOpts().ModulesCheckRelocated = false;
-
-  return true;
 }
 
 llvm::SmallVector<StringRef>
@@ -670,9 +666,8 @@ bool DependencyScanningAction::runInvocation(
   CompilerInstance &ScanInstance = *ScanInstanceStorage;
 
   assert(!DiagConsumerFinished && "attempt to reuse finished consumer");
-  if (!initializeScanCompilerInstance(ScanInstance, FS, DiagConsumer, Service,
-                                      DepFS))
-    return false;
+  initializeScanCompilerInstance(ScanInstance, FS, DiagConsumer, Service,
+                                 DepFS);
 
   llvm::SmallVector<StringRef> StableDirs = getInitialStableDirs(ScanInstance);
   auto MaybePrebuiltModulesASTMap =
@@ -748,10 +743,9 @@ bool CompilerInstanceWithContext::initialize(
       Worker.PCHContainerOps, ModCache.get());
   auto &CI = *CIPtr;
 
-  if (!initializeScanCompilerInstance(
-          CI, OverlayFS, DiagEngineWithCmdAndOpts->DiagEngine->getClient(),
-          Worker.Service, Worker.DepFS))
-    return false;
+  initializeScanCompilerInstance(
+      CI, OverlayFS, DiagEngineWithCmdAndOpts->DiagEngine->getClient(),
+      Worker.Service, Worker.DepFS);
 
   StableDirs = getInitialStableDirs(CI);
   auto MaybePrebuiltModulesASTMap =
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 46b2cc1ac99c1..f10aa524674da 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -447,19 +447,14 @@ bool FrontendActionFactory::runInvocation(
   CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
   Compiler.setVirtualFileSystem(Files->getVirtualFileSystemPtr());
   Compiler.setFileManager(Files);
+  Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
+  Compiler.createSourceManager();
 
   // The FrontendAction can have lifetime requirements for Compiler or its
   // members, and we need to ensure it's deleted earlier than Compiler. So we
   // pass it to an std::unique_ptr declared after the Compiler variable.
   std::unique_ptr<FrontendAction> ScopedToolAction(create());
 
-  // Create the compiler's actual diagnostics engine.
-  Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
-  if (!Compiler.hasDiagnostics())
-    return false;
-
-  Compiler.createSourceManager();
-
   const bool Success = Compiler.ExecuteAction(*ScopedToolAction);
 
   Files->clearStatCache();
diff --git a/clang/unittests/Frontend/CodeGenActionTest.cpp 
b/clang/unittests/Frontend/CodeGenActionTest.cpp
index 182afdc7ea313..d2befe1af4a59 100644
--- a/clang/unittests/Frontend/CodeGenActionTest.cpp
+++ b/clang/unittests/Frontend/CodeGenActionTest.cpp
@@ -54,7 +54,6 @@ TEST(CodeGenTest, TestNullCodeGen) {
   CompilerInstance Compiler(std::move(Invocation));
   Compiler.setVirtualFileSystem(llvm::vfs::getRealFileSystem());
   Compiler.createDiagnostics();
-  EXPECT_TRUE(Compiler.hasDiagnostics());
 
   std::unique_ptr<FrontendAction> Act(new NullCodeGenAction);
   bool Success = Compiler.ExecuteAction(*Act);
@@ -72,7 +71,6 @@ TEST(CodeGenTest, CodeGenFromIRMemBuffer) {
   CompilerInstance Compiler(std::move(Invocation));
   Compiler.setVirtualFileSystem(llvm::vfs::getRealFileSystem());
   Compiler.createDiagnostics();
-  EXPECT_TRUE(Compiler.hasDiagnostics());
 
   EmitLLVMOnlyAction Action;
   bool Success = Compiler.ExecuteAction(Action);
diff --git a/clang/unittests/Frontend/CompilerInstanceTest.cpp 
b/clang/unittests/Frontend/CompilerInstanceTest.cpp
index 39d35b48f394a..f9646b650d12b 100644
--- a/clang/unittests/Frontend/CompilerInstanceTest.cpp
+++ b/clang/unittests/Frontend/CompilerInstanceTest.cpp
@@ -93,10 +93,10 @@ TEST(CompilerInstance, 
AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
   llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput);
   auto DiagPrinter =
       std::make_unique<TextDiagnosticPrinter>(DiagnosticsOS, DiagOpts);
-  CompilerInstance Instance;
   IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
-      Instance.createDiagnostics(*llvm::vfs::getRealFileSystem(), DiagOpts,
-                                 DiagPrinter.get(), /*ShouldOwnClient=*/false);
+      CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
+                                          DiagOpts, DiagPrinter.get(),
+                                          /*ShouldOwnClient=*/false);
 
   Diags->Report(diag::err_expected) << "no crash";
   ASSERT_EQ(DiagnosticOutput, "error: expected no crash\n");
diff --git a/clang/unittests/Tooling/DependencyScannerTest.cpp 
b/clang/unittests/Tooling/DependencyScannerTest.cpp
index 3874b40ff8461..42d1a7b242aa9 100644
--- a/clang/unittests/Tooling/DependencyScannerTest.cpp
+++ b/clang/unittests/Tooling/DependencyScannerTest.cpp
@@ -64,11 +64,7 @@ class TestDependencyScanningAction : public 
tooling::ToolAction {
                               std::move(PCHContainerOps));
     Compiler.setVirtualFileSystem(FileMgr->getVirtualFileSystemPtr());
     Compiler.setFileManager(FileMgr);
-
     Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
-    if (!Compiler.hasDiagnostics())
-      return false;
-
     Compiler.createSourceManager();
     Compiler.addDependencyCollector(std::make_shared<TestFileCollector>(
         Compiler.getInvocation().getDependencyOutputOpts(), Deps));

``````````

</details>


https://github.com/llvm/llvm-project/pull/172705
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to