Skip to content

Commit 6d5a359

Browse files
committed
[clang] Make the entire CompilerInvocation ref-counted (llvm#65647)
This enables making the whole `CompilerInvocation` more efficient through copy-on-write.
1 parent af321b9 commit 6d5a359

File tree

2 files changed

+147
-159
lines changed

2 files changed

+147
-159
lines changed

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 69 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,12 @@ bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
7373
DiagnosticsEngine *Diags = nullptr,
7474
bool DefaultDiagColor = true);
7575

76-
/// The base class of CompilerInvocation with reference semantics.
77-
///
78-
/// This class stores option objects behind reference-counted pointers. This is
79-
/// useful for clients that want to keep some option object around even after
80-
/// CompilerInvocation gets destroyed, without making a copy.
81-
///
82-
/// This is a separate class so that we can implement the copy constructor and
83-
/// assignment here and leave them defaulted in the rest of CompilerInvocation.
84-
class CompilerInvocationRefBase {
85-
public:
76+
/// The base class of CompilerInvocation. It keeps individual option objects
77+
/// behind reference-counted pointers, which is useful for clients that want to
78+
/// keep select option objects alive (even after CompilerInvocation gets
79+
/// destroyed) without making a copy.
80+
class CompilerInvocationBase {
81+
protected:
8682
/// Options controlling the language variant.
8783
std::shared_ptr<LangOptions> LangOpts;
8884

@@ -93,115 +89,81 @@ class CompilerInvocationRefBase {
9389
IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
9490

9591
/// Options controlling the \#include directive.
96-
std::shared_ptr<HeaderSearchOptions> HeaderSearchOpts;
92+
std::shared_ptr<HeaderSearchOptions> HSOpts;
9793

9894
/// Options controlling the preprocessor (aside from \#include handling).
99-
std::shared_ptr<PreprocessorOptions> PreprocessorOpts;
95+
std::shared_ptr<PreprocessorOptions> PPOpts;
10096

10197
/// Options controlling the static analyzer.
10298
AnalyzerOptionsRef AnalyzerOpts;
10399

104-
CompilerInvocationRefBase();
105-
CompilerInvocationRefBase(const CompilerInvocationRefBase &X);
106-
CompilerInvocationRefBase(CompilerInvocationRefBase &&X);
107-
CompilerInvocationRefBase &operator=(CompilerInvocationRefBase X);
108-
CompilerInvocationRefBase &operator=(CompilerInvocationRefBase &&X);
109-
~CompilerInvocationRefBase();
110-
111-
LangOptions &getLangOpts() { return *LangOpts; }
112-
const LangOptions &getLangOpts() const { return *LangOpts; }
113-
114-
TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
115-
const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); }
116-
117-
DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
118-
119-
HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
120-
121-
const HeaderSearchOptions &getHeaderSearchOpts() const {
122-
return *HeaderSearchOpts;
123-
}
124-
125-
std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const {
126-
return HeaderSearchOpts;
127-
}
128-
129-
std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
130-
return PreprocessorOpts;
131-
}
132-
133-
PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
134-
135-
const PreprocessorOptions &getPreprocessorOpts() const {
136-
return *PreprocessorOpts;
137-
}
138-
139-
AnalyzerOptions &getAnalyzerOpts() { return *AnalyzerOpts; }
140-
const AnalyzerOptions &getAnalyzerOpts() const { return *AnalyzerOpts; }
141-
};
142-
143-
/// The base class of CompilerInvocation with value semantics.
144-
class CompilerInvocationValueBase {
145-
protected:
146-
MigratorOptions MigratorOpts;
100+
std::shared_ptr<MigratorOptions> MigratorOpts;
147101

148102
/// Options controlling API notes.
149-
APINotesOptions APINotesOpts;
103+
std::shared_ptr<APINotesOptions> APINotesOpts;
150104

151105
/// Options configuring the CAS.
152-
CASOptions CASOpts;
106+
std::shared_ptr<CASOptions> CASOpts;
153107

154108
/// Options controlling IRgen and the backend.
155-
CodeGenOptions CodeGenOpts;
156-
157-
/// Options controlling dependency output.
158-
DependencyOutputOptions DependencyOutputOpts;
109+
std::shared_ptr<CodeGenOptions> CodeGenOpts;
159110

160111
/// Options controlling file system operations.
161-
FileSystemOptions FileSystemOpts;
112+
std::shared_ptr<FileSystemOptions> FSOpts;
162113

163114
/// Options controlling the frontend itself.
164-
FrontendOptions FrontendOpts;
115+
std::shared_ptr<FrontendOptions> FrontendOpts;
116+
117+
/// Options controlling dependency output.
118+
std::shared_ptr<DependencyOutputOptions> DependencyOutputOpts;
165119

166120
/// Options controlling preprocessed output.
167-
PreprocessorOutputOptions PreprocessorOutputOpts;
121+
std::shared_ptr<PreprocessorOutputOptions> PreprocessorOutputOpts;
168122

169123
public:
170-
CASOptions &getCASOpts() { return CASOpts; }
171-
const CASOptions &getCASOpts() const { return CASOpts; }
172-
173-
MigratorOptions &getMigratorOpts() { return MigratorOpts; }
174-
const MigratorOptions &getMigratorOpts() const { return MigratorOpts; }
175-
176-
APINotesOptions &getAPINotesOpts() { return APINotesOpts; }
177-
const APINotesOptions &getAPINotesOpts() const { return APINotesOpts; }
178-
179-
CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
180-
const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
181-
182-
DependencyOutputOptions &getDependencyOutputOpts() {
183-
return DependencyOutputOpts;
184-
}
124+
CompilerInvocationBase();
125+
CompilerInvocationBase(const CompilerInvocationBase &X) { operator=(X); }
126+
CompilerInvocationBase(CompilerInvocationBase &&X) = default;
127+
CompilerInvocationBase &operator=(const CompilerInvocationBase &X);
128+
CompilerInvocationBase &operator=(CompilerInvocationBase &&X) = default;
129+
~CompilerInvocationBase() = default;
185130

131+
const LangOptions &getLangOpts() const { return *LangOpts; }
132+
const TargetOptions &getTargetOpts() const { return *TargetOpts; }
133+
const DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
134+
const HeaderSearchOptions &getHeaderSearchOpts() const { return *HSOpts; }
135+
const PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
136+
const AnalyzerOptions &getAnalyzerOpts() const { return *AnalyzerOpts; }
137+
const MigratorOptions &getMigratorOpts() const { return *MigratorOpts; }
138+
const APINotesOptions &getAPINotesOpts() const { return *APINotesOpts; }
139+
const CASOptions &getCASOpts() const { return *CASOpts; }
140+
const CodeGenOptions &getCodeGenOpts() const { return *CodeGenOpts; }
141+
const FileSystemOptions &getFileSystemOpts() const { return *FSOpts; }
142+
const FrontendOptions &getFrontendOpts() const { return *FrontendOpts; }
186143
const DependencyOutputOptions &getDependencyOutputOpts() const {
187-
return DependencyOutputOpts;
144+
return *DependencyOutputOpts;
188145
}
189-
190-
FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
191-
192-
const FileSystemOptions &getFileSystemOpts() const {
193-
return FileSystemOpts;
146+
const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
147+
return *PreprocessorOutputOpts;
194148
}
195149

196-
FrontendOptions &getFrontendOpts() { return FrontendOpts; }
197-
const FrontendOptions &getFrontendOpts() const { return FrontendOpts; }
198-
199-
PreprocessorOutputOptions &getPreprocessorOutputOpts() {
200-
return PreprocessorOutputOpts;
150+
LangOptions &getLangOpts() { return *LangOpts; }
151+
TargetOptions &getTargetOpts() { return *TargetOpts; }
152+
DiagnosticOptions &getDiagnosticOpts() { return *DiagnosticOpts; }
153+
HeaderSearchOptions &getHeaderSearchOpts() { return *HSOpts; }
154+
PreprocessorOptions &getPreprocessorOpts() { return *PPOpts; }
155+
AnalyzerOptions &getAnalyzerOpts() { return *AnalyzerOpts; }
156+
MigratorOptions &getMigratorOpts() { return *MigratorOpts; }
157+
APINotesOptions &getAPINotesOpts() { return *APINotesOpts; }
158+
CASOptions &getCASOpts() { return *CASOpts; }
159+
CodeGenOptions &getCodeGenOpts() { return *CodeGenOpts; }
160+
FileSystemOptions &getFileSystemOpts() { return *FSOpts; }
161+
FrontendOptions &getFrontendOpts() { return *FrontendOpts; }
162+
DependencyOutputOptions &getDependencyOutputOpts() {
163+
return *DependencyOutputOpts;
201164
}
202-
203-
const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
204-
return PreprocessorOutputOpts;
165+
PreprocessorOutputOptions &getPreprocessorOutputOpts() {
166+
return *PreprocessorOutputOpts;
205167
}
206168
};
207169

@@ -210,9 +172,21 @@ class CompilerInvocationValueBase {
210172
/// This class is designed to represent an abstract "invocation" of the
211173
/// compiler, including data such as the include paths, the code generation
212174
/// options, the warning flags, and so on.
213-
class CompilerInvocation : public CompilerInvocationRefBase,
214-
public CompilerInvocationValueBase {
175+
class CompilerInvocation : public CompilerInvocationBase {
215176
public:
177+
/// Base class internals.
178+
/// @{
179+
using CompilerInvocationBase::LangOpts;
180+
using CompilerInvocationBase::TargetOpts;
181+
using CompilerInvocationBase::DiagnosticOpts;
182+
std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() {
183+
return HSOpts;
184+
}
185+
std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
186+
return PPOpts;
187+
}
188+
/// @}
189+
216190
/// Create a compiler invocation from a list of input options.
217191
/// \returns true on success.
218192
///

0 commit comments

Comments
 (0)