[clang] [clang][test] Add test for comma operator rejection in preprocessor conditionals (PR #155570)
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 22 07:54:29 PDT 2025
================
@@ -0,0 +1,38 @@
+// 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 -std=c++23
+
+// Test 1: Top-level comma
+// expected-error at +1 {{expected end of line 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}}
+#if 1 ? 1, 0 : 3
+#endif
+
+// Test 3: Parenthesized comma
+// expected-error at +1 {{comma operator in operand of #if}}
+#if (1, 2)
+#endif
+
+// Test 4: Multiple commas
+// expected-error at +1 {{expected end of line 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}}
+#endif
+
----------------
Endilll wrote:
`limit` parameter of `#embed` directive is also evaluated as constant-expression https://eel.is/c++draft/cpp#embed.param.limit-2. I think it's worth adding a test case for this, too.
https://github.com/llvm/llvm-project/pull/155570
More information about the cfe-commits
mailing list