mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 19:55:11 +00:00
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>
This commit is contained in:
1 parent
611e6a210e
commit
68569d234d
173 files changed
+1782
-689
No files matched your search
@@ -17,7 +17,7 @@
|
||||
|
||||
namespace Carbon::Lex {
|
||||
|
||||
using DiagnosticEmitter = Diagnostics::Emitter<const char*>;
|
||||
using DiagnosticEmitter = Diagnostics::Emitter<SourceLoc>;
|
||||
|
||||
static constexpr char MultiLineIndicator[] = R"(''')";
|
||||
static constexpr char DoubleQuotedMultiLineIndicator[] = R"(""")";
|
||||
@@ -284,7 +284,9 @@ static auto CheckIndent(DiagnosticEmitter& emitter, llvm::StringRef text,
|
||||
ContentBeforeStringTerminator, Error,
|
||||
"only whitespace is permitted before the closing `'''` of a "
|
||||
"multi-line string");
|
||||
emitter.Emit(indent.end(), ContentBeforeStringTerminator);
|
||||
emitter.Build(indent.end(), ContentBeforeStringTerminator)
|
||||
.Attach(SourceLoc::Range(indent.end(), content.end()))
|
||||
.Emit();
|
||||
}
|
||||
|
||||
return indent;
|
||||
@@ -302,7 +304,9 @@ static auto ExpandUnicodeEscapeSequence(DiagnosticEmitter& emitter,
|
||||
CARBON_DIAGNOSTIC(UnicodeEscapeTooLarge, Error,
|
||||
"code point specified by `\\u{{...}}` escape is greater "
|
||||
"than 0x10FFFF");
|
||||
emitter.Emit(digits.begin(), UnicodeEscapeTooLarge);
|
||||
emitter.Build(digits.begin(), UnicodeEscapeTooLarge)
|
||||
.Attach(SourceLoc::Range(digits))
|
||||
.Emit();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -310,7 +314,9 @@ static auto ExpandUnicodeEscapeSequence(DiagnosticEmitter& emitter,
|
||||
CARBON_DIAGNOSTIC(UnicodeEscapeSurrogate, Error,
|
||||
"code point specified by `\\u{{...}}` escape is a "
|
||||
"surrogate character");
|
||||
emitter.Emit(digits.begin(), UnicodeEscapeSurrogate);
|
||||
emitter.Build(digits.begin(), UnicodeEscapeSurrogate)
|
||||
.Attach(SourceLoc::Range(digits))
|
||||
.Emit();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -348,10 +354,18 @@ static auto AppendFrontOfContents(char*& buffer_cursor,
|
||||
// `result` string. `content` is the string content, starting from the first
|
||||
// character after the escape sequence introducer (for example, the `n` in
|
||||
// `\n`), and will be updated to remove the leading escape sequence.
|
||||
// `escape_begin` points at the `\` that starts the introducer, which a raw
|
||||
// string follows with its `#`s.
|
||||
static auto ExpandAndConsumeEscapeSequence(DiagnosticEmitter& emitter,
|
||||
const char* escape_begin,
|
||||
llvm::StringRef& content,
|
||||
char*& buffer_cursor) -> void {
|
||||
CARBON_CHECK(!content.empty(), "should have escaped closing delimiter");
|
||||
// A diagnostic about the escape marks it from the `\` through as much of it
|
||||
// as was consumed when the problem was found.
|
||||
auto escape_through = [&](const char* end) {
|
||||
return SourceLoc::Range(escape_begin, end);
|
||||
};
|
||||
char first = content.front();
|
||||
content = content.drop_front(1);
|
||||
|
||||
@@ -381,11 +395,13 @@ static auto ExpandAndConsumeEscapeSequence(DiagnosticEmitter& emitter,
|
||||
DecimalEscapeSequence, Error,
|
||||
"decimal digit follows `\\0` escape sequence. Use `\\x00` instead "
|
||||
"of `\\0` if the next character is a digit");
|
||||
emitter.Emit(content.begin(), DecimalEscapeSequence);
|
||||
emitter.Build(escape_begin, DecimalEscapeSequence)
|
||||
.Attach(escape_through(content.begin() + 1))
|
||||
.Emit();
|
||||
return;
|
||||
}
|
||||
return;
|
||||
case 'x':
|
||||
case 'x': {
|
||||
if (content.size() >= 2 && IsUpperHexDigit(content[0]) &&
|
||||
IsUpperHexDigit(content[1])) {
|
||||
AppendChar(buffer_cursor, static_cast<char>(llvm::hexFromNibbles(
|
||||
@@ -396,8 +412,16 @@ static auto ExpandAndConsumeEscapeSequence(DiagnosticEmitter& emitter,
|
||||
CARBON_DIAGNOSTIC(HexadecimalEscapeMissingDigits, Error,
|
||||
"escape sequence `\\x` must be followed by two "
|
||||
"uppercase hexadecimal digits, for example `\\x0F`");
|
||||
emitter.Emit(content.begin(), HexadecimalEscapeMissingDigits);
|
||||
// The digit that is missing or wrong is where the escape needs fixing.
|
||||
const char* bad_digit =
|
||||
content.begin() +
|
||||
content.take_front(2).take_while(IsUpperHexDigit).size();
|
||||
emitter
|
||||
.Build(SourceLoc::Before(bad_digit), HexadecimalEscapeMissingDigits)
|
||||
.Attach(escape_through(bad_digit))
|
||||
.Emit();
|
||||
break;
|
||||
}
|
||||
case 'u': {
|
||||
llvm::StringRef remaining = content;
|
||||
if (remaining.consume_front("{")) {
|
||||
@@ -415,13 +439,21 @@ static auto ExpandAndConsumeEscapeSequence(DiagnosticEmitter& emitter,
|
||||
UnicodeEscapeMissingBracedDigits, Error,
|
||||
"escape sequence `\\u` must be followed by a braced sequence of "
|
||||
"uppercase hexadecimal digits, for example `\\u{{70AD}}`");
|
||||
emitter.Emit(content.begin(), UnicodeEscapeMissingBracedDigits);
|
||||
// `remaining` starts at the part of `{...}` that is missing or wrong,
|
||||
// which is where the escape needs fixing.
|
||||
emitter
|
||||
.Build(SourceLoc::Before(remaining.begin()),
|
||||
UnicodeEscapeMissingBracedDigits)
|
||||
.Attach(escape_through(remaining.begin()))
|
||||
.Emit();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
CARBON_DIAGNOSTIC(UnknownEscapeSequence, Error,
|
||||
"unrecognized escape sequence `{0}`", char);
|
||||
emitter.Emit(content.begin() - 1, UnknownEscapeSequence, first);
|
||||
emitter.Build(escape_begin, UnknownEscapeSequence, first)
|
||||
.Attach(escape_through(content.begin()))
|
||||
.Emit();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -453,7 +485,14 @@ static auto ExpandEscapeSequencesAndRemoveIndent(
|
||||
MismatchedIndentInString, Error,
|
||||
"indentation does not match that of the closing `'''` in "
|
||||
"multi-line string literal");
|
||||
emitter.Emit(line_start, MismatchedIndentInString);
|
||||
CARBON_DIAGNOSTIC_LABEL(ClosingDelimiterIndent, Info,
|
||||
"closing `'''` is indented to here");
|
||||
auto diag = emitter.Build(line_start, MismatchedIndentInString);
|
||||
// A line with no leading whitespace has no indent to mark.
|
||||
if (contents.begin() != line_start) {
|
||||
diag.Attach(SourceLoc::Range(line_start, contents.begin()));
|
||||
}
|
||||
diag.Attach(SourceLoc::Range(indent), ClosingDelimiterIndent).Emit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -500,13 +539,13 @@ static auto ExpandEscapeSequencesAndRemoveIndent(
|
||||
auto after_space = contents.find_if_not(IsHorizontalWhitespace);
|
||||
if (after_space == llvm::StringRef::npos ||
|
||||
contents[after_space] != '\n') {
|
||||
// TODO: Include the source range of the whitespace up to
|
||||
// `contents.begin() + after_space` in the diagnostic.
|
||||
CARBON_DIAGNOSTIC(
|
||||
InvalidHorizontalWhitespaceInString, Error,
|
||||
"whitespace other than plain space must be expressed with an "
|
||||
"escape sequence in a string literal");
|
||||
emitter.Emit(contents.begin(), InvalidHorizontalWhitespaceInString);
|
||||
emitter.Build(contents.begin(), InvalidHorizontalWhitespaceInString)
|
||||
.Attach(SourceLoc::Range(contents.take_front(after_space)))
|
||||
.Emit();
|
||||
// Include the whitespace in the string contents for error recovery.
|
||||
AppendFrontOfContents(buffer_cursor, contents, after_space);
|
||||
}
|
||||
@@ -514,6 +553,7 @@ static auto ExpandEscapeSequencesAndRemoveIndent(
|
||||
continue;
|
||||
}
|
||||
|
||||
const char* escape_begin = contents.begin();
|
||||
if (!contents.consume_front(escape)) {
|
||||
// This is not an escape sequence, just a raw `\`.
|
||||
AppendChar(buffer_cursor, contents.front());
|
||||
@@ -528,7 +568,8 @@ static auto ExpandEscapeSequencesAndRemoveIndent(
|
||||
}
|
||||
|
||||
// Handle this escape sequence.
|
||||
ExpandAndConsumeEscapeSequence(emitter, contents, buffer_cursor);
|
||||
ExpandAndConsumeEscapeSequence(emitter, escape_begin, contents,
|
||||
buffer_cursor);
|
||||
buffer_last_escape = buffer_cursor;
|
||||
}
|
||||
}
|
||||
@@ -540,7 +581,7 @@ static auto IsControlCharacter(llvm::UTF32 c) -> bool {
|
||||
}
|
||||
|
||||
auto StringLiteral::ComputeCharLiteralValue(
|
||||
Diagnostics::Emitter<const char*>& emitter) const
|
||||
Diagnostics::Emitter<SourceLoc>& emitter) const
|
||||
-> std::optional<CharLiteralValue> {
|
||||
CARBON_DCHECK(kind_ == Kind::Char);
|
||||
CARBON_DCHECK(is_terminated_);
|
||||
@@ -548,7 +589,9 @@ auto StringLiteral::ComputeCharLiteralValue(
|
||||
if (hash_level_ != 0) {
|
||||
CARBON_DIAGNOSTIC(CharLiteralRaw, Error,
|
||||
"unexpected `#` before character literal");
|
||||
emitter.Emit(text_.begin(), CharLiteralRaw);
|
||||
emitter.Build(text_.begin(), CharLiteralRaw)
|
||||
.Attach(SourceLoc::Range(text_))
|
||||
.Emit();
|
||||
}
|
||||
|
||||
// Allocate a buffer sized to the content. Note it's possible this could be
|
||||
@@ -577,7 +620,10 @@ auto StringLiteral::ComputeCharLiteralValue(
|
||||
case llvm::conversionOK: {
|
||||
if (target_cursor == target) {
|
||||
CARBON_DIAGNOSTIC(CharLiteralEmpty, Error, "empty character literal");
|
||||
emitter.Emit(text_.begin(), CharLiteralEmpty);
|
||||
// The character goes between the quotes.
|
||||
emitter.Build(SourceLoc::Before(content_.begin()), CharLiteralEmpty)
|
||||
.Attach(SourceLoc::Range(text_))
|
||||
.Emit();
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
@@ -593,7 +639,9 @@ auto StringLiteral::ComputeCharLiteralValue(
|
||||
"control character in character literal; specify as "
|
||||
"escape sequence `\\u{{{0:X-2}}`",
|
||||
llvm::UTF32);
|
||||
emitter.Emit(text_.begin(), CharLiteralControlCharacter, result);
|
||||
emitter.Build(text_.begin(), CharLiteralControlCharacter, result)
|
||||
.Attach(SourceLoc::Range(text_))
|
||||
.Emit();
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
@@ -602,7 +650,9 @@ auto StringLiteral::ComputeCharLiteralValue(
|
||||
"escape sequence `\\x` in character literal; specify "
|
||||
"as escape sequence `\\u{{{0:X-2}}`",
|
||||
llvm::UTF32);
|
||||
emitter.Emit(text_.begin(), CharLiteralHexEscape, result);
|
||||
emitter.Build(text_.begin(), CharLiteralHexEscape, result)
|
||||
.Attach(SourceLoc::Range(text_))
|
||||
.Emit();
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
@@ -610,18 +660,24 @@ auto StringLiteral::ComputeCharLiteralValue(
|
||||
}
|
||||
case llvm::sourceExhausted: {
|
||||
CARBON_DIAGNOSTIC(CharLiteralUnderflow, Error, "incomplete UTF-8");
|
||||
emitter.Emit(text_.begin(), CharLiteralUnderflow);
|
||||
emitter.Build(text_.begin(), CharLiteralUnderflow)
|
||||
.Attach(SourceLoc::Range(text_))
|
||||
.Emit();
|
||||
return std::nullopt;
|
||||
}
|
||||
case llvm::targetExhausted: {
|
||||
CARBON_DIAGNOSTIC(CharLiteralOverflow, Error, "too many characters");
|
||||
emitter.Emit(text_.begin(), CharLiteralOverflow);
|
||||
emitter.Build(text_.begin(), CharLiteralOverflow)
|
||||
.Attach(SourceLoc::Range(text_))
|
||||
.Emit();
|
||||
return std::nullopt;
|
||||
}
|
||||
case llvm::sourceIllegal: {
|
||||
CARBON_DIAGNOSTIC(CharLiteralInvalidUTF8, Error,
|
||||
"invalid UTF-8 character");
|
||||
emitter.Emit(text_.begin(), CharLiteralInvalidUTF8);
|
||||
emitter.Build(text_.begin(), CharLiteralInvalidUTF8)
|
||||
.Attach(SourceLoc::Range(text_))
|
||||
.Emit();
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
@@ -637,7 +693,9 @@ auto StringLiteral::ComputeStringValue(llvm::BumpPtrAllocator& allocator,
|
||||
CARBON_DIAGNOSTIC(
|
||||
MultiLineStringWithDoubleQuotes, Error,
|
||||
"use `'''` delimiters for a multi-line string literal, not `\"\"\"`");
|
||||
emitter.Emit(text_.begin(), MultiLineStringWithDoubleQuotes);
|
||||
emitter.Build(text_.begin(), MultiLineStringWithDoubleQuotes)
|
||||
.Attach(SourceLoc::Range(text_.take_front(hash_level_ + 3)))
|
||||
.Emit();
|
||||
}
|
||||
llvm::StringRef indent = IsMultiLine(kind_)
|
||||
? CheckIndent(emitter, text_, content_)
|
||||
|
||||
Reference in new issue
Block a user