Skip to content

Commit 91699b3

Browse files
author
git apple-llvm automerger
committed
Merge commit '1cf9f764ac41' from llvm.org/main into next
2 parents ce682d3 + 7a3848e commit 91699b3

File tree

2 files changed

+66
-100
lines changed

2 files changed

+66
-100
lines changed

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,13 @@ class CompilerInstance : public ModuleLoader {
153153

154154
std::vector<std::shared_ptr<DependencyCollector>> DependencyCollectors;
155155

156-
/// Records the set of modules
157-
class FailedModulesSet {
158-
llvm::StringSet<> Failed;
159-
160-
public:
161-
bool hasAlreadyFailed(StringRef module) { return Failed.count(module) > 0; }
162-
163-
void addFailed(StringRef module) { Failed.insert(module); }
164-
};
165-
166156
/// The set of modules that failed to build.
167157
///
168-
/// This pointer will be shared among all of the compiler instances created
158+
/// This value will be passed among all of the compiler instances created
169159
/// to (re)build modules, so that once a module fails to build anywhere,
170160
/// other instances will see that the module has failed and won't try to
171161
/// build it again.
172-
std::shared_ptr<FailedModulesSet> FailedModules;
162+
llvm::StringSet<> FailedModules;
173163

174164
/// The set of top-level modules that has already been built on the
175165
/// fly as part of this overall compilation action.
@@ -687,24 +677,6 @@ class CompilerInstance : public ModuleLoader {
687677
return *FrontendTimer;
688678
}
689679

690-
/// @}
691-
/// @name Failed modules set
692-
/// @{
693-
694-
bool hasFailedModulesSet() const { return (bool)FailedModules; }
695-
696-
void createFailedModulesSet() {
697-
FailedModules = std::make_shared<FailedModulesSet>();
698-
}
699-
700-
std::shared_ptr<FailedModulesSet> getFailedModulesSetPtr() const {
701-
return FailedModules;
702-
}
703-
704-
void setFailedModulesSet(std::shared_ptr<FailedModulesSet> FMS) {
705-
FailedModules = FMS;
706-
}
707-
708680
/// }
709681
/// @name Output Files
710682
/// @{
@@ -927,6 +899,13 @@ class CompilerInstance : public ModuleLoader {
927899
SourceLocation ModuleNameLoc,
928900
bool IsInclusionDirective);
929901

902+
/// Creates a \c CompilerInstance for compiling a module.
903+
///
904+
/// This expects a properly initialized \c FrontendInputFile.
905+
std::unique_ptr<CompilerInstance> cloneForModuleCompileImpl(
906+
SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
907+
StringRef OriginalModuleMapFile, StringRef ModuleFileName);
908+
930909
public:
931910
/// Creates a new \c CompilerInstance for compiling a module.
932911
///
@@ -936,6 +915,14 @@ class CompilerInstance : public ModuleLoader {
936915
cloneForModuleCompile(SourceLocation ImportLoc, Module *Module,
937916
StringRef ModuleFileName);
938917

918+
/// Compile a module file for the given module, using the options
919+
/// provided by the importing compiler instance. Returns true if the module
920+
/// was built without errors.
921+
// FIXME: This should be private, but it's called from static non-member
922+
// functions in the implementation file.
923+
bool compileModule(SourceLocation ImportLoc, StringRef ModuleName,
924+
StringRef ModuleFileName, CompilerInstance &Instance);
925+
939926
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
940927
Module::NameVisibilityKind Visibility,
941928
bool IsInclusionDirective) override;

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 49 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,19 +1307,11 @@ static Language getLanguageFromOptions(const LangOptions &LangOpts) {
13071307
return LangOpts.CPlusPlus ? Language::CXX : Language::C;
13081308
}
13091309

1310-
/// Creates a \c CompilerInstance for compiling a module.
1311-
///
1312-
/// This expects a properly initialized \c FrontendInputFile.
1313-
static std::unique_ptr<CompilerInstance>
1314-
createCompilerInstanceForModuleCompileImpl(CompilerInstance &ImportingInstance,
1315-
SourceLocation ImportLoc,
1316-
StringRef ModuleName,
1317-
FrontendInputFile Input,
1318-
StringRef OriginalModuleMapFile,
1319-
StringRef ModuleFileName) {
1310+
std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
1311+
SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
1312+
StringRef OriginalModuleMapFile, StringRef ModuleFileName) {
13201313
// Construct a compiler invocation for creating this module.
1321-
auto Invocation =
1322-
std::make_shared<CompilerInvocation>(ImportingInstance.getInvocation());
1314+
auto Invocation = std::make_shared<CompilerInvocation>(getInvocation());
13231315

13241316
PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
13251317

@@ -1339,7 +1331,7 @@ createCompilerInstanceForModuleCompileImpl(CompilerInstance &ImportingInstance,
13391331

13401332
// If the original compiler invocation had -fmodule-name, pass it through.
13411333
Invocation->getLangOpts().ModuleName =
1342-
ImportingInstance.getInvocation().getLangOpts().ModuleName;
1334+
getInvocation().getLangOpts().ModuleName;
13431335

13441336
// Note the name of the module we're building.
13451337
Invocation->getLangOpts().CurrentModule = std::string(ModuleName);
@@ -1364,93 +1356,80 @@ createCompilerInstanceForModuleCompileImpl(CompilerInstance &ImportingInstance,
13641356
DiagnosticOptions &DiagOpts = Invocation->getDiagnosticOpts();
13651357

13661358
DiagOpts.VerifyDiagnostics = 0;
1367-
assert(ImportingInstance.getInvocation().getModuleHash(
1368-
ImportingInstance.getDiagnostics()) ==
1369-
Invocation->getModuleHash(ImportingInstance.getDiagnostics()) &&
1359+
assert(getInvocation().getModuleHash(getDiagnostics()) ==
1360+
Invocation->getModuleHash(getDiagnostics()) &&
13701361
"Module hash mismatch!");
13711362

13721363
// Construct a compiler instance that will be used to actually create the
13731364
// module. Since we're sharing an in-memory module cache,
13741365
// CompilerInstance::CompilerInstance is responsible for finalizing the
13751366
// buffers to prevent use-after-frees.
13761367
auto InstancePtr = std::make_unique<CompilerInstance>(
1377-
ImportingInstance.getPCHContainerOperations(),
1378-
&ImportingInstance.getModuleCache());
1368+
getPCHContainerOperations(), &getModuleCache());
13791369
auto &Instance = *InstancePtr;
13801370

13811371
auto &Inv = *Invocation;
13821372
Instance.setInvocation(std::move(Invocation));
13831373

13841374
Instance.createDiagnostics(
1385-
ImportingInstance.getVirtualFileSystem(),
1386-
new ForwardingDiagnosticConsumer(ImportingInstance.getDiagnosticClient()),
1375+
getVirtualFileSystem(),
1376+
new ForwardingDiagnosticConsumer(getDiagnosticClient()),
13871377
/*ShouldOwnClient=*/true);
13881378

13891379
if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
13901380
Instance.getDiagnostics().setSuppressSystemWarnings(false);
13911381

13921382
if (FrontendOpts.ModulesShareFileManager) {
1393-
Instance.setFileManager(&ImportingInstance.getFileManager());
1383+
Instance.setFileManager(&getFileManager());
13941384
} else {
1395-
Instance.createFileManager(&ImportingInstance.getVirtualFileSystem());
1385+
Instance.createFileManager(&getVirtualFileSystem());
13961386
}
13971387
Instance.createSourceManager(Instance.getFileManager());
13981388
SourceManager &SourceMgr = Instance.getSourceManager();
13991389

14001390
// Note that this module is part of the module build stack, so that we
14011391
// can detect cycles in the module graph.
1402-
SourceMgr.setModuleBuildStack(
1403-
ImportingInstance.getSourceManager().getModuleBuildStack());
1392+
SourceMgr.setModuleBuildStack(getSourceManager().getModuleBuildStack());
14041393
SourceMgr.pushModuleBuildStack(ModuleName,
1405-
FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
1394+
FullSourceLoc(ImportLoc, getSourceManager()));
14061395

1407-
// Make sure that the failed-module structure has been allocated in
1408-
// the importing instance, and propagate the pointer to the newly-created
1409-
// instance.
1410-
if (!ImportingInstance.hasFailedModulesSet())
1411-
ImportingInstance.createFailedModulesSet();
1412-
Instance.setFailedModulesSet(ImportingInstance.getFailedModulesSetPtr());
1396+
// Make a copy for the new instance.
1397+
Instance.FailedModules = FailedModules;
14131398

14141399
// Pass along the GenModuleActionWrapper callback
1415-
auto WrapGenModuleAction = ImportingInstance.getGenModuleActionWrapper();
1400+
auto WrapGenModuleAction = getGenModuleActionWrapper();
14161401
Instance.setGenModuleActionWrapper(WrapGenModuleAction);
14171402

14181403
// Share an output manager.
1419-
assert(ImportingInstance.hasOutputBackend() &&
1404+
assert(hasOutputBackend() &&
14201405
"Expected an output manager to already be set up");
1421-
Instance.setOutputBackend(&ImportingInstance.getOutputBackend());
1406+
Instance.setOutputBackend(&getOutputBackend());
14221407

14231408
// If we're collecting module dependencies, we need to share a collector
14241409
// between all of the module CompilerInstances. Other than that, we don't
14251410
// want to produce any dependency output from the module build.
1426-
Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector());
1411+
Instance.setModuleDepCollector(getModuleDepCollector());
14271412
Inv.getDependencyOutputOpts() = DependencyOutputOptions();
14281413

14291414
return InstancePtr;
14301415
}
14311416

1432-
/// Compile a module file for the given module, using the options
1433-
/// provided by the importing compiler instance. Returns true if the module
1434-
/// was built without errors.
1435-
static bool compileModule(CompilerInstance &ImportingInstance,
1436-
SourceLocation ImportLoc, StringRef ModuleName,
1437-
StringRef ModuleFileName,
1438-
CompilerInstance &Instance) {
1417+
bool CompilerInstance::compileModule(SourceLocation ImportLoc,
1418+
StringRef ModuleName,
1419+
StringRef ModuleFileName,
1420+
CompilerInstance &Instance) {
14391421
llvm::TimeTraceScope TimeScope("Module Compile", ModuleName);
14401422

14411423
// Never compile a module that's already finalized - this would cause the
14421424
// existing module to be freed, causing crashes if it is later referenced
1443-
if (ImportingInstance.getModuleCache().getInMemoryModuleCache().isPCMFinal(
1444-
ModuleFileName)) {
1445-
ImportingInstance.getDiagnostics().Report(
1446-
ImportLoc, diag::err_module_rebuild_finalized)
1425+
if (getModuleCache().getInMemoryModuleCache().isPCMFinal(ModuleFileName)) {
1426+
getDiagnostics().Report(ImportLoc, diag::err_module_rebuild_finalized)
14471427
<< ModuleName;
14481428
return false;
14491429
}
14501430

1451-
ImportingInstance.getDiagnostics().Report(ImportLoc,
1452-
diag::remark_module_build)
1453-
<< ModuleName << ModuleFileName;
1431+
getDiagnostics().Report(ImportLoc, diag::remark_module_build)
1432+
<< ModuleName << ModuleFileName;
14541433

14551434
// Execute the action to actually build the module in-place. Use a separate
14561435
// thread so that we get a stack large enough.
@@ -1465,13 +1444,15 @@ static bool compileModule(CompilerInstance &ImportingInstance,
14651444
},
14661445
DesiredStackSize);
14671446

1468-
ImportingInstance.getDiagnostics().Report(ImportLoc,
1469-
diag::remark_module_build_done)
1470-
<< ModuleName;
1447+
getDiagnostics().Report(ImportLoc, diag::remark_module_build_done)
1448+
<< ModuleName;
14711449

14721450
// Propagate the statistics to the parent FileManager.
1473-
if (!ImportingInstance.getFrontendOpts().ModulesShareFileManager)
1474-
ImportingInstance.getFileManager().AddStats(Instance.getFileManager());
1451+
if (!getFrontendOpts().ModulesShareFileManager)
1452+
getFileManager().AddStats(Instance.getFileManager());
1453+
1454+
// Propagate the failed modules to the parent instance.
1455+
FailedModules = std::move(Instance.FailedModules);
14751456

14761457
if (Crashed) {
14771458
// Clear the ASTConsumer if it hasn't been already, in case it owns streams
@@ -1485,8 +1466,8 @@ static bool compileModule(CompilerInstance &ImportingInstance,
14851466

14861467
// We've rebuilt a module. If we're allowed to generate or update the global
14871468
// module index, record that fact in the importing compiler instance.
1488-
if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
1489-
ImportingInstance.setBuildGlobalModuleIndex(true);
1469+
if (getFrontendOpts().GenerateGlobalModuleIndex) {
1470+
setBuildGlobalModuleIndex(true);
14901471
}
14911472

14921473
// If \p AllowPCMWithCompilerErrors is set return 'success' even if errors
@@ -1551,8 +1532,8 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
15511532
bool IsSystem = isSystem(SLoc.getFile().getFileCharacteristic());
15521533

15531534
// Use the module map where this module resides.
1554-
return createCompilerInstanceForModuleCompileImpl(
1555-
*this, ImportLoc, ModuleName,
1535+
return cloneForModuleCompileImpl(
1536+
ImportLoc, ModuleName,
15561537
FrontendInputFile(ModuleMapFilePath, IK, IsSystem),
15571538
ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName);
15581539
}
@@ -1568,8 +1549,8 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
15681549
llvm::raw_string_ostream OS(InferredModuleMapContent);
15691550
Module->print(OS);
15701551

1571-
auto Instance = createCompilerInstanceForModuleCompileImpl(
1572-
*this, ImportLoc, ModuleName,
1552+
auto Instance = cloneForModuleCompileImpl(
1553+
ImportLoc, ModuleName,
15731554
FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
15741555
ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName);
15751556

@@ -1633,9 +1614,9 @@ static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance,
16331614
auto Instance = ImportingInstance.cloneForModuleCompile(ModuleNameLoc, Module,
16341615
ModuleFileName);
16351616

1636-
if (!compileModule(ImportingInstance, ModuleNameLoc,
1637-
Module->getTopLevelModuleName(), ModuleFileName,
1638-
*Instance)) {
1617+
if (!ImportingInstance.compileModule(ModuleNameLoc,
1618+
Module->getTopLevelModuleName(),
1619+
ModuleFileName, *Instance)) {
16391620
ImportingInstance.getDiagnostics().Report(ModuleNameLoc,
16401621
diag::err_module_not_built)
16411622
<< Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
@@ -2187,7 +2168,7 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
21872168
}
21882169

21892170
// Check whether we have already attempted to build this module (but failed).
2190-
if (FailedModules && FailedModules->hasAlreadyFailed(ModuleName)) {
2171+
if (FailedModules.contains(ModuleName)) {
21912172
getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
21922173
<< ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
21932174
return nullptr;
@@ -2198,8 +2179,7 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
21982179
ModuleFilename)) {
21992180
assert(getDiagnostics().hasErrorOccurred() &&
22002181
"undiagnosed error in compileModuleAndReadAST");
2201-
if (FailedModules)
2202-
FailedModules->addFailed(ModuleName);
2182+
FailedModules.insert(ModuleName);
22032183
return nullptr;
22042184
}
22052185

@@ -2425,8 +2405,8 @@ void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
24252405

24262406
std::string NullTerminatedSource(Source.str());
24272407

2428-
auto Other = createCompilerInstanceForModuleCompileImpl(
2429-
*this, ImportLoc, ModuleName, Input, StringRef(), ModuleFileName);
2408+
auto Other = cloneForModuleCompileImpl(ImportLoc, ModuleName, Input,
2409+
StringRef(), ModuleFileName);
24302410

24312411
// Create a virtual file containing our desired source.
24322412
// FIXME: We shouldn't need to do this.
@@ -2439,8 +2419,7 @@ void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
24392419
Other->DeleteBuiltModules = false;
24402420

24412421
// Build the module, inheriting any modules that we've built locally.
2442-
bool Success =
2443-
compileModule(*this, ImportLoc, ModuleName, ModuleFileName, *Other);
2422+
bool Success = compileModule(ImportLoc, ModuleName, ModuleFileName, *Other);
24442423

24452424
BuiltModules = std::move(Other->BuiltModules);
24462425

0 commit comments

Comments
 (0)