annotate clang/test/Parser/warn-dangling-else.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wdangling-else %s
anatofuz
parents:
diff changeset
2
anatofuz
parents:
diff changeset
3 void f(int a, int b, int c, int d, int e) {
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 // should warn
anatofuz
parents:
diff changeset
6 { if (a) if (b) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}}
anatofuz
parents:
diff changeset
7 { if (a) while (b) if (c) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}}
anatofuz
parents:
diff changeset
8 { if (a) switch (b) if (c) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}}
anatofuz
parents:
diff changeset
9 { if (a) for (;;) if (c) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}}
anatofuz
parents:
diff changeset
10 { if (a) if (b) if (d) d++; else e++; else d--; } // expected-warning {{add explicit braces to avoid dangling else}}
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 if (a)
anatofuz
parents:
diff changeset
13 if (b) {
anatofuz
parents:
diff changeset
14 d++;
anatofuz
parents:
diff changeset
15 } else e++; // expected-warning {{add explicit braces to avoid dangling else}}
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 // shouldn't
anatofuz
parents:
diff changeset
18 { if (a) if (b) d++; }
anatofuz
parents:
diff changeset
19 { if (a) if (b) if (c) d++; }
anatofuz
parents:
diff changeset
20 { if (a) if (b) d++; else e++; else d--; }
anatofuz
parents:
diff changeset
21 { if (a) if (b) if (d) d++; else e++; else d--; else e--; }
anatofuz
parents:
diff changeset
22 { if (a) do if (b) d++; else e++; while (c); }
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 if (a) {
anatofuz
parents:
diff changeset
25 if (b) d++;
anatofuz
parents:
diff changeset
26 else e++;
anatofuz
parents:
diff changeset
27 }
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 if (a) {
anatofuz
parents:
diff changeset
30 if (b) d++;
anatofuz
parents:
diff changeset
31 } else e++;
anatofuz
parents:
diff changeset
32 }
anatofuz
parents:
diff changeset
33
anatofuz
parents:
diff changeset
34 // Somewhat more elaborate case that shouldn't warn.
anatofuz
parents:
diff changeset
35 class A {
anatofuz
parents:
diff changeset
36 public:
anatofuz
parents:
diff changeset
37 void operator<<(const char* s) {}
anatofuz
parents:
diff changeset
38 };
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 void HandleDisabledThing() {}
anatofuz
parents:
diff changeset
41 A GetThing() { return A(); }
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 #define FOO(X) \
anatofuz
parents:
diff changeset
44 switch (0) default: \
anatofuz
parents:
diff changeset
45 if (!(X)) \
anatofuz
parents:
diff changeset
46 HandleDisabledThing(); \
anatofuz
parents:
diff changeset
47 else \
anatofuz
parents:
diff changeset
48 GetThing()
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 void f(bool cond) {
anatofuz
parents:
diff changeset
51 int x = 0;
anatofuz
parents:
diff changeset
52 if (cond)
anatofuz
parents:
diff changeset
53 FOO(x) << "hello"; // no warning
anatofuz
parents:
diff changeset
54 }
anatofuz
parents:
diff changeset
55