[clang] [clang][Lex] Fix crash on pragma annotations (PR #238342)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 05:50:24 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Pranjal Srivastava (ipranjalks)

<details>
<summary>Changes</summary>

`PragmaWarningHandler` and `PragmaExecCharsetHandler` lex their arguments with `PP.Lex(Tok)`, which expands macros and `_Pragma` / `_pragma` operators. When an expanded pragma operator produces an annotation token (such as `tok::annot_pragma_pack`), calling `Tok.getIdentifierInfo()` triggers an assertion failure (`!isAnnotation() && "getIdentifierInfo() on an annotation token!"`).

Guard `Tok.getIdentifierInfo()` with `Tok.isAnnotation()` so Clang emits the existing invalid-specifier warning instead of crashing.

Assisted-by: Gemini

---
Full diff: https://github.com/llvm/llvm-project/pull/238342.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+2) 
- (modified) clang/lib/Lex/Pragma.cpp (+3-3) 
- (modified) clang/test/Preprocessor/pragma_microsoft.c (+14) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 743c1ead8ec78..e7ca82763558e 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1189,6 +1189,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 
 #### Miscellaneous Clang Crashes Fixed
 
+- Fixed a crash in `#pragma warning` and `#pragma execution_character_set` when
+  containing an annotation-producing `_Pragma` or `__pragma`.
 - Fixed a crash in preprocessor conditionals containing `_Pragma`.
 - Fixed a crash when Microsoft extensions were enabled and an unterminated
   `__identifier` expression reached the end of a preprocessing directive or
diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index 078842ab08c5e..f088cdc4765d7 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -1453,7 +1453,7 @@ struct PragmaWarningHandler : public PragmaHandler {
     }
 
     PP.Lex(Tok);
-    IdentifierInfo *II = Tok.getIdentifierInfo();
+    IdentifierInfo *II = Tok.isAnnotation() ? nullptr : Tok.getIdentifierInfo();
 
     if (II && II->isStr("push")) {
       // #pragma warning( push[ ,n ] )
@@ -1484,7 +1484,7 @@ struct PragmaWarningHandler : public PragmaHandler {
       // #pragma warning( warning-specifier : warning-number-list
       //                  [; warning-specifier : warning-number-list...] )
       while (true) {
-        II = Tok.getIdentifierInfo();
+        II = Tok.isAnnotation() ? nullptr : Tok.getIdentifierInfo();
         if (!II && !Tok.is(tok::numeric_constant)) {
           PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
           return;
@@ -1601,7 +1601,7 @@ struct PragmaExecCharsetHandler : public PragmaHandler {
     }
 
     PP.Lex(Tok);
-    IdentifierInfo *II = Tok.getIdentifierInfo();
+    IdentifierInfo *II = Tok.isAnnotation() ? nullptr : Tok.getIdentifierInfo();
 
     if (II && II->isStr("push")) {
       // #pragma execution_character_set( push[ , string ] )
diff --git a/clang/test/Preprocessor/pragma_microsoft.c b/clang/test/Preprocessor/pragma_microsoft.c
index 0140672d9cb5e..1f2519bf56458 100644
--- a/clang/test/Preprocessor/pragma_microsoft.c
+++ b/clang/test/Preprocessor/pragma_microsoft.c
@@ -256,3 +256,17 @@ void pragma_optimize_foo() {
 #pragma execution_character_set(push, "utf-8")
 #pragma execution_character_set(push, "UTF-8")
 #pragma execution_character_set(pop)
+
+#define PRAGMA_PACK _Pragma("pack(push, 1)")
+#pragma warning(PRAGMA_PACK) // expected-warning {{expected 'push', 'pop', 'default', 'disable', 'error', 'once', 'suppress', 1, 2, 3, or 4}} \
+                             // expected-error {{expected identifier or '('}}
+;
+#pragma warning(disable : 4705 ; PRAGMA_PACK) // expected-warning {{expected 'push', 'pop', 'default', 'disable', 'error', 'once', 'suppress', 1, 2, 3, or 4}} \
+                                              // expected-error {{expected identifier or '('}}
+;
+#pragma execution_character_set(PRAGMA_PACK) // expected-warning {{expected 'push' or 'pop'}} \
+                                             // expected-error {{expected identifier or '('}}
+;
+__pragma(warning(_Pragma("pack(push, 1)"))) // expected-warning {{expected 'push', 'pop', 'default', 'disable', 'error', 'once', 'suppress', 1, 2, 3, or 4}}
+__pragma(warning(disable : 4705 ; _Pragma("pack(push, 1)"))) // expected-warning {{expected 'push', 'pop', 'default', 'disable', 'error', 'once', 'suppress', 1, 2, 3, or 4}}
+__pragma(execution_character_set(_Pragma("pack(push, 1)"))) // expected-warning {{expected 'push' or 'pop'}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/238342


More information about the cfe-commits mailing list