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