https://github.com/AaronBallman created https://github.com/llvm/llvm-project/pull/135368
If the invalid parameter was not the last parameter given, we would fail to skip to the end of the directive and trip a failed assertion. Fixes #126940 >From f7191e135ef4d2824b63009484571ae37bac2762 Mon Sep 17 00:00:00 2001 From: Aaron Ballman <aa...@aaronballman.com> Date: Fri, 11 Apr 2025 09:55:04 -0400 Subject: [PATCH] Fix failed assertions with invalid #embed parameters If the invalid parameter was not the last parameter given, we would fail to skip to the end of the directive and trip a failed assertion. Fixes #126940 --- clang/docs/ReleaseNotes.rst | 2 ++ clang/lib/Lex/PPDirectives.cpp | 4 +++- clang/test/Preprocessor/embed_parameter_unrecognized.c | 10 +++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 69c7369755c67..11f62bc881b03 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -170,6 +170,8 @@ C23 Feature Support scope. - Fixed a bug where you could not cast a null pointer constant to type ``nullptr_t``. Fixes #GH133644. +- Fixed a failed assertion with an invalid parameter to the ``#embed`` + directive. Fixes #GH126940. C11 Feature Support ^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index d97a6e8d64f9c..dfdba6bd09fd8 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -3897,7 +3897,9 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) { return std::nullopt; } if (!ForHasEmbed) { - Diag(CurTok, diag::err_pp_unknown_parameter) << 1 << Parameter; + Diag(ParamStartLoc, diag::err_pp_unknown_parameter) << 1 << Parameter; + if (CurTok.isNot(tok::eod)) + DiscardUntilEndOfDirective(CurTok); return std::nullopt; } } diff --git a/clang/test/Preprocessor/embed_parameter_unrecognized.c b/clang/test/Preprocessor/embed_parameter_unrecognized.c index b03384341a00a..c6fef941b7b88 100644 --- a/clang/test/Preprocessor/embed_parameter_unrecognized.c +++ b/clang/test/Preprocessor/embed_parameter_unrecognized.c @@ -1,5 +1,4 @@ // RUN: %clang_cc1 %s -std=c23 -E -verify -// okay-no-diagnostics #embed __FILE__ unrecognized // expected-error@-1 {{unknown embed preprocessor parameter 'unrecognized'}} @@ -7,3 +6,12 @@ // expected-error@-1 {{unknown embed preprocessor parameter 'unrecognized::param'}} #embed __FILE__ unrecognized::param(with, args) // expected-error@-1 {{unknown embed preprocessor parameter 'unrecognized::param'}} + +// The first of these two cases was causing a failed assertion due to not being +// at the end of the directive when we finished skipping past `bla`. +#embed __FILE__ bla(2) limit(2) // expected-error {{unknown embed preprocessor parameter 'bla'}} +#embed __FILE__ limit(2) bla(2) // expected-error {{unknown embed preprocessor parameter 'bla'}} + +// We intentionally only tell you about one invalid parameter at a time so that +// we can skip over invalid token soup. +#embed __FILE__ bla(2) limit(2) bork // expected-error {{unknown embed preprocessor parameter 'bla'}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits