Skip to content

Commit f051029

Browse files
authored
Merge pull request #7447 from apple/jan_svoboda/20230725-cow-ci
[clang][deps] Introduce copy-on-write `CompilerInvocation`
2 parents 4c03d27 + bbdb312 commit f051029

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+844
-509
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,13 @@ ClangTidyASTConsumerFactory::createASTConsumer(
435435
Consumers.push_back(Finder->newASTConsumer());
436436

437437
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
438-
AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
439-
AnalyzerOptions->CheckersAndPackages = getAnalyzerCheckersAndPackages(
438+
AnalyzerOptions &AnalyzerOptions = Compiler.getAnalyzerOpts();
439+
AnalyzerOptions.CheckersAndPackages = getAnalyzerCheckersAndPackages(
440440
Context, Context.canEnableAnalyzerAlphaCheckers());
441-
if (!AnalyzerOptions->CheckersAndPackages.empty()) {
442-
setStaticAnalyzerCheckerOpts(Context.getOptions(), *AnalyzerOptions);
443-
AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
444-
AnalyzerOptions->eagerlyAssumeBinOpBifurcation = true;
441+
if (!AnalyzerOptions.CheckersAndPackages.empty()) {
442+
setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
443+
AnalyzerOptions.AnalysisDiagOpt = PD_NONE;
444+
AnalyzerOptions.eagerlyAssumeBinOpBifurcation = true;
445445
std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
446446
ento::CreateAnalysisConsumer(Compiler);
447447
AnalysisConsumer->AddDiagnosticConsumer(

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct UpdateIndexCallbacks : public ParsingCallbacks {
8383

8484
auto &PP = ASTCtx.getPreprocessor();
8585
auto &CI = ASTCtx.getCompilerInvocation();
86-
if (auto Loc = Stdlib->add(*CI.getLangOpts(), PP.getHeaderSearchInfo()))
86+
if (auto Loc = Stdlib->add(CI.getLangOpts(), PP.getHeaderSearchInfo()))
8787
indexStdlib(CI, std::move(*Loc));
8888

8989
// FIndex outlives the UpdateIndexCallbacks.
@@ -105,7 +105,7 @@ struct UpdateIndexCallbacks : public ParsingCallbacks {
105105
// This task is owned by Tasks, which outlives the TUScheduler and
106106
// therefore the UpdateIndexCallbacks.
107107
// We must be careful that the references we capture outlive TUScheduler.
108-
auto Task = [LO(*CI.getLangOpts()), Loc(std::move(Loc)),
108+
auto Task = [LO(CI.getLangOpts()), Loc(std::move(Loc)),
109109
CI(std::make_unique<CompilerInvocation>(CI)),
110110
// External values that outlive ClangdServer
111111
TFS(&TFS),

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,11 +1356,11 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
13561356
auto &FrontendOpts = CI->getFrontendOpts();
13571357
FrontendOpts.SkipFunctionBodies = true;
13581358
// Disable typo correction in Sema.
1359-
CI->getLangOpts()->SpellChecking = false;
1359+
CI->getLangOpts().SpellChecking = false;
13601360
// Code completion won't trigger in delayed template bodies.
13611361
// This is on-by-default in windows to allow parsing SDK headers; we're only
13621362
// disabling it for the main-file (not preamble).
1363-
CI->getLangOpts()->DelayedTemplateParsing = false;
1363+
CI->getLangOpts().DelayedTemplateParsing = false;
13641364
// Setup code completion.
13651365
FrontendOpts.CodeCompleteOpts = Options;
13661366
FrontendOpts.CodeCompletionAt.FileName = std::string(Input.FileName);
@@ -1380,7 +1380,7 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
13801380
// overriding the preamble will break sema completion. Fortunately we can just
13811381
// skip all includes in this case; these completions are really simple.
13821382
PreambleBounds PreambleRegion =
1383-
ComputePreambleBounds(*CI->getLangOpts(), *ContentsBuffer, 0);
1383+
ComputePreambleBounds(CI->getLangOpts(), *ContentsBuffer, 0);
13841384
bool CompletingInPreamble = Input.Offset < PreambleRegion.Size ||
13851385
(!PreambleRegion.PreambleEndsAtStartOfLine &&
13861386
Input.Offset == PreambleRegion.Size);

clang-tools-extra/clangd/Compiler.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ void disableUnsupportedOptions(CompilerInvocation &CI) {
8484
// These options mostly affect codegen, and aren't relevant to clangd. And
8585
// clang will die immediately when these files are not existed.
8686
// Disable these uninteresting options to make clangd more robust.
87-
CI.getLangOpts()->NoSanitizeFiles.clear();
88-
CI.getLangOpts()->XRayAttrListFiles.clear();
89-
CI.getLangOpts()->ProfileListFiles.clear();
90-
CI.getLangOpts()->XRayAlwaysInstrumentFiles.clear();
91-
CI.getLangOpts()->XRayNeverInstrumentFiles.clear();
87+
CI.getLangOpts().NoSanitizeFiles.clear();
88+
CI.getLangOpts().XRayAttrListFiles.clear();
89+
CI.getLangOpts().ProfileListFiles.clear();
90+
CI.getLangOpts().XRayAlwaysInstrumentFiles.clear();
91+
CI.getLangOpts().XRayNeverInstrumentFiles.clear();
9292
}
9393

9494
std::unique_ptr<CompilerInvocation>
@@ -118,8 +118,8 @@ buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
118118
return nullptr;
119119
// createInvocationFromCommandLine sets DisableFree.
120120
CI->getFrontendOpts().DisableFree = false;
121-
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
122-
CI->getLangOpts()->RetainCommentsFromSystemHeaders = true;
121+
CI->getLangOpts().CommentOpts.ParseAllComments = true;
122+
CI->getLangOpts().RetainCommentsFromSystemHeaders = true;
123123

124124
disableUnsupportedOptions(*CI);
125125
return CI;

clang-tools-extra/clangd/ParsedAST.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
404404

405405
// This is on-by-default in windows to allow parsing SDK headers, but it
406406
// breaks many features. Disable it for the main-file (not preamble).
407-
CI->getLangOpts()->DelayedTemplateParsing = false;
407+
CI->getLangOpts().DelayedTemplateParsing = false;
408408

409409
std::vector<std::unique_ptr<FeatureModule::ASTListener>> ASTListeners;
410410
if (Inputs.FeatureModules) {

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ scanPreamble(llvm::StringRef Contents, const tooling::CompileCommand &Cmd) {
365365
// This means we're scanning (though not preprocessing) the preamble section
366366
// twice. However, it's important to precisely follow the preamble bounds used
367367
// elsewhere.
368-
auto Bounds = ComputePreambleBounds(*CI->getLangOpts(), *ContentsBuffer, 0);
368+
auto Bounds = ComputePreambleBounds(CI->getLangOpts(), *ContentsBuffer, 0);
369369
auto PreambleContents = llvm::MemoryBuffer::getMemBufferCopy(
370370
llvm::StringRef(PI.Contents).take_front(Bounds.Size));
371371
auto Clang = prepareCompilerInstance(
@@ -596,7 +596,7 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
596596
// without those.
597597
auto ContentsBuffer =
598598
llvm::MemoryBuffer::getMemBuffer(Inputs.Contents, FileName);
599-
auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), *ContentsBuffer, 0);
599+
auto Bounds = ComputePreambleBounds(CI.getLangOpts(), *ContentsBuffer, 0);
600600

601601
trace::Span Tracer("BuildPreamble");
602602
SPAN_ATTACH(Tracer, "File", FileName);
@@ -622,7 +622,7 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
622622
const clang::Diagnostic &Info) {
623623
if (Cfg.Diagnostics.SuppressAll ||
624624
isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress,
625-
*CI.getLangOpts()))
625+
CI.getLangOpts()))
626626
return DiagnosticsEngine::Ignored;
627627
switch (Info.getID()) {
628628
case diag::warn_no_newline_eof:
@@ -727,7 +727,7 @@ bool isPreambleCompatible(const PreambleData &Preamble,
727727
const CompilerInvocation &CI) {
728728
auto ContentsBuffer =
729729
llvm::MemoryBuffer::getMemBuffer(Inputs.Contents, FileName);
730-
auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), *ContentsBuffer, 0);
730+
auto Bounds = ComputePreambleBounds(CI.getLangOpts(), *ContentsBuffer, 0);
731731
auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
732732
return compileCommandsAreEqual(Inputs.CompileCommand,
733733
Preamble.CompileCommand) &&

clang-tools-extra/clangd/index/StdLib.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ SymbolSlab indexStandardLibrary(llvm::StringRef HeaderSources,
207207
}
208208
const FrontendInputFile &Input = CI->getFrontendOpts().Inputs.front();
209209
trace::Span Tracer("StandardLibraryIndex");
210-
LangStandard::Kind LangStd = standardFromOpts(*CI->getLangOpts());
210+
LangStandard::Kind LangStd = standardFromOpts(CI->getLangOpts());
211211
log("Indexing {0} standard library in the context of {1}",
212212
LangStandard::getLangStandardForKind(LangStd).getName(), Input.getFile());
213213

@@ -267,7 +267,7 @@ SymbolSlab indexStandardLibrary(llvm::StringRef HeaderSources,
267267
SymbolSlab indexStandardLibrary(std::unique_ptr<CompilerInvocation> Invocation,
268268
const StdLibLocation &Loc,
269269
const ThreadsafeFS &TFS) {
270-
llvm::StringRef Header = getStdlibUmbrellaHeader(*Invocation->getLangOpts());
270+
llvm::StringRef Header = getStdlibUmbrellaHeader(Invocation->getLangOpts());
271271
return indexStandardLibrary(Header, std::move(Invocation), Loc, TFS);
272272
}
273273

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ collectPatchedIncludes(llvm::StringRef ModifiedContents,
8888
// introduced by the patch is parsed and nothing else.
8989
// We don't run PP directly over the patch cotents to test production
9090
// behaviour.
91-
auto Bounds = Lexer::ComputePreamble(ModifiedContents, *CI->getLangOpts());
91+
auto Bounds = Lexer::ComputePreamble(ModifiedContents, CI->getLangOpts());
9292
auto Clang =
9393
prepareCompilerInstance(std::move(CI), &BaselinePreamble->Preamble,
9494
llvm::MemoryBuffer::getMemBufferCopy(
@@ -588,7 +588,7 @@ TEST(PreamblePatch, ModifiedBounds) {
588588
ASSERT_TRUE(CI);
589589

590590
const auto ExpectedBounds =
591-
Lexer::ComputePreamble(Case.Modified, *CI->getLangOpts());
591+
Lexer::ComputePreamble(Case.Modified, CI->getLangOpts());
592592
EXPECT_EQ(PP.modifiedBounds().Size, ExpectedBounds.Size);
593593
EXPECT_EQ(PP.modifiedBounds().PreambleEndsAtStartOfLine,
594594
ExpectedBounds.PreambleEndsAtStartOfLine);

clang/include/clang/ARCMigrate/ARCMT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class MigrationProcess {
102102
public:
103103
bool HadARCErrors;
104104

105-
MigrationProcess(const CompilerInvocation &CI,
105+
MigrationProcess(CompilerInvocation &CI,
106106
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
107107
DiagnosticConsumer *diagClient,
108108
StringRef outputDir = StringRef());

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace clang {
3333
/// that this large collection of bitfields is a trivial class type.
3434
class CodeGenOptionsBase {
3535
friend class CompilerInvocation;
36+
friend class CompilerInvocationBase;
3637

3738
public:
3839
#define CODEGENOPT(Name, Bits, Default) unsigned Name : Bits;

clang/include/clang/Basic/DiagnosticOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class DiagnosticOptions : public RefCountedBase<DiagnosticOptions>{
7272
clang::DiagnosticsEngine *, bool);
7373

7474
friend class CompilerInvocation;
75+
friend class CompilerInvocationBase;
7576

7677
public:
7778
enum TextDiagnosticFormat { Clang, MSVC, Vi, SARIF };

clang/include/clang/Basic/LangOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace clang {
3434
/// this large collection of bitfields is a trivial class type.
3535
class LangOptionsBase {
3636
friend class CompilerInvocation;
37+
friend class CompilerInvocationBase;
3738

3839
public:
3940
// Define simple language options (with no accessors).

clang/include/clang/Frontend/CompileJobCacheKey.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class raw_ostream;
2727
namespace clang {
2828

2929
class CompilerInvocation;
30+
class CowCompilerInvocation;
3031
class DiagnosticsEngine;
3132

3233
/// Caching-related options for a given \c CompilerInvocation that are
@@ -48,6 +49,9 @@ struct CompileJobCachingOptions {
4849
std::optional<llvm::cas::CASID>
4950
createCompileJobCacheKey(llvm::cas::ObjectStore &CAS, DiagnosticsEngine &Diags,
5051
const CompilerInvocation &Invocation);
52+
std::optional<llvm::cas::CASID>
53+
createCompileJobCacheKey(llvm::cas::ObjectStore &CAS, DiagnosticsEngine &Diags,
54+
const CowCompilerInvocation &Invocation);
5155

5256
/// Perform any destructive changes needed to canonicalize \p Invocation for
5357
/// caching, extracting the settings that affect compilation even if they do not

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,7 @@ class CompilerInstance : public ModuleLoader {
270270
/// @name Forwarding Methods
271271
/// {
272272

273-
AnalyzerOptionsRef getAnalyzerOpts() {
274-
return Invocation->getAnalyzerOpts();
275-
}
273+
AnalyzerOptions &getAnalyzerOpts() { return Invocation->getAnalyzerOpts(); }
276274

277275
CodeGenOptions &getCodeGenOpts() {
278276
return Invocation->getCodeGenOpts();
@@ -326,12 +324,8 @@ class CompilerInstance : public ModuleLoader {
326324
return Invocation->getAPINotesOpts();
327325
}
328326

329-
LangOptions &getLangOpts() {
330-
return *Invocation->getLangOpts();
331-
}
332-
const LangOptions &getLangOpts() const {
333-
return *Invocation->getLangOpts();
334-
}
327+
LangOptions &getLangOpts() { return Invocation->getLangOpts(); }
328+
const LangOptions &getLangOpts() const { return Invocation->getLangOpts(); }
335329

336330
PreprocessorOptions &getPreprocessorOpts() {
337331
return Invocation->getPreprocessorOpts();

0 commit comments

Comments
 (0)