Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions clang-tools-extra/clangd/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
12 changes: 3 additions & 9 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,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);
Expand Down Expand Up @@ -251,18 +253,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;

Expand Down
19 changes: 7 additions & 12 deletions clang/lib/Frontend/ASTUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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([&]() {
Expand Down Expand Up @@ -1487,7 +1486,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 = std::make_unique<LangOptions>(CI.getInvocation().getLangOpts());
TheSema = CI.takeSema();
Consumer = CI.takeASTConsumer();
Expand Down Expand Up @@ -1601,14 +1599,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());

Expand Down Expand Up @@ -2232,15 +2229,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());

Expand All @@ -2254,7 +2250,6 @@ void ASTUnit::CodeComplete(

// Create the target instance.
if (!Clang->createTarget()) {
Clang->setInvocation(nullptr);
return;
}

Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Frontend/ChainedIncludesSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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().getTargetOpts()));
Expand Down
17 changes: 7 additions & 10 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() &&
Expand Down Expand Up @@ -1210,11 +1208,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 (ThreadSafeConfig) {
Instance.createFileManager(ThreadSafeConfig->getVFS());
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Frontend/PrecompiledPreamble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/Frontend/Rewrite/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
&CI.getModuleCache());
Instance.setInvocation(
std::make_shared<CompilerInvocation>(CI.getInvocation()));
CompilerInstance Instance(
std::make_shared<CompilerInvocation>(CI.getInvocation()),
CI.getPCHContainerOperations(), &CI.getModuleCache());
Instance.createDiagnostics(
CI.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Testing/TestAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,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.
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Tooling/Tooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions clang/tools/clang-import-test/clang-import-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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().getTargetOpts());
Expand Down
11 changes: 7 additions & 4 deletions clang/tools/driver/cc1_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>());

Expand All @@ -242,8 +241,12 @@ 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(),
Argv, Diags, Argv0);
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(
Expand Down
17 changes: 9 additions & 8 deletions clang/unittests/AST/ExternalASTSourceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,25 @@ class TestFrontendAction : public ASTFrontendAction {
IntrusiveRefCntPtr<ExternalASTSource> Source;
};

bool testExternalASTSource(ExternalASTSource *Source,
StringRef FileContents) {
CompilerInstance Compiler;
Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem());
bool testExternalASTSource(ExternalASTSource *Source, StringRef FileContents) {

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::vfs::getRealFileSystem());

TestFrontendAction Action(Source);
return Compiler.ExecuteAction(Action);
}


// Ensure that a failed name lookup into an external source only occurs once.
TEST(ExternalASTSourceTest, FailedLookupOccursOnce) {
struct TestSource : ExternalASTSource {
Expand Down
Loading
Loading