parse_fuzzer.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 <cstring>
  6. #include "llvm/ADT/StringRef.h"
  7. #include "toolchain/diagnostics/null_diagnostics.h"
  8. #include "toolchain/lex/tokenized_buffer.h"
  9. #include "toolchain/parse/tree.h"
  10. namespace Carbon::Testing {
  11. // NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
  12. extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data,
  13. std::size_t size) {
  14. // Ignore large inputs.
  15. // TODO: See tokenized_buffer_fuzzer.cpp.
  16. if (size > 100000) {
  17. return 0;
  18. }
  19. static constexpr llvm::StringLiteral TestFileName = "test.carbon";
  20. llvm::vfs::InMemoryFileSystem fs;
  21. llvm::StringRef data_ref(reinterpret_cast<const char*>(data), size);
  22. CARBON_CHECK(fs.addFile(
  23. TestFileName, /*ModificationTime=*/0,
  24. llvm::MemoryBuffer::getMemBuffer(data_ref, /*BufferName=*/TestFileName,
  25. /*RequiresNullTerminator=*/false)));
  26. auto source =
  27. SourceBuffer::CreateFromFile(fs, TestFileName, NullDiagnosticConsumer());
  28. // Lex the input.
  29. auto tokens = Lex::TokenizedBuffer::Lex(*source, NullDiagnosticConsumer());
  30. if (tokens.has_errors()) {
  31. return 0;
  32. }
  33. // Now parse it into a tree. Note that parsing will (when asserts are enabled)
  34. // walk the entire tree to verify it so we don't have to do that here.
  35. Parse::Tree::Parse(tokens, NullDiagnosticConsumer(), /*vlog_stream=*/nullptr);
  36. return 0;
  37. }
  38. } // namespace Carbon::Testing