Skip to content

Commit ee3a302

Browse files
authored
[clang] Move state out of PreprocessorOptions (2/n) (#87099)
An instance of `PreprocessorOptions` is part of `CompilerInvocation` which is supposed to be a value type. The `FailedModules` member is problematic, since it's essentially a shared state used by multiple `CompilerInstance` objects, and not really a preprocessor option. Let's move it into `CompilerInstance` instead.
1 parent 7daa65a commit ee3a302

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ class CompilerInstance : public ModuleLoader {
133133

134134
std::vector<std::shared_ptr<DependencyCollector>> DependencyCollectors;
135135

136+
/// Records the set of modules
137+
class FailedModulesSet {
138+
llvm::StringSet<> Failed;
139+
140+
public:
141+
bool hasAlreadyFailed(StringRef module) { return Failed.count(module) > 0; }
142+
143+
void addFailed(StringRef module) { Failed.insert(module); }
144+
};
145+
146+
/// The set of modules that failed to build.
147+
///
148+
/// This pointer will be shared among all of the compiler instances created
149+
/// to (re)build modules, so that once a module fails to build anywhere,
150+
/// other instances will see that the module has failed and won't try to
151+
/// build it again.
152+
std::shared_ptr<FailedModulesSet> FailedModules;
153+
136154
/// The set of top-level modules that has already been built on the
137155
/// fly as part of this overall compilation action.
138156
std::map<std::string, std::string, std::less<>> BuiltModules;
@@ -619,6 +637,24 @@ class CompilerInstance : public ModuleLoader {
619637
}
620638

621639
/// @}
640+
/// @name Failed modules set
641+
/// @{
642+
643+
bool hasFailedModulesSet() const { return (bool)FailedModules; }
644+
645+
void createFailedModulesSet() {
646+
FailedModules = std::make_shared<FailedModulesSet>();
647+
}
648+
649+
std::shared_ptr<FailedModulesSet> getFailedModulesSetPtr() const {
650+
return FailedModules;
651+
}
652+
653+
void setFailedModulesSet(std::shared_ptr<FailedModulesSet> FMS) {
654+
FailedModules = FMS;
655+
}
656+
657+
/// }
622658
/// @name Output Files
623659
/// @{
624660

clang/include/clang/Lex/PreprocessorOptions.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,28 +186,6 @@ class PreprocessorOptions {
186186
/// with support for lifetime-qualified pointers.
187187
ObjCXXARCStandardLibraryKind ObjCXXARCStandardLibrary = ARCXX_nolib;
188188

189-
/// Records the set of modules
190-
class FailedModulesSet {
191-
llvm::StringSet<> Failed;
192-
193-
public:
194-
bool hasAlreadyFailed(StringRef module) {
195-
return Failed.count(module) > 0;
196-
}
197-
198-
void addFailed(StringRef module) {
199-
Failed.insert(module);
200-
}
201-
};
202-
203-
/// The set of modules that failed to build.
204-
///
205-
/// This pointer will be shared among all of the compiler instances created
206-
/// to (re)build modules, so that once a module fails to build anywhere,
207-
/// other instances will see that the module has failed and won't try to
208-
/// build it again.
209-
std::shared_ptr<FailedModulesSet> FailedModules;
210-
211189
/// Set up preprocessor for RunAnalysis action.
212190
bool SetUpStaticAnalyzer = false;
213191

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,16 +1206,6 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
12061206
// Note the name of the module we're building.
12071207
Invocation->getLangOpts().CurrentModule = std::string(ModuleName);
12081208

1209-
// Make sure that the failed-module structure has been allocated in
1210-
// the importing instance, and propagate the pointer to the newly-created
1211-
// instance.
1212-
PreprocessorOptions &ImportingPPOpts
1213-
= ImportingInstance.getInvocation().getPreprocessorOpts();
1214-
if (!ImportingPPOpts.FailedModules)
1215-
ImportingPPOpts.FailedModules =
1216-
std::make_shared<PreprocessorOptions::FailedModulesSet>();
1217-
PPOpts.FailedModules = ImportingPPOpts.FailedModules;
1218-
12191209
// If there is a module map file, build the module using the module map.
12201210
// Set up the inputs/outputs so that we build the module from its umbrella
12211211
// header.
@@ -1269,6 +1259,13 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
12691259
SourceMgr.pushModuleBuildStack(ModuleName,
12701260
FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
12711261

1262+
// Make sure that the failed-module structure has been allocated in
1263+
// the importing instance, and propagate the pointer to the newly-created
1264+
// instance.
1265+
if (!ImportingInstance.hasFailedModulesSet())
1266+
ImportingInstance.createFailedModulesSet();
1267+
Instance.setFailedModulesSet(ImportingInstance.getFailedModulesSetPtr());
1268+
12721269
// If we're collecting module dependencies, we need to share a collector
12731270
// between all of the module CompilerInstances. Other than that, we don't
12741271
// want to produce any dependency output from the module build.
@@ -1992,10 +1989,8 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
19921989
return nullptr;
19931990
}
19941991

1995-
// Check whether we have already attempted to build this module (but
1996-
// failed).
1997-
if (getPreprocessorOpts().FailedModules &&
1998-
getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) {
1992+
// Check whether we have already attempted to build this module (but failed).
1993+
if (FailedModules && FailedModules->hasAlreadyFailed(ModuleName)) {
19991994
getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
20001995
<< ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
20011996
return nullptr;
@@ -2006,8 +2001,8 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
20062001
ModuleFilename)) {
20072002
assert(getDiagnostics().hasErrorOccurred() &&
20082003
"undiagnosed error in compileModuleAndReadAST");
2009-
if (getPreprocessorOpts().FailedModules)
2010-
getPreprocessorOpts().FailedModules->addFailed(ModuleName);
2004+
if (FailedModules)
2005+
FailedModules->addFailed(ModuleName);
20112006
return nullptr;
20122007
}
20132008

0 commit comments

Comments
 (0)