string_literal_fuzzer.cpp 1008 B

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