Skip to content

[clang][frontend] Expose CompilerInstance::cloneForModuleCompile() #135405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,14 @@ class CompilerInstance : public ModuleLoader {
bool IsInclusionDirective);

public:
/// Creates a new \c CompilerInstance for compiling a module.
///
/// This takes care of creating appropriate \c FrontendInputFile for
/// public/private frameworks, inferred modules and such.
std::unique_ptr<CompilerInstance>
cloneForModuleCompile(SourceLocation ImportLoc, Module *Module,
StringRef ModuleFileName);

ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
Module::NameVisibilityKind Visibility,
bool IsInclusionDirective) override;
Expand Down
30 changes: 11 additions & 19 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1335,23 +1335,15 @@ static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File,
return FileMgr.getOptionalFileRef(PublicFilename);
}

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

InputKind IK(getLanguageFromOptions(ImportingInstance.getLangOpts()),
InputKind::ModuleMap);
InputKind IK(getLanguageFromOptions(getLangOpts()), InputKind::ModuleMap);

// Get or create the module map that we'll use to build this module.
ModuleMap &ModMap =
ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
SourceManager &SourceMgr = ImportingInstance.getSourceManager();
ModuleMap &ModMap = getPreprocessor().getHeaderSearchInfo().getModuleMap();
SourceManager &SourceMgr = getSourceManager();

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

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

// Use the module map where this module resides.
return createCompilerInstanceForModuleCompileImpl(
ImportingInstance, ImportLoc, ModuleName,
*this, ImportLoc, ModuleName,
FrontendInputFile(ModuleMapFilePath, IK, IsSystem),
ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName);
}
Expand All @@ -1404,7 +1396,7 @@ createCompilerInstanceForModuleCompile(CompilerInstance &ImportingInstance,
Module->print(OS);

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

Expand Down Expand Up @@ -1465,8 +1457,8 @@ static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance,
SourceLocation ModuleNameLoc,
Module *Module,
StringRef ModuleFileName) {
auto Instance = createCompilerInstanceForModuleCompile(
ImportingInstance, ModuleNameLoc, Module, ModuleFileName);
auto Instance = ImportingInstance.cloneForModuleCompile(ModuleNameLoc, Module,
ModuleFileName);

if (!compileModule(ImportingInstance, ModuleNameLoc,
Module->getTopLevelModuleName(), ModuleFileName,
Expand Down