numeric_literal_fuzzer.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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/numeric_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 = LexedNumericLiteral::Lex(
  14. llvm::StringRef(reinterpret_cast<const char*>(data), size));
  15. if (!token) {
  16. // Lexically not a numeric literal.
  17. return 0;
  18. }
  19. LexedNumericLiteral::Parser parser(NullDiagnosticEmitter(), *token);
  20. if (parser.Check() == LexedNumericLiteral::Parser::UnrecoverableError) {
  21. // Lexically OK, but token is meaningless.
  22. return 0;
  23. }
  24. // Ensure we can exercise the various queries on a valid literal.
  25. volatile auto radix = parser.GetRadix();
  26. volatile auto mantissa = parser.GetMantissa();
  27. volatile auto exponent = parser.GetExponent();
  28. (void)radix;
  29. (void)mantissa;
  30. (void)exponent;
  31. return 0;
  32. }
  33. } // namespace Carbon