https://github.com/a-tarasyuk created 
https://github.com/llvm/llvm-project/pull/178193

Manual backport of 1d316ef4ba71d843f1e49f86e831dbf8c64ebd83

>From f504552cb76b0564af0519d7d8b5701dab7a12b4 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <[email protected]>
Date: Mon, 26 Jan 2026 14:39:09 +0200
Subject: [PATCH] [Clang] prevent assertion in __has_embed parameter recovery
 at end-of-directive (#175104)

Fixes #175088

---

This PR addresses an assertion failure in the preprocessor triggered
when `__has_embed` parameter parsing reaches end-of-directive while
expecting a parenthesized argument.
---
 clang/docs/ReleaseNotes.rst                            |  1 +
 clang/lib/Lex/PPDirectives.cpp                         |  4 ++--
 .../Preprocessor/embed___has_embed_parsing_errors.c    | 10 ++++++++++
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 52c7c0466e87d..a01339cfb7b57 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -571,6 +571,7 @@ Bug Fixes in This Version
 - Fixed a crash when parsing malformed #pragma clang loop 
vectorize_width(4,8,16)
   by diagnosing invalid comma-separated argument lists. (#GH166325)
 - Clang now treats enumeration constants of fixed-underlying enums as the 
enumerated type. (#GH172118)
+- Fixed a failed assertion in the preprocessor when ``__has_embed`` parameters 
are missing parentheses. (#GH175088)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index d17e253556697..91bcb0af400bd 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -3686,14 +3686,14 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool 
ForHasEmbed) {
           std::pair<tok::TokenKind, SourceLocation> Matches) {
         Diag(CurTok, diag::err_expected) << Expected;
         Diag(Matches.second, diag::note_matching) << Matches.first;
-        if (CurTok.isNot(EndTokenKind))
+        if (CurTok.isNot(tok::eod))
           DiscardUntilEndOfDirective(CurTok);
       };
 
   auto ExpectOrDiagAndSkipToEOD = [&](tok::TokenKind Kind) {
     if (CurTok.isNot(Kind)) {
       Diag(CurTok, diag::err_expected) << Kind;
-      if (CurTok.isNot(EndTokenKind))
+      if (CurTok.isNot(tok::eod))
         DiscardUntilEndOfDirective(CurTok);
       return false;
     }
diff --git a/clang/test/Preprocessor/embed___has_embed_parsing_errors.c 
b/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
index 8ab53f6b89c0d..4c6b03069c518 100644
--- a/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
+++ b/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
@@ -282,3 +282,13 @@
 #if __has_embed (__FILE__ limit(1) foo
 int a = __has_embed (__FILE__);
 #endif
+
+// expected-error@+2 {{expected '('}} \
+   expected-error@+2 {{expected value in expression}}
+#if __has_embed("" if_empty
+#endif
+
+// expected-error@+2 {{expected '('}} \
+   expected-error@+2 {{expected value in expression}}
+#if __has_embed("" limit
+#endif

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to