string_literal_fuzzer.cpp 1.3 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 <cstring>
  5. #include "common/check.h"
  6. #include "llvm/ADT/StringRef.h"
  7. #include "toolchain/diagnostics/null_diagnostics.h"
  8. #include "toolchain/lex/string_literal.h"
  9. namespace Carbon::Testing {
  10. // NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
  11. extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data,
  12. std::size_t size) {
  13. auto token = Lex::StringLiteral::Lex(
  14. llvm::StringRef(reinterpret_cast<const char*>(data), size));
  15. if (!token) {
  16. // Lexically not a string literal.
  17. return 0;
  18. }
  19. if (!token->is_terminated()) {
  20. // Found errors while parsing.
  21. return 0;
  22. }
  23. fprintf(stderr, "valid: %d\n", token->is_terminated());
  24. fprintf(stderr, "size: %lu\n", token->text().size());
  25. fprintf(stderr, "text: %s\n", token->text().str().c_str());
  26. // Check multiline flag was computed correctly.
  27. CARBON_CHECK(token->is_multi_line() == token->text().contains('\n'));
  28. llvm::BumpPtrAllocator allocator;
  29. volatile auto value =
  30. token->ComputeValue(allocator, NullDiagnosticEmitter<const char*>());
  31. (void)value;
  32. return 0;
  33. }
  34. } // namespace Carbon::Testing