parse_fuzzer.cpp 1.8 KB

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