https://github.com/yronglin updated https://github.com/llvm/llvm-project/pull/187846
>From 92f8d9bb3a6636a2b2cd2dc376d427d07781d018 Mon Sep 17 00:00:00 2001 From: yronglin <[email protected]> Date: Sat, 21 Mar 2026 15:54:55 +0800 Subject: [PATCH 1/2] [clang] Diag unexpected token after module name in phase 7 Signed-off-by: yronglin <[email protected]> --- clang/docs/ReleaseNotes.rst | 1 + clang/include/clang/Basic/DiagnosticLexKinds.td | 3 --- .../include/clang/Basic/DiagnosticParseKinds.td | 3 +++ clang/lib/Lex/PPDirectives.cpp | 16 ---------------- clang/lib/Parse/Parser.cpp | 6 ++++-- clang/test/CXX/basic/basic.link/p3.cpp | 2 +- clang/test/CXX/drs/cwg2947.cpp | 4 ++-- .../basic/basic.link/module-declaration.cpp | 2 +- clang/test/CXX/module/cpp.pre/p1.cpp | 6 +++--- 9 files changed, 15 insertions(+), 28 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index d8ab080cecbe6..739a83c132eeb 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -702,6 +702,7 @@ Bug Fixes in This Version - Fixed crash when checking for overflow for unary operator that can't overflow (#GH170072) - Clang no longer handles a `" q-char-sequence "` header name as a string literal (#GH132643). - Fixed an assertion when ``__attribute__((alloc_size))`` is used with an argument type wider than the target's pointer width. (#GH190445) +- Fixed a crash when module directive export module foo not following a semicolon and there are no rest pp-tokens in current module file. (#GH187771) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td index 383bf1a7fdb3f..b5813ea44ce19 100644 --- a/clang/include/clang/Basic/DiagnosticLexKinds.td +++ b/clang/include/clang/Basic/DiagnosticLexKinds.td @@ -1035,9 +1035,6 @@ def err_pp_module_name_is_macro : Error< "%select{module|partition}0 name component %1 cannot be a object-like macro">; def err_pp_module_expected_ident : Error< "expected %select{identifier after '.' in |}0module name">; -def err_pp_unexpected_tok_after_module_name : Error< - "unexpected preprocessing token '%0' after module name, " - "only ';' and '[' (start of attribute specifier sequence) are allowed">; def warn_pp_extra_tokens_at_module_directive_eol : Warning<"extra tokens after semicolon in '%0' directive">, InGroup<ExtraTokens>; diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td index 0dc7d0fbeac0b..56854aa6446ea 100644 --- a/clang/include/clang/Basic/DiagnosticParseKinds.td +++ b/clang/include/clang/Basic/DiagnosticParseKinds.td @@ -1792,6 +1792,9 @@ def ext_bit_int : Extension< } // end of Parse Issue category. let CategoryName = "Modules Issue" in { +def err_unexpected_tok_after_module_name : Error< + "unexpected '%0' after module name, only ';' and '[' (start of attribute " + "specifier sequence) are allowed">; def err_unexpected_module_or_import_decl : Error< "%select{module|import}0 declaration can only appear at the top level">; def err_attribute_not_module_attr : Error< diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index eb21a510dcf83..42f0dca37b799 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -4416,22 +4416,6 @@ void Preprocessor::HandleCXXModuleDirective(Token ModuleTok) { break; } - // Consume the pp-import-suffix and expand any macros in it now, if we're not - // at the semicolon already. - std::optional<Token> NextPPTok = - DirToks.back().is(tok::eod) ? peekNextPPToken() : DirToks.back(); - - // Only ';' and '[' are allowed after module name. - // We also check 'private' because the previous is not a module name. - if (NextPPTok) { - if (NextPPTok->is(tok::raw_identifier)) - LookUpIdentifierInfo(*NextPPTok); - if (!NextPPTok->isOneOf(tok::semi, tok::eod, tok::l_square, - tok::kw_private)) - Diag(*NextPPTok, diag::err_pp_unexpected_tok_after_module_name) - << getSpelling(*NextPPTok); - } - if (!DirToks.back().isOneOf(tok::semi, tok::eod)) { // Consume the pp-import-suffix and expand any macros in it now. We'll add // it back into the token stream later. diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp index 5e1fd4df1a3f0..f62dce87db924 100644 --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -2382,9 +2382,11 @@ Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) { } // This should already diagnosed in phase 4, just skip unil semicolon. - if (!Tok.isOneOf(tok::semi, tok::l_square)) + if (!Tok.isOneOf(tok::semi, tok::l_square)) { + Diag(Tok, diag::err_unexpected_tok_after_module_name) + << PP.getSpelling(Tok); SkipUntil(tok::semi, SkipUntilFlags::StopBeforeMatch); - + } // We don't support any module attributes yet; just parse them and diagnose. ParsedAttributes Attrs(AttrFactory); MaybeParseCXX11Attributes(Attrs); diff --git a/clang/test/CXX/basic/basic.link/p3.cpp b/clang/test/CXX/basic/basic.link/p3.cpp index bc3622c7bbd64..0784a7d879d7d 100644 --- a/clang/test/CXX/basic/basic.link/p3.cpp +++ b/clang/test/CXX/basic/basic.link/p3.cpp @@ -14,7 +14,7 @@ constexpr int n = 123; export module m; // #1 module y = {}; // expected-error {{multiple module declarations}} -// expected-error@-1 {{unexpected preprocessing token '=' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} +// expected-error@-1 {{unexpected '=' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} // expected-note@#1 {{previous module declaration}} ::import x = {}; diff --git a/clang/test/CXX/drs/cwg2947.cpp b/clang/test/CXX/drs/cwg2947.cpp index 3d9d738f40de3..1fa59b4e00ab2 100644 --- a/clang/test/CXX/drs/cwg2947.cpp +++ b/clang/test/CXX/drs/cwg2947.cpp @@ -37,7 +37,7 @@ //--- cwg2947_example1.cpp // #define DOT_BAR .bar export module foo DOT_BAR; // error: expansion of DOT_BAR; does not begin with ; or [ -// expected-error@-1 {{unexpected preprocessing token '.' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} +// expected-error@-1 {{unexpected '.' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} //--- cwg2947_example2.cpp export module M MOD_ATTR; // OK @@ -46,7 +46,7 @@ export module M MOD_ATTR; // OK //--- cwg2947_example3.cpp export module a .b; // error: preprocessing token after pp-module-name is not ; or [ -// expected-error@-1 {{unexpected preprocessing token '.' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} +// expected-error@-1 {{unexpected '.' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} //--- cwg2947_example4.cpp export module M [[ diff --git a/clang/test/CXX/module/basic/basic.link/module-declaration.cpp b/clang/test/CXX/module/basic/basic.link/module-declaration.cpp index 52ba1d9f82f2f..b22fc87aa296c 100644 --- a/clang/test/CXX/module/basic/basic.link/module-declaration.cpp +++ b/clang/test/CXX/module/basic/basic.link/module-declaration.cpp @@ -47,7 +47,7 @@ export module x; //--- invalid_module_name.cppm export module z elderberry; -// expected-error@-1 {{unexpected preprocessing token 'elderberry' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} +// expected-error@-1 {{unexpected 'elderberry' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} //--- empty_attribute.cppm // expected-no-diagnostics diff --git a/clang/test/CXX/module/cpp.pre/p1.cpp b/clang/test/CXX/module/cpp.pre/p1.cpp index 989915004ff57..dc5a3c36d4139 100644 --- a/clang/test/CXX/module/cpp.pre/p1.cpp +++ b/clang/test/CXX/module/cpp.pre/p1.cpp @@ -153,7 +153,7 @@ export module m:n; //--- unexpected_character_in_pp_module_suffix.cpp export module m(); -// expected-error@-1 {{unexpected preprocessing token '(' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} +// expected-error@-1 {{unexpected '(' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} //--- semi_in_same_line.cpp export module m // OK @@ -189,13 +189,13 @@ module x; //--- func_like_macro.cpp // #define m(x) x export module m - (foo); // expected-error {{unexpected preprocessing token '(' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} + (foo); // expected-error {{unexpected '(' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} //--- lparen.cpp // #define m(x) x // #define LPAREN ( export module m - LPAREN foo); // expected-error {{unexpected preprocessing token 'LPAREN' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} + LPAREN foo); // expected-error {{unexpected '(' after module name, only ';' and '[' (start of attribute specifier sequence) are allowed}} //--- control_line.cpp #if 0 // #1 >From ed3aa52cb1981d09848ea496d2a82988d98a4c73 Mon Sep 17 00:00:00 2001 From: yronglin <[email protected]> Date: Fri, 12 Jun 2026 01:40:42 +0800 Subject: [PATCH 2/2] Don't diag when Tok is eof Signed-off-by: yronglin <[email protected]> --- clang/lib/Parse/Parser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp index f62dce87db924..387d930588eac 100644 --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -2381,12 +2381,12 @@ Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) { return nullptr; } - // This should already diagnosed in phase 4, just skip unil semicolon. - if (!Tok.isOneOf(tok::semi, tok::l_square)) { + if (Tok.isNoneOf(tok::semi, tok::l_square, tok::eof)) { Diag(Tok, diag::err_unexpected_tok_after_module_name) << PP.getSpelling(Tok); SkipUntil(tok::semi, SkipUntilFlags::StopBeforeMatch); } + // We don't support any module attributes yet; just parse them and diagnose. ParsedAttributes Attrs(AttrFactory); MaybeParseCXX11Attributes(Attrs); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
