Files
carbon-lang/toolchain/lex/source_loc.h
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

56 lines
1.9 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
#ifndef CARBON_TOOLCHAIN_LEX_SOURCE_LOC_H_
#define CARBON_TOOLCHAIN_LEX_SOURCE_LOC_H_
#include "common/check.h"
#include "llvm/ADT/StringRef.h"
namespace Carbon::Lex {
// The location of a lexer diagnostic: a range of the source buffer. An empty
// range names the point between two characters, where something missing would
// be written.
//
// A `const char*` into the source buffer converts implicitly to the one
// character it points at, and any other range has to be made by a factory
// function. Converting a bare `const char*` to `llvm::StringRef` scans for a
// null byte, and a source buffer isn't guaranteed to have one.
class SourceLoc {
public:
// The character at `pos`, which must point into the source buffer.
//
// NOLINTNEXTLINE(google-explicit-constructor): A character is a location.
SourceLoc(const char* pos) : text_(pos, 1) {}
// The range `text` covers, which must point into the source buffer.
static auto Range(llvm::StringRef text) -> SourceLoc {
return SourceLoc(text);
}
// The range from `begin` up to `end`, both of which must point into the
// source buffer.
static auto Range(const char* begin, const char* end) -> SourceLoc {
CARBON_CHECK(begin <= end, "A range's end can't come before its start.");
return SourceLoc(llvm::StringRef(begin, end - begin));
}
// The point just before `pos`, which must point into the source buffer or
// just past its end.
static auto Before(const char* pos) -> SourceLoc { return Range(pos, pos); }
// The source text this location covers.
auto text() const -> llvm::StringRef { return text_; }
private:
explicit SourceLoc(llvm::StringRef text) : text_(text) {}
llvm::StringRef text_;
};
} // namespace Carbon::Lex
#endif // CARBON_TOOLCHAIN_LEX_SOURCE_LOC_H_