[clang] [clang][Preprocessor] Improve error message for comma in #if and #embed (PR #246129)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 03:02:45 PST 2026
https://github.com/nehaGautam07 updated https://github.com/llvm/llvm-project/pull/246129
>From 15efc392bbf61838e3c3b84ad68c87d0578ed55e Mon Sep 17 00:00:00 2001
From: neharaj <neharajgautam07 at gmail.com>
Date: Wed, 25 Nov 2026 19:16:13 +0000
Subject: [PATCH 1/2] [clang] Improve error message for comma in #if and #embed
Report that the comma operator isn't allowed instead of complaining about
missing end of line or ')'.
Fixes #160152
---
clang/docs/ReleaseNotes.md | 4 ++++
clang/include/clang/Basic/DiagnosticLexKinds.td | 2 ++
clang/lib/Lex/PPDirectives.cpp | 8 ++++++++
clang/lib/Lex/PPExpressions.cpp | 6 +++++-
clang/test/Preprocessor/cxx_oper_comma.cpp | 6 +++---
clang/test/Preprocessor/expr_invalid_tok.c | 2 +-
6 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e774ceec4ee56c..bd0f6344eb69ac 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -550,6 +550,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
### Improvements to Clang's diagnostics
+- A comma operator in the controlling expression of `#if` or `#elif`, or in a
+ `#embed` parameter, is now diagnosed as such instead of being reported as a
+ missing end of line or a missing `')'`. (#GH160152)
+
- Errors for invalid element types during instantiation of an `ext_vector_type`
now include a source location, including in alias templates. (#GH229300)
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index a75c7ddd66cb87..0267be7a8d110d 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -625,6 +625,8 @@ def err_pp_expected_value_in_expr : Error<"expected value in expression">;
def err_pp_expected_rparen : Error<"expected ')' in preprocessor expression">;
def err_pp_expected_eol : Error<
"expected end of line in preprocessor expression">;
+def err_pp_comma_in_constant_expr : Error<
+ "comma operator is not allowed in a preprocessor expression">;
def err_pp_expected_after : Error<"missing %1 after %0">;
def err_pp_nested_paren : Error<"nested parentheses not permitted in %0">;
def err_pp_colon_without_question : Error<"':' without preceding '?'">;
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index ecb9e42c88bc0d..14122bdc0b2641 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -3866,6 +3866,14 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
assert(CurTok.is(tok::eod) && "expect to be at the end of directive");
return std::nullopt;
}
+ // The parameter takes a single constant-expression, so a comma here is
+ // never a comma operator; diagnose it directly instead of reporting a
+ // missing ')'.
+ if (CurTok.is(tok::comma)) {
+ Diag(CurTok, diag::err_pp_comma_in_constant_expr);
+ DiscardUntilEndOfDirective(CurTok);
+ return std::nullopt;
+ }
if (!ExpectOrDiagAndSkipToEOD(tok::r_paren))
return std::nullopt;
diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp
index 8ccfe50d20493a..15c11431e46be2 100644
--- a/clang/lib/Lex/PPExpressions.cpp
+++ b/clang/lib/Lex/PPExpressions.cpp
@@ -961,7 +961,11 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierLoc &IfNDefMacro,
// If we aren't at the tok::eod token, something bad happened, like an extra
// ')' token.
if (Tok.isNot(tok::eod)) {
- Diag(Tok, diag::err_pp_expected_eol);
+ // A top-level comma is left unconsumed because the controlling expression
+ // is parsed as a conditional-expression, so point at the comma itself
+ // rather than complaining about a missing end of line.
+ Diag(Tok, Tok.is(tok::comma) ? diag::err_pp_comma_in_constant_expr
+ : diag::err_pp_expected_eol);
DiscardUntilEndOfDirective(Tok);
}
}
diff --git a/clang/test/Preprocessor/cxx_oper_comma.cpp b/clang/test/Preprocessor/cxx_oper_comma.cpp
index cef2a0d905b20d..db4bb6bc285f61 100644
--- a/clang/test/Preprocessor/cxx_oper_comma.cpp
+++ b/clang/test/Preprocessor/cxx_oper_comma.cpp
@@ -7,7 +7,7 @@
// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,since-cxx23 -std=c++2c -Wno-c++26-extensions
// Test 1: Top-level comma
-// expected-error at +1 {{expected end of line in preprocessor expression}}
+// expected-error at +1 {{comma operator is not allowed in a preprocessor expression}}
#if 1, 2
#endif
@@ -24,7 +24,7 @@
#endif
// Test 4: Multiple commas
-// expected-error at +1 {{expected end of line in preprocessor expression}}
+// expected-error at +1 {{comma operator is not allowed in a preprocessor expression}}
#if 1, 2, 3
#endif
@@ -40,6 +40,6 @@
// Test 7: Comma in #embed limit parameter (C++23+)
#if __cplusplus >= 202302L
-// since-cxx23-error at +1 {{expected ')'}}
+// since-cxx23-error at +1 {{comma operator is not allowed in a preprocessor expression}}
#embed "jk.txt" limit(1, 2)
#endif
diff --git a/clang/test/Preprocessor/expr_invalid_tok.c b/clang/test/Preprocessor/expr_invalid_tok.c
index 0b97b255f11b55..abce504590bed0 100644
--- a/clang/test/Preprocessor/expr_invalid_tok.c
+++ b/clang/test/Preprocessor/expr_invalid_tok.c
@@ -11,7 +11,7 @@
// PR2284 - The constant-expr production does not including comma.
-// CHECK: [[@LINE+1]]:14: error: expected end of line in preprocessor expression
+// CHECK: [[@LINE+1]]:14: error: comma operator is not allowed in a preprocessor expression
#if 1 ? 2 : 0, 1
#endif
>From 4d3022885ab8ef1a51f746aa87c59583ce6adb28 Mon Sep 17 00:00:00 2001
From: neharaj <neharajgautam07 at gmail.com>
Date: Thu, 26 Nov 2026 11:02:10 +0000
Subject: [PATCH 2/2] Address review feedback: update diagnostic wording,
comment, and tests
---
clang/docs/ReleaseNotes.md | 6 ++--
.../include/clang/Basic/DiagnosticLexKinds.td | 2 +-
clang/lib/Lex/PPExpressions.cpp | 9 +++---
clang/test/Preprocessor/cxx_oper_comma.cpp | 31 +++++++++++--------
clang/test/Preprocessor/expr_invalid_tok.c | 2 +-
5 files changed, 27 insertions(+), 23 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index bd0f6344eb69ac..d11ed90de700f6 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -550,9 +550,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
### Improvements to Clang's diagnostics
-- A comma operator in the controlling expression of `#if` or `#elif`, or in a
- `#embed` parameter, is now diagnosed as such instead of being reported as a
- missing end of line or a missing `')'`. (#GH160152)
+- An unexpected comma in the controlling expression of `#if` or `#elif`, or in
+ a `#embed` parameter, is now diagnosed directly instead of being reported as
+ a missing end of line or a missing `')'`. (#GH160152)
- Errors for invalid element types during instantiation of an `ext_vector_type`
now include a source location, including in alias templates. (#GH229300)
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 0267be7a8d110d..83715b9c69be64 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -626,7 +626,7 @@ def err_pp_expected_rparen : Error<"expected ')' in preprocessor expression">;
def err_pp_expected_eol : Error<
"expected end of line in preprocessor expression">;
def err_pp_comma_in_constant_expr : Error<
- "comma operator is not allowed in a preprocessor expression">;
+ "unexpected comma in preprocessor expression">;
def err_pp_expected_after : Error<"missing %1 after %0">;
def err_pp_nested_paren : Error<"nested parentheses not permitted in %0">;
def err_pp_colon_without_question : Error<"':' without preceding '?'">;
diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp
index 15c11431e46be2..accdb9d4be7ddf 100644
--- a/clang/lib/Lex/PPExpressions.cpp
+++ b/clang/lib/Lex/PPExpressions.cpp
@@ -944,7 +944,9 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierLoc &IfNDefMacro,
}
// Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
- // operator and the stuff after it.
+ // operator and the stuff after it. Use the precedence of '?' so that commas
+ // are not consumed here and can be diagnosed below (or by callers such as
+ // #embed parameter parsing).
if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
Tok, true, DT.IncludedUndefinedIds, *this)) {
// Parse error, skip the rest of the macro line.
@@ -959,11 +961,8 @@ Preprocessor::EvaluateDirectiveExpression(IdentifierLoc &IfNDefMacro,
if (CheckForEoD) {
// If we aren't at the tok::eod token, something bad happened, like an extra
- // ')' token.
+ // ')' token or an unexpected comma.
if (Tok.isNot(tok::eod)) {
- // A top-level comma is left unconsumed because the controlling expression
- // is parsed as a conditional-expression, so point at the comma itself
- // rather than complaining about a missing end of line.
Diag(Tok, Tok.is(tok::comma) ? diag::err_pp_comma_in_constant_expr
: diag::err_pp_expected_eol);
DiscardUntilEndOfDirective(Tok);
diff --git a/clang/test/Preprocessor/cxx_oper_comma.cpp b/clang/test/Preprocessor/cxx_oper_comma.cpp
index db4bb6bc285f61..585bd94bb27125 100644
--- a/clang/test/Preprocessor/cxx_oper_comma.cpp
+++ b/clang/test/Preprocessor/cxx_oper_comma.cpp
@@ -1,36 +1,41 @@
-// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++98
-// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++11
-// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++14
-// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++17
-// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++20
-// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,since-cxx23 -std=c++23 -Wno-c++26-extensions
-// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,since-cxx23 -std=c++2c -Wno-c++26-extensions
+// RUN: %clang_cc1 -E %s -verify -std=c++98
+// RUN: %clang_cc1 -E %s -verify=expected,since-cxx23 -std=c++23 -Wno-c++26-extensions
+// RUN: %clang_cc1 -E -pedantic %s -verify=expected,ped-warn -std=c++98
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,ped-err -std=c++98
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,ped-err -std=c++11
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,ped-err -std=c++14
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,ped-err -std=c++17
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,ped-err -std=c++20
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,ped-err,since-cxx23 -std=c++23 -Wno-c++26-extensions
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,ped-err,since-cxx23 -std=c++2c -Wno-c++26-extensions
// Test 1: Top-level comma
-// expected-error at +1 {{comma operator is not allowed in a preprocessor expression}}
+// expected-error at +1 {{unexpected comma in preprocessor expression}}
#if 1, 2
#endif
// Test 2: Comma in conditional expression(CWG3017)
// Per CWG 3017, this exact case highlights the specification gap
// where C++ lacks explicit prohibition of comma operators in #if
-// expected-error at +1 {{comma operator in operand of #if}}
+// ped-warn-warning at +2 {{comma operator in operand of #if}}
+// ped-err-error at +1 {{comma operator in operand of #if}}
#if 1 ? 1, 0 : 3
#endif
// Test 3: Parenthesized comma
-// expected-error at +1 {{comma operator in operand of #if}}
+// ped-warn-warning at +2 {{comma operator in operand of #if}}
+// ped-err-error at +1 {{comma operator in operand of #if}}
#if (1, 2)
#endif
// Test 4: Multiple commas
-// expected-error at +1 {{comma operator is not allowed in a preprocessor expression}}
+// expected-error at +1 {{unexpected comma in preprocessor expression}}
#if 1, 2, 3
#endif
// Test 5: Comma in #elif
#if 0
-#elif (1, 2) // expected-error {{comma operator in operand of #if}}
+#elif (1, 2) // ped-warn-warning {{comma operator in operand of #if}} ped-err-error {{comma operator in operand of #if}}
#endif
// Test 6: Leading comma (syntax error)
@@ -40,6 +45,6 @@
// Test 7: Comma in #embed limit parameter (C++23+)
#if __cplusplus >= 202302L
-// since-cxx23-error at +1 {{comma operator is not allowed in a preprocessor expression}}
+// since-cxx23-error at +1 {{unexpected comma in preprocessor expression}}
#embed "jk.txt" limit(1, 2)
#endif
diff --git a/clang/test/Preprocessor/expr_invalid_tok.c b/clang/test/Preprocessor/expr_invalid_tok.c
index abce504590bed0..909177d48b6c03 100644
--- a/clang/test/Preprocessor/expr_invalid_tok.c
+++ b/clang/test/Preprocessor/expr_invalid_tok.c
@@ -11,7 +11,7 @@
// PR2284 - The constant-expr production does not including comma.
-// CHECK: [[@LINE+1]]:14: error: comma operator is not allowed in a preprocessor expression
+// CHECK: [[@LINE+1]]:14: error: unexpected comma in preprocessor expression
#if 1 ? 2 : 0, 1
#endif
More information about the cfe-commits
mailing list