string_literal_fuzzer.cpp 1.4 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 <cstdint>
  5. #include <cstring>
  6. #include "common/check.h"
  7. #include "llvm/ADT/StringRef.h"
  8. #include "toolchain/diagnostics/diagnostic_emitter.h"
  9. #include "toolchain/diagnostics/null_diagnostics.h"
  10. #include "toolchain/lexer/string_literal.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. auto token = LexedStringLiteral::Lex(
  16. llvm::StringRef(reinterpret_cast<const char*>(data), size));
  17. if (!token) {
  18. // Lexically not a string literal.
  19. return 0;
  20. }
  21. if (!token->is_terminated()) {
  22. // Found errors while parsing.
  23. return 0;
  24. }
  25. fprintf(stderr, "valid: %d\n", token->is_terminated());
  26. fprintf(stderr, "size: %lu\n", token->text().size());
  27. fprintf(stderr, "text: %s\n", token->text().str().c_str());
  28. // Check multiline flag was computed correctly.
  29. CARBON_CHECK(token->is_multi_line() == token->text().contains('\n'));
  30. volatile auto value =
  31. token->ComputeValue(NullDiagnosticEmitter<const char*>());
  32. (void)value;
  33. return 0;
  34. }
  35. } // namespace Carbon::Testing