[clang] [clang][Lex] Skip to the end of directive after diagnosing 'defined' in an #embed parameter (PR #235934)

Nikolai Fedotenko via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 26 14:46:03 PDT 2026


https://github.com/nfedotenko created https://github.com/llvm/llvm-project/pull/235934

The `EvaluatedDefined` error path in `LexEmbedParameters` returned without 
discarding the rest of the directive, the only error path in the function that didn't. 
With any token following the parameter's closing paren, e.g.

    #embed <f> limit(defined(x)),

`HandleEmbedDirective` hit its "expected success or to be at the end of the directive" 
assertion (a no-assert build would resume lexing mid-directive).

Discard the remaining tokens like the neighboring error paths do, and add coverage 
for both the `#embed` and `__has_embed` forms.

Fixes #212075

>From 63594685d46bc2ed2818fbcc3e6ec5939824b868 Mon Sep 17 00:00:00 2001
From: Nikolay Fedotenko <kalkolay at protonmail.com>
Date: Tue, 27 Oct 2026 01:40:01 +0400
Subject: [PATCH] [clang][Lex] Skip to the end of directive after diagnosing
 'defined' in an #embed parameter

The EvaluatedDefined error path in LexEmbedParameters returned without discarding the rest of the directive, unlike every other error path in the function. With any token following the parameter's closing paren, such as `#embed <f> limit(defined(x)),`, HandleEmbedDirective's assertion that the lexer is at the end of the directive fired (and a no-assert build would try to resume lexing mid-directive).

Discard the remaining tokens like the neighboring error paths do.

Fixes #212075
---
 clang/docs/ReleaseNotes.md                          |  3 +++
 clang/lib/Lex/PPDirectives.cpp                      |  2 ++
 .../Preprocessor/embed___has_embed_parsing_errors.c | 13 +++++++++++++
 clang/test/Preprocessor/embed_parameter_limit.c     |  6 ++++++
 4 files changed, 24 insertions(+)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 200c41fb9e4a29..c5c9f8a8c7f380 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -766,6 +766,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 
 ### Bug Fixes in This Version
 
+- Fixed an assertion failure when a token followed an `#embed` parameter whose
+  expression evaluated `defined`, such as `#embed <f> limit(defined(x)),`.
+  (#GH212075)
 - Fixed incorrect handling of C++ import preprocessing token when a digraph character after import. (#GH190693)
 - Fixed a crash when emitting RTTI for a `dllexport` class, or the fundamental type descriptors for `__cxxabiv1::__fundamental_type_info`, under `-fvisibility=hidden`. (#GH207963)
 - Fixed an assertion failure when passing a wide string literal to `__builtin_nan`. (#GH212108)
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 5d8adf36e755cf..da0098f54bd334 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -3876,6 +3876,8 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
     // expression.
     if (EvaluatedDefined) {
       Diag(CurTok, diag::err_defined_in_pp_embed);
+      if (CurTok.isNot(EndTokenKind))
+        DiscardUntilEndOfDirective(CurTok);
       return std::nullopt;
     }
 
diff --git a/clang/test/Preprocessor/embed___has_embed_parsing_errors.c b/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
index d9f78fa82ecce7..4963755a118fb4 100644
--- a/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
+++ b/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
@@ -303,3 +303,16 @@ int a = __has_embed (__FILE__);
 // expected-error at +1 {{expected "FILENAME" or <FILENAME>}}
 #if __has_embed(__has_embed)
 #endif
+
+// expected-error at +2 {{'defined' cannot appear within this context}} \
+   expected-error at +2 {{expected value in expression}}
+#if __has_embed("media/empty" limit(defined(FOO)))
+#endif
+
+// Used to crash in the '#embed' form; ensure __has_embed recovers here too
+// when a token follows the parameter whose expression evaluated 'defined'
+// (GH212075).
+// expected-error at +2 {{'defined' cannot appear within this context}} \
+   expected-error at +2 {{expected value in expression}}
+#if __has_embed("media/empty" limit(defined(FOO)) prefix())
+#endif
diff --git a/clang/test/Preprocessor/embed_parameter_limit.c b/clang/test/Preprocessor/embed_parameter_limit.c
index da3e4fb877c1b9..bddca49020e448 100644
--- a/clang/test/Preprocessor/embed_parameter_limit.c
+++ b/clang/test/Preprocessor/embed_parameter_limit.c
@@ -92,3 +92,9 @@ const unsigned char a[] = {
 static_assert(
 #embed <jk.txt> limit(defined(FOO)) // expected-error {{'defined' cannot appear within this context}}
   == 0); // expected-error {{expected expression}}
+
+// Used to crash instead of skipping to the end of the directive when a token
+// followed the parameter whose expression evaluated 'defined' (GH212075).
+static_assert(
+#embed <jk.txt> limit(defined(FOO)), // expected-error {{'defined' cannot appear within this context}}
+  == 0); // expected-error {{expected expression}}



More information about the cfe-commits mailing list