Skip to content

Commit 5a01d26

Browse files
swote-gitLukacma
authored andcommitted
[clang][test] Add test for comma operator rejection in preprocessor conditionals (llvm#155570)
Add test coverage to ensure comma operators remain properly rejected in `#if` directives. Per CWG 1436, comma is not among the permitted operators in preprocessor conditional expressions. This test prevents regressions and clarifies the intended behavior. Fixes llvm#132822
1 parent 39831a6 commit 5a01d26

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++98
2+
// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++11
3+
// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++14
4+
// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++17
5+
// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++20
6+
// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,since-cxx23 -std=c++23 -Wno-c23-extensions
7+
// RUN: %clang_cc1 -E -pedantic-errors %s -verify=expected,since-cxx23 -std=c++2c -Wno-c23-extensions
8+
9+
// Test 1: Top-level comma
10+
// expected-error@+1 {{expected end of line in preprocessor expression}}
11+
#if 1, 2
12+
#endif
13+
14+
// Test 2: Comma in conditional expression(CWG3017)
15+
// Per CWG 3017, this exact case highlights the specification gap
16+
// where C++ lacks explicit prohibition of comma operators in #if
17+
// expected-error@+1 {{comma operator in operand of #if}}
18+
#if 1 ? 1, 0 : 3
19+
#endif
20+
21+
// Test 3: Parenthesized comma
22+
// expected-error@+1 {{comma operator in operand of #if}}
23+
#if (1, 2)
24+
#endif
25+
26+
// Test 4: Multiple commas
27+
// expected-error@+1 {{expected end of line in preprocessor expression}}
28+
#if 1, 2, 3
29+
#endif
30+
31+
// Test 5: Comma in #elif
32+
#if 0
33+
#elif (1, 2) // expected-error {{comma operator in operand of #if}}
34+
#endif
35+
36+
// Test 6: Leading comma (syntax error)
37+
// expected-error@+1 {{invalid token at start of a preprocessor expression}}
38+
#if ,
39+
#endif
40+
41+
// Test 7: Comma in #embed limit parameter (C++23+)
42+
#if __cplusplus >= 202302L
43+
// since-cxx23-error@+1 {{expected ')'}}
44+
#embed "jk.txt" limit(1, 2)
45+
#endif

0 commit comments

Comments
 (0)