xref: /minix3/external/bsd/llvm/dist/clang/test/Parser/warn-dangling-else.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wdangling-else %s
2*f4a2713aSLionel Sambuc 
f(int a,int b,int c,int d,int e)3*f4a2713aSLionel Sambuc void f(int a, int b, int c, int d, int e) {
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc   // should warn
6*f4a2713aSLionel Sambuc   { if (a) if (b) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}}
7*f4a2713aSLionel Sambuc   { if (a) while (b) if (c) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}}
8*f4a2713aSLionel Sambuc   { if (a) switch (b) if (c) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}}
9*f4a2713aSLionel Sambuc   { if (a) for (;;) if (c) d++; else e++; } // expected-warning {{add explicit braces to avoid dangling else}}
10*f4a2713aSLionel Sambuc   { if (a) if (b) if (d) d++; else e++; else d--; } // expected-warning {{add explicit braces to avoid dangling else}}
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc   if (a)
13*f4a2713aSLionel Sambuc     if (b) {
14*f4a2713aSLionel Sambuc       d++;
15*f4a2713aSLionel Sambuc     } else e++; // expected-warning {{add explicit braces to avoid dangling else}}
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc   // shouldn't
18*f4a2713aSLionel Sambuc   { if (a) if (b) d++; }
19*f4a2713aSLionel Sambuc   { if (a) if (b) if (c) d++; }
20*f4a2713aSLionel Sambuc   { if (a) if (b) d++; else e++; else d--; }
21*f4a2713aSLionel Sambuc   { if (a) if (b) if (d) d++; else e++; else d--; else e--; }
22*f4a2713aSLionel Sambuc   { if (a) do if (b) d++; else e++; while (c); }
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc   if (a) {
25*f4a2713aSLionel Sambuc     if (b) d++;
26*f4a2713aSLionel Sambuc     else e++;
27*f4a2713aSLionel Sambuc   }
28*f4a2713aSLionel Sambuc 
29*f4a2713aSLionel Sambuc   if (a) {
30*f4a2713aSLionel Sambuc     if (b) d++;
31*f4a2713aSLionel Sambuc   } else e++;
32*f4a2713aSLionel Sambuc }
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc // Somewhat more elaborate case that shouldn't warn.
35*f4a2713aSLionel Sambuc class A {
36*f4a2713aSLionel Sambuc  public:
operator <<(const char * s)37*f4a2713aSLionel Sambuc   void operator<<(const char* s) {}
38*f4a2713aSLionel Sambuc };
39*f4a2713aSLionel Sambuc 
HandleDisabledThing()40*f4a2713aSLionel Sambuc void HandleDisabledThing() {}
GetThing()41*f4a2713aSLionel Sambuc A GetThing() { return A(); }
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc #define FOO(X) \
44*f4a2713aSLionel Sambuc    switch (0) default: \
45*f4a2713aSLionel Sambuc      if (!(X)) \
46*f4a2713aSLionel Sambuc        HandleDisabledThing(); \
47*f4a2713aSLionel Sambuc      else \
48*f4a2713aSLionel Sambuc        GetThing()
49*f4a2713aSLionel Sambuc 
f(bool cond)50*f4a2713aSLionel Sambuc void f(bool cond) {
51*f4a2713aSLionel Sambuc   int x = 0;
52*f4a2713aSLionel Sambuc   if (cond)
53*f4a2713aSLionel Sambuc     FOO(x) << "hello"; // no warning
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc 
56