1 #define __cond_lock(c) ((c) ? ({ __context__(1); 1; }) : 0) 2 3 static void a(void) __attribute__((context(0,1))) 4 { 5 __context__(1); 6 } 7 8 static void r(void) __attribute__((context(1,0))) 9 { 10 __context__(-1); 11 } 12 13 extern int _ca(int fail); 14 #define ca(fail) __cond_lock(_ca(fail)) 15 16 static void good_paired1(void) 17 { 18 a(); 19 r(); 20 } 21 22 static void good_paired2(void) 23 { 24 a(); 25 r(); 26 a(); 27 r(); 28 } 29 30 static void good_paired3(void) 31 { 32 a(); 33 a(); 34 r(); 35 r(); 36 } 37 38 static void good_lock1(void) __attribute__((context(0,1))) 39 { 40 a(); 41 } 42 43 static void good_lock2(void) __attribute__((context(0,1))) 44 { 45 a(); 46 r(); 47 a(); 48 } 49 50 static void good_lock3(void) __attribute__((context(0,1))) 51 { 52 a(); 53 a(); 54 r(); 55 } 56 57 static void good_unlock1(void) __attribute__((context(1,0))) 58 { 59 r(); 60 } 61 62 static void good_unlock2(void) __attribute__((context(1,0))) 63 { 64 a(); 65 r(); 66 r(); 67 } 68 69 static void warn_lock1(void) 70 { 71 a(); 72 } 73 74 static void warn_lock2(void) 75 { 76 a(); 77 r(); 78 a(); 79 } 80 81 static void warn_lock3(void) 82 { 83 a(); 84 a(); 85 r(); 86 } 87 88 static void warn_unlock1(void) 89 { 90 r(); 91 } 92 93 static void warn_unlock2(void) 94 { 95 a(); 96 r(); 97 r(); 98 } 99 100 extern int condition, condition2; 101 102 static int good_if1(void) 103 { 104 a(); 105 if(condition) { 106 r(); 107 return -1; 108 } 109 r(); 110 return 0; 111 } 112 113 static void good_if2(void) 114 { 115 if(condition) { 116 a(); 117 r(); 118 } 119 } 120 121 static void good_if3(void) 122 { 123 a(); 124 if(condition) { 125 a(); 126 r(); 127 } 128 r(); 129 } 130 131 static int warn_if1(void) 132 { 133 a(); 134 if(condition) 135 return -1; 136 r(); 137 return 0; 138 } 139 140 static int warn_if2(void) 141 { 142 a(); 143 if(condition) { 144 r(); 145 return -1; 146 } 147 return 0; 148 } 149 150 static void good_while1(void) 151 { 152 a(); 153 while(condition) 154 ; 155 r(); 156 } 157 158 static void good_while2(void) 159 { 160 while(condition) { 161 a(); 162 r(); 163 } 164 } 165 166 static void good_while3(void) 167 { 168 while(condition) { 169 a(); 170 r(); 171 if(condition2) 172 break; 173 a(); 174 r(); 175 } 176 } 177 178 static void good_while4(void) 179 { 180 a(); 181 while(1) { 182 if(condition2) { 183 r(); 184 break; 185 } 186 } 187 } 188 189 static void good_while5(void) 190 { 191 a(); 192 while(1) { 193 r(); 194 if(condition2) 195 break; 196 a(); 197 } 198 } 199 200 static void warn_while1(void) 201 { 202 while(condition) { 203 a(); 204 } 205 } 206 207 static void warn_while2(void) 208 { 209 while(condition) { 210 r(); 211 } 212 } 213 214 static void warn_while3(void) 215 { 216 while(condition) { 217 a(); 218 if(condition2) 219 break; 220 r(); 221 } 222 } 223 224 static void good_goto1(void) 225 { 226 a(); 227 goto label; 228 label: 229 r(); 230 } 231 232 static void good_goto2(void) 233 { 234 a(); 235 goto label; 236 a(); 237 r(); 238 label: 239 r(); 240 } 241 242 static void good_goto3(void) 243 { 244 a(); 245 if(condition) 246 goto label; 247 a(); 248 r(); 249 label: 250 r(); 251 } 252 253 static void good_goto4(void) 254 { 255 if(condition) 256 goto label; 257 a(); 258 r(); 259 label: 260 ; 261 } 262 263 static void good_goto5(void) 264 { 265 a(); 266 if(condition) 267 goto label; 268 r(); 269 return; 270 label: 271 r(); 272 } 273 274 static void warn_goto1(void) 275 { 276 a(); 277 goto label; 278 r(); 279 label: 280 ; 281 } 282 283 static void warn_goto2(void) 284 { 285 a(); 286 goto label; 287 r(); 288 label: 289 a(); 290 r(); 291 } 292 293 static void warn_goto3(void) 294 { 295 a(); 296 if(condition) 297 goto label; 298 r(); 299 label: 300 r(); 301 } 302 303 static void good_cond_lock1(void) 304 { 305 if(ca(condition)) { 306 condition2 = 1; /* do stuff */ 307 r(); 308 } 309 } 310 311 static void warn_cond_lock1(void) 312 { 313 if(ca(condition)) 314 condition2 = 1; /* do stuff */ 315 r(); 316 } 317 /* 318 * check-name: Check -Wcontext 319 * 320 * check-error-start 321 context.c:69:13: warning: context imbalance in 'warn_lock1' - wrong count at exit 322 context.c:74:13: warning: context imbalance in 'warn_lock2' - wrong count at exit 323 context.c:81:13: warning: context imbalance in 'warn_lock3' - wrong count at exit 324 context.c:88:13: warning: context imbalance in 'warn_unlock1' - unexpected unlock 325 context.c:93:13: warning: context imbalance in 'warn_unlock2' - unexpected unlock 326 context.c:131:12: warning: context imbalance in 'warn_if1' - wrong count at exit 327 context.c:140:12: warning: context imbalance in 'warn_if2' - different lock contexts for basic block 328 context.c:202:9: warning: context imbalance in 'warn_while1' - different lock contexts for basic block 329 context.c:210:17: warning: context imbalance in 'warn_while2' - unexpected unlock 330 context.c:216:9: warning: context imbalance in 'warn_while3' - wrong count at exit 331 context.c:274:13: warning: context imbalance in 'warn_goto1' - wrong count at exit 332 context.c:283:13: warning: context imbalance in 'warn_goto2' - wrong count at exit 333 context.c:300:5: warning: context imbalance in 'warn_goto3' - different lock contexts for basic block 334 context.c:315:5: warning: context imbalance in 'warn_cond_lock1' - different lock contexts for basic block 335 * check-error-end 336 */