[clang] [clang] Remove pointless `hasDiagnostics()` checks (PR #172705)
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 17 11:22:06 PST 2025
https://github.com/jansvoboda11 updated https://github.com/llvm/llvm-project/pull/172705
>From 4d614fe70c010dc18e05d8aa7396a7209bf9190d Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Wed, 17 Dec 2025 09:46:55 -0800
Subject: [PATCH 1/2] [clang] Remove pointless `hasDiagnostics()` checks
---
.../DependencyScanning/DependencyScannerImpl.h | 2 +-
.../DependencyScannerImpl.cpp | 18 ++++++------------
clang/lib/Tooling/Tooling.cpp | 9 ++-------
clang/unittests/Frontend/CodeGenActionTest.cpp | 2 --
.../Frontend/CompilerInstanceTest.cpp | 6 +++---
.../Tooling/DependencyScannerTest.cpp | 4 ----
6 files changed, 12 insertions(+), 29 deletions(-)
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));
>From 0562795191d0de9fa2a53b8d0b39cd3f187f2bf6 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Wed, 17 Dec 2025 11:21:52 -0800
Subject: [PATCH 2/2] Handle missed calls
---
clang/lib/Interpreter/Interpreter.cpp | 4 ----
clang/tools/clang-installapi/ClangInstallAPI.cpp | 2 --
clang/tools/driver/cc1_main.cpp | 2 --
3 files changed, 8 deletions(-)
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 6cbc5e9910bcc..1a6f6ea0813b7 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -112,10 +112,6 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
// Create the actual diagnostics engine.
Clang->createDiagnostics();
- if (!Clang->hasDiagnostics())
- return llvm::createStringError(llvm::errc::not_supported,
- "Initialization failed. "
- "Unable to create diagnostics engine");
DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
if (!Success)
diff --git a/clang/tools/clang-installapi/ClangInstallAPI.cpp b/clang/tools/clang-installapi/ClangInstallAPI.cpp
index 8bef9690ad855..baa10ff3e2812 100644
--- a/clang/tools/clang-installapi/ClangInstallAPI.cpp
+++ b/clang/tools/clang-installapi/ClangInstallAPI.cpp
@@ -117,8 +117,6 @@ static bool run(ArrayRef<const char *> Args, const char *ProgName) {
CI->setVirtualFileSystem(FM->getVirtualFileSystemPtr());
CI->setFileManager(FM);
CI->createDiagnostics();
- if (!CI->hasDiagnostics())
- return EXIT_FAILURE;
// Execute, verify and gather AST results.
// An invocation is ran for each unique target triple and for each header
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index 550d61cdb8422..1adb217014973 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -282,8 +282,6 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
// Create the actual diagnostics engine.
Clang->createDiagnostics();
- if (!Clang->hasDiagnostics())
- return 1;
// Set an error handler, so that any LLVM backend diagnostics go through our
// error handler.
More information about the cfe-commits
mailing list