[clang] 5236af5 - [clang][Lex] Avoid discarding past EOD in __has_embed parameters (#237233)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 2 02:05:09 PST 2026
Author: Chen Miao
Date: 2026-11-02T11:05:03+01:00
New Revision: 5236af5727170cc1bf62d5bfc1de05c4d27f28aa
URL: https://github.com/llvm/llvm-project/commit/5236af5727170cc1bf62d5bfc1de05c4d27f28aa
DIFF: https://github.com/llvm/llvm-project/commit/5236af5727170cc1bf62d5bfc1de05c4d27f28aa.diff
LOG: [clang][Lex] Avoid discarding past EOD in __has_embed parameters (#237233)
When an invalid integer parameter in `__has_embed` reaches the end of a
directive, `LexEmbedParameters` may try to discard more tokens after
consuming EOD. This can consume the following directive and assert at
EOF.
Skip discarding when already at EOD, while retaining recovery when
tokens remain.
Fixes #236416
Added:
Modified:
clang/docs/ReleaseNotes.md
clang/lib/Lex/PPDirectives.cpp
clang/test/Preprocessor/embed___has_embed_parsing_errors.c
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e769ad6a24e6bf..6d1dbbbb6eb77b 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -775,6 +775,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed an assertion failure when a token followed an `#embed` parameter whose
expression evaluated `defined`, such as `#embed <f> limit(defined(x)),`.
(#GH212075)
+- Fixed an assertion failure while recovering from invalid `__has_embed` parameters at the end of a preprocessing directive. (#GH236416)
- 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 da0098f54bd334..5ff165e3ddf81f 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -3876,7 +3876,7 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
// expression.
if (EvaluatedDefined) {
Diag(CurTok, diag::err_defined_in_pp_embed);
- if (CurTok.isNot(EndTokenKind))
+ if (!CurTok.isOneOf(EndTokenKind, tok::eod))
DiscardUntilEndOfDirective(CurTok);
return std::nullopt;
}
@@ -3886,7 +3886,7 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
if (Result.isNegative()) {
Diag(CurTok, diag::err_requires_positive_value)
<< toString(Result, 10) << /*positive*/ 0;
- if (CurTok.isNot(EndTokenKind))
+ if (!CurTok.isOneOf(EndTokenKind, tok::eod))
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 4963755a118fb4..7feda949f21993 100644
--- a/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
+++ b/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
@@ -243,6 +243,17 @@
#if __has_embed (__FILE__ limit(-1))
#endif
+// An invalid parameter at eod must not discard the following #endif.
+// expected-error at +2 {{invalid value '-1'; must be positive}} \
+ expected-error at +2 {{expected value in expression}}
+#if __has_embed(__FILE__ limit(-1)
+#endif
+
+// expected-error at +2 {{'defined' cannot appear within this context}} \
+ expected-error at +2 {{expected value in expression}}
+#if __has_embed(__FILE__ limit(defined(X))
+#endif
+
// expected-error at +2 {{invalid value '-100000000000000000'; must be positive}}\
expected-error at +2 {{expected value in expression}}
#if __has_embed (__FILE__ limit(-100000000000000000)) != __STDC_EMBED_NOT_FOUND__
More information about the cfe-commits
mailing list