mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 15:18:11 +00:00
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>
194 lines
7.8 KiB
Plaintext
194 lines
7.8 KiB
Plaintext
// 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
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/parse/testdata/packages/import/cpp_inline.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/packages/import/cpp_inline.carbon
|
|
|
|
// --- fail_bad_syntax.carbon
|
|
|
|
// CHECK:STDERR: fail_bad_syntax.carbon:[[@LINE+2]]:18: error: expected string literal after `inline` [ExpectedStringAfterInline]
|
|
// CHECK:STDERR:
|
|
import Cpp inline;
|
|
// CHECK:STDERR: fail_bad_syntax.carbon:[[@LINE+2]]:18: error: expected string literal after `inline` [ExpectedStringAfterInline]
|
|
// CHECK:STDERR:
|
|
import Cpp inline library "foo.h";
|
|
|
|
// TODO: We might want to allow this as a "preprocessed source" syntax.
|
|
// CHECK:STDERR: fail_bad_syntax.carbon:[[@LINE+2]]:27: error: `import` declarations must end with a `;` [ExpectedDeclSemi]
|
|
// CHECK:STDERR:
|
|
import Cpp library "foo.h" inline "bar";
|
|
|
|
// CHECK:STDERR: fail_bad_syntax.carbon:[[@LINE+2]]:23: error: `import` declarations must end with a `;` [ExpectedDeclSemi]
|
|
// CHECK:STDERR:
|
|
import library "foo.h" inline "bar";
|
|
|
|
// --- fail_no_semi_at_eof.carbon
|
|
|
|
// CHECK:STDERR: fail_no_semi_at_eof.carbon:[[@LINE+2]]:18: error: expected string literal after `inline` [ExpectedStringAfterInline]
|
|
// CHECK:STDERR:
|
|
import Cpp inline
|
|
|
|
// --- fail_package_inline.carbon
|
|
|
|
// CHECK:STDERR: fail_package_inline.carbon:[[@LINE+2]]:15: error: `package` declarations must end with a `;` [ExpectedDeclSemi]
|
|
// CHECK:STDERR:
|
|
package NotCpp inline "int n;";
|
|
|
|
// --- fail_library_inline.carbon
|
|
|
|
// CHECK:STDERR: fail_library_inline.carbon:[[@LINE+2]]:16: error: `library` declarations must end with a `;` [ExpectedDeclSemi]
|
|
// CHECK:STDERR:
|
|
library "foo.h" inline "int n;";
|
|
|
|
// --- wrong_package.carbon
|
|
|
|
// We reject this in check.
|
|
import NotCpp inline "bar";
|
|
|
|
// --- valid_syntax.carbon
|
|
|
|
import Cpp inline "int m;";
|
|
|
|
import Cpp inline '''
|
|
// C++ comment.
|
|
int n;
|
|
''';
|
|
|
|
// --- inline_cpp.carbon
|
|
|
|
inline Cpp '''
|
|
int n;
|
|
''';
|
|
|
|
// --- fail_inline_cpp_syntax_errors.carbon
|
|
|
|
// CHECK:STDERR: fail_inline_cpp_syntax_errors.carbon:[[@LINE+2]]:7: error: expected `Cpp` after `inline` [ExpectedCppAfterInline]
|
|
// CHECK:STDERR:
|
|
inline;
|
|
|
|
// CHECK:STDERR: fail_inline_cpp_syntax_errors.carbon:[[@LINE+2]]:7: error: expected `Cpp` after `inline` [ExpectedCppAfterInline]
|
|
// CHECK:STDERR:
|
|
inline '''
|
|
int n;
|
|
''';
|
|
|
|
// Recovery skips to the `;` even when tokens follow the closing `'''` on its
|
|
// line, so the `;` ends the declaration rather than starting an empty one.
|
|
// CHECK:STDERR: fail_inline_cpp_syntax_errors.carbon:[[@LINE+2]]:7: error: expected `Cpp` after `inline` [ExpectedCppAfterInline]
|
|
// CHECK:STDERR:
|
|
inline '''
|
|
int n;
|
|
''' + "more";
|
|
|
|
// CHECK:STDERR: fail_inline_cpp_syntax_errors.carbon:[[@LINE+2]]:11: error: expected string literal after `inline Cpp` [ExpectedStringAfterInlineCpp]
|
|
// CHECK:STDERR:
|
|
inline Cpp;
|
|
|
|
// CHECK:STDERR: fail_inline_cpp_syntax_errors.carbon:[[@LINE+2]]:11: error: expected string literal after `inline Cpp` [ExpectedStringAfterInlineCpp]
|
|
// CHECK:STDERR:
|
|
inline Cpp x;
|
|
|
|
|
|
// CHECK:STDOUT: - filename: fail_bad_syntax.carbon
|
|
// CHECK:STDOUT: ╭─FileStart ''
|
|
// CHECK:STDOUT: │ ╭─ImportIntroducer 'import'
|
|
// CHECK:STDOUT: │ ├─CppPackageName 'Cpp'
|
|
// CHECK:STDOUT: │ │ ╭─InlineImportBody ';' has_error
|
|
// CHECK:STDOUT: │ ├─InlineImportSpecifier 'inline'
|
|
// CHECK:STDOUT: ├─ImportDecl ';' has_error
|
|
// CHECK:STDOUT: │ ╭─ImportIntroducer 'import'
|
|
// CHECK:STDOUT: │ ├─CppPackageName 'Cpp'
|
|
// CHECK:STDOUT: │ │ ╭─InlineImportBody 'library' has_error
|
|
// CHECK:STDOUT: │ ├─InlineImportSpecifier 'inline'
|
|
// CHECK:STDOUT: ├─ImportDecl ';' has_error
|
|
// CHECK:STDOUT: │ ╭─ImportIntroducer 'import'
|
|
// CHECK:STDOUT: │ ├─CppPackageName 'Cpp'
|
|
// CHECK:STDOUT: │ │ ╭─LibraryName '"foo.h"'
|
|
// CHECK:STDOUT: │ ├─LibrarySpecifier 'library'
|
|
// CHECK:STDOUT: ├─ImportDecl ';' has_error
|
|
// CHECK:STDOUT: │ ╭─ImportIntroducer 'import'
|
|
// CHECK:STDOUT: │ │ ╭─LibraryName '"foo.h"'
|
|
// CHECK:STDOUT: │ ├─LibrarySpecifier 'library'
|
|
// CHECK:STDOUT: ├─ImportDecl ';' has_error
|
|
// CHECK:STDOUT: ├─FileEnd ''
|
|
// CHECK:STDOUT: (root)
|
|
// CHECK:STDOUT: - filename: fail_no_semi_at_eof.carbon
|
|
// CHECK:STDOUT: ╭─FileStart ''
|
|
// CHECK:STDOUT: │ ╭─ImportIntroducer 'import'
|
|
// CHECK:STDOUT: │ ├─CppPackageName 'Cpp'
|
|
// CHECK:STDOUT: │ │ ╭─InlineImportBody '' has_error
|
|
// CHECK:STDOUT: │ ├─InlineImportSpecifier 'inline'
|
|
// CHECK:STDOUT: ├─ImportDecl 'inline' has_error
|
|
// CHECK:STDOUT: ├─FileEnd ''
|
|
// CHECK:STDOUT: (root)
|
|
// CHECK:STDOUT: - filename: fail_package_inline.carbon
|
|
// CHECK:STDOUT: ╭─FileStart ''
|
|
// CHECK:STDOUT: │ ╭─PackageIntroducer 'package'
|
|
// CHECK:STDOUT: │ ├─IdentifierPackageName 'NotCpp'
|
|
// CHECK:STDOUT: ├─PackageDecl ';' has_error
|
|
// CHECK:STDOUT: ├─FileEnd ''
|
|
// CHECK:STDOUT: (root)
|
|
// CHECK:STDOUT: - filename: fail_library_inline.carbon
|
|
// CHECK:STDOUT: ╭─FileStart ''
|
|
// CHECK:STDOUT: │ ╭─LibraryIntroducer 'library'
|
|
// CHECK:STDOUT: │ ├─LibraryName '"foo.h"'
|
|
// CHECK:STDOUT: ├─LibraryDecl ';' has_error
|
|
// CHECK:STDOUT: ├─FileEnd ''
|
|
// CHECK:STDOUT: (root)
|
|
// CHECK:STDOUT: - filename: wrong_package.carbon
|
|
// CHECK:STDOUT: ╭─FileStart ''
|
|
// CHECK:STDOUT: │ ╭─ImportIntroducer 'import'
|
|
// CHECK:STDOUT: │ ├─IdentifierPackageName 'NotCpp'
|
|
// CHECK:STDOUT: │ │ ╭─InlineImportBody '"bar"'
|
|
// CHECK:STDOUT: │ ├─InlineImportSpecifier 'inline'
|
|
// CHECK:STDOUT: ├─ImportDecl ';'
|
|
// CHECK:STDOUT: ├─FileEnd ''
|
|
// CHECK:STDOUT: (root)
|
|
// CHECK:STDOUT: - filename: valid_syntax.carbon
|
|
// CHECK:STDOUT: ╭─FileStart ''
|
|
// CHECK:STDOUT: │ ╭─ImportIntroducer 'import'
|
|
// CHECK:STDOUT: │ ├─CppPackageName 'Cpp'
|
|
// CHECK:STDOUT: │ │ ╭─InlineImportBody '"int m;"'
|
|
// CHECK:STDOUT: │ ├─InlineImportSpecifier 'inline'
|
|
// CHECK:STDOUT: ├─ImportDecl ';'
|
|
// CHECK:STDOUT: │ ╭─ImportIntroducer 'import'
|
|
// CHECK:STDOUT: │ ├─CppPackageName 'Cpp'
|
|
// CHECK:STDOUT: │ │ ╭─InlineImportBody ''''
|
|
// CHECK:STDOUT: // C++ comment.
|
|
// CHECK:STDOUT: int n;
|
|
// CHECK:STDOUT: ''''
|
|
// CHECK:STDOUT: │ ├─InlineImportSpecifier 'inline'
|
|
// CHECK:STDOUT: ├─ImportDecl ';'
|
|
// CHECK:STDOUT: ├─FileEnd ''
|
|
// CHECK:STDOUT: (root)
|
|
// CHECK:STDOUT: - filename: inline_cpp.carbon
|
|
// CHECK:STDOUT: ╭─FileStart ''
|
|
// CHECK:STDOUT: │ ╭─InlineIntroducer 'inline'
|
|
// CHECK:STDOUT: │ ├─CppNameExpr 'Cpp'
|
|
// CHECK:STDOUT: │ ├─InlineImportBody ''''
|
|
// CHECK:STDOUT: int n;
|
|
// CHECK:STDOUT: ''''
|
|
// CHECK:STDOUT: ├─InlineCppDecl ';'
|
|
// CHECK:STDOUT: ├─FileEnd ''
|
|
// CHECK:STDOUT: (root)
|
|
// CHECK:STDOUT: - filename: fail_inline_cpp_syntax_errors.carbon
|
|
// CHECK:STDOUT: ╭─FileStart ''
|
|
// CHECK:STDOUT: │ ╭─InlineIntroducer 'inline'
|
|
// CHECK:STDOUT: ├─InlineCppDecl ';' has_error
|
|
// CHECK:STDOUT: │ ╭─InlineIntroducer 'inline'
|
|
// CHECK:STDOUT: ├─InlineCppDecl ';' has_error
|
|
// CHECK:STDOUT: │ ╭─InlineIntroducer 'inline'
|
|
// CHECK:STDOUT: ├─InlineCppDecl ';' has_error
|
|
// CHECK:STDOUT: │ ╭─InlineIntroducer 'inline'
|
|
// CHECK:STDOUT: │ ├─CppNameExpr 'Cpp'
|
|
// CHECK:STDOUT: ├─InlineCppDecl ';' has_error
|
|
// CHECK:STDOUT: │ ╭─InlineIntroducer 'inline'
|
|
// CHECK:STDOUT: │ ├─CppNameExpr 'Cpp'
|
|
// CHECK:STDOUT: ├─InlineCppDecl ';' has_error
|
|
// CHECK:STDOUT: ├─FileEnd ''
|
|
// CHECK:STDOUT: (root)
|