Skip to content

[clang] Hide the TargetOptions pointer from CompilerInvocation #106271

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

Merged
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
7 changes: 5 additions & 2 deletions clang-tools-extra/clangd/Preamble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/Preamble.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/SystemIncludeExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/modularize/ModularizeUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions clang/include/clang/Basic/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -310,10 +310,9 @@ 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);
/// what the backend expects. These must outlive the returned TargetInfo.
static TargetInfo *CreateTargetInfo(DiagnosticsEngine &Diags,
TargetOptions &Opts);

virtual ~TargetInfo();

Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Frontend/ASTUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/Frontend/CompilerInvocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ class CompilerInvocation : public CompilerInvocationBase {
/// Base class internals.
/// @{
using CompilerInvocationBase::LangOpts;
using CompilerInvocationBase::TargetOpts;
std::shared_ptr<LangOptions> getLangOptsPtr() { return LangOpts; }
/// @}

Expand Down
7 changes: 4 additions & 3 deletions clang/lib/Basic/Targets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/Frontend/ASTUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Frontend/ChainedIncludesSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. "
Expand Down
2 changes: 1 addition & 1 deletion clang/tools/clang-import-test/clang-import-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Analysis/MacroExpansionContextTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__
}

Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Basic/SourceManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions clang/unittests/CodeGen/TestCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions clang/unittests/Driver/ToolChainTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Frontend/UtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Lex/HeaderSearchTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Lex/LexerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Lex/ModuleDeclStateTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Lex/PPCallbacksTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Lex/PPDependencyDirectivesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Lex/PPMemoryAllocationsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Loading