@@ -1307,19 +1307,11 @@ static Language getLanguageFromOptions(const LangOptions &LangOpts) {
1307
1307
return LangOpts.CPlusPlus ? Language::CXX : Language::C;
1308
1308
}
1309
1309
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) {
1320
1313
// 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 ());
1323
1315
1324
1316
PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts ();
1325
1317
@@ -1339,7 +1331,7 @@ createCompilerInstanceForModuleCompileImpl(CompilerInstance &ImportingInstance,
1339
1331
1340
1332
// If the original compiler invocation had -fmodule-name, pass it through.
1341
1333
Invocation->getLangOpts ().ModuleName =
1342
- ImportingInstance. getInvocation ().getLangOpts ().ModuleName ;
1334
+ getInvocation ().getLangOpts ().ModuleName ;
1343
1335
1344
1336
// Note the name of the module we're building.
1345
1337
Invocation->getLangOpts ().CurrentModule = std::string (ModuleName);
@@ -1364,93 +1356,80 @@ createCompilerInstanceForModuleCompileImpl(CompilerInstance &ImportingInstance,
1364
1356
DiagnosticOptions &DiagOpts = Invocation->getDiagnosticOpts ();
1365
1357
1366
1358
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 ()) &&
1370
1361
" Module hash mismatch!" );
1371
1362
1372
1363
// Construct a compiler instance that will be used to actually create the
1373
1364
// module. Since we're sharing an in-memory module cache,
1374
1365
// CompilerInstance::CompilerInstance is responsible for finalizing the
1375
1366
// buffers to prevent use-after-frees.
1376
1367
auto InstancePtr = std::make_unique<CompilerInstance>(
1377
- ImportingInstance.getPCHContainerOperations (),
1378
- &ImportingInstance.getModuleCache ());
1368
+ getPCHContainerOperations (), &getModuleCache ());
1379
1369
auto &Instance = *InstancePtr;
1380
1370
1381
1371
auto &Inv = *Invocation;
1382
1372
Instance.setInvocation (std::move (Invocation));
1383
1373
1384
1374
Instance.createDiagnostics (
1385
- ImportingInstance. getVirtualFileSystem (),
1386
- new ForwardingDiagnosticConsumer (ImportingInstance. getDiagnosticClient ()),
1375
+ getVirtualFileSystem (),
1376
+ new ForwardingDiagnosticConsumer (getDiagnosticClient ()),
1387
1377
/* ShouldOwnClient=*/ true );
1388
1378
1389
1379
if (llvm::is_contained (DiagOpts.SystemHeaderWarningsModules , ModuleName))
1390
1380
Instance.getDiagnostics ().setSuppressSystemWarnings (false );
1391
1381
1392
1382
if (FrontendOpts.ModulesShareFileManager ) {
1393
- Instance.setFileManager (&ImportingInstance. getFileManager ());
1383
+ Instance.setFileManager (&getFileManager ());
1394
1384
} else {
1395
- Instance.createFileManager (&ImportingInstance. getVirtualFileSystem ());
1385
+ Instance.createFileManager (&getVirtualFileSystem ());
1396
1386
}
1397
1387
Instance.createSourceManager (Instance.getFileManager ());
1398
1388
SourceManager &SourceMgr = Instance.getSourceManager ();
1399
1389
1400
1390
// Note that this module is part of the module build stack, so that we
1401
1391
// can detect cycles in the module graph.
1402
- SourceMgr.setModuleBuildStack (
1403
- ImportingInstance.getSourceManager ().getModuleBuildStack ());
1392
+ SourceMgr.setModuleBuildStack (getSourceManager ().getModuleBuildStack ());
1404
1393
SourceMgr.pushModuleBuildStack (ModuleName,
1405
- FullSourceLoc (ImportLoc, ImportingInstance. getSourceManager ()));
1394
+ FullSourceLoc (ImportLoc, getSourceManager ()));
1406
1395
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;
1413
1398
1414
1399
// Pass along the GenModuleActionWrapper callback
1415
- auto WrapGenModuleAction = ImportingInstance. getGenModuleActionWrapper ();
1400
+ auto WrapGenModuleAction = getGenModuleActionWrapper ();
1416
1401
Instance.setGenModuleActionWrapper (WrapGenModuleAction);
1417
1402
1418
1403
// Share an output manager.
1419
- assert (ImportingInstance. hasOutputBackend () &&
1404
+ assert (hasOutputBackend () &&
1420
1405
" Expected an output manager to already be set up" );
1421
- Instance.setOutputBackend (&ImportingInstance. getOutputBackend ());
1406
+ Instance.setOutputBackend (&getOutputBackend ());
1422
1407
1423
1408
// If we're collecting module dependencies, we need to share a collector
1424
1409
// between all of the module CompilerInstances. Other than that, we don't
1425
1410
// want to produce any dependency output from the module build.
1426
- Instance.setModuleDepCollector (ImportingInstance. getModuleDepCollector ());
1411
+ Instance.setModuleDepCollector (getModuleDepCollector ());
1427
1412
Inv.getDependencyOutputOpts () = DependencyOutputOptions ();
1428
1413
1429
1414
return InstancePtr;
1430
1415
}
1431
1416
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) {
1439
1421
llvm::TimeTraceScope TimeScope (" Module Compile" , ModuleName);
1440
1422
1441
1423
// Never compile a module that's already finalized - this would cause the
1442
1424
// 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)
1447
1427
<< ModuleName;
1448
1428
return false ;
1449
1429
}
1450
1430
1451
- ImportingInstance.getDiagnostics ().Report (ImportLoc,
1452
- diag::remark_module_build)
1453
- << ModuleName << ModuleFileName;
1431
+ getDiagnostics ().Report (ImportLoc, diag::remark_module_build)
1432
+ << ModuleName << ModuleFileName;
1454
1433
1455
1434
// Execute the action to actually build the module in-place. Use a separate
1456
1435
// thread so that we get a stack large enough.
@@ -1465,13 +1444,15 @@ static bool compileModule(CompilerInstance &ImportingInstance,
1465
1444
},
1466
1445
DesiredStackSize);
1467
1446
1468
- ImportingInstance.getDiagnostics ().Report (ImportLoc,
1469
- diag::remark_module_build_done)
1470
- << ModuleName;
1447
+ getDiagnostics ().Report (ImportLoc, diag::remark_module_build_done)
1448
+ << ModuleName;
1471
1449
1472
1450
// 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 );
1475
1456
1476
1457
if (Crashed) {
1477
1458
// Clear the ASTConsumer if it hasn't been already, in case it owns streams
@@ -1485,8 +1466,8 @@ static bool compileModule(CompilerInstance &ImportingInstance,
1485
1466
1486
1467
// We've rebuilt a module. If we're allowed to generate or update the global
1487
1468
// 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 );
1490
1471
}
1491
1472
1492
1473
// If \p AllowPCMWithCompilerErrors is set return 'success' even if errors
@@ -1551,8 +1532,8 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
1551
1532
bool IsSystem = isSystem (SLoc.getFile ().getFileCharacteristic ());
1552
1533
1553
1534
// Use the module map where this module resides.
1554
- return createCompilerInstanceForModuleCompileImpl (
1555
- * this , ImportLoc, ModuleName,
1535
+ return cloneForModuleCompileImpl (
1536
+ ImportLoc, ModuleName,
1556
1537
FrontendInputFile (ModuleMapFilePath, IK, IsSystem),
1557
1538
ModMap.getModuleMapFileForUniquing (Module)->getName (), ModuleFileName);
1558
1539
}
@@ -1568,8 +1549,8 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
1568
1549
llvm::raw_string_ostream OS (InferredModuleMapContent);
1569
1550
Module->print (OS);
1570
1551
1571
- auto Instance = createCompilerInstanceForModuleCompileImpl (
1572
- * this , ImportLoc, ModuleName,
1552
+ auto Instance = cloneForModuleCompileImpl (
1553
+ ImportLoc, ModuleName,
1573
1554
FrontendInputFile (FakeModuleMapFile, IK, +Module->IsSystem ),
1574
1555
ModMap.getModuleMapFileForUniquing (Module)->getName (), ModuleFileName);
1575
1556
@@ -1633,9 +1614,9 @@ static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance,
1633
1614
auto Instance = ImportingInstance.cloneForModuleCompile (ModuleNameLoc, Module,
1634
1615
ModuleFileName);
1635
1616
1636
- if (!compileModule (ImportingInstance, ModuleNameLoc,
1637
- Module->getTopLevelModuleName (), ModuleFileName ,
1638
- *Instance)) {
1617
+ if (!ImportingInstance. compileModule (ModuleNameLoc,
1618
+ Module->getTopLevelModuleName (),
1619
+ ModuleFileName, *Instance)) {
1639
1620
ImportingInstance.getDiagnostics ().Report (ModuleNameLoc,
1640
1621
diag::err_module_not_built)
1641
1622
<< Module->Name << SourceRange (ImportLoc, ModuleNameLoc);
@@ -2187,7 +2168,7 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
2187
2168
}
2188
2169
2189
2170
// Check whether we have already attempted to build this module (but failed).
2190
- if (FailedModules && FailedModules-> hasAlreadyFailed (ModuleName)) {
2171
+ if (FailedModules. contains (ModuleName)) {
2191
2172
getDiagnostics ().Report (ModuleNameLoc, diag::err_module_not_built)
2192
2173
<< ModuleName << SourceRange (ImportLoc, ModuleNameLoc);
2193
2174
return nullptr ;
@@ -2198,8 +2179,7 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
2198
2179
ModuleFilename)) {
2199
2180
assert (getDiagnostics ().hasErrorOccurred () &&
2200
2181
" undiagnosed error in compileModuleAndReadAST" );
2201
- if (FailedModules)
2202
- FailedModules->addFailed (ModuleName);
2182
+ FailedModules.insert (ModuleName);
2203
2183
return nullptr ;
2204
2184
}
2205
2185
@@ -2425,8 +2405,8 @@ void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
2425
2405
2426
2406
std::string NullTerminatedSource (Source.str ());
2427
2407
2428
- auto Other = createCompilerInstanceForModuleCompileImpl (
2429
- * this , ImportLoc, ModuleName, Input, StringRef (), ModuleFileName);
2408
+ auto Other = cloneForModuleCompileImpl (ImportLoc, ModuleName, Input,
2409
+ StringRef (), ModuleFileName);
2430
2410
2431
2411
// Create a virtual file containing our desired source.
2432
2412
// FIXME: We shouldn't need to do this.
@@ -2439,8 +2419,7 @@ void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
2439
2419
Other->DeleteBuiltModules = false ;
2440
2420
2441
2421
// 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);
2444
2423
2445
2424
BuiltModules = std::move (Other->BuiltModules );
2446
2425
0 commit comments