Files
carbon-lang/toolchain/lex/string_literal_benchmark.cpp
Chandler CarruthandRichard Smith 68569d234d Improve lexer and parser diagnostic locations (#7990)
Lexer diagnostics used a single `const char*` as their source location,
which only allowed pointing at a single character rather than a run of
characters. This limited the underlining of unrecognized tokens, and
also made escape sequence diagnostics particularly strange by stopping
at whatever point the lexer discovered the error and pointing only at
that location. For example: `"\w"` was reported at the `w`, and `"\x"`
at the closing `"`.

This change tries to always keep the primary location of a diagnostic
the point at which typing (inserting or deleting) might fix the error.
So for `"\x"`, `\x` is underlined, but the position is at the `"` as
that's where you would type a hex code.

The lexer's emitter now takes a special lexing source location that
represents either a single position _between_ two characters, a single
character, or a range of the source buffer. The lexer diagnostics are
updated to construct these for the character range as appropriate.

Parser diagnostics for a missing token were located at the _next_ token,
which is particularly confusing for terminators like missing `;`s. Now
they are located at a position after the previous token, with or without
a space depending on the token, but typically on the same line.
Diagnostics still sort by how far the parser has read.

The parser diagnostics for unclosed groups also now attaches a label to
the opening token when it is on a separate line. And the diagnostics for
mismatched indent in a multi-line string attaches a label to the closing
`'''` that establishes that indent.

There are likely more location improvements we should do here, but this
seemed like a reasonable incremental improvement and is what Claude
found when doing an initial audit. One example where I think we should
make further improvements is around suggesting raw identifiers when we
find an unexpected `Cpp` or `Core` or `base`. We do this consistently
now, but raw identifiers should be rare. A TODO is added for these
cases.

Assisted-by: Claude Code

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2026-11-01 06:22:00 +00:00

148 lines
4.6 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 <benchmark/benchmark.h>
#include <string>
#include <string_view>
#include "toolchain/diagnostics/null_diagnostics.h"
#include "toolchain/lex/string_literal.h"
namespace Carbon::Lex {
namespace {
static void BM_ValidString(benchmark::State& state, std::string_view introducer,
std::string_view terminator) {
std::string x(introducer);
x.append(100000, 'a');
x.append(terminator);
for (auto _ : state) {
StringLiteral::Lex(x);
}
}
static void BM_ValidString_Simple(benchmark::State& state) {
BM_ValidString(state, "\"", "\"");
}
static void BM_ValidString_Multiline(benchmark::State& state) {
BM_ValidString(state, "'''\n", "\n'''");
}
static void BM_ValidString_MultilineDoubleQuote(benchmark::State& state) {
BM_ValidString(state, "\"\"\"\n", "\n\"\"\"");
}
static void BM_ValidString_Raw(benchmark::State& state) {
BM_ValidString(state, "#\"", "\"#");
}
BENCHMARK(BM_ValidString_Simple);
BENCHMARK(BM_ValidString_Multiline);
BENCHMARK(BM_ValidString_MultilineDoubleQuote);
BENCHMARK(BM_ValidString_Raw);
static void BM_IncompleteWithRepeatedEscapes(benchmark::State& state,
std::string_view introducer,
std::string_view escape) {
std::string x(introducer);
// Aim for about 100k to emphasize escape parsing issues.
while (x.size() < 100000) {
x.append("key: ");
x.append(escape);
x.append("\"");
x.append(escape);
x.append("\"");
x.append(escape);
x.append("n ");
}
for (auto _ : state) {
StringLiteral::Lex(x);
}
}
static void BM_IncompleteWithEscapes_Simple(benchmark::State& state) {
BM_IncompleteWithRepeatedEscapes(state, "\"", "\\");
}
static void BM_IncompleteWithEscapes_Multiline(benchmark::State& state) {
BM_IncompleteWithRepeatedEscapes(state, "'''\n", "\\");
}
static void BM_IncompleteWithEscapes_MultilineDoubleQuote(
benchmark::State& state) {
BM_IncompleteWithRepeatedEscapes(state, "\"\"\"\n", "\\");
}
static void BM_IncompleteWithEscapes_Raw(benchmark::State& state) {
BM_IncompleteWithRepeatedEscapes(state, "#\"", "\\#");
}
BENCHMARK(BM_IncompleteWithEscapes_Simple);
BENCHMARK(BM_IncompleteWithEscapes_Multiline);
BENCHMARK(BM_IncompleteWithEscapes_MultilineDoubleQuote);
BENCHMARK(BM_IncompleteWithEscapes_Raw);
static void BM_SimpleStringValue(benchmark::State& state, int size,
std::string_view introducer, bool add_escape,
std::string_view terminator) {
llvm::BumpPtrAllocator allocator;
std::string x(introducer);
x.append(size, 'a');
if (add_escape) {
// Adds a basic escape that forces ComputeStringValue to generate a new
// string.
x.append("\\\\");
}
x.append(terminator);
for (auto _ : state) {
StringLiteral::Lex(x)->ComputeStringValue(
allocator, Diagnostics::NullEmitter<Lex::SourceLoc>());
}
}
static void BM_ComputeStringValue_NoGenerate_Short(benchmark::State& state) {
BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/false, "\"");
}
static void BM_ComputeStringValue_NoGenerate_Long(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/false, "\"");
}
static void BM_ComputeStringValue_WillGenerate_Short(benchmark::State& state) {
BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/true, "\"");
}
static void BM_ComputeStringValue_WillGenerate_Long(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/true, "\"");
}
static void BM_ComputeStringValue_WillGenerate_Multiline(
benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "'''\n", /*add_escape=*/true, "\n'''");
}
static void BM_ComputeStringValue_WillGenerate_MultilineDoubleQuote(
benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "\"\"\"\n", /*add_escape=*/true,
"\n\"\"\"");
}
static void BM_ComputeStringValue_WillGenerate_Raw(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "#\"", /*add_escape=*/true, "\"#");
}
BENCHMARK(BM_ComputeStringValue_NoGenerate_Short);
BENCHMARK(BM_ComputeStringValue_NoGenerate_Long);
BENCHMARK(BM_ComputeStringValue_WillGenerate_Short);
BENCHMARK(BM_ComputeStringValue_WillGenerate_Long);
BENCHMARK(BM_ComputeStringValue_WillGenerate_Multiline);
BENCHMARK(BM_ComputeStringValue_WillGenerate_MultilineDoubleQuote);
BENCHMARK(BM_ComputeStringValue_WillGenerate_Raw);
} // namespace
} // namespace Carbon::Lex