[swift-syntax-test] Add batch option to execute action for every swift file in directory

This commit is contained in:
Alex Hoppen
2018-04-17 13:40:51 -07:00
parent 3116517a65
commit 699ca79ac5

View File

@@ -95,6 +95,12 @@ static llvm::cl::opt<std::string>
InputSourceFilename("input-source-filename",
llvm::cl::desc("Path to the input .swift file"));
static llvm::cl::opt<std::string>
InputSourceDirectory("input-source-directory",
llvm::cl::desc("Directory to be scanned recursively and"
"run the selected action on every .swift"
"file"));
static llvm::cl::opt<std::string>
OutputFilename("output-filename",
llvm::cl::desc("Path to the output file"));
@@ -313,52 +319,90 @@ int dumpEOFSourceLoc(const char *MainExecutablePath,
}
}// end of anonymous namespace
int invokeCommand(const char *MainExecutablePath,
const StringRef InputSourceFilename) {
int ExitCode = EXIT_SUCCESS;
switch (options::Action) {
case ActionType::DumpRawTokenSyntax:
ExitCode = doDumpRawTokenSyntax(InputSourceFilename);
break;
case ActionType::FullLexRoundTrip:
ExitCode = doFullLexRoundTrip(InputSourceFilename);
break;
case ActionType::FullParseRoundTrip:
ExitCode = doFullParseRoundTrip(MainExecutablePath, InputSourceFilename);
break;
case ActionType::SerializeRawTree:
ExitCode = doSerializeRawTree(MainExecutablePath, InputSourceFilename);
break;
case ActionType::DeserializeRawTree:
ExitCode = doDeserializeRawTree(MainExecutablePath, InputSourceFilename,
options::OutputFilename);
break;
case ActionType::ParseOnly:
ExitCode = doParseOnly(MainExecutablePath, InputSourceFilename);
break;
case ActionType::ParserGen:
ExitCode = dumpParserGen(MainExecutablePath, InputSourceFilename);
break;
case ActionType::EOFPos:
ExitCode = dumpEOFSourceLoc(MainExecutablePath, InputSourceFilename);
break;
case ActionType::None:
llvm::errs() << "an action is required\n";
llvm::cl::PrintHelpMessage();
ExitCode = EXIT_FAILURE;
break;
}
return ExitCode;
}
int main(int argc, char *argv[]) {
PROGRAM_START(argc, argv);
llvm::cl::ParseCommandLineOptions(argc, argv, "Swift Syntax Test\n");
int ExitCode = EXIT_SUCCESS;
if (options::InputSourceFilename.empty()) {
if (options::InputSourceFilename.empty() &&
options::InputSourceDirectory.empty()) {
llvm::errs() << "input source file is required\n";
ExitCode = EXIT_FAILURE;
}
if (!options::InputSourceFilename.empty() &&
!options::InputSourceDirectory.empty()) {
llvm::errs() << "input-source-filename and input-source-directory cannot "
"be used together\n\n";
ExitCode = EXIT_FAILURE;
}
if (options::Action == ActionType::None) {
llvm::errs() << "an action is required\n";
ExitCode = EXIT_FAILURE;
}
if (ExitCode == EXIT_FAILURE) {
llvm::cl::PrintHelpMessage();
return ExitCode;
}
switch (options::Action) {
case ActionType::DumpRawTokenSyntax:
ExitCode = doDumpRawTokenSyntax(options::InputSourceFilename);
break;
case ActionType::FullLexRoundTrip:
ExitCode = doFullLexRoundTrip(options::InputSourceFilename);
break;
case ActionType::FullParseRoundTrip:
ExitCode = doFullParseRoundTrip(argv[0], options::InputSourceFilename);
break;
case ActionType::SerializeRawTree:
ExitCode = doSerializeRawTree(argv[0], options::InputSourceFilename);
break;
case ActionType::DeserializeRawTree:
ExitCode = doDeserializeRawTree(argv[0], options::InputSourceFilename, options::OutputFilename);
break;
case ActionType::ParseOnly:
ExitCode = doParseOnly(argv[0], options::InputSourceFilename);
break;
case ActionType::ParserGen:
ExitCode = dumpParserGen(argv[0], options::InputSourceFilename);
break;
case ActionType::EOFPos:
ExitCode = dumpEOFSourceLoc(argv[0], options::InputSourceFilename);
break;
case ActionType::None:
llvm::errs() << "an action is required\n";
llvm::cl::PrintHelpMessage();
ExitCode = EXIT_FAILURE;
break;
if (!options::InputSourceFilename.empty()) {
ExitCode = invokeCommand(argv[0], options::InputSourceFilename);
} else {
assert(!options::InputSourceDirectory.empty());
std::error_code errorCode;
llvm::sys::fs::recursive_directory_iterator DI(options::InputSourceDirectory, errorCode);
llvm::sys::fs::recursive_directory_iterator endIterator;
for (; DI != endIterator; DI.increment(errorCode)) {
auto entry = *DI;
auto path = entry.path();
if (!llvm::sys::fs::is_directory(path) &&
StringRef(path).endswith(".swift")) {
ExitCode = invokeCommand(argv[0], path);
}
}
}
return ExitCode;