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

@@ -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(