parse_tree_fuzzer.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include <cstddef>
  5. #include <cstdint>
  6. #include <cstring>
  7. #include "common/check.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. #include "toolchain/diagnostics/null_diagnostics.h"
  11. #include "toolchain/lexer/tokenized_buffer.h"
  12. #include "toolchain/parser/parse_tree.h"
  13. namespace Carbon::Testing {
  14. // NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
  15. extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data,
  16. std::size_t size) {
  17. // Ignore large inputs.
  18. // TODO: See tokenized_buffer_fuzzer.cpp.
  19. if (size > 100000) {
  20. return 0;
  21. }
  22. auto source = SourceBuffer::CreateFromText(
  23. llvm::StringRef(reinterpret_cast<const char*>(data), size));
  24. // Lex the input.
  25. auto tokens = TokenizedBuffer::Lex(*source, NullDiagnosticConsumer());
  26. if (tokens.has_errors()) {
  27. return 0;
  28. }
  29. // Now parse it into a tree. Note that parsing will (when asserts are enabled)
  30. // walk the entire tree to verify it so we don't have to do that here.
  31. ParseTree::Parse(tokens, NullDiagnosticConsumer());
  32. return 0;
  33. }
  34. } // namespace Carbon::Testing