[clang] Remove pointless hasDiagnostics() checks (#172705)

Calling `CompilerInstance::hasDiagnostics()` after
`CompilerInstance::createDiagnostics()` is pointless, since the creation
step cannot fail. This removes such calls.
This commit is contained in:
Jan Svoboda 2025-12-17 12:18:28 -08:00 committed by GitHub
parent f09f578c0d
commit db1fd3fd2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 12 additions and 37 deletions

View File

@ -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,

View File

@ -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 =

View File

@ -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)

View File

@ -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();

View File

@ -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

View File

@ -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.

View File

@ -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);

View File

@ -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");

View File

@ -64,11 +64,7 @@ public:
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));