[clang] [clang][test] Add test for comma operator rejection in preprocessor conditionals (PR #155570)

NohHyeon Kwon via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 22 07:33:07 PDT 2025


https://github.com/swote-git updated https://github.com/llvm/llvm-project/pull/155570

>From 36f751e7b92d8907bfe60f3c28748519578097c3 Mon Sep 17 00:00:00 2001
From: swote <kst7703 at gmail.com>
Date: Wed, 27 Aug 2025 16:34:13 +0900
Subject: [PATCH 1/3] [clang][test] Add tests for comma operator rejection in
 preprocessor expressions

Add test coverage for comma operator usage in #if preprocessor directives
to ensure it continues to be properly rejected across all C++ standard versions.
Per CWG 1436, comma operators are not among the permitted operators in
preprocessor conditional expressions.

Addresses #132822
---
 clang/test/Preprocessor/cxx_oper_comma.cpp | 26 ++++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 clang/test/Preprocessor/cxx_oper_comma.cpp

diff --git a/clang/test/Preprocessor/cxx_oper_comma.cpp b/clang/test/Preprocessor/cxx_oper_comma.cpp
new file mode 100644
index 0000000000000..5589024ede01c
--- /dev/null
+++ b/clang/test/Preprocessor/cxx_oper_comma.cpp
@@ -0,0 +1,26 @@
+// 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
+// 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

>From 52bc5dbd77f0a8d79aacdad99bb29b2d6c566588 Mon Sep 17 00:00:00 2001
From: swote <kst7703 at gmail.com>
Date: Mon, 22 Sep 2025 23:21:54 +0900
Subject: [PATCH 2/3] Add #elif test and CWG 3017 context

- Add test coverage for comma operator in #elif directive
- Reference CWG 3017 in comments for additional context
- Addresses reviewer feedback
---
 clang/test/Preprocessor/cxx_oper_comma.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/test/Preprocessor/cxx_oper_comma.cpp b/clang/test/Preprocessor/cxx_oper_comma.cpp
index 5589024ede01c..39d020e73ebad 100644
--- a/clang/test/Preprocessor/cxx_oper_comma.cpp
+++ b/clang/test/Preprocessor/cxx_oper_comma.cpp
@@ -10,7 +10,9 @@
 #if 1, 2
 #endif
 
-// Test 2: Comma in conditional expression
+// 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
@@ -24,3 +26,8 @@
 // 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
\ No newline at end of file

>From 1c32551b53cd0695add34865734ff34353fa6520 Mon Sep 17 00:00:00 2001
From: swote <kst7703 at gmail.com>
Date: Mon, 22 Sep 2025 23:32:08 +0900
Subject: [PATCH 3/3] Add test for #if , (leading comma syntax error)

---
 clang/test/Preprocessor/cxx_oper_comma.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang/test/Preprocessor/cxx_oper_comma.cpp b/clang/test/Preprocessor/cxx_oper_comma.cpp
index 39d020e73ebad..6e3a6112a5217 100644
--- a/clang/test/Preprocessor/cxx_oper_comma.cpp
+++ b/clang/test/Preprocessor/cxx_oper_comma.cpp
@@ -30,4 +30,9 @@
 // Test 5: Comma in #elif
 #if 0
 #elif (1, 2) // expected-error {{comma operator in operand of #if}}
+#endif
+
+// Test 6: Leading comma (syntax error)
+// expected-error at +1 {{invalid token at start of a preprocessor expression}}
+#if ,
 #endif
\ No newline at end of file



More information about the cfe-commits mailing list