[clang] [clang][Lex] Fix crash on pragma annotations in preprocessor expressions (PR #232413)
Yihan Wang via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 17 21:37:33 PDT 2026
https://github.com/yronglin updated https://github.com/llvm/llvm-project/pull/232413
>From ac0235919b20bece30fd263879647790949338c8 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Fri, 16 Oct 2026 01:11:07 +0800
Subject: [PATCH 1/2] [clang][Lex] Fix crash on pragma annotations in
preprocessor expressions
Signed-off-by: yronglin <yronglin777 at gmail.com>
---
clang/lib/Lex/PPExpressions.cpp | 5 ++++-
clang/test/Preprocessor/expr-pragma.c | 24 ++++++++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Preprocessor/expr-pragma.c
diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp
index eb614cba959a49..731ee036f72627 100644
--- a/clang/lib/Lex/PPExpressions.cpp
+++ b/clang/lib/Lex/PPExpressions.cpp
@@ -248,7 +248,10 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
// If this token's spelling is a pp-identifier, check to see if it is
// 'defined' or if it is a macro. Note that we check here because many
// keywords are pp-identifiers, so we can't check the kind.
- if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
+ // Pragma operators can produce annotation tokens, which have no
+ // IdentifierInfo and should be diagnosed as invalid expression tokens.
+ if (!PeekTok.isAnnotation() && PeekTok.getIdentifierInfo()) {
+ IdentifierInfo *II = PeekTok.getIdentifierInfo();
// Handle "defined X" and "defined(X)".
if (II->isStr("defined"))
return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
diff --git a/clang/test/Preprocessor/expr-pragma.c b/clang/test/Preprocessor/expr-pragma.c
new file mode 100644
index 00000000000000..a5934a07d66021
--- /dev/null
+++ b/clang/test/Preprocessor/expr-pragma.c
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -fsyntax-only -fms-extensions -verify -verify-ignore-unexpected=note %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -fms-extensions -verify -verify-ignore-unexpected=note %s
+
+// Use -fsyntax-only so pragma handlers can produce annotation tokens.
+#define M _Pragma("pack(push, 1)")
+
+#if (0 + M 1) // expected-error {{invalid token at start of a preprocessor expression}}
+#endif
+
+#if _Pragma("pack(push, 1)") 1 // expected-error {{invalid token at start of a preprocessor expression}}
+#endif
+
+#if 0
+#elif M 1 // expected-error {{invalid token at start of a preprocessor expression}}
+#endif
+
+#if !M 1 // expected-error {{invalid token at start of a preprocessor expression}}
+#endif
+
+#if 0 && M 1 // expected-error {{invalid token at start of a preprocessor expression}}
+#endif
+
+#if (0 + __pragma(pack(push, 1)) 1) // expected-error {{invalid token at start of a preprocessor expression}}
+#endif
>From dbcd60462352761e2f26e49a93c598bb36b7ad9e Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Sun, 18 Oct 2026 12:37:11 +0800
Subject: [PATCH 2/2] Add release notes
Signed-off-by: yronglin <yronglin777 at gmail.com>
---
clang/docs/ReleaseNotes.md | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index aba1dba5854fea..621faf6d62144d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1016,6 +1016,9 @@ features cannot lower the translation-unit ABI level;
#### Miscellaneous Clang Crashes Fixed
+- Fixed a crash preprocessor conditional expression evaluation caused by reading
+ IdentifierInfo from annotation tokens produced by `_Pragma` or `__pragma`.
+ Clang now diagnoses these tokens as invalid expression tokens.
- Fixed a crash when Microsoft extensions were enabled and an unterminated
`__identifier` expression reached the end of a preprocessing directive or
source file. (#GH222310)
More information about the cfe-commits
mailing list