[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / Sema / warn-unused-value.c
blob9e353ef1d14c4e30d8be605a939858e4903709d1
1 // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wunused-value -Wunused-label %s
2 // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wunused %s
3 // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wall %s
5 int i = 0;
6 int j = 0;
8 void foo(void);
10 // PR4806
11 void pr4806(void) {
12 1,foo(); // expected-warning {{left operand of comma operator has no effect}}
14 // other
15 foo();
16 i; // expected-warning {{expression result unused}}
18 i,foo(); // expected-warning {{left operand of comma operator has no effect}}
19 foo(),i; // expected-warning {{expression result unused}}
21 i,j,foo(); // expected-warning 2{{left operand of comma operator has no effect}}
22 i,foo(),j; // expected-warning {{left operand of comma operator has no effect}} expected-warning {{expression result unused}}
23 foo(),i,j; // expected-warning {{expression result unused}} expected-warning {{left operand of comma operator has no effect}}
25 i++;
27 i++,foo();
28 foo(),i++;
30 i++,j,foo(); // expected-warning {{left operand of comma operator has no effect}}
31 i++,foo(),j; // expected-warning {{expression result unused}}
32 foo(),i++,j; // expected-warning {{expression result unused}}
34 i,j++,foo(); // expected-warning {{left operand of comma operator has no effect}}
35 i,foo(),j++; // expected-warning {{left operand of comma operator has no effect}}
36 foo(),i,j++; // expected-warning {{left operand of comma operator has no effect}}
38 i++,j++,foo();
39 i++,foo(),j++;
40 foo(),i++,j++;
42 {};
43 ({});
44 ({}),foo();
45 foo(),({});
47 (int)1U; // expected-warning {{expression result unused}}
48 (void)1U;
50 // pointer to volatile has side effect (thus no warning)
51 int* pi = &i;
52 volatile int* pj = &j;
53 *pi; // expected-warning {{expression result unused}}
54 *pj;
56 foo_label: // expected-warning {{unused label}}
57 i; // expected-warning {{expression result unused}}
60 // Don't warn about unused '||', '&&' expressions that contain assignments.
61 int test_logical_foo1(void);
62 int test_logical_foo2(void);
63 int test_logical_foo3(void);
64 int test_logical_bar(void) {
65 int x = 0;
66 (x = test_logical_foo1()) || // no-warning
67 (x = test_logical_foo2()) || // no-warning
68 (x = test_logical_foo3()); // no-warning
70 x || test_logical_foo1(); // no-warning
72 return x;
75 // PR8282
76 void conditional_for_control_flow(int cond, int x, int y)
78 cond? y++ : x; // no-warning
79 cond? y : ++x; // no-warning
80 cond? (x |= y) : ++x; // no-warning
81 cond? y : x; // expected-warning {{expression result unused}}
84 struct s0 { int f0; };
86 void f0(int a);
87 void f1(struct s0 *a) {
88 // rdar://8139785
89 f0((int)(a->f0 + 1, 10)); // expected-warning {{left operand of comma operator has no effect}}
92 void blah(int a);
93 #define GenTest(x) _Generic(x, default : blah)(x)
95 void unevaluated_operands(void) {
96 int val = 0;
98 (void)sizeof(++val); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
99 (void)_Generic(val++, default : 0); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
100 (void)_Alignof(val++); // expected-warning {{expression with side effects has no effect in an unevaluated context}} expected-warning {{'_Alignof' applied to an expression is a GNU extension}}
102 // VLAs can have side effects so long as it's part of the type and not an
103 // expression, except for sizeof() where it can also have a side effect if
104 // the operand is of VLA type.
105 (void)sizeof(int[++val]); // Ok
106 (void)_Alignof(int[++val]); // Ok
108 int GH48010[val];
109 (void)sizeof(*(val = 1, &GH48010)); // Ok
111 // Side effects as part of macro expansion are ok.
112 GenTest(val++);