Skip to content

Commit 3a2d9a7

Browse files
authored
[clang][frontend] Expose CompilerInstance::cloneForModuleCompile() (#135405)
This PR exposes `cloneForModuleCompile()` as a public `CompilerInstance` member function. This will be eventually used in the dependency scanner to customize implicit module builds.
1 parent 336b290 commit 3a2d9a7

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,14 @@ class CompilerInstance : public ModuleLoader {
871871
bool IsInclusionDirective);
872872

873873
public:
874+
/// Creates a new \c CompilerInstance for compiling a module.
875+
///
876+
/// This takes care of creating appropriate \c FrontendInputFile for
877+
/// public/private frameworks, inferred modules and such.
878+
std::unique_ptr<CompilerInstance>
879+
cloneForModuleCompile(SourceLocation ImportLoc, Module *Module,
880+
StringRef ModuleFileName);
881+
874882
ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
875883
Module::NameVisibilityKind Visibility,
876884
bool IsInclusionDirective) override;

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,23 +1335,15 @@ static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File,
13351335
return FileMgr.getOptionalFileRef(PublicFilename);
13361336
}
13371337

1338-
/// Creates a \c CompilerInstance for compiling a module.
1339-
///
1340-
/// This takes care of creating appropriate \c FrontendInputFile for
1341-
/// public/private frameworks, inferred modules and such.
1342-
static std::unique_ptr<CompilerInstance>
1343-
createCompilerInstanceForModuleCompile(CompilerInstance &ImportingInstance,
1344-
SourceLocation ImportLoc, Module *Module,
1345-
StringRef ModuleFileName) {
1338+
std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
1339+
SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName) {
13461340
StringRef ModuleName = Module->getTopLevelModuleName();
13471341

1348-
InputKind IK(getLanguageFromOptions(ImportingInstance.getLangOpts()),
1349-
InputKind::ModuleMap);
1342+
InputKind IK(getLanguageFromOptions(getLangOpts()), InputKind::ModuleMap);
13501343

13511344
// Get or create the module map that we'll use to build this module.
1352-
ModuleMap &ModMap =
1353-
ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1354-
SourceManager &SourceMgr = ImportingInstance.getSourceManager();
1345+
ModuleMap &ModMap = getPreprocessor().getHeaderSearchInfo().getModuleMap();
1346+
SourceManager &SourceMgr = getSourceManager();
13551347

13561348
if (FileID ModuleMapFID = ModMap.getContainingModuleMapFileID(Module);
13571349
ModuleMapFID.isValid()) {
@@ -1372,8 +1364,8 @@ createCompilerInstanceForModuleCompile(CompilerInstance &ImportingInstance,
13721364
// Canonicalize compilation to start with the public module map. This is
13731365
// vital for submodules declarations in the private module maps to be
13741366
// correctly parsed when depending on a top level module in the public one.
1375-
if (OptionalFileEntryRef PublicMMFile = getPublicModuleMap(
1376-
*ModuleMapFile, ImportingInstance.getFileManager()))
1367+
if (OptionalFileEntryRef PublicMMFile =
1368+
getPublicModuleMap(*ModuleMapFile, getFileManager()))
13771369
ModuleMapFile = PublicMMFile;
13781370

13791371
StringRef ModuleMapFilePath = ModuleMapFile->getNameAsRequested();
@@ -1387,7 +1379,7 @@ createCompilerInstanceForModuleCompile(CompilerInstance &ImportingInstance,
13871379

13881380
// Use the module map where this module resides.
13891381
return createCompilerInstanceForModuleCompileImpl(
1390-
ImportingInstance, ImportLoc, ModuleName,
1382+
*this, ImportLoc, ModuleName,
13911383
FrontendInputFile(ModuleMapFilePath, IK, IsSystem),
13921384
ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName);
13931385
}
@@ -1404,7 +1396,7 @@ createCompilerInstanceForModuleCompile(CompilerInstance &ImportingInstance,
14041396
Module->print(OS);
14051397

14061398
auto Instance = createCompilerInstanceForModuleCompileImpl(
1407-
ImportingInstance, ImportLoc, ModuleName,
1399+
*this, ImportLoc, ModuleName,
14081400
FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
14091401
ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName);
14101402

@@ -1465,8 +1457,8 @@ static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance,
14651457
SourceLocation ModuleNameLoc,
14661458
Module *Module,
14671459
StringRef ModuleFileName) {
1468-
auto Instance = createCompilerInstanceForModuleCompile(
1469-
ImportingInstance, ModuleNameLoc, Module, ModuleFileName);
1460+
auto Instance = ImportingInstance.cloneForModuleCompile(ModuleNameLoc, Module,
1461+
ModuleFileName);
14701462

14711463
if (!compileModule(ImportingInstance, ModuleNameLoc,
14721464
Module->getTopLevelModuleName(), ModuleFileName,

0 commit comments

Comments
 (0)