Files
carbon-lang/toolchain/lexer/string_literal_fuzzer.cpp
T
Jon MeowandRichard Smith 0a8c0dc271 Adjust string parsing to consume everything until the terminator. (#1111)
Note I've added a few TODOs, particularly that multi-line strings should only consume until the dedent.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-03-03 10:21:16 -08:00

46 lines
1.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <cstdint>
#include <cstring>
#include "common/check.h"
#include "llvm/ADT/StringRef.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/diagnostics/null_diagnostics.h"
#include "toolchain/lexer/string_literal.h"
namespace Carbon::Testing {
// NOLINTNEXTLINE: Match the documented fuzzer entry point declaration style.
extern "C" int LLVMFuzzerTestOneInput(const unsigned char* data,
std::size_t size) {
auto token = LexedStringLiteral::Lex(
llvm::StringRef(reinterpret_cast<const char*>(data), size));
if (!token) {
// Lexically not a string literal.
return 0;
}
if (!token->is_terminated()) {
// Found errors while parsing.
return 0;
}
fprintf(stderr, "valid: %d\n", token->is_terminated());
fprintf(stderr, "size: %lu\n", token->text().size());
fprintf(stderr, "text: %s\n", token->text().str().c_str());
// Check multiline flag was computed correctly.
CHECK(token->is_multi_line() == token->text().contains('\n'));
volatile auto value =
token->ComputeValue(NullDiagnosticEmitter<const char*>());
(void)value;
return 0;
}
} // namespace Carbon::Testing