parse_tree_fuzzer.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 "llvm/ADT/StringRef.h"
  8. #include "toolchain/diagnostics/diagnostic_emitter.h"
  9. #include "toolchain/diagnostics/null_diagnostics.h"
  10. #include "toolchain/lexer/tokenized_buffer.h"
  11. #include "toolchain/parser/parse_tree.h"
  12. namespace Carbon::Testing {
  13. // NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
  14. extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data,
  15. std::size_t size) {
  16. // Ignore large inputs.
  17. // TODO: See tokenized_buffer_fuzzer.cpp.
  18. if (size > 100000) {
  19. return 0;
  20. }
  21. auto source = SourceBuffer::CreateFromText(
  22. llvm::StringRef(reinterpret_cast<const char*>(data), size));
  23. // Lex the input.
  24. auto tokens = TokenizedBuffer::Lex(*source, NullDiagnosticConsumer());
  25. if (tokens.has_errors()) {
  26. return 0;
  27. }
  28. // Now parse it into a tree. Note that parsing will (when asserts are enabled)
  29. // walk the entire tree to verify it so we don't have to do that here.
  30. ParseTree::Parse(tokens, NullDiagnosticConsumer(), /*vlog_stream=*/nullptr);
  31. return 0;
  32. }
  33. } // namespace Carbon::Testing