parse_tree_fuzzer.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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/lexer/tokenized_buffer.h"
  9. #include "toolchain/parser/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 = SourceBuffer::CreateFromFile(fs, TestFileName);
  27. // Lex the input.
  28. auto tokens = TokenizedBuffer::Lex(*source, NullDiagnosticConsumer());
  29. if (tokens.has_errors()) {
  30. return 0;
  31. }
  32. // Now parse it into a tree. Note that parsing will (when asserts are enabled)
  33. // walk the entire tree to verify it so we don't have to do that here.
  34. ParseTree::Parse(tokens, NullDiagnosticConsumer(), /*vlog_stream=*/nullptr);
  35. return 0;
  36. }
  37. } // namespace Carbon::Testing