[clang] [clang-tools-extra] [lldb] [clang] Hide the `TargetOptions` pointer from `CompilerInvocation` (PR #106271)
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 25 09:19:07 PDT 2025
https://github.com/jansvoboda11 updated https://github.com/llvm/llvm-project/pull/106271
>From 4431265f6be2b989b63e4562fa8def4448d336ab Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Tue, 27 Aug 2024 09:34:01 -0700
Subject: [PATCH 1/7] [clang] `TargetInfo` does not own `TargetOptions`
---
clang-tools-extra/clangd/SystemIncludeExtractor.cpp | 2 +-
clang-tools-extra/modularize/ModularizeUtilities.cpp | 2 +-
clang/include/clang/Basic/TargetInfo.h | 7 +++----
clang/include/clang/Frontend/CompilerInstance.h | 3 +++
clang/lib/Basic/Targets.cpp | 7 ++++---
clang/lib/Frontend/ASTUnit.cpp | 2 +-
clang/lib/Frontend/ChainedIncludesSource.cpp | 2 +-
clang/lib/Frontend/CompilerInstance.cpp | 6 +++---
clang/lib/Interpreter/Interpreter.cpp | 2 +-
clang/tools/clang-import-test/clang-import-test.cpp | 2 +-
clang/unittests/Analysis/MacroExpansionContextTest.cpp | 2 +-
clang/unittests/Basic/SourceManagerTest.cpp | 2 +-
clang/unittests/CodeGen/TestCompiler.h | 3 +--
clang/unittests/Frontend/UtilsTest.cpp | 2 +-
clang/unittests/Lex/HeaderSearchTest.cpp | 2 +-
clang/unittests/Lex/LexerTest.cpp | 2 +-
clang/unittests/Lex/ModuleDeclStateTest.cpp | 2 +-
clang/unittests/Lex/PPCallbacksTest.cpp | 2 +-
clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp | 2 +-
clang/unittests/Lex/PPDependencyDirectivesTest.cpp | 2 +-
clang/unittests/Lex/PPMemoryAllocationsTest.cpp | 2 +-
21 files changed, 30 insertions(+), 28 deletions(-)
diff --git a/clang-tools-extra/clangd/SystemIncludeExtractor.cpp b/clang-tools-extra/clangd/SystemIncludeExtractor.cpp
index d9642f1115a6d..6417bf8765622 100644
--- a/clang-tools-extra/clangd/SystemIncludeExtractor.cpp
+++ b/clang-tools-extra/clangd/SystemIncludeExtractor.cpp
@@ -256,7 +256,7 @@ bool isValidTarget(llvm::StringRef Triple) {
DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions,
new IgnoringDiagConsumer);
llvm::IntrusiveRefCntPtr<TargetInfo> Target =
- TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
return bool(Target);
}
diff --git a/clang-tools-extra/modularize/ModularizeUtilities.cpp b/clang-tools-extra/modularize/ModularizeUtilities.cpp
index f45190f8aebec..ef57b0f5bbea5 100644
--- a/clang-tools-extra/modularize/ModularizeUtilities.cpp
+++ b/clang-tools-extra/modularize/ModularizeUtilities.cpp
@@ -53,7 +53,7 @@ ModularizeUtilities::ModularizeUtilities(std::vector<std::string> &InputPaths,
Diagnostics(
new DiagnosticsEngine(DiagIDs, DiagnosticOpts.get(), &DC, false)),
TargetOpts(new ModuleMapTargetOptions()),
- Target(TargetInfo::CreateTargetInfo(*Diagnostics, TargetOpts)),
+ Target(TargetInfo::CreateTargetInfo(*Diagnostics, *TargetOpts)),
FileMgr(new FileManager(FileSystemOpts)),
SourceMgr(new SourceManager(*Diagnostics, *FileMgr, false)), HSOpts(),
HeaderInfo(new HeaderSearch(HSOpts, *SourceMgr, *Diagnostics, *LangOpts,
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 8c3dcda25bc8d..e4d51e298e313 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -224,7 +224,7 @@ enum OpenCLTypeKind : uint8_t {
///
class TargetInfo : public TransferrableTargetInfo,
public RefCountedBase<TargetInfo> {
- std::shared_ptr<TargetOptions> TargetOpts;
+ TargetOptions *TargetOpts;
llvm::Triple Triple;
protected:
// Target values set by the ctor of the actual target implementation. Default
@@ -311,9 +311,8 @@ class TargetInfo : public TransferrableTargetInfo,
/// \param Opts - The options to use to initialize the target. The target may
/// modify the options to canonicalize the target feature information to match
/// what the backend expects.
- static TargetInfo *
- CreateTargetInfo(DiagnosticsEngine &Diags,
- const std::shared_ptr<TargetOptions> &Opts);
+ static TargetInfo *CreateTargetInfo(DiagnosticsEngine &Diags,
+ TargetOptions &Opts);
virtual ~TargetInfo();
diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h
index d8ef2691b46bb..7de4fb531d0e7 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -88,6 +88,9 @@ class CompilerInstance : public ModuleLoader {
/// The target being compiled for.
IntrusiveRefCntPtr<TargetInfo> Target;
+ /// Options for the auxiliary target.
+ std::unique_ptr<TargetOptions> AuxTargetOpts;
+
/// Auxiliary Target info.
IntrusiveRefCntPtr<TargetInfo> AuxTarget;
diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index c6d228fe98100..9889141ad2085 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -774,9 +774,10 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
using namespace clang::targets;
/// CreateTargetInfo - Return the target info object for the specified target
/// options.
-TargetInfo *
-TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
- const std::shared_ptr<TargetOptions> &Opts) {
+TargetInfo *TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
+ TargetOptions &OptsRef) {
+ TargetOptions *Opts = &OptsRef;
+
llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
// Construct the target
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 23a1324e68e15..025b88cf64884 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -615,7 +615,7 @@ class ASTInfoCollector : public ASTReaderListener {
this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts);
Target =
- TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts);
+ TargetInfo::CreateTargetInfo(PP.getDiagnostics(), *this->TargetOpts);
updated();
return false;
diff --git a/clang/lib/Frontend/ChainedIncludesSource.cpp b/clang/lib/Frontend/ChainedIncludesSource.cpp
index a7096e27796a0..4cad1b886c8cf 100644
--- a/clang/lib/Frontend/ChainedIncludesSource.cpp
+++ b/clang/lib/Frontend/ChainedIncludesSource.cpp
@@ -127,7 +127,7 @@ IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
Clang->setInvocation(std::move(CInvok));
Clang->setDiagnostics(Diags.get());
Clang->setTarget(TargetInfo::CreateTargetInfo(
- Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
+ Clang->getDiagnostics(), Clang->getInvocation().getTargetOpts()));
Clang->createFileManager();
Clang->createSourceManager(Clang->getFileManager());
Clang->createPreprocessor(TU_Prefix);
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 1526ea53add7d..2d6ab87964486 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -110,7 +110,7 @@ void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }
bool CompilerInstance::createTarget() {
// Create the target instance.
setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(),
- getInvocation().TargetOpts));
+ getInvocation().getTargetOpts()));
if (!hasTarget())
return false;
@@ -119,14 +119,14 @@ bool CompilerInstance::createTarget() {
if (!getAuxTarget() &&
(getLangOpts().CUDA || getLangOpts().isTargetDevice()) &&
!getFrontendOpts().AuxTriple.empty()) {
- auto TO = std::make_shared<TargetOptions>();
+ auto &TO = AuxTargetOpts = std::make_unique<TargetOptions>();
TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
if (getFrontendOpts().AuxTargetCPU)
TO->CPU = *getFrontendOpts().AuxTargetCPU;
if (getFrontendOpts().AuxTargetFeatures)
TO->FeaturesAsWritten = *getFrontendOpts().AuxTargetFeatures;
TO->HostTriple = getTarget().getTriple().str();
- setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO));
+ setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), *TO));
}
if (!getTarget().hasStrictFP() && !getLangOpts().ExpStrictFP) {
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 45fa583650f03..121ea9d638b5a 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -125,7 +125,7 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
Clang->getPreprocessorOpts().addRemappedFile("<<< inputs >>>", MB);
Clang->setTarget(TargetInfo::CreateTargetInfo(
- Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
+ Clang->getDiagnostics(), Clang->getInvocation().getTargetOpts()));
if (!Clang->hasTarget())
return llvm::createStringError(llvm::errc::not_supported,
"Initialization failed. "
diff --git a/clang/tools/clang-import-test/clang-import-test.cpp b/clang/tools/clang-import-test/clang-import-test.cpp
index 41a8a63a2e22b..ce3d0f6c503b2 100644
--- a/clang/tools/clang-import-test/clang-import-test.cpp
+++ b/clang/tools/clang-import-test/clang-import-test.cpp
@@ -208,7 +208,7 @@ std::unique_ptr<CompilerInstance> BuildCompilerInstance() {
Ins->setInvocation(std::move(Inv));
TargetInfo *TI = TargetInfo::CreateTargetInfo(
- Ins->getDiagnostics(), Ins->getInvocation().TargetOpts);
+ Ins->getDiagnostics(), Ins->getInvocation().getTargetOpts());
Ins->setTarget(TI);
Ins->getTarget().adjust(Ins->getDiagnostics(), Ins->getLangOpts());
Ins->createFileManager();
diff --git a/clang/unittests/Analysis/MacroExpansionContextTest.cpp b/clang/unittests/Analysis/MacroExpansionContextTest.cpp
index 19074d7dcfdd4..929d4122cdec7 100644
--- a/clang/unittests/Analysis/MacroExpansionContextTest.cpp
+++ b/clang/unittests/Analysis/MacroExpansionContextTest.cpp
@@ -39,7 +39,7 @@ class MacroExpansionContextTest : public ::testing::Test {
Diags(DiagID, DiagOpts.get(), new IgnoringDiagConsumer()),
SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions()) {
TargetOpts->Triple = "x86_64-pc-linux-unknown";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
LangOpts.CPlusPlus20 = 1; // For __VA_OPT__
}
diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp
index 201c3f9a68d1d..1f0986f61dfa9 100644
--- a/clang/unittests/Basic/SourceManagerTest.cpp
+++ b/clang/unittests/Basic/SourceManagerTest.cpp
@@ -46,7 +46,7 @@ class SourceManagerTest : public ::testing::Test {
SourceMgr(Diags, FileMgr),
TargetOpts(new TargetOptions) {
TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
}
FileSystemOptions FileMgrOpts;
diff --git a/clang/unittests/CodeGen/TestCompiler.h b/clang/unittests/CodeGen/TestCompiler.h
index 931c75effbf75..a6fec7fb0945d 100644
--- a/clang/unittests/CodeGen/TestCompiler.h
+++ b/clang/unittests/CodeGen/TestCompiler.h
@@ -45,8 +45,7 @@ struct TestCompiler {
Tr.setEnvironment(Triple::EnvironmentType::UnknownEnvironment);
compiler.getTargetOpts().Triple = Tr.getTriple();
compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
- compiler.getDiagnostics(),
- std::make_shared<clang::TargetOptions>(compiler.getTargetOpts())));
+ compiler.getDiagnostics(), compiler.getTargetOpts()));
const clang::TargetInfo &TInfo = compiler.getTarget();
PtrSize = TInfo.getPointerWidth(clang::LangAS::Default) / 8;
diff --git a/clang/unittests/Frontend/UtilsTest.cpp b/clang/unittests/Frontend/UtilsTest.cpp
index 304fbe2a8e69f..47ca3210ab596 100644
--- a/clang/unittests/Frontend/UtilsTest.cpp
+++ b/clang/unittests/Frontend/UtilsTest.cpp
@@ -33,7 +33,7 @@ TEST(BuildCompilerInvocationTest, RecoverMultipleJobs) {
*Opts.VFS, new DiagnosticOptions, &D, false);
std::unique_ptr<CompilerInvocation> CI = createInvocation(Args, Opts);
ASSERT_TRUE(CI);
- EXPECT_THAT(CI->TargetOpts->Triple, testing::StartsWith("i386-"));
+ EXPECT_THAT(CI->getTargetOpts().Triple, testing::StartsWith("i386-"));
}
// buildInvocationFromCommandLine should not translate -include to -include-pch,
diff --git a/clang/unittests/Lex/HeaderSearchTest.cpp b/clang/unittests/Lex/HeaderSearchTest.cpp
index e7cc3d7f08cc4..b8896261703cd 100644
--- a/clang/unittests/Lex/HeaderSearchTest.cpp
+++ b/clang/unittests/Lex/HeaderSearchTest.cpp
@@ -34,7 +34,7 @@ class HeaderSearchTest : public ::testing::Test {
SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions),
Search(HSOpts, SourceMgr, Diags, LangOpts, Target.get()) {
TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
}
void addSearchDir(llvm::StringRef Dir) {
diff --git a/clang/unittests/Lex/LexerTest.cpp b/clang/unittests/Lex/LexerTest.cpp
index 3bbc571ee5307..9806d4c7bf145 100644
--- a/clang/unittests/Lex/LexerTest.cpp
+++ b/clang/unittests/Lex/LexerTest.cpp
@@ -48,7 +48,7 @@ class LexerTest : public ::testing::Test {
TargetOpts(new TargetOptions)
{
TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
}
std::unique_ptr<Preprocessor> CreatePP(StringRef Source,
diff --git a/clang/unittests/Lex/ModuleDeclStateTest.cpp b/clang/unittests/Lex/ModuleDeclStateTest.cpp
index 5b2719d36de85..6fbc6bff12d8a 100644
--- a/clang/unittests/Lex/ModuleDeclStateTest.cpp
+++ b/clang/unittests/Lex/ModuleDeclStateTest.cpp
@@ -58,7 +58,7 @@ class ModuleDeclStateTest : public ::testing::Test {
Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) {
TargetOpts->Triple = "x86_64-unknown-linux-gnu";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
}
std::unique_ptr<Preprocessor>
diff --git a/clang/unittests/Lex/PPCallbacksTest.cpp b/clang/unittests/Lex/PPCallbacksTest.cpp
index 5b1e25ce9bdd5..15385c13879d3 100644
--- a/clang/unittests/Lex/PPCallbacksTest.cpp
+++ b/clang/unittests/Lex/PPCallbacksTest.cpp
@@ -139,7 +139,7 @@ class PPCallbacksTest : public ::testing::Test {
Diags(DiagID, DiagOpts.get(), new IgnoringDiagConsumer()),
SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions()) {
TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
}
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem;
diff --git a/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp b/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
index 349490aa7eef5..5c3ce6fa33012 100644
--- a/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
+++ b/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
@@ -36,7 +36,7 @@ class PPConditionalDirectiveRecordTest : public ::testing::Test {
TargetOpts(new TargetOptions)
{
TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
}
FileSystemOptions FileMgrOpts;
diff --git a/clang/unittests/Lex/PPDependencyDirectivesTest.cpp b/clang/unittests/Lex/PPDependencyDirectivesTest.cpp
index 6ab80ba01677e..74e3c00b451ba 100644
--- a/clang/unittests/Lex/PPDependencyDirectivesTest.cpp
+++ b/clang/unittests/Lex/PPDependencyDirectivesTest.cpp
@@ -35,7 +35,7 @@ class PPDependencyDirectivesTest : public ::testing::Test {
Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) {
TargetOpts->Triple = "x86_64-apple-macos12";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
}
FileSystemOptions FileMgrOpts;
diff --git a/clang/unittests/Lex/PPMemoryAllocationsTest.cpp b/clang/unittests/Lex/PPMemoryAllocationsTest.cpp
index ecbd7f47a2c88..304a66ab96caa 100644
--- a/clang/unittests/Lex/PPMemoryAllocationsTest.cpp
+++ b/clang/unittests/Lex/PPMemoryAllocationsTest.cpp
@@ -31,7 +31,7 @@ class PPMemoryAllocationsTest : public ::testing::Test {
Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) {
TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
}
FileSystemOptions FileMgrOpts;
>From 84445a4309f4305400fa1cd0c19593334e816c12 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Tue, 27 Aug 2024 11:27:28 -0700
Subject: [PATCH 2/7] [clang] Extend the lifetime of `CompilerInvocation` in
`ASTUnit::Parse()`
---
clang/include/clang/Frontend/ASTUnit.h | 4 ++++
clang/lib/Frontend/ASTUnit.cpp | 4 ++++
2 files changed, 8 insertions(+)
diff --git a/clang/include/clang/Frontend/ASTUnit.h b/clang/include/clang/Frontend/ASTUnit.h
index eadc4f3719ba4..2baa2d1cc540d 100644
--- a/clang/include/clang/Frontend/ASTUnit.h
+++ b/clang/include/clang/Frontend/ASTUnit.h
@@ -143,6 +143,10 @@ class ASTUnit {
/// Parse available.
std::shared_ptr<CompilerInvocation> CCInvocation;
+ /// Optional owned invocation, just used to keep the invocation alive for the
+ /// members initialized in transferASTDataFromCompilerInstance.
+ std::shared_ptr<CompilerInvocation> ModifiedInvocation;
+
/// Fake module loader: the AST unit doesn't need to load any modules.
TrivialModuleLoader ModuleLoader;
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 025b88cf64884..3e4da76916585 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -1499,6 +1499,10 @@ void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
Target = &CI.getTarget();
Reader = CI.getASTReader();
HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
+ if (Invocation != CI.getInvocationPtr()) {
+ // This happens when Parse creates a copy of \c Invocation to modify.
+ ModifiedInvocation = CI.getInvocationPtr();
+ }
}
StringRef ASTUnit::getMainFileName() const {
>From bd1b2aae8a4e4e62eade1f20de8ee04f396a60f7 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Tue, 27 Aug 2024 11:41:15 -0700
Subject: [PATCH 3/7] [clangd] `Preamble` owns `TargetOptions`
---
clang-tools-extra/clangd/Preamble.cpp | 7 +++++--
clang-tools-extra/clangd/Preamble.h | 2 +-
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp
index 6fab3e2191426..ba9a53db8a0dc 100644
--- a/clang-tools-extra/clangd/Preamble.cpp
+++ b/clang-tools-extra/clangd/Preamble.cpp
@@ -711,7 +711,10 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
Result->Marks = CapturedInfo.takeMarks();
Result->StatCache = StatCache;
Result->MainIsIncludeGuarded = CapturedInfo.isMainFileIncludeGuarded();
- Result->TargetOpts = CI.TargetOpts;
+ // Move the options instead of copying them. The invocation doesn't need
+ // them anymore.
+ Result->TargetOpts =
+ std::make_unique<TargetOptions>(std::move(CI.getTargetOpts()));
if (PreambleCallback) {
trace::Span Tracer("Running PreambleCallback");
auto Ctx = CapturedInfo.takeLife();
@@ -932,7 +935,7 @@ void PreamblePatch::apply(CompilerInvocation &CI) const {
// ParsedASTTest.PreambleWithDifferentTarget.
// Make sure this is a deep copy, as the same Baseline might be used
// concurrently.
- *CI.TargetOpts = *Baseline->TargetOpts;
+ CI.getTargetOpts() = *Baseline->TargetOpts;
// No need to map an empty file.
if (PatchContents.empty())
diff --git a/clang-tools-extra/clangd/Preamble.h b/clang-tools-extra/clangd/Preamble.h
index be8fed4ab88cd..7f2eb233ee1aa 100644
--- a/clang-tools-extra/clangd/Preamble.h
+++ b/clang-tools-extra/clangd/Preamble.h
@@ -103,7 +103,7 @@ struct PreambleData {
// Target options used when building the preamble. Changes in target can cause
// crashes when deserializing preamble, this enables consumers to use the
// same target (without reparsing CompileCommand).
- std::shared_ptr<TargetOptions> TargetOpts = nullptr;
+ std::unique_ptr<TargetOptions> TargetOpts = nullptr;
PrecompiledPreamble Preamble;
std::vector<Diag> Diags;
// Processes like code completions and go-to-definitions will need #include
>From 5a8c39266596d196cc4337e30e3d696827f5f8b6 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Tue, 27 Aug 2024 11:41:38 -0700
Subject: [PATCH 4/7] [clang] Hide `CompilerInvocation::TargetOpts`
---
clang/include/clang/Frontend/CompilerInvocation.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/include/clang/Frontend/CompilerInvocation.h b/clang/include/clang/Frontend/CompilerInvocation.h
index 3f61cef7462cb..1827ff0f6816d 100644
--- a/clang/include/clang/Frontend/CompilerInvocation.h
+++ b/clang/include/clang/Frontend/CompilerInvocation.h
@@ -268,7 +268,6 @@ class CompilerInvocation : public CompilerInvocationBase {
/// Base class internals.
/// @{
using CompilerInvocationBase::LangOpts;
- using CompilerInvocationBase::TargetOpts;
std::shared_ptr<LangOptions> getLangOptsPtr() { return LangOpts; }
/// @}
>From f49055225d21cec52913830f3efb4789944477ae Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Tue, 27 Aug 2024 15:59:19 -0700
Subject: [PATCH 5/7] Document lifetime on `CreateTargetInfo()`
---
clang/include/clang/Basic/TargetInfo.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index e4d51e298e313..77763a5b1d574 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -310,7 +310,7 @@ class TargetInfo : public TransferrableTargetInfo,
///
/// \param Opts - The options to use to initialize the target. The target may
/// modify the options to canonicalize the target feature information to match
- /// what the backend expects.
+ /// what the backend expects. These must outlive the returned TargetInfo.
static TargetInfo *CreateTargetInfo(DiagnosticsEngine &Diags,
TargetOptions &Opts);
>From 61b039767a49e3f4573a4ac144cbe6048ebf4ba5 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Tue, 11 Mar 2025 10:59:10 -0700
Subject: [PATCH 6/7] Update ToolChainTest.cpp
---
clang/unittests/Driver/ToolChainTest.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/unittests/Driver/ToolChainTest.cpp b/clang/unittests/Driver/ToolChainTest.cpp
index 14c505b5fbd9e..9fe8cd18beb9b 100644
--- a/clang/unittests/Driver/ToolChainTest.cpp
+++ b/clang/unittests/Driver/ToolChainTest.cpp
@@ -589,8 +589,7 @@ TEST(ToolChainTest, UEFICallingConventionTest) {
compiler.getTargetOpts().Triple = Tr.getTriple();
compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
- compiler.getDiagnostics(),
- std::make_shared<clang::TargetOptions>(compiler.getTargetOpts())));
+ compiler.getDiagnostics(), compiler.getTargetOpts()));
EXPECT_EQ(compiler.getTarget().getCallingConvKind(true),
TargetInfo::CallingConvKind::CCK_MicrosoftWin64);
>From 172194827d5df4f39d6fd995780118604c038012 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Fri, 25 Apr 2025 09:18:45 -0700
Subject: [PATCH 7/7] Update ParseHLSLRootSignatureTest.cpp & LLDB
---
clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp | 2 +-
.../Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp | 2 +-
.../Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp | 2 +-
lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp | 2 +-
4 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
index 1d89567509e72..28c13201731e5 100644
--- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
+++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
@@ -72,7 +72,7 @@ class ParseHLSLRootSignatureTest : public ::testing::Test {
SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) {
// This is an arbitrarily chosen target triple to create the target info.
TargetOpts->Triple = "dxil";
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
}
std::unique_ptr<Preprocessor> createPP(StringRef Source,
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
index 078c9312ce4a7..25b4c8b876265 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
@@ -789,7 +789,7 @@ ClangExpressionParser::ClangExpressionParser(
if (auto *target_info = TargetInfo::CreateTargetInfo(
m_compiler->getDiagnostics(),
- m_compiler->getInvocation().TargetOpts)) {
+ m_compiler->getInvocation().getTargetOpts())) {
if (log) {
LLDB_LOGF(log, "Target datalayout string: '%s'",
target_info->getDataLayoutString());
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp
index 3eb5c3b2101b5..16d2beb48c82c 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp
@@ -742,7 +742,7 @@ ClangModulesDeclVendor::Create(Target &target) {
std::unique_ptr<clang::FrontendAction> action(new clang::SyntaxOnlyAction);
instance->setTarget(clang::TargetInfo::CreateTargetInfo(
- *diagnostics_engine, instance->getInvocation().TargetOpts));
+ *diagnostics_engine, instance->getInvocation().getTargetOpts()));
if (!instance->hasTarget())
return nullptr;
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 59292f4b24af3..2dc92f8ce033b 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -744,7 +744,7 @@ TargetInfo *TypeSystemClang::getTargetInfo() {
// target_triple should be something like "x86_64-apple-macosx"
if (m_target_info_up == nullptr && !m_target_triple.empty())
m_target_info_up.reset(TargetInfo::CreateTargetInfo(
- getASTContext().getDiagnostics(), getTargetOptions()));
+ getASTContext().getDiagnostics(), *getTargetOptions()));
return m_target_info_up.get();
}
More information about the cfe-commits
mailing list