mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Break up ParseFrontendOptions and reorganize module and output files computations.
This commit is contained in:
@@ -36,19 +36,6 @@ bool FrontendInputs::shouldTreatAsLLVM() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
StringRef FrontendInputs::baseNameOfOutput(const llvm::opt::ArgList &Args,
|
||||
StringRef ModuleName) const {
|
||||
StringRef pifn = primaryInputFilenameIfAny();
|
||||
if (!pifn.empty()) {
|
||||
return llvm::sys::path::stem(pifn);
|
||||
}
|
||||
bool UserSpecifiedModuleName = Args.getLastArg(options::OPT_module_name);
|
||||
if (!UserSpecifiedModuleName && haveUniqueInputFilename()) {
|
||||
return llvm::sys::path::stem(getFilenameOfFirstInput());
|
||||
}
|
||||
return ModuleName;
|
||||
}
|
||||
|
||||
bool FrontendInputs::shouldTreatAsSIL() const {
|
||||
if (haveUniqueInputFilename()) {
|
||||
// If we have exactly one input filename, and its extension is "sil",
|
||||
@@ -195,34 +182,6 @@ void FrontendOptions::forAllOutputPaths(
|
||||
}
|
||||
}
|
||||
|
||||
void FrontendOptions::setModuleName(DiagnosticEngine &Diags,
|
||||
const llvm::opt::ArgList &Args) {
|
||||
const Arg *A = Args.getLastArg(options::OPT_module_name);
|
||||
if (A) {
|
||||
ModuleName = A->getValue();
|
||||
} else if (ModuleName.empty()) {
|
||||
// The user did not specify a module name, so determine a default fallback
|
||||
// based on other options.
|
||||
|
||||
// Note: this code path will only be taken when running the frontend
|
||||
// directly; the driver should always pass -module-name when invoking the
|
||||
// frontend.
|
||||
ModuleName = determineFallbackModuleName();
|
||||
}
|
||||
|
||||
if (Lexer::isIdentifier(ModuleName) &&
|
||||
(ModuleName != STDLIB_NAME || ParseStdlib)) {
|
||||
return;
|
||||
}
|
||||
if (!actionHasOutput() || isCompilingExactlyOneSwiftFile()) {
|
||||
ModuleName = "main";
|
||||
return;
|
||||
}
|
||||
auto DID = (ModuleName == STDLIB_NAME) ? diag::error_stdlib_module_name
|
||||
: diag::error_bad_module_name;
|
||||
Diags.diagnose(SourceLoc(), DID, ModuleName, A == nullptr);
|
||||
ModuleName = "__bad__";
|
||||
}
|
||||
|
||||
StringRef FrontendOptions::originalPath() const {
|
||||
if (haveNamedOutputFile())
|
||||
@@ -236,57 +195,276 @@ StringRef FrontendOptions::originalPath() const {
|
||||
return !fn.empty() ? llvm::sys::path::filename(fn) : StringRef(ModuleName);
|
||||
}
|
||||
|
||||
StringRef FrontendOptions::determineFallbackModuleName() const {
|
||||
// Note: this code path will only be taken when running the frontend
|
||||
// directly; the driver should always pass -module-name when invoking the
|
||||
// frontend.
|
||||
if (RequestedAction == FrontendOptions::ActionType::REPL) {
|
||||
// Default to a module named "REPL" if we're in REPL mode.
|
||||
return "REPL";
|
||||
}
|
||||
// In order to pass Driver/options.swift test must leave ModuleName empty
|
||||
if (!Inputs.haveInputFilenames()) {
|
||||
return StringRef();
|
||||
}
|
||||
StringRef OutputFilename = getSingleOutputFilename();
|
||||
bool useOutputFilename = isOutputFilePlainFile();
|
||||
return llvm::sys::path::stem(
|
||||
useOutputFilename ? OutputFilename
|
||||
: StringRef(Inputs.getFilenameOfFirstInput()));
|
||||
}
|
||||
|
||||
/// Try to read an output file list file.
|
||||
static void readOutputFileList(DiagnosticEngine &diags,
|
||||
std::vector<std::string> &outputFiles,
|
||||
const llvm::opt::Arg *filelistPath) {
|
||||
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
|
||||
llvm::MemoryBuffer::getFile(filelistPath->getValue());
|
||||
if (!buffer) {
|
||||
diags.diagnose(SourceLoc(), diag::cannot_open_file,
|
||||
filelistPath->getValue(), buffer.getError().message());
|
||||
}
|
||||
for (StringRef line : make_range(llvm::line_iterator(*buffer.get()), {})) {
|
||||
outputFiles.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
void FrontendOptions::setOutputFileList(swift::DiagnosticEngine &Diags,
|
||||
const llvm::opt::ArgList &Args) {
|
||||
if (const Arg *A = Args.getLastArg(options::OPT_output_filelist)) {
|
||||
readOutputFileList(Diags, OutputFilenames, A);
|
||||
assert(!Args.hasArg(options::OPT_o) &&
|
||||
"don't use -o with -output-filelist");
|
||||
} else {
|
||||
OutputFilenames = Args.getAllArgValues(options::OPT_o);
|
||||
}
|
||||
}
|
||||
|
||||
bool FrontendOptions::isOutputFileDirectory() const {
|
||||
return haveNamedOutputFile() &&
|
||||
llvm::sys::fs::is_directory(getSingleOutputFilename());
|
||||
}
|
||||
|
||||
bool FrontendOptions::isOutputFilePlainFile() const {
|
||||
return haveNamedOutputFile() &&
|
||||
!llvm::sys::fs::is_directory(getSingleOutputFilename());
|
||||
const char *
|
||||
FrontendOptions::suffixForPrincipalOutputFileForAction(ActionType action) {
|
||||
switch (action) {
|
||||
case ActionType::NoneAction:
|
||||
return nullptr;
|
||||
|
||||
case ActionType::Parse:
|
||||
case ActionType::Typecheck:
|
||||
case ActionType::DumpParse:
|
||||
case ActionType::DumpInterfaceHash:
|
||||
case ActionType::DumpAST:
|
||||
case ActionType::EmitSyntax:
|
||||
case ActionType::PrintAST:
|
||||
case ActionType::DumpScopeMaps:
|
||||
case ActionType::DumpTypeRefinementContexts:
|
||||
return nullptr;
|
||||
|
||||
case ActionType::EmitPCH:
|
||||
return PCH_EXTENSION;
|
||||
|
||||
case ActionType::EmitSILGen:
|
||||
case ActionType::EmitSIL:
|
||||
return SIL_EXTENSION;
|
||||
|
||||
case ActionType::EmitSIBGen:
|
||||
case ActionType::EmitSIB:
|
||||
return SIB_EXTENSION;
|
||||
|
||||
case ActionType::MergeModules:
|
||||
case ActionType::EmitModuleOnly:
|
||||
return SERIALIZED_MODULE_EXTENSION;
|
||||
|
||||
case ActionType::Immediate:
|
||||
case ActionType::REPL:
|
||||
// These modes have no frontend-generated output.
|
||||
return nullptr;
|
||||
|
||||
case ActionType::EmitAssembly:
|
||||
return "s";
|
||||
|
||||
case ActionType::EmitIR:
|
||||
return "ll";
|
||||
|
||||
case ActionType::EmitBC:
|
||||
return "bc";
|
||||
|
||||
case ActionType::EmitObject:
|
||||
return "o";
|
||||
|
||||
case ActionType::EmitImportedModules:
|
||||
return "importedmodules";
|
||||
}
|
||||
}
|
||||
|
||||
bool FrontendOptions::hasUnusedDependenciesFilePath() const {
|
||||
return !DependenciesFilePath.empty() &&
|
||||
!canActionEmitDependencies(RequestedAction);
|
||||
}
|
||||
|
||||
bool FrontendOptions::canActionEmitDependencies(ActionType action) {
|
||||
switch (action) {
|
||||
case ActionType::NoneAction:
|
||||
case ActionType::DumpParse:
|
||||
case ActionType::DumpInterfaceHash:
|
||||
case ActionType::DumpAST:
|
||||
case ActionType::EmitSyntax:
|
||||
case ActionType::PrintAST:
|
||||
case ActionType::DumpScopeMaps:
|
||||
case ActionType::DumpTypeRefinementContexts:
|
||||
case ActionType::Immediate:
|
||||
case ActionType::REPL:
|
||||
return false;
|
||||
case ActionType::Parse:
|
||||
case ActionType::Typecheck:
|
||||
case ActionType::MergeModules:
|
||||
case ActionType::EmitModuleOnly:
|
||||
case ActionType::EmitPCH:
|
||||
case ActionType::EmitSILGen:
|
||||
case ActionType::EmitSIL:
|
||||
case ActionType::EmitSIBGen:
|
||||
case ActionType::EmitSIB:
|
||||
case ActionType::EmitIR:
|
||||
case ActionType::EmitBC:
|
||||
case ActionType::EmitAssembly:
|
||||
case ActionType::EmitObject:
|
||||
case ActionType::EmitImportedModules:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool FrontendOptions::hasUnusedObjCHeaderOutputPath() const {
|
||||
return !ObjCHeaderOutputPath.empty() && !canActionEmitHeader(RequestedAction);
|
||||
}
|
||||
|
||||
bool FrontendOptions::canActionEmitHeader(ActionType action) {
|
||||
switch (action) {
|
||||
case ActionType::NoneAction:
|
||||
case ActionType::DumpParse:
|
||||
case ActionType::DumpInterfaceHash:
|
||||
case ActionType::DumpAST:
|
||||
case ActionType::EmitSyntax:
|
||||
case ActionType::PrintAST:
|
||||
case ActionType::EmitPCH:
|
||||
case ActionType::DumpScopeMaps:
|
||||
case ActionType::DumpTypeRefinementContexts:
|
||||
case ActionType::Immediate:
|
||||
case ActionType::REPL:
|
||||
return false;
|
||||
case ActionType::Parse:
|
||||
case ActionType::Typecheck:
|
||||
case ActionType::MergeModules:
|
||||
case ActionType::EmitModuleOnly:
|
||||
case ActionType::EmitSILGen:
|
||||
case ActionType::EmitSIL:
|
||||
case ActionType::EmitSIBGen:
|
||||
case ActionType::EmitSIB:
|
||||
case ActionType::EmitIR:
|
||||
case ActionType::EmitBC:
|
||||
case ActionType::EmitAssembly:
|
||||
case ActionType::EmitObject:
|
||||
case ActionType::EmitImportedModules:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool FrontendOptions::hasUnusedLoadedModuleTracePath() const {
|
||||
return !LoadedModuleTracePath.empty() &&
|
||||
!canActionEmitLoadedModuleTrace(RequestedAction);
|
||||
}
|
||||
|
||||
bool FrontendOptions::canActionEmitLoadedModuleTrace(ActionType action) {
|
||||
switch (action) {
|
||||
case ActionType::NoneAction:
|
||||
case ActionType::Parse:
|
||||
case ActionType::DumpParse:
|
||||
case ActionType::DumpInterfaceHash:
|
||||
case ActionType::DumpAST:
|
||||
case ActionType::EmitSyntax:
|
||||
case ActionType::PrintAST:
|
||||
case ActionType::DumpScopeMaps:
|
||||
case ActionType::DumpTypeRefinementContexts:
|
||||
case ActionType::Immediate:
|
||||
case ActionType::REPL:
|
||||
return false;
|
||||
case ActionType::Typecheck:
|
||||
case ActionType::MergeModules:
|
||||
case ActionType::EmitModuleOnly:
|
||||
case ActionType::EmitPCH:
|
||||
case ActionType::EmitSILGen:
|
||||
case ActionType::EmitSIL:
|
||||
case ActionType::EmitSIBGen:
|
||||
case ActionType::EmitSIB:
|
||||
case ActionType::EmitIR:
|
||||
case ActionType::EmitBC:
|
||||
case ActionType::EmitAssembly:
|
||||
case ActionType::EmitObject:
|
||||
case ActionType::EmitImportedModules:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool FrontendOptions::hasUnusedModuleOutputPath() const {
|
||||
return !ModuleOutputPath.empty() && !canActionEmitModule(RequestedAction);
|
||||
}
|
||||
|
||||
bool FrontendOptions::hasUnusedModuleDocOutputPath() const {
|
||||
return !ModuleDocOutputPath.empty() && !canActionEmitModule(RequestedAction);
|
||||
}
|
||||
|
||||
bool FrontendOptions::canActionEmitModule(ActionType action) {
|
||||
switch (action) {
|
||||
case ActionType::NoneAction:
|
||||
case ActionType::Parse:
|
||||
case ActionType::Typecheck:
|
||||
case ActionType::DumpParse:
|
||||
case ActionType::DumpInterfaceHash:
|
||||
case ActionType::DumpAST:
|
||||
case ActionType::EmitSyntax:
|
||||
case ActionType::PrintAST:
|
||||
case ActionType::EmitPCH:
|
||||
case ActionType::DumpScopeMaps:
|
||||
case ActionType::DumpTypeRefinementContexts:
|
||||
case ActionType::EmitSILGen:
|
||||
case ActionType::Immediate:
|
||||
case ActionType::REPL:
|
||||
return false;
|
||||
case ActionType::MergeModules:
|
||||
case ActionType::EmitModuleOnly:
|
||||
case ActionType::EmitSIL:
|
||||
case ActionType::EmitSIBGen:
|
||||
case ActionType::EmitSIB:
|
||||
case ActionType::EmitIR:
|
||||
case ActionType::EmitBC:
|
||||
case ActionType::EmitAssembly:
|
||||
case ActionType::EmitObject:
|
||||
case ActionType::EmitImportedModules:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool FrontendOptions::canActionEmitModuleDoc(ActionType action) {
|
||||
return canActionEmitModule(action);
|
||||
}
|
||||
|
||||
bool FrontendOptions::doesActionProduceOutput(ActionType action) {
|
||||
switch (action) {
|
||||
case ActionType::NoneAction:
|
||||
case ActionType::EmitPCH:
|
||||
case ActionType::EmitSIBGen:
|
||||
case ActionType::EmitSIB:
|
||||
case ActionType::MergeModules:
|
||||
case ActionType::EmitModuleOnly:
|
||||
case ActionType::EmitBC:
|
||||
case ActionType::EmitObject:
|
||||
case ActionType::Parse:
|
||||
case ActionType::Typecheck:
|
||||
case ActionType::DumpParse:
|
||||
case ActionType::DumpInterfaceHash:
|
||||
case ActionType::DumpAST:
|
||||
case ActionType::EmitSyntax:
|
||||
case ActionType::PrintAST:
|
||||
case ActionType::DumpScopeMaps:
|
||||
case ActionType::DumpTypeRefinementContexts:
|
||||
case ActionType::EmitImportedModules:
|
||||
case ActionType::EmitSILGen:
|
||||
case ActionType::EmitSIL:
|
||||
case ActionType::EmitAssembly:
|
||||
case ActionType::EmitIR:
|
||||
return true;
|
||||
|
||||
case ActionType::Immediate:
|
||||
case ActionType::REPL:
|
||||
// These modes have no frontend-generated output.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FrontendOptions::doesActionProduceTextualOutput(ActionType action) {
|
||||
switch (action) {
|
||||
case ActionType::NoneAction:
|
||||
case ActionType::EmitPCH:
|
||||
case ActionType::EmitSIBGen:
|
||||
case ActionType::EmitSIB:
|
||||
case ActionType::MergeModules:
|
||||
case ActionType::EmitModuleOnly:
|
||||
case ActionType::EmitBC:
|
||||
case ActionType::EmitObject:
|
||||
case ActionType::Immediate:
|
||||
case ActionType::REPL:
|
||||
return false;
|
||||
|
||||
case ActionType::Parse:
|
||||
case ActionType::Typecheck:
|
||||
case ActionType::DumpParse:
|
||||
case ActionType::DumpInterfaceHash:
|
||||
case ActionType::DumpAST:
|
||||
case ActionType::EmitSyntax:
|
||||
case ActionType::PrintAST:
|
||||
case ActionType::DumpScopeMaps:
|
||||
case ActionType::DumpTypeRefinementContexts:
|
||||
case ActionType::EmitImportedModules:
|
||||
case ActionType::EmitSILGen:
|
||||
case ActionType::EmitSIL:
|
||||
case ActionType::EmitAssembly:
|
||||
case ActionType::EmitIR:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user