1 //===-- Fuzzer.cpp - Fuzz the pseudoparser --------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang-pseudo/DirectiveTree.h"
10 #include "clang-pseudo/Forest.h"
11 #include "clang-pseudo/GLR.h"
12 #include "clang-pseudo/Token.h"
13 #include "clang-pseudo/cli/CLI.h"
14 #include "clang-pseudo/grammar/Grammar.h"
15 #include "clang-pseudo/grammar/LRTable.h"
16 #include "clang/Basic/LangOptions.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
27 clang::LangOptions LangOpts
= clang::pseudo::genericLangOpts();
31 Fuzzer(bool Print
) : Print(Print
) {}
33 void operator()(llvm::StringRef Code
) {
34 std::string CodeStr
= Code
.str(); // Must be null-terminated.
35 auto RawStream
= lex(CodeStr
, LangOpts
);
36 auto DirectiveStructure
= DirectiveTree::parse(RawStream
);
37 clang::pseudo::chooseConditionalBranches(DirectiveStructure
, RawStream
);
38 // FIXME: strip preprocessor directives
39 auto ParseableStream
=
40 clang::pseudo::stripComments(cook(RawStream
, LangOpts
));
42 clang::pseudo::ForestArena Arena
;
43 clang::pseudo::GSS GSS
;
44 const Language
&Lang
= getLanguageFromFlags();
46 glrParse(clang::pseudo::ParseParams
{ParseableStream
, Arena
, GSS
},
47 *Lang
.G
.findNonterminal("translation-unit"), Lang
);
49 llvm::outs() << Root
.dumpRecursive(Lang
.G
);
53 Fuzzer
*Fuzz
= nullptr;
61 // Set up the fuzzer from command line flags:
62 // -print - used for testing the fuzzer
63 int LLVMFuzzerInitialize(int *Argc
, char ***Argv
) {
64 bool PrintForest
= false;
65 auto ConsumeArg
= [&](llvm::StringRef Arg
) -> bool {
66 if (Arg
== "-print") {
72 *Argc
= std::remove_if(*Argv
+ 1, *Argv
+ *Argc
, ConsumeArg
) - *Argv
;
74 clang::pseudo::Fuzz
= new clang::pseudo::Fuzzer(PrintForest
);
78 int LLVMFuzzerTestOneInput(uint8_t *Data
, size_t Size
) {
79 (*clang::pseudo::Fuzz
)(llvm::StringRef(reinterpret_cast<char *>(Data
), Size
));