[PATCH] D126633: [pseudo] Use the pseudoCXX library in fuzzer.
Haojian Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon May 30 00:28:18 PDT 2022
hokein created this revision.
hokein added a reviewer: sammccall.
Herald added a subscriber: mgorny.
Herald added a project: All.
hokein requested review of this revision.
Herald added a subscriber: alextsao1999.
Herald added a project: clang-tools-extra.
Get rid of the tedious grammar command-flag handling, and simply the
code.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D126633
Files:
clang-tools-extra/pseudo/fuzzer/CMakeLists.txt
clang-tools-extra/pseudo/fuzzer/Fuzzer.cpp
Index: clang-tools-extra/pseudo/fuzzer/Fuzzer.cpp
===================================================================
--- clang-tools-extra/pseudo/fuzzer/Fuzzer.cpp
+++ clang-tools-extra/pseudo/fuzzer/Fuzzer.cpp
@@ -12,6 +12,7 @@
#include "clang-pseudo/Grammar.h"
#include "clang-pseudo/LRTable.h"
#include "clang-pseudo/Token.h"
+#include "clang-pseudo/cxx/CXX.h"
#include "clang/Basic/LangOptions.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -24,28 +25,10 @@
class Fuzzer {
clang::LangOptions LangOpts = clang::pseudo::genericLangOpts();
- std::unique_ptr<Grammar> G;
- LRTable T;
bool Print;
public:
- Fuzzer(llvm::StringRef GrammarPath, bool Print) : Print(Print) {
- llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> GrammarText =
- llvm::MemoryBuffer::getFile(GrammarPath);
- if (std::error_code EC = GrammarText.getError()) {
- llvm::errs() << "Error: can't read grammar file '" << GrammarPath
- << "': " << EC.message() << "\n";
- std::exit(1);
- }
- std::vector<std::string> Diags;
- G = Grammar::parseBNF(GrammarText->get()->getBuffer(), Diags);
- if (!Diags.empty()) {
- for (const auto &Diag : Diags)
- llvm::errs() << Diag << "\n";
- std::exit(1);
- }
- T = LRTable::buildSLR(*G);
- }
+ Fuzzer(bool Print) : Print(Print) {}
void operator()(llvm::StringRef Code) {
std::string CodeStr = Code.str(); // Must be null-terminated.
@@ -58,11 +41,13 @@
clang::pseudo::ForestArena Arena;
clang::pseudo::GSS GSS;
+ const auto &G = cxx::getGrammar();
+ const LRTable &T = cxx::getLRTable();
auto &Root =
- glrParse(ParseableStream, clang::pseudo::ParseParams{*G, T, Arena, GSS},
- *G->findNonterminal("translation-unit"));
+ glrParse(ParseableStream, clang::pseudo::ParseParams{G, T, Arena, GSS},
+ static_cast<SymbolID>(cxx::Symbol::translation_unit));
if (Print)
- llvm::outs() << Root.dumpRecursive(*G);
+ llvm::outs() << Root.dumpRecursive(G);
}
};
@@ -78,13 +63,9 @@
// -grammar=<file> (required) - path to cxx.bnf
// -print - used for testing the fuzzer
int LLVMFuzzerInitialize(int *Argc, char ***Argv) {
- llvm::StringRef GrammarFile;
bool PrintForest = false;
auto ConsumeArg = [&](llvm::StringRef Arg) -> bool {
- if (Arg.consume_front("-grammar=")) {
- GrammarFile = Arg;
- return true;
- } else if (Arg == "-print") {
+ if (Arg == "-print") {
PrintForest = true;
return true;
}
@@ -92,11 +73,7 @@
};
*Argc = std::remove_if(*Argv + 1, *Argv + *Argc, ConsumeArg) - *Argv;
- if (GrammarFile.empty()) {
- fprintf(stderr, "Fuzzer needs -grammar=/path/to/cxx.bnf\n");
- exit(1);
- }
- clang::pseudo::Fuzz = new clang::pseudo::Fuzzer(GrammarFile, PrintForest);
+ clang::pseudo::Fuzz = new clang::pseudo::Fuzzer(PrintForest);
return 0;
}
Index: clang-tools-extra/pseudo/fuzzer/CMakeLists.txt
===================================================================
--- clang-tools-extra/pseudo/fuzzer/CMakeLists.txt
+++ clang-tools-extra/pseudo/fuzzer/CMakeLists.txt
@@ -11,5 +11,6 @@
target_link_libraries(clang-pseudo-fuzzer
PRIVATE
clangPseudo
+ clangPseudoCXX
clangPseudoGrammar
)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126633.432850.patch
Type: text/x-patch
Size: 3327 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220530/4f2a06ac/attachment.bin>
More information about the cfe-commits
mailing list