Skip to content

Commit 901e8be

Browse files
committed
Frontend: Make getPrimaryOrMainSourceFile() a utility on CompilerInstance.
1 parent 512cf21 commit 901e8be

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,12 @@ class CompilerInstance {
710710
}
711711
}
712712

713+
SourceFile &getPrimaryOrMainSourceFile() const {
714+
if (SourceFile *SF = getPrimarySourceFile())
715+
return *SF;
716+
return getMainModule()->getMainSourceFile();
717+
}
718+
713719
/// Returns true if there was an error during setup.
714720
bool setup(const CompilerInvocation &Invocation, std::string &Error,
715721
ArrayRef<const char *> Args = {});

lib/FrontendTool/FrontendTool.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,6 @@ static bool dumpAndPrintScopeMap(const CompilerInstance &Instance,
458458
return Instance.getASTContext().hadError();
459459
}
460460

461-
static SourceFile &
462-
getPrimaryOrMainSourceFile(const CompilerInstance &Instance) {
463-
if (SourceFile *SF = Instance.getPrimarySourceFile()) {
464-
return *SF;
465-
}
466-
return Instance.getMainModule()->getMainSourceFile();
467-
}
468-
469461
/// Dumps the AST of all available primary source files. If corresponding output
470462
/// files were specified, use them; otherwise, dump the AST to stdout.
471463
static bool dumpAST(CompilerInstance &Instance,
@@ -512,7 +504,7 @@ static bool dumpAST(CompilerInstance &Instance,
512504
} else {
513505
// Some invocations don't have primary files. In that case, we default to
514506
// looking for the main file and dumping it to `stdout`.
515-
auto &SF = getPrimaryOrMainSourceFile(Instance);
507+
auto &SF = Instance.getPrimaryOrMainSourceFile();
516508
dumpAST(&SF, llvm::outs());
517509
}
518510
return Instance.getASTContext().hadError();
@@ -1041,7 +1033,7 @@ static void performEndOfPipelineActions(CompilerInstance &Instance) {
10411033
ctx.getClangModuleLoader()->dumpSwiftLookupTables();
10421034

10431035
if (opts.DumpAvailabilityScopes)
1044-
getPrimaryOrMainSourceFile(Instance).getAvailabilityScope()->dump(
1036+
Instance.getPrimaryOrMainSourceFile().getAvailabilityScope()->dump(
10451037
llvm::errs(), Instance.getASTContext().SourceMgr);
10461038

10471039
// Report mangling stats if there was no error.
@@ -1286,14 +1278,14 @@ static bool performAction(CompilerInstance &Instance,
12861278
case FrontendOptions::ActionType::PrintAST:
12871279
return withSemanticAnalysis(
12881280
Instance, observer, [](CompilerInstance &Instance) {
1289-
getPrimaryOrMainSourceFile(Instance).print(
1281+
Instance.getPrimaryOrMainSourceFile().print(
12901282
llvm::outs(), PrintOptions::printEverything());
12911283
return Instance.getASTContext().hadError();
12921284
});
12931285
case FrontendOptions::ActionType::PrintASTDecl:
12941286
return withSemanticAnalysis(
12951287
Instance, observer, [](CompilerInstance &Instance) {
1296-
getPrimaryOrMainSourceFile(Instance).print(
1288+
Instance.getPrimaryOrMainSourceFile().print(
12971289
llvm::outs(), PrintOptions::printDeclarations());
12981290
return Instance.getASTContext().hadError();
12991291
});
@@ -1302,11 +1294,11 @@ static bool performAction(CompilerInstance &Instance,
13021294
Instance, observer,
13031295
[](CompilerInstance &Instance) {
13041296
return dumpAndPrintScopeMap(Instance,
1305-
getPrimaryOrMainSourceFile(Instance));
1297+
Instance.getPrimaryOrMainSourceFile());
13061298
},
13071299
/*runDespiteErrors=*/true);
13081300
case FrontendOptions::ActionType::DumpInterfaceHash:
1309-
getPrimaryOrMainSourceFile(Instance).dumpInterfaceHash(llvm::errs());
1301+
Instance.getPrimaryOrMainSourceFile().dumpInterfaceHash(llvm::errs());
13101302
return Instance.getASTContext().hadError();
13111303
case FrontendOptions::ActionType::EmitImportedModules:
13121304
return emitImportedModules(Instance.getMainModule(), opts,

0 commit comments

Comments
 (0)