Factor common code out of tools into CompilerInvocation::setUpInputForSILTool.

This commit is contained in:
David Ungar
2017-11-14 14:14:23 -08:00
parent 72ad29a5fb
commit a406d3ae3f
6 changed files with 63 additions and 94 deletions

View File

@@ -309,6 +309,17 @@ public:
}
return SourceFile::ImplicitModuleImportKind::Stdlib;
}
/// Performs input setup common to these tools:
/// sil-opt, sil-func-extractor, sil-llvm-gen, and sil-nm.
/// Return value includes the buffer so caller can keep it alive.
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
setUpInputForSILTool(StringRef InputFilename, StringRef ModuleNameArg,
bool alwaysSetModuleToMain,
serialization::ExtendedValidationInfo &extendedInfo);
bool hasSerializedAST() {
return FrontendOpts.InputKind == InputFileKind::IFK_Swift_Library;
}
};
/// A class which manages the state and execution of the compiler.

View File

@@ -1698,3 +1698,39 @@ CompilerInvocation::loadFromSerializedAST(StringRef data) {
extendedInfo.getExtraClangImporterOptions().end());
return info.status;
}
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
CompilerInvocation::setUpInputForSILTool(
StringRef InputFilename, StringRef ModuleNameArg,
bool alwaysSetModuleToMain,
serialization::ExtendedValidationInfo &extendedInfo) {
// Load the input file.
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(InputFilename);
if (!FileBufOrErr) {
return FileBufOrErr;
}
// If it looks like we have an AST, set the source file kind to SIL and the
// name of the module to the file's name.
addInputBuffer(FileBufOrErr.get().get());
auto result = serialization::validateSerializedAST(
FileBufOrErr.get()->getBuffer(), &extendedInfo);
bool HasSerializedAST = result.status == serialization::Status::Valid;
if (HasSerializedAST) {
const StringRef Stem = !ModuleNameArg.empty()
? ModuleNameArg
: llvm::sys::path::stem(InputFilename);
setModuleName(Stem);
setInputKind(InputFileKind::IFK_Swift_Library);
} else {
const StringRef Name = (alwaysSetModuleToMain || ModuleNameArg.empty())
? "main"
: StringRef(ModuleNameArg);
setModuleName(Name);
setInputKind(InputFileKind::IFK_SIL);
}
return FileBufOrErr;
}

View File

@@ -254,34 +254,15 @@ int main(int argc, char **argv) {
Invocation.getLangOptions().EnableAccessControl = false;
Invocation.getLangOptions().EnableObjCAttrRequiresFoundation = false;
// Load the input file.
serialization::ExtendedValidationInfo extendedInfo;
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(InputFilename);
Invocation.setUpInputForSILTool(InputFilename, ModuleName, true,
extendedInfo);
if (!FileBufOrErr) {
fprintf(stderr, "Error! Failed to open file: %s\n", InputFilename.c_str());
exit(-1);
}
// If it looks like we have an AST, set the source file kind to SIL and the
// name of the module to the file's name.
Invocation.addInputBuffer(FileBufOrErr.get().get());
serialization::ExtendedValidationInfo extendedInfo;
auto result = serialization::validateSerializedAST(
FileBufOrErr.get()->getBuffer(), &extendedInfo);
bool HasSerializedAST = result.status == serialization::Status::Valid;
if (HasSerializedAST) {
const StringRef Stem = ModuleName.size()
? StringRef(ModuleName)
: llvm::sys::path::stem(InputFilename);
Invocation.setModuleName(Stem);
Invocation.setInputKind(InputFileKind::IFK_Swift_Library);
} else {
Invocation.setModuleName("main");
Invocation.setInputKind(InputFileKind::IFK_SIL);
}
SILOptions &SILOpts = Invocation.getSILOptions();
SILOpts.AssumeUnqualifiedOwnershipWhenParsing =
AssumeUnqualifiedOwnershipWhenParsing;
@@ -300,7 +281,7 @@ int main(int argc, char **argv) {
// Load the SIL if we have a module. We have to do this after SILParse
// creating the unfortunate double if statement.
if (HasSerializedAST) {
if (Invocation.hasSerializedAST()) {
assert(!CI.hasSILModule() &&
"performSema() should not create a SILModule.");
CI.setSILModule(

View File

@@ -167,35 +167,15 @@ int main(int argc, char **argv) {
Opts.OutputFilenames.push_back(OutputFilename);
Opts.OutputKind = OutputKind;
// Load the input file.
serialization::ExtendedValidationInfo extendedInfo;
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(InputFilename);
Invocation.setUpInputForSILTool(InputFilename, ModuleName, false,
extendedInfo);
if (!FileBufOrErr) {
fprintf(stderr, "Error! Failed to open file: %s\n", InputFilename.c_str());
exit(-1);
}
// If it looks like we have an AST, set the source file kind to SIL and the
// name of the module to the file's name.
Invocation.addInputBuffer(FileBufOrErr.get().get());
serialization::ExtendedValidationInfo extendedInfo;
auto result = serialization::validateSerializedAST(
FileBufOrErr.get()->getBuffer(), &extendedInfo);
bool HasSerializedAST = result.status == serialization::Status::Valid;
if (HasSerializedAST) {
const StringRef Stem = ModuleName.size()
? StringRef(ModuleName)
: llvm::sys::path::stem(InputFilename);
Invocation.setModuleName(Stem);
Invocation.setInputKind(InputFileKind::IFK_Swift_Library);
} else {
const StringRef Name = ModuleName.size() ? StringRef(ModuleName) : "main";
Invocation.setModuleName(Name);
Invocation.setInputKind(InputFileKind::IFK_SIL);
}
CompilerInstance CI;
PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags);
@@ -216,7 +196,7 @@ int main(int argc, char **argv) {
// Load the SIL if we have a module. We have to do this after SILParse
// creating the unfortunate double if statement.
if (HasSerializedAST) {
if (Invocation.hasSerializedAST()) {
assert(!CI.hasSILModule() &&
"performSema() should not create a SILModule.");
CI.setSILModule(SILModule::createEmptyModule(

View File

@@ -168,34 +168,15 @@ int main(int argc, char **argv) {
Invocation.getLangOptions().EnableAccessControl = false;
Invocation.getLangOptions().EnableObjCAttrRequiresFoundation = false;
// Load the input file.
serialization::ExtendedValidationInfo extendedInfo;
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(InputFilename);
Invocation.setUpInputForSILTool(InputFilename, ModuleName, true,
extendedInfo);
if (!FileBufOrErr) {
fprintf(stderr, "Error! Failed to open file: %s\n", InputFilename.c_str());
exit(-1);
}
// If it looks like we have an AST, set the source file kind to SIL and the
// name of the module to the file's name.
Invocation.addInputBuffer(FileBufOrErr.get().get());
serialization::ExtendedValidationInfo extendedInfo;
auto result = serialization::validateSerializedAST(
FileBufOrErr.get()->getBuffer(), &extendedInfo);
bool HasSerializedAST = result.status == serialization::Status::Valid;
if (HasSerializedAST) {
const StringRef Stem = ModuleName.size()
? StringRef(ModuleName)
: llvm::sys::path::stem(InputFilename);
Invocation.setModuleName(Stem);
Invocation.setInputKind(InputFileKind::IFK_Swift_Library);
} else {
Invocation.setModuleName("main");
Invocation.setInputKind(InputFileKind::IFK_SIL);
}
SILOptions &SILOpts = Invocation.getSILOptions();
SILOpts.AssumeUnqualifiedOwnershipWhenParsing =
AssumeUnqualifiedOwnershipWhenParsing;
@@ -214,7 +195,7 @@ int main(int argc, char **argv) {
// Load the SIL if we have a module. We have to do this after SILParse
// creating the unfortunate double if statement.
if (HasSerializedAST) {
if (Invocation.hasSerializedAST()) {
assert(!CI.hasSILModule() &&
"performSema() should not create a SILModule.");
CI.setSILModule(

View File

@@ -342,35 +342,15 @@ int main(int argc, char **argv) {
}
}
// Load the input file.
serialization::ExtendedValidationInfo extendedInfo;
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(InputFilename);
Invocation.setUpInputForSILTool(InputFilename, ModuleName, false,
extendedInfo);
if (!FileBufOrErr) {
fprintf(stderr, "Error! Failed to open file: %s\n", InputFilename.c_str());
exit(-1);
}
// If it looks like we have an AST, set the source file kind to SIL and the
// name of the module to the file's name.
Invocation.addInputBuffer(FileBufOrErr.get().get());
serialization::ExtendedValidationInfo extendedInfo;
auto result = serialization::validateSerializedAST(
FileBufOrErr.get()->getBuffer(), &extendedInfo);
bool HasSerializedAST = result.status == serialization::Status::Valid;
if (HasSerializedAST) {
const StringRef Stem = ModuleName.size() ?
StringRef(ModuleName) :
llvm::sys::path::stem(InputFilename);
Invocation.setModuleName(Stem);
Invocation.setInputKind(InputFileKind::IFK_Swift_Library);
} else {
const StringRef Name = ModuleName.size() ? StringRef(ModuleName) : "main";
Invocation.setModuleName(Name);
Invocation.setInputKind(InputFileKind::IFK_SIL);
}
CompilerInstance CI;
PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags);
@@ -391,7 +371,7 @@ int main(int argc, char **argv) {
// Load the SIL if we have a module. We have to do this after SILParse
// creating the unfortunate double if statement.
if (HasSerializedAST) {
if (Invocation.hasSerializedAST()) {
assert(!CI.hasSILModule() &&
"performSema() should not create a SILModule.");
CI.setSILModule(SILModule::createEmptyModule(