string_literal_fuzzer.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 "diagnostics/diagnostic_emitter.h"
  7. #include "diagnostics/null_diagnostics.h"
  8. #include "lexer/string_literal.h"
  9. #include "llvm/ADT/StringRef.h"
  10. namespace Carbon {
  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. auto token = LexedStringLiteral::Lex(
  15. llvm::StringRef(reinterpret_cast<const char*>(data), size));
  16. if (!token) {
  17. // Lexically not a string literal.
  18. return 0;
  19. }
  20. // Check multiline flag was computed correctly.
  21. if (token->IsMultiLine() != token->Text().contains('\n')) {
  22. __builtin_trap();
  23. }
  24. volatile auto value =
  25. token->ComputeValue(NullDiagnosticEmitter<const char*>());
  26. (void)value;
  27. return 0;
  28. }
  29. } // namespace Carbon