Skip to content

Commit 7a37074

Browse files
authored
[clang][lex] Store non-owning options ref in HeaderSearch (#132780)
This makes it so that `CompilerInvocation` can be the only entity that manages ownership of `HeaderSearchOptions`, making it possible to implement copy-on-write semantics.
1 parent 613a077 commit 7a37074

20 files changed

+86
-81
lines changed

clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ ExpandModularHeadersPPCallbacks::ExpandModularHeadersPPCallbacks(
7373
// Forward the new diagnostics to the original DiagnosticConsumer.
7474
Diags(new DiagnosticIDs, new DiagnosticOptions,
7575
new ForwardingDiagnosticConsumer(Compiler.getDiagnosticClient())),
76-
LangOpts(Compiler.getLangOpts()) {
76+
LangOpts(Compiler.getLangOpts()), HSOpts(Compiler.getHeaderSearchOpts()) {
7777
// Add a FileSystem containing the extra files needed in place of modular
7878
// headers.
7979
OverlayFS->pushOverlay(InMemoryFs);
@@ -86,11 +86,8 @@ ExpandModularHeadersPPCallbacks::ExpandModularHeadersPPCallbacks(
8686

8787
LangOpts.Modules = false;
8888

89-
auto HSO = std::make_shared<HeaderSearchOptions>();
90-
*HSO = Compiler.getHeaderSearchOpts();
91-
92-
HeaderInfo = std::make_unique<HeaderSearch>(HSO, Sources, Diags, LangOpts,
93-
&Compiler.getTarget());
89+
HeaderInfo = std::make_unique<HeaderSearch>(HSOpts, Sources, Diags, LangOpts,
90+
&Compiler.getTarget());
9491

9592
auto PO = std::make_shared<PreprocessorOptions>();
9693
*PO = Compiler.getPreprocessorOpts();
@@ -102,7 +99,7 @@ ExpandModularHeadersPPCallbacks::ExpandModularHeadersPPCallbacks(
10299
PP->Initialize(Compiler.getTarget(), Compiler.getAuxTarget());
103100
InitializePreprocessor(*PP, *PO, Compiler.getPCHContainerReader(),
104101
Compiler.getFrontendOpts(), Compiler.getCodeGenOpts());
105-
ApplyHeaderSearchOptions(*HeaderInfo, *HSO, LangOpts,
102+
ApplyHeaderSearchOptions(*HeaderInfo, HSOpts, LangOpts,
106103
Compiler.getTarget().getTriple());
107104
}
108105

clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_CLANG_TOOLING_EXPANDMODULARHEADERSPPCALLBACKS_H_
1010
#define LLVM_CLANG_TOOLING_EXPANDMODULARHEADERSPPCALLBACKS_H_
1111

12+
#include "clang/Lex/HeaderSearchOptions.h"
1213
#include "clang/Lex/PPCallbacks.h"
1314
#include "clang/Lex/Preprocessor.h"
1415
#include "llvm/ADT/DenseSet.h"
@@ -129,6 +130,7 @@ class ExpandModularHeadersPPCallbacks : public PPCallbacks {
129130
SourceManager &Sources;
130131
DiagnosticsEngine Diags;
131132
LangOptions LangOpts;
133+
HeaderSearchOptions HSOpts;
132134
TrivialModuleLoader ModuleLoader;
133135

134136
std::unique_ptr<HeaderSearch> HeaderInfo;

clang-tools-extra/clangd/ModulesBuilder.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ class ReusablePrerequisiteModules : public PrerequisiteModules {
181181
bool IsModuleFileUpToDate(PathRef ModuleFilePath,
182182
const PrerequisiteModules &RequisiteModules,
183183
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
184-
auto HSOpts = std::make_shared<HeaderSearchOptions>();
185-
RequisiteModules.adjustHeaderSearchOptions(*HSOpts);
186-
HSOpts->ForceCheckCXX20ModulesInputFiles = true;
187-
HSOpts->ValidateASTInputFilesContent = true;
184+
HeaderSearchOptions HSOpts;
185+
RequisiteModules.adjustHeaderSearchOptions(HSOpts);
186+
HSOpts.ForceCheckCXX20ModulesInputFiles = true;
187+
HSOpts.ValidateASTInputFilesContent = true;
188188

189189
clang::clangd::IgnoreDiagnostics IgnoreDiags;
190190
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
@@ -199,7 +199,7 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath,
199199

200200
SourceManager SourceMgr(*Diags, FileMgr);
201201

202-
HeaderSearch HeaderInfo(std::move(HSOpts), SourceMgr, *Diags, LangOpts,
202+
HeaderSearch HeaderInfo(HSOpts, SourceMgr, *Diags, LangOpts,
203203
/*Target=*/nullptr);
204204

205205
TrivialModuleLoader ModuleLoader;

clang-tools-extra/clangd/unittests/StdLibTests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ TEST(StdLibTests, StdLibSet) {
8585
FS.Files["std/_"] = "";
8686
FS.Files["libc/_"] = "";
8787

88+
HeaderSearchOptions HSOpts;
8889
auto Add = [&](const LangOptions &LO,
8990
std::vector<llvm::StringRef> SearchPath) {
9091
SourceManagerForFile SM("scratch", "");
9192
SM.get().getFileManager().setVirtualFileSystem(FS.view(std::nullopt));
92-
HeaderSearch HS(/*HSOpts=*/nullptr, SM.get(), SM.get().getDiagnostics(), LO,
93+
HeaderSearch HS(HSOpts, SM.get(), SM.get().getDiagnostics(), LO,
9394
/*Target=*/nullptr);
9495
for (auto P : SearchPath)
9596
HS.AddSearchPath(

clang-tools-extra/modularize/ModularizeUtilities.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ ModularizeUtilities::ModularizeUtilities(std::vector<std::string> &InputPaths,
5555
TargetOpts(new ModuleMapTargetOptions()),
5656
Target(TargetInfo::CreateTargetInfo(*Diagnostics, TargetOpts)),
5757
FileMgr(new FileManager(FileSystemOpts)),
58-
SourceMgr(new SourceManager(*Diagnostics, *FileMgr, false)),
59-
HeaderInfo(new HeaderSearch(std::make_shared<HeaderSearchOptions>(),
60-
*SourceMgr, *Diagnostics, *LangOpts,
58+
SourceMgr(new SourceManager(*Diagnostics, *FileMgr, false)), HSOpts(),
59+
HeaderInfo(new HeaderSearch(HSOpts, *SourceMgr, *Diagnostics, *LangOpts,
6160
Target.get())) {}
6261

6362
// Create instance of ModularizeUtilities, to simplify setting up

clang-tools-extra/modularize/ModularizeUtilities.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ class ModularizeUtilities {
213213
llvm::IntrusiveRefCntPtr<clang::FileManager> FileMgr;
214214
/// Source manager.
215215
llvm::IntrusiveRefCntPtr<clang::SourceManager> SourceMgr;
216+
/// Header search options.
217+
clang::HeaderSearchOptions HSOpts;
216218
/// Header search manager.
217219
std::unique_ptr<clang::HeaderSearch> HeaderInfo;
218220
// The loaded module map objects.

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ class ASTUnit {
139139
/// Optional owned invocation, just used to make the invocation used in
140140
/// LoadFromCommandLine available.
141141
std::shared_ptr<CompilerInvocation> Invocation;
142+
/// Optional owned invocation, just used to make the invocation used in
143+
/// Parse available.
144+
std::shared_ptr<CompilerInvocation> CCInvocation;
142145

143146
/// Fake module loader: the AST unit doesn't need to load any modules.
144147
TrivialModuleLoader ModuleLoader;

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class HeaderSearch {
241241
friend SearchDirIterator;
242242

243243
/// Header-search options used to initialize this header search.
244-
std::shared_ptr<const HeaderSearchOptions> HSOpts;
244+
const HeaderSearchOptions &HSOpts;
245245

246246
/// Mapping from SearchDir to HeaderSearchOptions::UserEntries indices.
247247
llvm::DenseMap<unsigned, unsigned> SearchDirToHSEntry;
@@ -359,15 +359,15 @@ class HeaderSearch {
359359
void indexInitialHeaderMaps();
360360

361361
public:
362-
HeaderSearch(std::shared_ptr<const HeaderSearchOptions> HSOpts,
363-
SourceManager &SourceMgr, DiagnosticsEngine &Diags,
364-
const LangOptions &LangOpts, const TargetInfo *Target);
362+
HeaderSearch(const HeaderSearchOptions &HSOpts, SourceManager &SourceMgr,
363+
DiagnosticsEngine &Diags, const LangOptions &LangOpts,
364+
const TargetInfo *Target);
365365
HeaderSearch(const HeaderSearch &) = delete;
366366
HeaderSearch &operator=(const HeaderSearch &) = delete;
367367

368368
/// Retrieve the header-search options with which this header search
369369
/// was initialized.
370-
const HeaderSearchOptions &getHeaderSearchOpts() const { return *HSOpts; }
370+
const HeaderSearchOptions &getHeaderSearchOpts() const { return HSOpts; }
371371

372372
FileManager &getFileMgr() const { return FileMgr; }
373373

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
832832
AST->ModCache = createCrossProcessModuleCache();
833833
AST->HSOpts = HSOpts ? HSOpts : std::make_shared<HeaderSearchOptions>();
834834
AST->HSOpts->ModuleFormat = std::string(PCHContainerRdr.getFormats().front());
835-
AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
835+
AST->HeaderInfo.reset(new HeaderSearch(AST->getHeaderSearchOpts(),
836836
AST->getSourceManager(),
837837
AST->getDiagnostics(),
838838
AST->getLangOpts(),
@@ -1153,7 +1153,7 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
11531153
assert(VFS == &FileMgr->getVirtualFileSystem() &&
11541154
"VFS passed to Parse and VFS in FileMgr are different");
11551155

1156-
auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
1156+
CCInvocation = std::make_shared<CompilerInvocation>(*Invocation);
11571157
if (OverrideMainBuffer) {
11581158
assert(Preamble &&
11591159
"No preamble was built, but OverrideMainBuffer is not null");

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
451451

452452
// Create the Preprocessor.
453453
HeaderSearch *HeaderInfo =
454-
new HeaderSearch(getHeaderSearchOptsPtr(), getSourceManager(),
454+
new HeaderSearch(getHeaderSearchOpts(), getSourceManager(),
455455
getDiagnostics(), getLangOpts(), &getTarget());
456456
PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOptsPtr(),
457457
getDiagnostics(), getLangOpts(),

0 commit comments

Comments
 (0)