forked from OSchip/llvm-project
[pseudo-gen] Add -o flag, make --grammar required
Virtually all LLVM tools accept a `-o` flag, so add one. This will make it possible to possibly add a --write-if-changed flag later. It also makes it so that the file isn't partially written if the tool oesn't run successfully. Marking --grammar as `Required` allows removing some manual verification code for it. Differential Revision: https://reviews.llvm.org/D126373
This commit is contained in:
parent
b3c856d10c
commit
788463e72a
|
@ -15,13 +15,17 @@
|
||||||
#include "clang-pseudo/Grammar.h"
|
#include "clang-pseudo/Grammar.h"
|
||||||
#include "llvm/ADT/StringExtras.h"
|
#include "llvm/ADT/StringExtras.h"
|
||||||
#include "llvm/Support/CommandLine.h"
|
#include "llvm/Support/CommandLine.h"
|
||||||
|
#include "llvm/Support/FileSystem.h"
|
||||||
#include "llvm/Support/FormatVariadic.h"
|
#include "llvm/Support/FormatVariadic.h"
|
||||||
#include "llvm/Support/MemoryBuffer.h"
|
#include "llvm/Support/MemoryBuffer.h"
|
||||||
|
#include "llvm/Support/ToolOutputFile.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
using llvm::cl::desc;
|
using llvm::cl::desc;
|
||||||
using llvm::cl::init;
|
using llvm::cl::init;
|
||||||
using llvm::cl::opt;
|
using llvm::cl::opt;
|
||||||
|
using llvm::cl::Required;
|
||||||
|
using llvm::cl::value_desc;
|
||||||
using llvm::cl::values;
|
using llvm::cl::values;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -31,13 +35,17 @@ enum EmitType {
|
||||||
};
|
};
|
||||||
|
|
||||||
opt<std::string> Grammar("grammar", desc("Parse a BNF grammar file."),
|
opt<std::string> Grammar("grammar", desc("Parse a BNF grammar file."),
|
||||||
init(""));
|
Required);
|
||||||
opt<EmitType>
|
opt<EmitType>
|
||||||
Emit(desc("which information to emit:"),
|
Emit(desc("which information to emit:"),
|
||||||
values(clEnumValN(EmitSymbolList, "emit-symbol-list",
|
values(clEnumValN(EmitSymbolList, "emit-symbol-list",
|
||||||
"Print nonterminal symbols (default)"),
|
"Print nonterminal symbols (default)"),
|
||||||
clEnumValN(EmitGrammarContent, "emit-grammar-content",
|
clEnumValN(EmitGrammarContent, "emit-grammar-content",
|
||||||
"Print the BNF grammar content as a string")));
|
"Print the BNF grammar content as a string")));
|
||||||
|
|
||||||
|
opt<std::string> OutputFilename("o", init("-"), desc("Output"),
|
||||||
|
value_desc("file"));
|
||||||
|
|
||||||
std::string readOrDie(llvm::StringRef Path) {
|
std::string readOrDie(llvm::StringRef Path) {
|
||||||
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
|
||||||
llvm::MemoryBuffer::getFile(Path);
|
llvm::MemoryBuffer::getFile(Path);
|
||||||
|
@ -52,10 +60,6 @@ std::string readOrDie(llvm::StringRef Path) {
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
llvm::cl::ParseCommandLineOptions(argc, argv, "");
|
llvm::cl::ParseCommandLineOptions(argc, argv, "");
|
||||||
if (!Grammar.getNumOccurrences()) {
|
|
||||||
llvm::errs() << "Grammar file must be provided!\n";
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string GrammarText = readOrDie(Grammar);
|
std::string GrammarText = readOrDie(Grammar);
|
||||||
std::vector<std::string> Diags;
|
std::vector<std::string> Diags;
|
||||||
|
@ -65,25 +69,34 @@ int main(int argc, char *argv[]) {
|
||||||
llvm::errs() << llvm::join(Diags, "\n");
|
llvm::errs() << llvm::join(Diags, "\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
switch (Emit) {
|
|
||||||
|
|
||||||
|
std::error_code EC;
|
||||||
|
llvm::ToolOutputFile Out{OutputFilename, EC, llvm::sys::fs::OF_None};
|
||||||
|
if (EC) {
|
||||||
|
llvm::errs() << EC.message() << '\n';
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (Emit) {
|
||||||
case EmitSymbolList:
|
case EmitSymbolList:
|
||||||
for (clang::pseudo::SymbolID ID = 0; ID < G->table().Nonterminals.size();
|
for (clang::pseudo::SymbolID ID = 0; ID < G->table().Nonterminals.size();
|
||||||
++ID) {
|
++ID) {
|
||||||
std::string Name = G->symbolName(ID).str();
|
std::string Name = G->symbolName(ID).str();
|
||||||
// translation-unit -> translation_unit
|
// translation-unit -> translation_unit
|
||||||
std::replace(Name.begin(), Name.end(), '-', '_');
|
std::replace(Name.begin(), Name.end(), '-', '_');
|
||||||
llvm::outs() << (llvm::formatv("NONTERMINAL({0}, {1})\n", Name, ID));
|
Out.os() << llvm::formatv("NONTERMINAL({0}, {1})\n", Name, ID);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case EmitGrammarContent:
|
case EmitGrammarContent:
|
||||||
for (llvm::StringRef Line : llvm::split(GrammarText, '\n')) {
|
for (llvm::StringRef Line : llvm::split(GrammarText, '\n')) {
|
||||||
llvm::outs() << '"';
|
Out.os() << '"';
|
||||||
llvm::outs().write_escaped((Line + "\n").str());
|
Out.os().write_escaped((Line + "\n").str());
|
||||||
llvm::outs() << "\"\n";
|
Out.os() << "\"\n";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Out.keep();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ add_custom_command(OUTPUT ${cxx_symbols_inc}
|
||||||
COMMAND "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/pseudo-gen"
|
COMMAND "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/pseudo-gen"
|
||||||
--grammar ${cxx_bnf}
|
--grammar ${cxx_bnf}
|
||||||
--emit-symbol-list
|
--emit-symbol-list
|
||||||
> ${cxx_symbols_inc}
|
-o ${cxx_symbols_inc}
|
||||||
COMMENT "Generating nonterminal symbol file for cxx grammar..."
|
COMMENT "Generating nonterminal symbol file for cxx grammar..."
|
||||||
DEPENDS pseudo-gen
|
DEPENDS pseudo-gen
|
||||||
VERBATIM)
|
VERBATIM)
|
||||||
|
@ -17,7 +17,7 @@ add_custom_command(OUTPUT ${cxx_bnf_inc}
|
||||||
COMMAND "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/pseudo-gen"
|
COMMAND "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/pseudo-gen"
|
||||||
--grammar ${cxx_bnf}
|
--grammar ${cxx_bnf}
|
||||||
--emit-grammar-content
|
--emit-grammar-content
|
||||||
> ${cxx_bnf_inc}
|
-o ${cxx_bnf_inc}
|
||||||
COMMENT "Generating bnf string file for cxx grammar..."
|
COMMENT "Generating bnf string file for cxx grammar..."
|
||||||
DEPENDS pseudo-gen
|
DEPENDS pseudo-gen
|
||||||
VERBATIM)
|
VERBATIM)
|
||||||
|
|
Loading…
Reference in New Issue