/src/llvm-project/clang-tools-extra/pseudo/fuzzer/Fuzzer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- Fuzzer.cpp - Fuzz the pseudoparser --------------------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
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" |
20 | | #include <algorithm> |
21 | | |
22 | | namespace clang { |
23 | | namespace pseudo { |
24 | | namespace { |
25 | | |
26 | | class Fuzzer { |
27 | | clang::LangOptions LangOpts = clang::pseudo::genericLangOpts(); |
28 | | bool Print; |
29 | | |
30 | | public: |
31 | 2 | Fuzzer(bool Print) : Print(Print) {} |
32 | | |
33 | 9.56k | void operator()(llvm::StringRef Code) { |
34 | 9.56k | std::string CodeStr = Code.str(); // Must be null-terminated. |
35 | 9.56k | auto RawStream = lex(CodeStr, LangOpts); |
36 | 9.56k | auto DirectiveStructure = DirectiveTree::parse(RawStream); |
37 | 9.56k | clang::pseudo::chooseConditionalBranches(DirectiveStructure, RawStream); |
38 | | // FIXME: strip preprocessor directives |
39 | 9.56k | auto ParseableStream = |
40 | 9.56k | clang::pseudo::stripComments(cook(RawStream, LangOpts)); |
41 | | |
42 | 9.56k | clang::pseudo::ForestArena Arena; |
43 | 9.56k | clang::pseudo::GSS GSS; |
44 | 9.56k | const Language &Lang = getLanguageFromFlags(); |
45 | 9.56k | auto &Root = |
46 | 9.56k | glrParse(clang::pseudo::ParseParams{ParseableStream, Arena, GSS}, |
47 | 9.56k | *Lang.G.findNonterminal("translation-unit"), Lang); |
48 | 9.56k | if (Print) |
49 | 0 | llvm::outs() << Root.dumpRecursive(Lang.G); |
50 | 9.56k | } |
51 | | }; |
52 | | |
53 | | Fuzzer *Fuzz = nullptr; |
54 | | |
55 | | } // namespace |
56 | | } // namespace pseudo |
57 | | } // namespace clang |
58 | | |
59 | | extern "C" { |
60 | | |
61 | | // Set up the fuzzer from command line flags: |
62 | | // -print - used for testing the fuzzer |
63 | 6 | int LLVMFuzzerInitialize(int *Argc, char ***Argv) { |
64 | 6 | bool PrintForest = false; |
65 | 9 | auto ConsumeArg = [&](llvm::StringRef Arg) -> bool { |
66 | 9 | if (Arg == "-print") { |
67 | 0 | PrintForest = true; |
68 | 0 | return true; |
69 | 0 | } |
70 | 9 | return false; |
71 | 9 | }; |
72 | 6 | *Argc = std::remove_if(*Argv + 1, *Argv + *Argc, ConsumeArg) - *Argv; |
73 | | |
74 | 6 | clang::pseudo::Fuzz = new clang::pseudo::Fuzzer(PrintForest); |
75 | 6 | return 0; |
76 | 6 | } |
77 | | |
78 | 51.2k | int LLVMFuzzerTestOneInput(uint8_t *Data, size_t Size) { |
79 | 51.2k | (*clang::pseudo::Fuzz)(llvm::StringRef(reinterpret_cast<char *>(Data), Size)); |
80 | 51.2k | return 0; |
81 | 51.2k | } |
82 | | } |