Files
carbon-lang/toolchain/parse/handle_inline_decl.cpp
T
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

46 lines
1.8 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 "toolchain/lex/token_kind.h"
#include "toolchain/parse/context.h"
#include "toolchain/parse/handle.h"
#include "toolchain/parse/node_kind.h"
#include "toolchain/parse/state.h"
namespace Carbon::Parse {
auto HandleInlineDeclAfterIntroducer(Context& context) -> void {
auto state = context.PopState();
if (auto cpp_token = context.ConsumeIf(Lex::TokenKind::Cpp)) {
context.AddLeafNode(NodeKind::CppNameExpr, *cpp_token);
} else {
CARBON_DIAGNOSTIC(ExpectedCppAfterInline, Error,
"expected `Cpp` after `inline`");
context.emitter().Emit(context.PointAfterPreviousToken(),
ExpectedCppAfterInline);
context.AddNode(NodeKind::InlineCppDecl,
context.SkipPastLikelyEnd(state.token),
/*has_error=*/true);
return;
}
if (auto str = context.ConsumeIf(Lex::TokenKind::StringLiteral)) {
context.AddLeafNode(NodeKind::InlineImportBody, *str);
context.AddNodeExpectingDeclSemi(state, NodeKind::InlineCppDecl,
Lex::TokenKind::Inline,
/*is_def_allowed=*/false);
} else {
CARBON_DIAGNOSTIC(ExpectedStringAfterInlineCpp, Error,
"expected string literal after `inline Cpp`");
context.emitter().Emit(context.PointAfterPreviousToken(),
ExpectedStringAfterInlineCpp);
context.AddNode(NodeKind::InlineCppDecl,
context.SkipPastLikelyEnd(state.token),
/*has_error=*/true);
}
}
} // namespace Carbon::Parse