xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/warn-unused-value.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wunused-value -Wunused-label %s
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wunused %s
3*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wall %s
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc int i = 0;
6f4a2713aSLionel Sambuc int j = 0;
7f4a2713aSLionel Sambuc 
8f4a2713aSLionel Sambuc void foo();
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc // PR4806
pr4806()11f4a2713aSLionel Sambuc void pr4806() {
12f4a2713aSLionel Sambuc   1,foo();          // expected-warning {{expression result unused}}
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc   // other
15f4a2713aSLionel Sambuc   foo();
16f4a2713aSLionel Sambuc   i;                // expected-warning {{expression result unused}}
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc   i,foo();          // expected-warning {{expression result unused}}
19f4a2713aSLionel Sambuc   foo(),i;          // expected-warning {{expression result unused}}
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc   i,j,foo();        // expected-warning {{expression result unused}} expected-warning {{expression result unused}}
22f4a2713aSLionel Sambuc   i,foo(),j;        // expected-warning {{expression result unused}} expected-warning {{expression result unused}}
23f4a2713aSLionel Sambuc   foo(),i,j;        // expected-warning {{expression result unused}} expected-warning {{expression result unused}}
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc   i++;
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc   i++,foo();
28f4a2713aSLionel Sambuc   foo(),i++;
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc   i++,j,foo();      // expected-warning {{expression result unused}}
31f4a2713aSLionel Sambuc   i++,foo(),j;      // expected-warning {{expression result unused}}
32f4a2713aSLionel Sambuc   foo(),i++,j;      // expected-warning {{expression result unused}}
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc   i,j++,foo();      // expected-warning {{expression result unused}}
35f4a2713aSLionel Sambuc   i,foo(),j++;      // expected-warning {{expression result unused}}
36f4a2713aSLionel Sambuc   foo(),i,j++;      // expected-warning {{expression result unused}}
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc   i++,j++,foo();
39f4a2713aSLionel Sambuc   i++,foo(),j++;
40f4a2713aSLionel Sambuc   foo(),i++,j++;
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc   {};
43f4a2713aSLionel Sambuc   ({});
44f4a2713aSLionel Sambuc   ({}),foo();
45f4a2713aSLionel Sambuc   foo(),({});
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc   (int)1U;          // expected-warning {{expression result unused}}
48f4a2713aSLionel Sambuc   (void)1U;
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc   // pointer to volatile has side effect (thus no warning)
51f4a2713aSLionel Sambuc   int* pi = &i;
52f4a2713aSLionel Sambuc   volatile int* pj = &j;
53f4a2713aSLionel Sambuc   *pi;              // expected-warning {{expression result unused}}
54f4a2713aSLionel Sambuc   *pj;
55f4a2713aSLionel Sambuc 
56f4a2713aSLionel Sambuc   foo_label:        // expected-warning {{unused label}}
57f4a2713aSLionel Sambuc   i;                // expected-warning {{expression result unused}}
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc // Don't warn about unused '||', '&&' expressions that contain assignments.
61f4a2713aSLionel Sambuc int test_logical_foo1();
62f4a2713aSLionel Sambuc int test_logical_foo2();
63f4a2713aSLionel Sambuc int test_logical_foo3();
test_logical_bar()64f4a2713aSLionel Sambuc int test_logical_bar() {
65f4a2713aSLionel Sambuc   int x = 0;
66f4a2713aSLionel Sambuc   (x = test_logical_foo1()) ||  // no-warning
67f4a2713aSLionel Sambuc   (x = test_logical_foo2()) ||  // no-warning
68f4a2713aSLionel Sambuc   (x = test_logical_foo3());    // no-warning
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc   x || test_logical_foo1();     // no-warning
71f4a2713aSLionel Sambuc 
72f4a2713aSLionel Sambuc   return x;
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc // PR8282
conditional_for_control_flow(int cond,int x,int y)76f4a2713aSLionel Sambuc void conditional_for_control_flow(int cond, int x, int y)
77f4a2713aSLionel Sambuc {
78f4a2713aSLionel Sambuc     cond? y++ : x; // no-warning
79f4a2713aSLionel Sambuc     cond? y : ++x; // no-warning
80f4a2713aSLionel Sambuc     cond? (x |= y) : ++x; // no-warning
81f4a2713aSLionel Sambuc     cond? y : x; // expected-warning {{expression result unused}}
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc struct s0 { int f0; };
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc void f0(int a);
f1(struct s0 * a)87f4a2713aSLionel Sambuc void f1(struct s0 *a) {
88f4a2713aSLionel Sambuc   // rdar://8139785
89f4a2713aSLionel Sambuc   f0((int)(a->f0 + 1, 10)); // expected-warning {{expression result unused}}
90f4a2713aSLionel Sambuc }
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc void blah(int a);
93*0a6a1f1dSLionel Sambuc #define GenTest(x) _Generic(x, default : blah)(x)
94*0a6a1f1dSLionel Sambuc 
unevaluated_operands(void)95*0a6a1f1dSLionel Sambuc void unevaluated_operands(void) {
96*0a6a1f1dSLionel Sambuc   int val = 0;
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc   (void)sizeof(++val); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
99*0a6a1f1dSLionel Sambuc   (void)_Generic(val++, default : 0); // expected-warning {{expression with side effects has no effect in an unevaluated context}}
100*0a6a1f1dSLionel Sambuc   (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}}
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc   // VLAs can have side effects so long as it's part of the type and not
103*0a6a1f1dSLionel Sambuc   // an expression.
104*0a6a1f1dSLionel Sambuc   (void)sizeof(int[++val]); // Ok
105*0a6a1f1dSLionel Sambuc   (void)_Alignof(int[++val]); // Ok
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc   // Side effects as part of macro expansion are ok.
108*0a6a1f1dSLionel Sambuc   GenTest(val++);
109*0a6a1f1dSLionel Sambuc }
110