[clang] Check empty macro name in #pragma push_macro("") or #pragma pop_macro("") (#149982)

Fixes https://github.com/llvm/llvm-project/issues/149762.

---------

Signed-off-by: yronglin <yronglin777@gmail.com>
This commit is contained in:
yronglin 2025-07-22 22:57:34 +08:00 committed by GitHub
parent 25e97fc420
commit 44a6e0099b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 1 deletions

View File

@ -814,6 +814,8 @@ Bug Fixes in This Version
- Fixed a failed assertion with an operator call expression which comes from a
macro expansion when performing analysis for nullability attributes. (#GH138371)
- Fixed a concept equivalent checking crash due to untransformed constraint expressions. (#GH146614)
- Fix a crash when marco name is empty in ``#pragma push_macro("")`` or
``#pragma pop_macro("")``. (GH149762).
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -694,6 +694,9 @@ def err_pragma_push_pop_macro_malformed : Error<
def warn_pragma_pop_macro_no_push : Warning<
"pragma pop_macro could not pop '%0', no matching push_macro">,
InGroup<IgnoredPragmas>;
def warn_pargma_push_pop_macro_empty_string : Warning<
"'#pragma %select{push_macro|pop_macro}0' expected a non-empty string">,
InGroup<IgnoredPragmas>;
def warn_pragma_message : Warning<"%0">,
InGroup<PoundPragmaMessage>, DefaultWarnNoWerror;
def err_pragma_message : Error<"%0">;

View File

@ -591,7 +591,8 @@ IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
}
// Remember the macro string.
std::string StrVal = getSpelling(Tok);
Token StrTok = Tok;
std::string StrVal = getSpelling(StrTok);
// Read the ')'.
Lex(Tok);
@ -604,6 +605,15 @@ IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
"Invalid string token!");
if (StrVal.size() <= 2) {
Diag(StrTok.getLocation(), diag::warn_pargma_push_pop_macro_empty_string)
<< SourceRange(
StrTok.getLocation(),
StrTok.getLocation().getLocWithOffset(StrTok.getLength()))
<< PragmaTok.getIdentifierInfo()->isStr("pop_macro");
return nullptr;
}
// Create a Token from the string.
Token MacroTok;
MacroTok.startToken();

View File

@ -0,0 +1,4 @@
// RUN: %clang_cc1 -fms-extensions %s -fsyntax-only -verify
#pragma push_macro("") // expected-warning {{'#pragma push_macro' expected a non-empty string}}
#pragma pop_macro("") // expected-warning {{'#pragma pop_macro' expected a non-empty string}}

View File

@ -56,3 +56,6 @@ int P;
// CHECK: int pmy2 = 4
// CHECK: int Q;
// CHECK: int P;
#pragma push_macro("")
#pragma pop_macro("")