From 412f7d58ce2a949b9b70dd942dbdcc646e7b3fee Mon Sep 17 00:00:00 2001 From: tanhongmin Date: Fri, 27 Jun 2025 06:42:09 +0000 Subject: [PATCH] [Parser] Fix the recognition issue of the `impl` keyword and optimize the parsing of `impl` trait declarations. Previously, in the definition of the marker, the `impl` keyword was not correctly recognized as a BSC keyword, but was instead treated as an identifier for lexical analysis. This patch adds `impl` as the correct BSC keyword to `TokenKinds.def` and refactors the parsing of `impl` trait declarations in the following ways: - Moves the recognition of the `impl` keyword directly into the main parsing switch statement - Removes the separate `ShouldParseImplTraitDecl` static function - Simplifies the condition checks for `impl` trait declarations This enhances the reliability and consistency of BSC interface parsing. --- clang/include/clang/Basic/TokenKinds.def | 1 + clang/lib/Parse/Parser.cpp | 22 +++++-------------- .../impl_keyword/impl_keyword.cbs | 19 ++++++++++++++++ 3 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 clang/test/BSC/Negative/Trait/GenericTrait/impl_keyword/impl_keyword.cbs diff --git a/clang/include/clang/Basic/TokenKinds.def b/clang/include/clang/Basic/TokenKinds.def index 0def6903efe1..4a428d77ef14 100644 --- a/clang/include/clang/Basic/TokenKinds.def +++ b/clang/include/clang/Basic/TokenKinds.def @@ -478,6 +478,7 @@ KEYWORD(await , KEYBSC) KEYWORD(owned , KEYBSC) KEYWORD(borrow , KEYBSC) KEYWORD(trait , KEYBSC) +KEYWORD(impl , KEYBSC) KEYWORD(safe , KEYBSC) KEYWORD(unsafe , KEYBSC) KEYWORD(__conditional , KEYBSC) diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp index 178d939e3056..2bd24ffe8ae2 100644 --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -785,19 +785,6 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result, return false; } -#if ENABLE_BSC -static bool ShouldParseImplTraitDecl(Sema &S, const Token &Tok, - const Token &TokNext) { - if (!S.getLangOpts().BSC) - return false; - if (Tok.is(tok::identifier) && - Tok.getIdentifierInfo()->getName().equals("impl")) - if (TokNext.isOneOf(tok::kw_trait, tok::identifier)) - return true; - return false; -} -#endif - /// ParseExternalDeclaration: /// /// The `Attrs` that are passed in are C++11 attributes and appertain to the @@ -1059,13 +1046,16 @@ Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(ParsedAttributes &Attrs, SkipUntil(tok::semi); return nullptr; - default: - dont_know: #if ENABLE_BSC + case tok::kw_impl: if (getLangOpts().BSC && - ShouldParseImplTraitDecl(Actions, Tok, NextToken())) + NextToken().isOneOf(tok::kw_trait, tok::identifier)) return ParseImplTraitDeclaration(); +#endif + default: + dont_know: +#if ENABLE_BSC // parse BSC template declaration // TODO: change if entrance condition, abandon isBSCTemplateDecl() if (isBSCTemplateDecl(Tok)) { diff --git a/clang/test/BSC/Negative/Trait/GenericTrait/impl_keyword/impl_keyword.cbs b/clang/test/BSC/Negative/Trait/GenericTrait/impl_keyword/impl_keyword.cbs new file mode 100644 index 000000000000..b414722c0fc8 --- /dev/null +++ b/clang/test/BSC/Negative/Trait/GenericTrait/impl_keyword/impl_keyword.cbs @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -verify %s + +trait T { + void f(This* this); +}; + +void int::f(int* this) {}; + +impl trait T for int; + +struct S { // expected-error{{expected ';' at end of declaration list}} + impl x; // expected-error{{type name requires a specifier or qualifier}} expected-error{{expected member name or ';' after declaration specifiers}} +}; + +struct S2 {}; // expected-error{{expected expression}} expected-error{{explicit specialization of undeclared template struct 'S2'}} + +int main() { + int impl = 1; // expected-error{{expected identifier or '('}} +} \ No newline at end of file -- Gitee