Manuel Klimek | b91bee0 | 2015-10-22 11:31:44 +0000 | [diff] [blame] | 1 | // RUN: %check_clang_tidy %s readability-braces-around-statements %t |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 2 | |
| 3 | void do_something(const char *) {} |
| 4 | |
| 5 | bool cond(const char *) { |
| 6 | return false; |
| 7 | } |
| 8 | |
| 9 | #define EMPTY_MACRO |
| 10 | #define EMPTY_MACRO_FUN() |
| 11 | |
| 12 | void test() { |
Alexander Kornienko | f305000 | 2014-10-13 12:46:22 +0000 | [diff] [blame] | 13 | if (cond("if0") /*comment*/) do_something("same-line"); |
| 14 | // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: statement should be inside braces |
| 15 | // CHECK-FIXES: if (cond("if0") /*comment*/) { do_something("same-line"); |
| 16 | // CHECK-FIXES: } |
| 17 | |
| 18 | if (cond("if0.1")) |
| 19 | do_something("single-line"); |
| 20 | // CHECK-MESSAGES: :[[@LINE-2]]:21: warning: statement should be inside braces |
| 21 | // CHECK-FIXES: if (cond("if0.1")) { |
| 22 | // CHECK-FIXES: } |
| 23 | |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 24 | if (cond("if1") /*comment*/) |
| 25 | // some comment |
| 26 | do_something("if1"); |
| 27 | // CHECK-MESSAGES: :[[@LINE-3]]:31: warning: statement should be inside braces |
| 28 | // CHECK-FIXES: if (cond("if1") /*comment*/) { |
| 29 | // CHECK-FIXES: } |
| 30 | if (cond("if2")) { |
| 31 | do_something("if2"); |
| 32 | } |
| 33 | if (cond("if3")) |
| 34 | ; |
| 35 | // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: statement should be inside braces |
| 36 | // CHECK-FIXES: if (cond("if3")) { |
| 37 | // CHECK-FIXES: } |
| 38 | |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 39 | if (cond("if-else1")) |
| 40 | do_something("if-else1"); |
| 41 | // CHECK-MESSAGES: :[[@LINE-2]]:24: warning: statement should be inside braces |
| 42 | // CHECK-FIXES: if (cond("if-else1")) { |
| 43 | // CHECK-FIXES: } else { |
| 44 | else |
| 45 | do_something("if-else1 else"); |
| 46 | // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: statement should be inside braces |
| 47 | // CHECK-FIXES: } |
| 48 | if (cond("if-else2")) { |
| 49 | do_something("if-else2"); |
| 50 | } else { |
| 51 | do_something("if-else2 else"); |
| 52 | } |
| 53 | |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 54 | if (cond("if-else if-else1")) |
| 55 | do_something("if"); |
| 56 | // CHECK-MESSAGES: :[[@LINE-2]]:32: warning: statement should be inside braces |
| 57 | // CHECK-FIXES: } else if (cond("else if1")) { |
| 58 | else if (cond("else if1")) |
| 59 | do_something("else if"); |
| 60 | // CHECK-MESSAGES: :[[@LINE-2]]:29: warning: statement should be inside braces |
| 61 | else |
| 62 | do_something("else"); |
| 63 | // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: statement should be inside braces |
| 64 | // CHECK-FIXES: } |
| 65 | if (cond("if-else if-else2")) { |
| 66 | do_something("if"); |
| 67 | } else if (cond("else if2")) { |
| 68 | do_something("else if"); |
| 69 | } else { |
| 70 | do_something("else"); |
| 71 | } |
| 72 | |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 73 | for (;;) |
| 74 | do_something("for"); |
| 75 | // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: statement should be inside braces |
| 76 | // CHECK-FIXES: for (;;) { |
| 77 | // CHECK-FIXES: } |
| 78 | for (;;) { |
| 79 | do_something("for"); |
| 80 | } |
| 81 | for (;;) |
| 82 | ; |
| 83 | // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: statement should be inside braces |
| 84 | // CHECK-FIXES: for (;;) { |
| 85 | // CHECK-FIXES: } |
| 86 | |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 87 | int arr[4] = {1, 2, 3, 4}; |
| 88 | for (int a : arr) |
| 89 | do_something("for-range"); |
| 90 | // CHECK-MESSAGES: :[[@LINE-2]]:20: warning: statement should be inside braces |
| 91 | // CHECK-FIXES: for (int a : arr) { |
| 92 | // CHECK-FIXES: } |
| 93 | for (int a : arr) { |
| 94 | do_something("for-range"); |
| 95 | } |
| 96 | for (int a : arr) |
| 97 | ; |
| 98 | // CHECK-MESSAGES: :[[@LINE-2]]:20: warning: statement should be inside braces |
| 99 | // CHECK-FIXES: for (int a : arr) { |
| 100 | // CHECK-FIXES: } |
| 101 | |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 102 | while (cond("while1")) |
| 103 | do_something("while"); |
| 104 | // CHECK-MESSAGES: :[[@LINE-2]]:25: warning: statement should be inside braces |
| 105 | // CHECK-FIXES: while (cond("while1")) { |
| 106 | // CHECK-FIXES: } |
| 107 | while (cond("while2")) { |
| 108 | do_something("while"); |
| 109 | } |
| 110 | while (false) |
| 111 | ; |
| 112 | // CHECK-MESSAGES: :[[@LINE-2]]:16: warning: statement should be inside braces |
| 113 | // CHECK-FIXES: while (false) { |
| 114 | // CHECK-FIXES: } |
| 115 | |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 116 | do |
| 117 | do_something("do1"); |
| 118 | while (cond("do1")); |
| 119 | // CHECK-MESSAGES: :[[@LINE-3]]:5: warning: statement should be inside braces |
| 120 | // CHECK-FIXES: do { |
| 121 | // CHECK-FIXES: } while (cond("do1")); |
| 122 | do { |
| 123 | do_something("do2"); |
| 124 | } while (cond("do2")); |
| 125 | |
| 126 | do |
| 127 | ; |
| 128 | while (false); |
| 129 | // CHECK-MESSAGES: :[[@LINE-3]]:5: warning: statement should be inside braces |
| 130 | // CHECK-FIXES: do { |
| 131 | // CHECK-FIXES: } while (false); |
| 132 | |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 133 | if (cond("ifif1")) |
| 134 | // comment |
| 135 | if (cond("ifif2")) |
| 136 | // comment |
| 137 | /*comment*/ ; // comment |
| 138 | // CHECK-MESSAGES: :[[@LINE-5]]:21: warning: statement should be inside braces |
| 139 | // CHECK-MESSAGES: :[[@LINE-4]]:23: warning: statement should be inside braces |
| 140 | // CHECK-FIXES: if (cond("ifif1")) { |
| 141 | // CHECK-FIXES: if (cond("ifif2")) { |
| 142 | // CHECK-FIXES: } |
| 143 | // CHECK-FIXES-NEXT: } |
| 144 | |
| 145 | if (cond("ifif3")) |
| 146 | // comment |
| 147 | if (cond("ifif4")) { |
| 148 | // comment |
| 149 | /*comment*/; // comment |
| 150 | } |
| 151 | // CHECK-MESSAGES: :[[@LINE-6]]:21: warning: statement should be inside braces |
| 152 | // CHECK-FIXES: if (cond("ifif3")) { |
| 153 | // CHECK-FIXES: } |
| 154 | |
| 155 | if (cond("ifif5")) |
| 156 | ; /* multi-line |
| 157 | comment */ |
| 158 | // CHECK-MESSAGES: :[[@LINE-3]]:21: warning: statement should be inside braces |
| 159 | // CHECK-FIXES: if (cond("ifif5")) { |
| 160 | // CHECK-FIXES: }/* multi-line |
| 161 | |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 162 | if (1) while (2) if (3) for (;;) do ; while(false) /**/;/**/ |
| 163 | // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: statement should be inside braces |
| 164 | // CHECK-MESSAGES: :[[@LINE-2]]:19: warning: statement should be inside braces |
| 165 | // CHECK-MESSAGES: :[[@LINE-3]]:26: warning: statement should be inside braces |
| 166 | // CHECK-MESSAGES: :[[@LINE-4]]:35: warning: statement should be inside braces |
| 167 | // CHECK-MESSAGES: :[[@LINE-5]]:38: warning: statement should be inside braces |
| 168 | // CHECK-FIXES: if (1) { while (2) { if (3) { for (;;) { do { ; } while(false) /**/;/**/ |
| 169 | // CHECK-FIXES-NEXT: } |
| 170 | // CHECK-FIXES-NEXT: } |
| 171 | // CHECK-FIXES-NEXT: } |
| 172 | // CHECK-FIXES-NEXT: } |
Alexander Kornienko | 8f7e7f7 | 2014-10-02 19:09:56 +0000 | [diff] [blame] | 173 | } |
Alexander Kornienko | ffc2779 | 2015-09-09 17:06:09 +0000 | [diff] [blame] | 174 | |
| 175 | #define M(x) x |
| 176 | |
| 177 | int test_macros(bool b) { |
| 178 | if (b) { |
| 179 | return 1; |
| 180 | } else |
| 181 | M(return 2); |
| 182 | // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: statement should be inside braces |
| 183 | // CHECK-FIXES: } else { |
| 184 | // CHECK-FIXES-NEXT: M(return 2); |
| 185 | // CHECK-FIXES-NEXT: } |
Alexander Kornienko | 08023c6 | 2015-09-30 12:48:42 +0000 | [diff] [blame] | 186 | M( |
| 187 | for (;;) |
| 188 | ; |
| 189 | ); |
| 190 | // CHECK-MESSAGES: :[[@LINE-3]]:13: warning: statement should be inside braces |
| 191 | // CHECK-FIXES: {{^}} for (;;) {{{$}} |
| 192 | // CHECK-FIXES-NEXT: {{^ ;$}} |
| 193 | // CHECK-FIXES-NEXT: {{^}$}} |
Alexander Kornienko | ffc2779 | 2015-09-09 17:06:09 +0000 | [diff] [blame] | 194 | } |