From b69dcb873476cd8e7d3f6f9ffd5b6d0bbe1a3a17 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Thu, 1 May 2025 07:31:30 -0700 Subject: [clang][frontend] Require invocation to construct `CompilerInstance` (#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). --- clang/lib/Frontend/CompilerInstance.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'clang/lib/Frontend/CompilerInstance.cpp') diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index fc69987..9b4147f 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 Invocation, std::shared_ptr 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 Value) { - Invocation = std::move(Value); -} - bool CompilerInstance::shouldBuildGlobalModuleIndex() const { return (BuildGlobalModuleIndex || (TheASTReader && TheASTReader->isGlobalIndexUnavailable() && @@ -1210,11 +1208,10 @@ std::unique_ptr CompilerInstance::cloneForModuleCompileImpl( // CompilerInstance::CompilerInstance is responsible for finalizing the // buffers to prevent use-after-frees. auto InstancePtr = std::make_unique( - 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()); -- cgit v1.1