-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang][frontend] Require invocation to construct CompilerInstance
#137668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang][frontend] Require invocation to construct CompilerInstance
#137668
Conversation
|
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: Jan Svoboda (jansvoboda11) ChangesThis PR makes it so that
Patch is 41.12 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/137668.diff 33 Files Affected:
diff --git a/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp b/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
index bba8f8acc77da..7b0e4ecda8214 100644
--- a/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
+++ b/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
@@ -89,8 +89,7 @@ bool IncludeFixerActionFactory::runInvocation(
assert(Invocation->getFrontendOpts().Inputs.size() == 1);
// Set up Clang.
- clang::CompilerInstance Compiler(PCHContainerOps);
- Compiler.setInvocation(std::move(Invocation));
+ CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
Compiler.setFileManager(Files);
// Create the compiler's actual diagnostics engine. We want to drop all
diff --git a/clang-tools-extra/clangd/Compiler.cpp b/clang-tools-extra/clangd/Compiler.cpp
index 161cc9ae0ca36..9be0152afd2f7 100644
--- a/clang-tools-extra/clangd/Compiler.cpp
+++ b/clang-tools-extra/clangd/Compiler.cpp
@@ -145,9 +145,7 @@ prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
}
- auto Clang = std::make_unique<CompilerInstance>(
- std::make_shared<PCHContainerOperations>());
- Clang->setInvocation(std::move(CI));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(CI));
Clang->createDiagnostics(*VFS, &DiagsClient, false);
if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
diff --git a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
index b1bbb2eb82414..a10c0d5a54a95 100644
--- a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -618,14 +618,14 @@ TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
llvm::MemoryBuffer::getMemBufferCopy(Extra.getValue(),
/*BufferName=*/""));
- auto Clang = std::make_unique<CompilerInstance>(
- std::make_shared<PCHContainerOperations>());
- Clang->createDiagnostics(*VFS);
+ auto DiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
+ auto Diags = CompilerInstance::createDiagnostics(*VFS, DiagOpts.get());
+ auto Invocation = std::make_unique<CompilerInvocation>();
+ ASSERT_TRUE(CompilerInvocation::CreateFromArgs(*Invocation, {Filename.data()},
+ *Diags, "clang"));
- Clang->setInvocation(std::make_unique<CompilerInvocation>());
- ASSERT_TRUE(CompilerInvocation::CreateFromArgs(
- Clang->getInvocation(), {Filename.data()}, Clang->getDiagnostics(),
- "clang"));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(Invocation));
+ Clang->createDiagnostics(*VFS);
auto *FM = Clang->createFileManager(VFS);
ASSERT_TRUE(Clang->ExecuteAction(*Inputs.MakeAction()));
diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h
index d70f5c45b3d38..ebfbfc91b79e6 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -197,6 +197,8 @@ class CompilerInstance : public ModuleLoader {
void operator=(const CompilerInstance &) = delete;
public:
explicit CompilerInstance(
+ std::shared_ptr<CompilerInvocation> Invocation =
+ std::make_shared<CompilerInvocation>(),
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>(),
ModuleCache *ModCache = nullptr);
@@ -244,18 +246,10 @@ class CompilerInstance : public ModuleLoader {
/// @name Compiler Invocation and Options
/// @{
- bool hasInvocation() const { return Invocation != nullptr; }
-
- CompilerInvocation &getInvocation() {
- assert(Invocation && "Compiler instance has no invocation!");
- return *Invocation;
- }
+ CompilerInvocation &getInvocation() { return *Invocation; }
std::shared_ptr<CompilerInvocation> getInvocationPtr() { return Invocation; }
- /// setInvocation - Replace the current invocation.
- void setInvocation(std::shared_ptr<CompilerInvocation> Value);
-
/// Indicates whether we should (re)build the global module index.
bool shouldBuildGlobalModuleIndex() const;
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 04ddc93415507..dae08fa344008 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -1162,9 +1162,8 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
}
// Create the compiler instance to use for building the AST.
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(std::move(PCHContainerOps)));
- Clang->setInvocation(CCInvocation);
+ auto Clang = std::make_unique<CompilerInstance>(CCInvocation,
+ std::move(PCHContainerOps));
// Clean up on error, disengage it if the function returns successfully.
auto CleanOnError = llvm::make_scope_exit([&]() {
@@ -1486,7 +1485,6 @@ void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
// Steal the created target, context, and preprocessor if they have been
// created.
- assert(CI.hasInvocation() && "missing invocation");
LangOpts = CI.getInvocation().LangOpts;
TheSema = CI.takeSema();
Consumer = CI.takeASTConsumer();
@@ -1596,14 +1594,13 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
AST->getFileManager().getVirtualFileSystem());
// Create the compiler instance to use for building the AST.
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(std::move(PCHContainerOps)));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(CI),
+ std::move(PCHContainerOps));
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
CICleanup(Clang.get());
- Clang->setInvocation(std::move(CI));
AST->OriginalSourceFile =
std::string(Clang->getFrontendOpts().Inputs[0].getFile());
@@ -2227,15 +2224,14 @@ void ASTUnit::CodeComplete(
LangOpts.SpellChecking = false;
CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(PCHContainerOps));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(CCInvocation),
+ PCHContainerOps);
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
CICleanup(Clang.get());
- auto &Inv = *CCInvocation;
- Clang->setInvocation(std::move(CCInvocation));
+ auto &Inv = Clang->getInvocation();
OriginalSourceFile =
std::string(Clang->getFrontendOpts().Inputs[0].getFile());
@@ -2249,7 +2245,6 @@ void ASTUnit::CodeComplete(
// Create the target instance.
if (!Clang->createTarget()) {
- Clang->setInvocation(nullptr);
return;
}
diff --git a/clang/lib/Frontend/ChainedIncludesSource.cpp b/clang/lib/Frontend/ChainedIncludesSource.cpp
index a7096e27796a0..ce09328188ad5 100644
--- a/clang/lib/Frontend/ChainedIncludesSource.cpp
+++ b/clang/lib/Frontend/ChainedIncludesSource.cpp
@@ -122,9 +122,8 @@ IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient));
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(CI.getPCHContainerOperations()));
- Clang->setInvocation(std::move(CInvok));
+ auto Clang = std::make_unique<CompilerInstance>(
+ std::move(CInvok), CI.getPCHContainerOperations());
Clang->setDiagnostics(Diags.get());
Clang->setTarget(TargetInfo::CreateTargetInfo(
Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index bc663acb034e7..a172b3dd77632 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -67,22 +67,20 @@
using namespace clang;
CompilerInstance::CompilerInstance(
+ std::shared_ptr<CompilerInvocation> Invocation,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
ModuleCache *ModCache)
: ModuleLoader(/*BuildingModule=*/ModCache),
- Invocation(new CompilerInvocation()),
+ Invocation(std::move(Invocation)),
ModCache(ModCache ? ModCache : createCrossProcessModuleCache()),
- ThePCHContainerOperations(std::move(PCHContainerOps)) {}
+ ThePCHContainerOperations(std::move(PCHContainerOps)) {
+ assert(this->Invocation && "Invocation must not be null");
+}
CompilerInstance::~CompilerInstance() {
assert(OutputFiles.empty() && "Still output files in flight?");
}
-void CompilerInstance::setInvocation(
- std::shared_ptr<CompilerInvocation> Value) {
- Invocation = std::move(Value);
-}
-
bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
return (BuildGlobalModuleIndex ||
(TheASTReader && TheASTReader->isGlobalIndexUnavailable() &&
@@ -1207,11 +1205,10 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
// CompilerInstance::CompilerInstance is responsible for finalizing the
// buffers to prevent use-after-frees.
auto InstancePtr = std::make_unique<CompilerInstance>(
- getPCHContainerOperations(), &getModuleCache());
+ std::move(Invocation), getPCHContainerOperations(), &getModuleCache());
auto &Instance = *InstancePtr;
- auto &Inv = *Invocation;
- Instance.setInvocation(std::move(Invocation));
+ auto &Inv = Instance.getInvocation();
if (VFS) {
Instance.createFileManager(std::move(VFS));
diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp
index 49fa2e911fea4..4832478d0f8dc 100644
--- a/clang/lib/Frontend/PrecompiledPreamble.cpp
+++ b/clang/lib/Frontend/PrecompiledPreamble.cpp
@@ -454,14 +454,13 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
PreprocessorOpts.GeneratePreamble = true;
// Create the compiler instance to use for building the precompiled preamble.
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(std::move(PCHContainerOps)));
+ auto Clang = std::make_unique<CompilerInstance>(
+ std::move(PreambleInvocation), std::move(PCHContainerOps));
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
Clang.get());
- Clang->setInvocation(std::move(PreambleInvocation));
Clang->setDiagnostics(&Diagnostics);
// Create the target instance.
diff --git a/clang/lib/Frontend/Rewrite/FrontendActions.cpp b/clang/lib/Frontend/Rewrite/FrontendActions.cpp
index 5d2e1d7877095..3c8fc599433d1 100644
--- a/clang/lib/Frontend/Rewrite/FrontendActions.cpp
+++ b/clang/lib/Frontend/Rewrite/FrontendActions.cpp
@@ -242,10 +242,9 @@ class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
(*OS) << '\n';
// Rewrite the contents of the module in a separate compiler instance.
- CompilerInstance Instance(CI.getPCHContainerOperations(),
+ CompilerInstance Instance(std::make_shared<CompilerInvocation>(CI.getInvocation()),
+ CI.getPCHContainerOperations(),
&CI.getModuleCache());
- Instance.setInvocation(
- std::make_shared<CompilerInvocation>(CI.getInvocation()));
Instance.createDiagnostics(
CI.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
diff --git a/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp b/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
index 168c73df393ff..8dcb6fadd3ada 100644
--- a/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
+++ b/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
@@ -75,8 +75,8 @@ void ModelInjector::onBodySynthesis(const NamedDecl *D) {
// Modules are parsed by a separate CompilerInstance, so this code mimics that
// behavior for models
- CompilerInstance Instance(CI.getPCHContainerOperations());
- Instance.setInvocation(std::move(Invocation));
+ CompilerInstance Instance(std::move(Invocation),
+ CI.getPCHContainerOperations());
Instance.createDiagnostics(
CI.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
diff --git a/clang/lib/Testing/TestAST.cpp b/clang/lib/Testing/TestAST.cpp
index f7348aa068c51..748f59b856e83 100644
--- a/clang/lib/Testing/TestAST.cpp
+++ b/clang/lib/Testing/TestAST.cpp
@@ -75,8 +75,7 @@ void createMissingComponents(CompilerInstance &Clang) {
} // namespace
TestAST::TestAST(const TestInputs &In) {
- Clang = std::make_unique<CompilerInstance>(
- std::make_shared<PCHContainerOperations>());
+ Clang = std::make_unique<CompilerInstance>();
// If we don't manage to finish parsing, create CompilerInstance components
// anyway so that the test will see an empty AST instead of crashing.
auto RecoverFromEarlyExit =
@@ -109,7 +108,6 @@ TestAST::TestAST(const TestInputs &In) {
for (const auto &S : In.ExtraArgs)
Argv.push_back(S.c_str());
Argv.push_back(Filename.c_str());
- Clang->setInvocation(std::make_unique<CompilerInvocation>());
if (!CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv,
Clang->getDiagnostics(), "clang")) {
ADD_FAILURE() << "Failed to create invocation";
diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index b88a7cb2dca21..cd67706fb3d3c 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -386,9 +386,9 @@ class DependencyScanningAction : public tooling::ToolAction {
// Create a compiler instance to handle the actual work.
auto ModCache = makeInProcessModuleCache(Service.getModuleCacheMutexes());
- ScanInstanceStorage.emplace(std::move(PCHContainerOps), ModCache.get());
+ ScanInstanceStorage.emplace(std::move(Invocation),
+ std::move(PCHContainerOps), ModCache.get());
CompilerInstance &ScanInstance = *ScanInstanceStorage;
- ScanInstance.setInvocation(std::move(Invocation));
ScanInstance.setBuildingModule(false);
// Create the compiler's actual diagnostics engine.
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index f0955ef4a0829..3c72f52040142 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -447,8 +447,7 @@ bool FrontendActionFactory::runInvocation(
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
DiagnosticConsumer *DiagConsumer) {
// Create a compiler instance to handle the actual work.
- CompilerInstance Compiler(std::move(PCHContainerOps));
- Compiler.setInvocation(std::move(Invocation));
+ CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
Compiler.setFileManager(Files);
// The FrontendAction can have lifetime requirements for Compiler or its
diff --git a/clang/tools/clang-import-test/clang-import-test.cpp b/clang/tools/clang-import-test/clang-import-test.cpp
index 41a8a63a2e22b..ccafef075a85e 100644
--- a/clang/tools/clang-import-test/clang-import-test.cpp
+++ b/clang/tools/clang-import-test/clang-import-test.cpp
@@ -162,18 +162,18 @@ class TestDiagnosticConsumer : public DiagnosticConsumer {
};
std::unique_ptr<CompilerInstance> BuildCompilerInstance() {
- auto Ins = std::make_unique<CompilerInstance>();
+ auto DiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
auto DC = std::make_unique<TestDiagnosticConsumer>();
- const bool ShouldOwnClient = true;
- Ins->createDiagnostics(*llvm::vfs::getRealFileSystem(), DC.release(),
- ShouldOwnClient);
+ auto Diags = CompilerInstance::createDiagnostics(
+ *llvm::vfs::getRealFileSystem(), DiagOpts.get(), DC.get(),
+ /*ShouldOwnClient=*/false);
auto Inv = std::make_unique<CompilerInvocation>();
std::vector<const char *> ClangArgv(ClangArgs.size());
std::transform(ClangArgs.begin(), ClangArgs.end(), ClangArgv.begin(),
[](const std::string &s) -> const char * { return s.data(); });
- CompilerInvocation::CreateFromArgs(*Inv, ClangArgv, Ins->getDiagnostics());
+ CompilerInvocation::CreateFromArgs(*Inv, ClangArgv, *Diags);
{
using namespace driver::types;
@@ -205,7 +205,10 @@ std::unique_ptr<CompilerInstance> BuildCompilerInstance() {
Inv->getCodeGenOpts().setDebugInfo(llvm::codegenoptions::FullDebugInfo);
Inv->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
- Ins->setInvocation(std::move(Inv));
+ auto Ins = std::make_unique<CompilerInstance>(std::move(Inv));
+
+ Ins->createDiagnostics(*llvm::vfs::getRealFileSystem(), DC.release(),
+ /*ShouldOwnClient=*/true);
TargetInfo *TI = TargetInfo::CreateTargetInfo(
Ins->getDiagnostics(), Ins->getInvocation().TargetOpts);
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index 341520c6a6f73..187528532a44d 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -217,11 +217,10 @@ static int PrintEnabledExtensions(const TargetOptions& TargetOpts) {
int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
ensureSufficientStack();
- std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
// Register the support for object-file-wrapped Clang modules.
- auto PCHOps = Clang->getPCHContainerOperations();
+ auto PCHOps = std::make_shared<PCHContainerOperations>();
PCHOps->registerWriter(std::make_unique<ObjectFilePCHContainerWriter>());
PCHOps->registerReader(std::make_unique<ObjectFilePCHContainerReader>());
@@ -242,9 +241,13 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
Diags.setSeverity(diag::remark_cc1_round_trip_generated,
diag::Severity::Remark, {});
- bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
+ auto Invocation = std::make_shared<CompilerInvocation>();
+ bool Success = CompilerInvocation::CreateFromArgs(*Invocation,
Argv, Diags, Argv0);
+ auto Clang = std::make_unique<CompilerInstance>(std::move(Invocation),
+ std::move(PCHOps));
+
if (!Clang->getFrontendOpts().TimeTracePath.empty()) {
llvm::timeTraceProfilerInitialize(
Clang->getFrontendOpts().TimeTraceGranularity, Argv0,
diff --git a/clang/unittests/AST/ExternalASTSourceTest.cpp b/clang/unittests/AST/ExternalASTSourceTest.cpp
index 512f21e8efff4..50df8b5bac2c1 100644
--- a/clang/unittests/AST/ExternalASTSourceTest.cpp
+++ b/clang/unittests/AST/ExternalASTSourceTest.cpp
@@ -46,16 +46,19 @@ class TestFrontendAction : public ASTFrontendAction {
bool testExternalASTSource(ExternalASTSource *Source,
StringRef FileContents) {
- CompilerInstance Compiler;
- Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem());
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer(FileContents).release());
const char *Args[] = { "test.cc" };
- CompilerInvocation::CreateFromArgs(*Invocation, Args,
- Compiler.getDiagnostics());
- Compiler.setInvocation(std::move(Invocation));
+
+ auto InvocationDiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
+ auto InvocationDiags = CompilerInstance::createDiagnostics(
+ *llvm::vfs::getRealFileSystem(), InvocationDiagOpts.get());
+ CompilerInvocation::CreateFromArgs(*Invocation, Args, *InvocationDiags);
+
+ CompilerInstance Compiler(std::move(Invocation));
+ Compiler.createDiagnostics(*llvm::vf...
[truncated]
|
|
@llvm/pr-subscribers-clangd Author: Jan Svoboda (jansvoboda11) ChangesThis PR makes it so that
Patch is 41.12 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/137668.diff 33 Files Affected:
diff --git a/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp b/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
index bba8f8acc77da..7b0e4ecda8214 100644
--- a/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
+++ b/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
@@ -89,8 +89,7 @@ bool IncludeFixerActionFactory::runInvocation(
assert(Invocation->getFrontendOpts().Inputs.size() == 1);
// Set up Clang.
- clang::CompilerInstance Compiler(PCHContainerOps);
- Compiler.setInvocation(std::move(Invocation));
+ CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
Compiler.setFileManager(Files);
// Create the compiler's actual diagnostics engine. We want to drop all
diff --git a/clang-tools-extra/clangd/Compiler.cpp b/clang-tools-extra/clangd/Compiler.cpp
index 161cc9ae0ca36..9be0152afd2f7 100644
--- a/clang-tools-extra/clangd/Compiler.cpp
+++ b/clang-tools-extra/clangd/Compiler.cpp
@@ -145,9 +145,7 @@ prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
}
- auto Clang = std::make_unique<CompilerInstance>(
- std::make_shared<PCHContainerOperations>());
- Clang->setInvocation(std::move(CI));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(CI));
Clang->createDiagnostics(*VFS, &DiagsClient, false);
if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
diff --git a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
index b1bbb2eb82414..a10c0d5a54a95 100644
--- a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -618,14 +618,14 @@ TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
llvm::MemoryBuffer::getMemBufferCopy(Extra.getValue(),
/*BufferName=*/""));
- auto Clang = std::make_unique<CompilerInstance>(
- std::make_shared<PCHContainerOperations>());
- Clang->createDiagnostics(*VFS);
+ auto DiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
+ auto Diags = CompilerInstance::createDiagnostics(*VFS, DiagOpts.get());
+ auto Invocation = std::make_unique<CompilerInvocation>();
+ ASSERT_TRUE(CompilerInvocation::CreateFromArgs(*Invocation, {Filename.data()},
+ *Diags, "clang"));
- Clang->setInvocation(std::make_unique<CompilerInvocation>());
- ASSERT_TRUE(CompilerInvocation::CreateFromArgs(
- Clang->getInvocation(), {Filename.data()}, Clang->getDiagnostics(),
- "clang"));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(Invocation));
+ Clang->createDiagnostics(*VFS);
auto *FM = Clang->createFileManager(VFS);
ASSERT_TRUE(Clang->ExecuteAction(*Inputs.MakeAction()));
diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h
index d70f5c45b3d38..ebfbfc91b79e6 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -197,6 +197,8 @@ class CompilerInstance : public ModuleLoader {
void operator=(const CompilerInstance &) = delete;
public:
explicit CompilerInstance(
+ std::shared_ptr<CompilerInvocation> Invocation =
+ std::make_shared<CompilerInvocation>(),
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>(),
ModuleCache *ModCache = nullptr);
@@ -244,18 +246,10 @@ class CompilerInstance : public ModuleLoader {
/// @name Compiler Invocation and Options
/// @{
- bool hasInvocation() const { return Invocation != nullptr; }
-
- CompilerInvocation &getInvocation() {
- assert(Invocation && "Compiler instance has no invocation!");
- return *Invocation;
- }
+ CompilerInvocation &getInvocation() { return *Invocation; }
std::shared_ptr<CompilerInvocation> getInvocationPtr() { return Invocation; }
- /// setInvocation - Replace the current invocation.
- void setInvocation(std::shared_ptr<CompilerInvocation> Value);
-
/// Indicates whether we should (re)build the global module index.
bool shouldBuildGlobalModuleIndex() const;
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 04ddc93415507..dae08fa344008 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -1162,9 +1162,8 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
}
// Create the compiler instance to use for building the AST.
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(std::move(PCHContainerOps)));
- Clang->setInvocation(CCInvocation);
+ auto Clang = std::make_unique<CompilerInstance>(CCInvocation,
+ std::move(PCHContainerOps));
// Clean up on error, disengage it if the function returns successfully.
auto CleanOnError = llvm::make_scope_exit([&]() {
@@ -1486,7 +1485,6 @@ void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
// Steal the created target, context, and preprocessor if they have been
// created.
- assert(CI.hasInvocation() && "missing invocation");
LangOpts = CI.getInvocation().LangOpts;
TheSema = CI.takeSema();
Consumer = CI.takeASTConsumer();
@@ -1596,14 +1594,13 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
AST->getFileManager().getVirtualFileSystem());
// Create the compiler instance to use for building the AST.
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(std::move(PCHContainerOps)));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(CI),
+ std::move(PCHContainerOps));
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
CICleanup(Clang.get());
- Clang->setInvocation(std::move(CI));
AST->OriginalSourceFile =
std::string(Clang->getFrontendOpts().Inputs[0].getFile());
@@ -2227,15 +2224,14 @@ void ASTUnit::CodeComplete(
LangOpts.SpellChecking = false;
CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(PCHContainerOps));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(CCInvocation),
+ PCHContainerOps);
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
CICleanup(Clang.get());
- auto &Inv = *CCInvocation;
- Clang->setInvocation(std::move(CCInvocation));
+ auto &Inv = Clang->getInvocation();
OriginalSourceFile =
std::string(Clang->getFrontendOpts().Inputs[0].getFile());
@@ -2249,7 +2245,6 @@ void ASTUnit::CodeComplete(
// Create the target instance.
if (!Clang->createTarget()) {
- Clang->setInvocation(nullptr);
return;
}
diff --git a/clang/lib/Frontend/ChainedIncludesSource.cpp b/clang/lib/Frontend/ChainedIncludesSource.cpp
index a7096e27796a0..ce09328188ad5 100644
--- a/clang/lib/Frontend/ChainedIncludesSource.cpp
+++ b/clang/lib/Frontend/ChainedIncludesSource.cpp
@@ -122,9 +122,8 @@ IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient));
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(CI.getPCHContainerOperations()));
- Clang->setInvocation(std::move(CInvok));
+ auto Clang = std::make_unique<CompilerInstance>(
+ std::move(CInvok), CI.getPCHContainerOperations());
Clang->setDiagnostics(Diags.get());
Clang->setTarget(TargetInfo::CreateTargetInfo(
Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index bc663acb034e7..a172b3dd77632 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -67,22 +67,20 @@
using namespace clang;
CompilerInstance::CompilerInstance(
+ std::shared_ptr<CompilerInvocation> Invocation,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
ModuleCache *ModCache)
: ModuleLoader(/*BuildingModule=*/ModCache),
- Invocation(new CompilerInvocation()),
+ Invocation(std::move(Invocation)),
ModCache(ModCache ? ModCache : createCrossProcessModuleCache()),
- ThePCHContainerOperations(std::move(PCHContainerOps)) {}
+ ThePCHContainerOperations(std::move(PCHContainerOps)) {
+ assert(this->Invocation && "Invocation must not be null");
+}
CompilerInstance::~CompilerInstance() {
assert(OutputFiles.empty() && "Still output files in flight?");
}
-void CompilerInstance::setInvocation(
- std::shared_ptr<CompilerInvocation> Value) {
- Invocation = std::move(Value);
-}
-
bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
return (BuildGlobalModuleIndex ||
(TheASTReader && TheASTReader->isGlobalIndexUnavailable() &&
@@ -1207,11 +1205,10 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
// CompilerInstance::CompilerInstance is responsible for finalizing the
// buffers to prevent use-after-frees.
auto InstancePtr = std::make_unique<CompilerInstance>(
- getPCHContainerOperations(), &getModuleCache());
+ std::move(Invocation), getPCHContainerOperations(), &getModuleCache());
auto &Instance = *InstancePtr;
- auto &Inv = *Invocation;
- Instance.setInvocation(std::move(Invocation));
+ auto &Inv = Instance.getInvocation();
if (VFS) {
Instance.createFileManager(std::move(VFS));
diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp
index 49fa2e911fea4..4832478d0f8dc 100644
--- a/clang/lib/Frontend/PrecompiledPreamble.cpp
+++ b/clang/lib/Frontend/PrecompiledPreamble.cpp
@@ -454,14 +454,13 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
PreprocessorOpts.GeneratePreamble = true;
// Create the compiler instance to use for building the precompiled preamble.
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(std::move(PCHContainerOps)));
+ auto Clang = std::make_unique<CompilerInstance>(
+ std::move(PreambleInvocation), std::move(PCHContainerOps));
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
Clang.get());
- Clang->setInvocation(std::move(PreambleInvocation));
Clang->setDiagnostics(&Diagnostics);
// Create the target instance.
diff --git a/clang/lib/Frontend/Rewrite/FrontendActions.cpp b/clang/lib/Frontend/Rewrite/FrontendActions.cpp
index 5d2e1d7877095..3c8fc599433d1 100644
--- a/clang/lib/Frontend/Rewrite/FrontendActions.cpp
+++ b/clang/lib/Frontend/Rewrite/FrontendActions.cpp
@@ -242,10 +242,9 @@ class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
(*OS) << '\n';
// Rewrite the contents of the module in a separate compiler instance.
- CompilerInstance Instance(CI.getPCHContainerOperations(),
+ CompilerInstance Instance(std::make_shared<CompilerInvocation>(CI.getInvocation()),
+ CI.getPCHContainerOperations(),
&CI.getModuleCache());
- Instance.setInvocation(
- std::make_shared<CompilerInvocation>(CI.getInvocation()));
Instance.createDiagnostics(
CI.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
diff --git a/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp b/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
index 168c73df393ff..8dcb6fadd3ada 100644
--- a/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
+++ b/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
@@ -75,8 +75,8 @@ void ModelInjector::onBodySynthesis(const NamedDecl *D) {
// Modules are parsed by a separate CompilerInstance, so this code mimics that
// behavior for models
- CompilerInstance Instance(CI.getPCHContainerOperations());
- Instance.setInvocation(std::move(Invocation));
+ CompilerInstance Instance(std::move(Invocation),
+ CI.getPCHContainerOperations());
Instance.createDiagnostics(
CI.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
diff --git a/clang/lib/Testing/TestAST.cpp b/clang/lib/Testing/TestAST.cpp
index f7348aa068c51..748f59b856e83 100644
--- a/clang/lib/Testing/TestAST.cpp
+++ b/clang/lib/Testing/TestAST.cpp
@@ -75,8 +75,7 @@ void createMissingComponents(CompilerInstance &Clang) {
} // namespace
TestAST::TestAST(const TestInputs &In) {
- Clang = std::make_unique<CompilerInstance>(
- std::make_shared<PCHContainerOperations>());
+ Clang = std::make_unique<CompilerInstance>();
// If we don't manage to finish parsing, create CompilerInstance components
// anyway so that the test will see an empty AST instead of crashing.
auto RecoverFromEarlyExit =
@@ -109,7 +108,6 @@ TestAST::TestAST(const TestInputs &In) {
for (const auto &S : In.ExtraArgs)
Argv.push_back(S.c_str());
Argv.push_back(Filename.c_str());
- Clang->setInvocation(std::make_unique<CompilerInvocation>());
if (!CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv,
Clang->getDiagnostics(), "clang")) {
ADD_FAILURE() << "Failed to create invocation";
diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index b88a7cb2dca21..cd67706fb3d3c 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -386,9 +386,9 @@ class DependencyScanningAction : public tooling::ToolAction {
// Create a compiler instance to handle the actual work.
auto ModCache = makeInProcessModuleCache(Service.getModuleCacheMutexes());
- ScanInstanceStorage.emplace(std::move(PCHContainerOps), ModCache.get());
+ ScanInstanceStorage.emplace(std::move(Invocation),
+ std::move(PCHContainerOps), ModCache.get());
CompilerInstance &ScanInstance = *ScanInstanceStorage;
- ScanInstance.setInvocation(std::move(Invocation));
ScanInstance.setBuildingModule(false);
// Create the compiler's actual diagnostics engine.
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index f0955ef4a0829..3c72f52040142 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -447,8 +447,7 @@ bool FrontendActionFactory::runInvocation(
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
DiagnosticConsumer *DiagConsumer) {
// Create a compiler instance to handle the actual work.
- CompilerInstance Compiler(std::move(PCHContainerOps));
- Compiler.setInvocation(std::move(Invocation));
+ CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
Compiler.setFileManager(Files);
// The FrontendAction can have lifetime requirements for Compiler or its
diff --git a/clang/tools/clang-import-test/clang-import-test.cpp b/clang/tools/clang-import-test/clang-import-test.cpp
index 41a8a63a2e22b..ccafef075a85e 100644
--- a/clang/tools/clang-import-test/clang-import-test.cpp
+++ b/clang/tools/clang-import-test/clang-import-test.cpp
@@ -162,18 +162,18 @@ class TestDiagnosticConsumer : public DiagnosticConsumer {
};
std::unique_ptr<CompilerInstance> BuildCompilerInstance() {
- auto Ins = std::make_unique<CompilerInstance>();
+ auto DiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
auto DC = std::make_unique<TestDiagnosticConsumer>();
- const bool ShouldOwnClient = true;
- Ins->createDiagnostics(*llvm::vfs::getRealFileSystem(), DC.release(),
- ShouldOwnClient);
+ auto Diags = CompilerInstance::createDiagnostics(
+ *llvm::vfs::getRealFileSystem(), DiagOpts.get(), DC.get(),
+ /*ShouldOwnClient=*/false);
auto Inv = std::make_unique<CompilerInvocation>();
std::vector<const char *> ClangArgv(ClangArgs.size());
std::transform(ClangArgs.begin(), ClangArgs.end(), ClangArgv.begin(),
[](const std::string &s) -> const char * { return s.data(); });
- CompilerInvocation::CreateFromArgs(*Inv, ClangArgv, Ins->getDiagnostics());
+ CompilerInvocation::CreateFromArgs(*Inv, ClangArgv, *Diags);
{
using namespace driver::types;
@@ -205,7 +205,10 @@ std::unique_ptr<CompilerInstance> BuildCompilerInstance() {
Inv->getCodeGenOpts().setDebugInfo(llvm::codegenoptions::FullDebugInfo);
Inv->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
- Ins->setInvocation(std::move(Inv));
+ auto Ins = std::make_unique<CompilerInstance>(std::move(Inv));
+
+ Ins->createDiagnostics(*llvm::vfs::getRealFileSystem(), DC.release(),
+ /*ShouldOwnClient=*/true);
TargetInfo *TI = TargetInfo::CreateTargetInfo(
Ins->getDiagnostics(), Ins->getInvocation().TargetOpts);
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index 341520c6a6f73..187528532a44d 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -217,11 +217,10 @@ static int PrintEnabledExtensions(const TargetOptions& TargetOpts) {
int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
ensureSufficientStack();
- std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
// Register the support for object-file-wrapped Clang modules.
- auto PCHOps = Clang->getPCHContainerOperations();
+ auto PCHOps = std::make_shared<PCHContainerOperations>();
PCHOps->registerWriter(std::make_unique<ObjectFilePCHContainerWriter>());
PCHOps->registerReader(std::make_unique<ObjectFilePCHContainerReader>());
@@ -242,9 +241,13 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
Diags.setSeverity(diag::remark_cc1_round_trip_generated,
diag::Severity::Remark, {});
- bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
+ auto Invocation = std::make_shared<CompilerInvocation>();
+ bool Success = CompilerInvocation::CreateFromArgs(*Invocation,
Argv, Diags, Argv0);
+ auto Clang = std::make_unique<CompilerInstance>(std::move(Invocation),
+ std::move(PCHOps));
+
if (!Clang->getFrontendOpts().TimeTracePath.empty()) {
llvm::timeTraceProfilerInitialize(
Clang->getFrontendOpts().TimeTraceGranularity, Argv0,
diff --git a/clang/unittests/AST/ExternalASTSourceTest.cpp b/clang/unittests/AST/ExternalASTSourceTest.cpp
index 512f21e8efff4..50df8b5bac2c1 100644
--- a/clang/unittests/AST/ExternalASTSourceTest.cpp
+++ b/clang/unittests/AST/ExternalASTSourceTest.cpp
@@ -46,16 +46,19 @@ class TestFrontendAction : public ASTFrontendAction {
bool testExternalASTSource(ExternalASTSource *Source,
StringRef FileContents) {
- CompilerInstance Compiler;
- Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem());
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer(FileContents).release());
const char *Args[] = { "test.cc" };
- CompilerInvocation::CreateFromArgs(*Invocation, Args,
- Compiler.getDiagnostics());
- Compiler.setInvocation(std::move(Invocation));
+
+ auto InvocationDiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
+ auto InvocationDiags = CompilerInstance::createDiagnostics(
+ *llvm::vfs::getRealFileSystem(), InvocationDiagOpts.get());
+ CompilerInvocation::CreateFromArgs(*Invocation, Args, *InvocationDiags);
+
+ CompilerInstance Compiler(std::move(Invocation));
+ Compiler.createDiagnostics(*llvm::vf...
[truncated]
|
|
@llvm/pr-subscribers-clang-tools-extra Author: Jan Svoboda (jansvoboda11) ChangesThis PR makes it so that
Patch is 41.12 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/137668.diff 33 Files Affected:
diff --git a/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp b/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
index bba8f8acc77da..7b0e4ecda8214 100644
--- a/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
+++ b/clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
@@ -89,8 +89,7 @@ bool IncludeFixerActionFactory::runInvocation(
assert(Invocation->getFrontendOpts().Inputs.size() == 1);
// Set up Clang.
- clang::CompilerInstance Compiler(PCHContainerOps);
- Compiler.setInvocation(std::move(Invocation));
+ CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
Compiler.setFileManager(Files);
// Create the compiler's actual diagnostics engine. We want to drop all
diff --git a/clang-tools-extra/clangd/Compiler.cpp b/clang-tools-extra/clangd/Compiler.cpp
index 161cc9ae0ca36..9be0152afd2f7 100644
--- a/clang-tools-extra/clangd/Compiler.cpp
+++ b/clang-tools-extra/clangd/Compiler.cpp
@@ -145,9 +145,7 @@ prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
}
- auto Clang = std::make_unique<CompilerInstance>(
- std::make_shared<PCHContainerOperations>());
- Clang->setInvocation(std::move(CI));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(CI));
Clang->createDiagnostics(*VFS, &DiagsClient, false);
if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
diff --git a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
index b1bbb2eb82414..a10c0d5a54a95 100644
--- a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -618,14 +618,14 @@ TEST_F(PragmaIncludeTest, ExportInUnnamedBuffer) {
llvm::MemoryBuffer::getMemBufferCopy(Extra.getValue(),
/*BufferName=*/""));
- auto Clang = std::make_unique<CompilerInstance>(
- std::make_shared<PCHContainerOperations>());
- Clang->createDiagnostics(*VFS);
+ auto DiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
+ auto Diags = CompilerInstance::createDiagnostics(*VFS, DiagOpts.get());
+ auto Invocation = std::make_unique<CompilerInvocation>();
+ ASSERT_TRUE(CompilerInvocation::CreateFromArgs(*Invocation, {Filename.data()},
+ *Diags, "clang"));
- Clang->setInvocation(std::make_unique<CompilerInvocation>());
- ASSERT_TRUE(CompilerInvocation::CreateFromArgs(
- Clang->getInvocation(), {Filename.data()}, Clang->getDiagnostics(),
- "clang"));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(Invocation));
+ Clang->createDiagnostics(*VFS);
auto *FM = Clang->createFileManager(VFS);
ASSERT_TRUE(Clang->ExecuteAction(*Inputs.MakeAction()));
diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h
index d70f5c45b3d38..ebfbfc91b79e6 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -197,6 +197,8 @@ class CompilerInstance : public ModuleLoader {
void operator=(const CompilerInstance &) = delete;
public:
explicit CompilerInstance(
+ std::shared_ptr<CompilerInvocation> Invocation =
+ std::make_shared<CompilerInvocation>(),
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>(),
ModuleCache *ModCache = nullptr);
@@ -244,18 +246,10 @@ class CompilerInstance : public ModuleLoader {
/// @name Compiler Invocation and Options
/// @{
- bool hasInvocation() const { return Invocation != nullptr; }
-
- CompilerInvocation &getInvocation() {
- assert(Invocation && "Compiler instance has no invocation!");
- return *Invocation;
- }
+ CompilerInvocation &getInvocation() { return *Invocation; }
std::shared_ptr<CompilerInvocation> getInvocationPtr() { return Invocation; }
- /// setInvocation - Replace the current invocation.
- void setInvocation(std::shared_ptr<CompilerInvocation> Value);
-
/// Indicates whether we should (re)build the global module index.
bool shouldBuildGlobalModuleIndex() const;
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 04ddc93415507..dae08fa344008 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -1162,9 +1162,8 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
}
// Create the compiler instance to use for building the AST.
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(std::move(PCHContainerOps)));
- Clang->setInvocation(CCInvocation);
+ auto Clang = std::make_unique<CompilerInstance>(CCInvocation,
+ std::move(PCHContainerOps));
// Clean up on error, disengage it if the function returns successfully.
auto CleanOnError = llvm::make_scope_exit([&]() {
@@ -1486,7 +1485,6 @@ void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
// Steal the created target, context, and preprocessor if they have been
// created.
- assert(CI.hasInvocation() && "missing invocation");
LangOpts = CI.getInvocation().LangOpts;
TheSema = CI.takeSema();
Consumer = CI.takeASTConsumer();
@@ -1596,14 +1594,13 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
AST->getFileManager().getVirtualFileSystem());
// Create the compiler instance to use for building the AST.
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(std::move(PCHContainerOps)));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(CI),
+ std::move(PCHContainerOps));
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
CICleanup(Clang.get());
- Clang->setInvocation(std::move(CI));
AST->OriginalSourceFile =
std::string(Clang->getFrontendOpts().Inputs[0].getFile());
@@ -2227,15 +2224,14 @@ void ASTUnit::CodeComplete(
LangOpts.SpellChecking = false;
CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(PCHContainerOps));
+ auto Clang = std::make_unique<CompilerInstance>(std::move(CCInvocation),
+ PCHContainerOps);
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
CICleanup(Clang.get());
- auto &Inv = *CCInvocation;
- Clang->setInvocation(std::move(CCInvocation));
+ auto &Inv = Clang->getInvocation();
OriginalSourceFile =
std::string(Clang->getFrontendOpts().Inputs[0].getFile());
@@ -2249,7 +2245,6 @@ void ASTUnit::CodeComplete(
// Create the target instance.
if (!Clang->createTarget()) {
- Clang->setInvocation(nullptr);
return;
}
diff --git a/clang/lib/Frontend/ChainedIncludesSource.cpp b/clang/lib/Frontend/ChainedIncludesSource.cpp
index a7096e27796a0..ce09328188ad5 100644
--- a/clang/lib/Frontend/ChainedIncludesSource.cpp
+++ b/clang/lib/Frontend/ChainedIncludesSource.cpp
@@ -122,9 +122,8 @@ IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient));
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(CI.getPCHContainerOperations()));
- Clang->setInvocation(std::move(CInvok));
+ auto Clang = std::make_unique<CompilerInstance>(
+ std::move(CInvok), CI.getPCHContainerOperations());
Clang->setDiagnostics(Diags.get());
Clang->setTarget(TargetInfo::CreateTargetInfo(
Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index bc663acb034e7..a172b3dd77632 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -67,22 +67,20 @@
using namespace clang;
CompilerInstance::CompilerInstance(
+ std::shared_ptr<CompilerInvocation> Invocation,
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
ModuleCache *ModCache)
: ModuleLoader(/*BuildingModule=*/ModCache),
- Invocation(new CompilerInvocation()),
+ Invocation(std::move(Invocation)),
ModCache(ModCache ? ModCache : createCrossProcessModuleCache()),
- ThePCHContainerOperations(std::move(PCHContainerOps)) {}
+ ThePCHContainerOperations(std::move(PCHContainerOps)) {
+ assert(this->Invocation && "Invocation must not be null");
+}
CompilerInstance::~CompilerInstance() {
assert(OutputFiles.empty() && "Still output files in flight?");
}
-void CompilerInstance::setInvocation(
- std::shared_ptr<CompilerInvocation> Value) {
- Invocation = std::move(Value);
-}
-
bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
return (BuildGlobalModuleIndex ||
(TheASTReader && TheASTReader->isGlobalIndexUnavailable() &&
@@ -1207,11 +1205,10 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
// CompilerInstance::CompilerInstance is responsible for finalizing the
// buffers to prevent use-after-frees.
auto InstancePtr = std::make_unique<CompilerInstance>(
- getPCHContainerOperations(), &getModuleCache());
+ std::move(Invocation), getPCHContainerOperations(), &getModuleCache());
auto &Instance = *InstancePtr;
- auto &Inv = *Invocation;
- Instance.setInvocation(std::move(Invocation));
+ auto &Inv = Instance.getInvocation();
if (VFS) {
Instance.createFileManager(std::move(VFS));
diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp
index 49fa2e911fea4..4832478d0f8dc 100644
--- a/clang/lib/Frontend/PrecompiledPreamble.cpp
+++ b/clang/lib/Frontend/PrecompiledPreamble.cpp
@@ -454,14 +454,13 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
PreprocessorOpts.GeneratePreamble = true;
// Create the compiler instance to use for building the precompiled preamble.
- std::unique_ptr<CompilerInstance> Clang(
- new CompilerInstance(std::move(PCHContainerOps)));
+ auto Clang = std::make_unique<CompilerInstance>(
+ std::move(PreambleInvocation), std::move(PCHContainerOps));
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
Clang.get());
- Clang->setInvocation(std::move(PreambleInvocation));
Clang->setDiagnostics(&Diagnostics);
// Create the target instance.
diff --git a/clang/lib/Frontend/Rewrite/FrontendActions.cpp b/clang/lib/Frontend/Rewrite/FrontendActions.cpp
index 5d2e1d7877095..3c8fc599433d1 100644
--- a/clang/lib/Frontend/Rewrite/FrontendActions.cpp
+++ b/clang/lib/Frontend/Rewrite/FrontendActions.cpp
@@ -242,10 +242,9 @@ class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
(*OS) << '\n';
// Rewrite the contents of the module in a separate compiler instance.
- CompilerInstance Instance(CI.getPCHContainerOperations(),
+ CompilerInstance Instance(std::make_shared<CompilerInvocation>(CI.getInvocation()),
+ CI.getPCHContainerOperations(),
&CI.getModuleCache());
- Instance.setInvocation(
- std::make_shared<CompilerInvocation>(CI.getInvocation()));
Instance.createDiagnostics(
CI.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
diff --git a/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp b/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
index 168c73df393ff..8dcb6fadd3ada 100644
--- a/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
+++ b/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
@@ -75,8 +75,8 @@ void ModelInjector::onBodySynthesis(const NamedDecl *D) {
// Modules are parsed by a separate CompilerInstance, so this code mimics that
// behavior for models
- CompilerInstance Instance(CI.getPCHContainerOperations());
- Instance.setInvocation(std::move(Invocation));
+ CompilerInstance Instance(std::move(Invocation),
+ CI.getPCHContainerOperations());
Instance.createDiagnostics(
CI.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
diff --git a/clang/lib/Testing/TestAST.cpp b/clang/lib/Testing/TestAST.cpp
index f7348aa068c51..748f59b856e83 100644
--- a/clang/lib/Testing/TestAST.cpp
+++ b/clang/lib/Testing/TestAST.cpp
@@ -75,8 +75,7 @@ void createMissingComponents(CompilerInstance &Clang) {
} // namespace
TestAST::TestAST(const TestInputs &In) {
- Clang = std::make_unique<CompilerInstance>(
- std::make_shared<PCHContainerOperations>());
+ Clang = std::make_unique<CompilerInstance>();
// If we don't manage to finish parsing, create CompilerInstance components
// anyway so that the test will see an empty AST instead of crashing.
auto RecoverFromEarlyExit =
@@ -109,7 +108,6 @@ TestAST::TestAST(const TestInputs &In) {
for (const auto &S : In.ExtraArgs)
Argv.push_back(S.c_str());
Argv.push_back(Filename.c_str());
- Clang->setInvocation(std::make_unique<CompilerInvocation>());
if (!CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv,
Clang->getDiagnostics(), "clang")) {
ADD_FAILURE() << "Failed to create invocation";
diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index b88a7cb2dca21..cd67706fb3d3c 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -386,9 +386,9 @@ class DependencyScanningAction : public tooling::ToolAction {
// Create a compiler instance to handle the actual work.
auto ModCache = makeInProcessModuleCache(Service.getModuleCacheMutexes());
- ScanInstanceStorage.emplace(std::move(PCHContainerOps), ModCache.get());
+ ScanInstanceStorage.emplace(std::move(Invocation),
+ std::move(PCHContainerOps), ModCache.get());
CompilerInstance &ScanInstance = *ScanInstanceStorage;
- ScanInstance.setInvocation(std::move(Invocation));
ScanInstance.setBuildingModule(false);
// Create the compiler's actual diagnostics engine.
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index f0955ef4a0829..3c72f52040142 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -447,8 +447,7 @@ bool FrontendActionFactory::runInvocation(
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
DiagnosticConsumer *DiagConsumer) {
// Create a compiler instance to handle the actual work.
- CompilerInstance Compiler(std::move(PCHContainerOps));
- Compiler.setInvocation(std::move(Invocation));
+ CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
Compiler.setFileManager(Files);
// The FrontendAction can have lifetime requirements for Compiler or its
diff --git a/clang/tools/clang-import-test/clang-import-test.cpp b/clang/tools/clang-import-test/clang-import-test.cpp
index 41a8a63a2e22b..ccafef075a85e 100644
--- a/clang/tools/clang-import-test/clang-import-test.cpp
+++ b/clang/tools/clang-import-test/clang-import-test.cpp
@@ -162,18 +162,18 @@ class TestDiagnosticConsumer : public DiagnosticConsumer {
};
std::unique_ptr<CompilerInstance> BuildCompilerInstance() {
- auto Ins = std::make_unique<CompilerInstance>();
+ auto DiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
auto DC = std::make_unique<TestDiagnosticConsumer>();
- const bool ShouldOwnClient = true;
- Ins->createDiagnostics(*llvm::vfs::getRealFileSystem(), DC.release(),
- ShouldOwnClient);
+ auto Diags = CompilerInstance::createDiagnostics(
+ *llvm::vfs::getRealFileSystem(), DiagOpts.get(), DC.get(),
+ /*ShouldOwnClient=*/false);
auto Inv = std::make_unique<CompilerInvocation>();
std::vector<const char *> ClangArgv(ClangArgs.size());
std::transform(ClangArgs.begin(), ClangArgs.end(), ClangArgv.begin(),
[](const std::string &s) -> const char * { return s.data(); });
- CompilerInvocation::CreateFromArgs(*Inv, ClangArgv, Ins->getDiagnostics());
+ CompilerInvocation::CreateFromArgs(*Inv, ClangArgv, *Diags);
{
using namespace driver::types;
@@ -205,7 +205,10 @@ std::unique_ptr<CompilerInstance> BuildCompilerInstance() {
Inv->getCodeGenOpts().setDebugInfo(llvm::codegenoptions::FullDebugInfo);
Inv->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
- Ins->setInvocation(std::move(Inv));
+ auto Ins = std::make_unique<CompilerInstance>(std::move(Inv));
+
+ Ins->createDiagnostics(*llvm::vfs::getRealFileSystem(), DC.release(),
+ /*ShouldOwnClient=*/true);
TargetInfo *TI = TargetInfo::CreateTargetInfo(
Ins->getDiagnostics(), Ins->getInvocation().TargetOpts);
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index 341520c6a6f73..187528532a44d 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -217,11 +217,10 @@ static int PrintEnabledExtensions(const TargetOptions& TargetOpts) {
int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
ensureSufficientStack();
- std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
// Register the support for object-file-wrapped Clang modules.
- auto PCHOps = Clang->getPCHContainerOperations();
+ auto PCHOps = std::make_shared<PCHContainerOperations>();
PCHOps->registerWriter(std::make_unique<ObjectFilePCHContainerWriter>());
PCHOps->registerReader(std::make_unique<ObjectFilePCHContainerReader>());
@@ -242,9 +241,13 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
Diags.setSeverity(diag::remark_cc1_round_trip_generated,
diag::Severity::Remark, {});
- bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
+ auto Invocation = std::make_shared<CompilerInvocation>();
+ bool Success = CompilerInvocation::CreateFromArgs(*Invocation,
Argv, Diags, Argv0);
+ auto Clang = std::make_unique<CompilerInstance>(std::move(Invocation),
+ std::move(PCHOps));
+
if (!Clang->getFrontendOpts().TimeTracePath.empty()) {
llvm::timeTraceProfilerInitialize(
Clang->getFrontendOpts().TimeTraceGranularity, Argv0,
diff --git a/clang/unittests/AST/ExternalASTSourceTest.cpp b/clang/unittests/AST/ExternalASTSourceTest.cpp
index 512f21e8efff4..50df8b5bac2c1 100644
--- a/clang/unittests/AST/ExternalASTSourceTest.cpp
+++ b/clang/unittests/AST/ExternalASTSourceTest.cpp
@@ -46,16 +46,19 @@ class TestFrontendAction : public ASTFrontendAction {
bool testExternalASTSource(ExternalASTSource *Source,
StringRef FileContents) {
- CompilerInstance Compiler;
- Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem());
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer(FileContents).release());
const char *Args[] = { "test.cc" };
- CompilerInvocation::CreateFromArgs(*Invocation, Args,
- Compiler.getDiagnostics());
- Compiler.setInvocation(std::move(Invocation));
+
+ auto InvocationDiagOpts = llvm::makeIntrusiveRefCnt<DiagnosticOptions>();
+ auto InvocationDiags = CompilerInstance::createDiagnostics(
+ *llvm::vfs::getRealFileSystem(), InvocationDiagOpts.get());
+ CompilerInvocation::CreateFromArgs(*Invocation, Args, *InvocationDiags);
+
+ CompilerInstance Compiler(std::move(Invocation));
+ Compiler.createDiagnostics(*llvm::vf...
[truncated]
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
7b45d90 to
1fd592d
Compare
Bigcheese
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Every non-test creation of CompilerInstance has an associated invocation.
…lvm#137668) This PR makes it so that `CompilerInvocation` needs to be provided to `CompilerInstance` on construction. There are a couple of benefits in my view: * Making it impossible to mis-use some `CompilerInstance` APIs. For example there are cases, where `createDiagnostics()` was called before `setInvocation()`, causing the `DiagnosticEngine` to use the default-constructed `DiagnosticOptions` instead of the intended ones. * This shrinks `CompilerInstance`'s state space. * This makes it possible to access **the** invocation in `CompilerInstance`'s constructor (to be used in a follow-up).
…lvm#137668) This PR makes it so that `CompilerInvocation` needs to be provided to `CompilerInstance` on construction. There are a couple of benefits in my view: * Making it impossible to mis-use some `CompilerInstance` APIs. For example there are cases, where `createDiagnostics()` was called before `setInvocation()`, causing the `DiagnosticEngine` to use the default-constructed `DiagnosticOptions` instead of the intended ones. * This shrinks `CompilerInstance`'s state space. * This makes it possible to access **the** invocation in `CompilerInstance`'s constructor (to be used in a follow-up).
This PR makes it so that
CompilerInvocationneeds to be provided toCompilerInstanceon construction. There are a couple of benefits in my view:CompilerInstanceAPIs. For example there are cases, wherecreateDiagnostics()was called beforesetInvocation(), causing theDiagnosticEngineto use the default-constructedDiagnosticOptionsinstead of the intended ones.CompilerInstance's state space.CompilerInstance's constructor (to be used in a follow-up).