Skip to content

Commit c5a6cd6

Browse files
committed
[NFC] Refactor getPrimaryOrMainSourceFile
1 parent 9b89c0c commit c5a6cd6

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

lib/FrontendTool/FrontendTool.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -990,12 +990,12 @@ getFileOutputStream(StringRef OutputFilename, ASTContext &Ctx) {
990990
}
991991

992992
/// Writes the Syntax tree to the given file
993-
static bool emitSyntax(SourceFile *SF, StringRef OutputFilename) {
994-
auto os = getFileOutputStream(OutputFilename, SF->getASTContext());
993+
static bool emitSyntax(SourceFile &SF, StringRef OutputFilename) {
994+
auto os = getFileOutputStream(OutputFilename, SF.getASTContext());
995995
if (!os) return true;
996996

997997
json::Output jsonOut(*os, /*UserInfo=*/{}, /*PrettyPrint=*/false);
998-
auto Root = SF->getSyntaxRoot().getRaw();
998+
auto Root = SF.getSyntaxRoot().getRaw();
999999
jsonOut << *Root;
10001000
*os << "\n";
10011001
return false;
@@ -1323,9 +1323,9 @@ static void verifyGenericSignaturesIfNeeded(const CompilerInvocation &Invocation
13231323
}
13241324

13251325
static bool dumpAndPrintScopeMap(const CompilerInstance &Instance,
1326-
SourceFile *SF) {
1326+
SourceFile &SF) {
13271327
// Not const because may require reexpansion
1328-
ASTScope &scope = SF->getScope();
1328+
ASTScope &scope = SF.getScope();
13291329

13301330
const auto &opts = Instance.getInvocation().getFrontendOptions();
13311331
if (opts.DumpScopeMapLocations.empty()) {
@@ -1342,14 +1342,12 @@ static bool dumpAndPrintScopeMap(const CompilerInstance &Instance,
13421342
return Instance.getASTContext().hadError();
13431343
}
13441344

1345-
static SourceFile *
1345+
static SourceFile &
13461346
getPrimaryOrMainSourceFile(const CompilerInstance &Instance) {
1347-
SourceFile *SF = Instance.getPrimarySourceFile();
1348-
if (!SF) {
1349-
SourceFileKind Kind = Instance.getInvocation().getSourceFileKind();
1350-
SF = &Instance.getMainModule()->getMainSourceFile(Kind);
1347+
if (SourceFile *SF = Instance.getPrimarySourceFile()) {
1348+
return *SF;
13511349
}
1352-
return SF;
1350+
return Instance.getMainModule()->getMainSourceFile();
13531351
}
13541352

13551353
/// Dumps the AST of all available primary source files. If corresponding output
@@ -1366,8 +1364,8 @@ static bool dumpAST(CompilerInstance &Instance) {
13661364
} else {
13671365
// Some invocations don't have primary files. In that case, we default to
13681366
// looking for the main file and dumping it to `stdout`.
1369-
auto *SF = getPrimaryOrMainSourceFile(Instance);
1370-
SF->dump(llvm::outs(), /*parseIfNeeded*/ true);
1367+
auto &SF = getPrimaryOrMainSourceFile(Instance);
1368+
SF.dump(llvm::outs(), /*parseIfNeeded*/ true);
13711369
}
13721370
return Instance.getASTContext().hadError();
13731371
}
@@ -1902,7 +1900,7 @@ static bool performAction(CompilerInstance &Instance,
19021900
case FrontendOptions::ActionType::PrintAST:
19031901
return withSemanticAnalysis(
19041902
Instance, observer, [](CompilerInstance &Instance) {
1905-
getPrimaryOrMainSourceFile(Instance)->print(
1903+
getPrimaryOrMainSourceFile(Instance).print(
19061904
llvm::outs(), PrintOptions::printEverything());
19071905
return Instance.getASTContext().hadError();
19081906
});
@@ -1915,13 +1913,12 @@ static bool performAction(CompilerInstance &Instance,
19151913
case FrontendOptions::ActionType::DumpTypeRefinementContexts:
19161914
return withSemanticAnalysis(
19171915
Instance, observer, [](CompilerInstance &Instance) {
1918-
getPrimaryOrMainSourceFile(Instance)
1919-
->getTypeRefinementContext()
1920-
->dump(llvm::errs(), Instance.getASTContext().SourceMgr);
1916+
getPrimaryOrMainSourceFile(Instance).getTypeRefinementContext()->dump(
1917+
llvm::errs(), Instance.getASTContext().SourceMgr);
19211918
return Instance.getASTContext().hadError();
19221919
});
19231920
case FrontendOptions::ActionType::DumpInterfaceHash:
1924-
getPrimaryOrMainSourceFile(Instance)->dumpInterfaceHash(llvm::errs());
1921+
getPrimaryOrMainSourceFile(Instance).dumpInterfaceHash(llvm::errs());
19251922
return Context.hadError();
19261923
case FrontendOptions::ActionType::EmitSyntax:
19271924
return emitSyntax(getPrimaryOrMainSourceFile(Instance),

0 commit comments

Comments
 (0)