1 // RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 %s -D CPP11
4 
5 #define LOCKABLE            __attribute__ ((lockable))
6 #define SCOPED_LOCKABLE     __attribute__ ((scoped_lockable))
7 #define GUARDED_BY(x)       __attribute__ ((guarded_by(x)))
8 #define GUARDED_VAR         __attribute__ ((guarded_var))
9 #define PT_GUARDED_BY(x)    __attribute__ ((pt_guarded_by(x)))
10 #define PT_GUARDED_VAR      __attribute__ ((pt_guarded_var))
11 #define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__)))
12 #define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__)))
13 #define EXCLUSIVE_LOCK_FUNCTION(...)    __attribute__ ((exclusive_lock_function(__VA_ARGS__)))
14 #define SHARED_LOCK_FUNCTION(...)       __attribute__ ((shared_lock_function(__VA_ARGS__)))
15 #define ASSERT_EXCLUSIVE_LOCK(...)      __attribute__ ((assert_exclusive_lock(__VA_ARGS__)))
16 #define ASSERT_SHARED_LOCK(...)         __attribute__ ((assert_shared_lock(__VA_ARGS__)))
17 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__)))
18 #define SHARED_TRYLOCK_FUNCTION(...)    __attribute__ ((shared_trylock_function(__VA_ARGS__)))
19 #define UNLOCK_FUNCTION(...)            __attribute__ ((unlock_function(__VA_ARGS__)))
20 #define LOCK_RETURNED(x)    __attribute__ ((lock_returned(x)))
21 #define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__)))
22 #define EXCLUSIVE_LOCKS_REQUIRED(...) \
23   __attribute__ ((exclusive_locks_required(__VA_ARGS__)))
24 #define SHARED_LOCKS_REQUIRED(...) \
25   __attribute__ ((shared_locks_required(__VA_ARGS__)))
26 #define NO_THREAD_SAFETY_ANALYSIS  __attribute__ ((no_thread_safety_analysis))
27 
28 
29 class LOCKABLE Mutex {
30   public:
31   void Lock()          EXCLUSIVE_LOCK_FUNCTION();
32   void ReaderLock()    SHARED_LOCK_FUNCTION();
33   void Unlock()        UNLOCK_FUNCTION();
34 
35   bool TryLock()       EXCLUSIVE_TRYLOCK_FUNCTION(true);
36   bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true);
37 
38   void AssertHeld()       ASSERT_EXCLUSIVE_LOCK();
39   void AssertReaderHeld() ASSERT_SHARED_LOCK();
40 };
41 
42 class UnlockableMu{
43 };
44 
45 class MuWrapper {
46   public:
47   Mutex mu;
getMu()48   Mutex getMu() {
49     return mu;
50   }
getMuPointer()51   Mutex * getMuPointer() {
52     return μ
53   }
54 };
55 
56 
57 class MuDoubleWrapper {
58   public:
59   MuWrapper* muWrapper;
getWrapper()60   MuWrapper* getWrapper() {
61     return muWrapper;
62   }
63 };
64 
65 Mutex mu1;
66 UnlockableMu umu;
67 Mutex mu2;
68 MuWrapper muWrapper;
69 MuDoubleWrapper muDoubleWrapper;
70 Mutex* muPointer;
71 Mutex** muDoublePointer = & muPointer;
72 Mutex& muRef = mu1;
73 
74 //---------------------------------------//
75 // Scoping tests
76 //--------------------------------------//
77 
78 class Foo {
79   Mutex foomu;
80   void needLock() EXCLUSIVE_LOCK_FUNCTION(foomu);
81 };
82 
83 class Foo2 {
84   void needLock() EXCLUSIVE_LOCK_FUNCTION(foomu);
85   Mutex foomu;
86 };
87 
88 class Bar {
89  Mutex barmu;
90  Mutex barmu2 ACQUIRED_AFTER(barmu);
91 };
92 
93 
94 //-----------------------------------------//
95 //   No Thread Safety Analysis (noanal)    //
96 //-----------------------------------------//
97 
98 // FIXME: Right now we cannot parse attributes put on function definitions
99 // We would like to patch this at some point.
100 
101 #if !__has_attribute(no_thread_safety_analysis)
102 #error "Should support no_thread_safety_analysis attribute"
103 #endif
104 
105 void noanal_fun() NO_THREAD_SAFETY_ANALYSIS;
106 
107 void noanal_fun_args() __attribute__((no_thread_safety_analysis(1))); // \
108   // expected-error {{'no_thread_safety_analysis' attribute takes no arguments}}
109 
110 int noanal_testfn(int y) NO_THREAD_SAFETY_ANALYSIS;
111 
noanal_testfn(int y)112 int noanal_testfn(int y) {
113   int x NO_THREAD_SAFETY_ANALYSIS = y; // \
114     // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}}
115   return x;
116 };
117 
118 int noanal_test_var NO_THREAD_SAFETY_ANALYSIS; // \
119   // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}}
120 
121 class NoanalFoo {
122  private:
123   int test_field NO_THREAD_SAFETY_ANALYSIS; // \
124     // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}}
125   void test_method() NO_THREAD_SAFETY_ANALYSIS;
126 };
127 
128 class NO_THREAD_SAFETY_ANALYSIS NoanalTestClass { // \
129   // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}}
130 };
131 
132 void noanal_fun_params(int lvar NO_THREAD_SAFETY_ANALYSIS); // \
133   // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}}
134 
135 
136 //-----------------------------------------//
137 //  Guarded Var Attribute (gv)
138 //-----------------------------------------//
139 
140 #if !__has_attribute(guarded_var)
141 #error "Should support guarded_var attribute"
142 #endif
143 
144 int gv_var_noargs GUARDED_VAR;
145 
146 int gv_var_args __attribute__((guarded_var(1))); // \
147   // expected-error {{'guarded_var' attribute takes no arguments}}
148 
149 class GVFoo {
150  private:
151   int gv_field_noargs GUARDED_VAR;
152   int gv_field_args __attribute__((guarded_var(1))); // \
153     // expected-error {{'guarded_var' attribute takes no arguments}}
154 };
155 
156 class GUARDED_VAR GV { // \
157   // expected-warning {{'guarded_var' attribute only applies to non-static data members and global variables}}
158 };
159 
160 void gv_function() GUARDED_VAR; // \
161   // expected-warning {{'guarded_var' attribute only applies to}}
162 
163 void gv_function_params(int gv_lvar GUARDED_VAR); // \
164   // expected-warning {{'guarded_var' attribute only applies to}}
165 
gv_testfn(int y)166 int gv_testfn(int y){
167   int x GUARDED_VAR = y; // \
168     // expected-warning {{'guarded_var' attribute only applies to}}
169   return x;
170 }
171 
172 //-----------------------------------------//
173 //   Pt Guarded Var Attribute (pgv)
174 //-----------------------------------------//
175 
176 //FIXME: add support for boost::scoped_ptr<int> fancyptr  and references
177 
178 #if !__has_attribute(pt_guarded_var)
179 #error "Should support pt_guarded_var attribute"
180 #endif
181 
182 int *pgv_pt_var_noargs PT_GUARDED_VAR;
183 
184 int pgv_var_noargs PT_GUARDED_VAR; // \
185     // expected-warning {{'pt_guarded_var' only applies to pointer types; type here is 'int'}}
186 
187 class PGVFoo {
188  private:
189   int *pt_field_noargs PT_GUARDED_VAR;
190   int field_noargs PT_GUARDED_VAR; // \
191     // expected-warning {{'pt_guarded_var' only applies to pointer types; type here is 'int'}}
192   int *gv_field_args __attribute__((pt_guarded_var(1))); // \
193     // expected-error {{'pt_guarded_var' attribute takes no arguments}}
194 };
195 
196 class PT_GUARDED_VAR PGV { // \
197   // expected-warning {{'pt_guarded_var' attribute only applies to non-static data members and global variables}}
198 };
199 
200 int *pgv_var_args __attribute__((pt_guarded_var(1))); // \
201   // expected-error {{'pt_guarded_var' attribute takes no arguments}}
202 
203 
204 void pgv_function() PT_GUARDED_VAR; // \
205   // expected-warning {{'pt_guarded_var' attribute only applies to}}
206 
207 void pgv_function_params(int *gv_lvar PT_GUARDED_VAR); // \
208   // expected-warning {{'pt_guarded_var' attribute only applies to}}
209 
pgv_testfn(int y)210 void pgv_testfn(int y){
211   int *x PT_GUARDED_VAR = new int(0); // \
212     // expected-warning {{'pt_guarded_var' attribute only applies to}}
213   delete x;
214 }
215 
216 //-----------------------------------------//
217 //  Lockable Attribute (l)
218 //-----------------------------------------//
219 
220 //FIXME: In future we may want to add support for structs, ObjC classes, etc.
221 
222 #if !__has_attribute(lockable)
223 #error "Should support lockable attribute"
224 #endif
225 
226 class LOCKABLE LTestClass {
227 };
228 
229 class __attribute__((lockable (1))) LTestClass_args { // \
230     // expected-error {{'lockable' attribute takes no arguments}}
231 };
232 
233 void l_test_function() LOCKABLE;  // \
234   // expected-warning {{'lockable' attribute only applies to structs, unions, and classes}}
235 
l_testfn(int y)236 int l_testfn(int y) {
237   int x LOCKABLE = y; // \
238     // expected-warning {{'lockable' attribute only applies to}}
239   return x;
240 }
241 
242 int l_test_var LOCKABLE; // \
243   // expected-warning {{'lockable' attribute only applies to}}
244 
245 class LFoo {
246  private:
247   int test_field LOCKABLE; // \
248     // expected-warning {{'lockable' attribute only applies to}}
249   void test_method() LOCKABLE; // \
250     // expected-warning {{'lockable' attribute only applies to}}
251 };
252 
253 
254 void l_function_params(int lvar LOCKABLE); // \
255   // expected-warning {{'lockable' attribute only applies to}}
256 
257 
258 //-----------------------------------------//
259 //  Scoped Lockable Attribute (sl)
260 //-----------------------------------------//
261 
262 #if !__has_attribute(scoped_lockable)
263 #error "Should support scoped_lockable attribute"
264 #endif
265 
266 class SCOPED_LOCKABLE SLTestClass {
267 };
268 
269 class __attribute__((scoped_lockable (1))) SLTestClass_args { // \
270   // expected-error {{'scoped_lockable' attribute takes no arguments}}
271 };
272 
273 void sl_test_function() SCOPED_LOCKABLE;  // \
274   // expected-warning {{'scoped_lockable' attribute only applies to structs, unions, and classes}}
275 
sl_testfn(int y)276 int sl_testfn(int y) {
277   int x SCOPED_LOCKABLE = y; // \
278     // expected-warning {{'scoped_lockable' attribute only applies to}}
279   return x;
280 }
281 
282 int sl_test_var SCOPED_LOCKABLE; // \
283   // expected-warning {{'scoped_lockable' attribute only applies to}}
284 
285 class SLFoo {
286  private:
287   int test_field SCOPED_LOCKABLE; // \
288     // expected-warning {{'scoped_lockable' attribute only applies to}}
289   void test_method() SCOPED_LOCKABLE; // \
290     // expected-warning {{'scoped_lockable' attribute only applies to}}
291 };
292 
293 
294 void sl_function_params(int lvar SCOPED_LOCKABLE); // \
295   // expected-warning {{'scoped_lockable' attribute only applies to}}
296 
297 
298 //-----------------------------------------//
299 //  Guarded By Attribute (gb)
300 //-----------------------------------------//
301 
302 // FIXME: Eventually, would we like this attribute to take more than 1 arg?
303 
304 #if !__has_attribute(guarded_by)
305 #error "Should support guarded_by attribute"
306 #endif
307 
308 //1. Check applied to the right types & argument number
309 
310 int gb_var_arg GUARDED_BY(mu1);
311 
312 int gb_non_ascii GUARDED_BY(L"wide"); // expected-warning {{ignoring 'guarded_by' attribute because its argument is invalid}}
313 
314 int gb_var_args __attribute__((guarded_by(mu1, mu2))); // \
315   // expected-error {{'guarded_by' attribute takes one argument}}
316 
317 int gb_var_noargs __attribute__((guarded_by)); // \
318   // expected-error {{'guarded_by' attribute takes one argument}}
319 
320 class GBFoo {
321  private:
322   int gb_field_noargs __attribute__((guarded_by)); // \
323     // expected-error {{'guarded_by' attribute takes one argument}}
324   int gb_field_args GUARDED_BY(mu1);
325 };
326 
GUARDED_BY(mu1)327 class GUARDED_BY(mu1) GB { // \
328   // expected-warning {{'guarded_by' attribute only applies to non-static data members and global variables}}
329 };
330 
331 void gb_function() GUARDED_BY(mu1); // \
332   // expected-warning {{'guarded_by' attribute only applies to}}
333 
334 void gb_function_params(int gv_lvar GUARDED_BY(mu1)); // \
335   // expected-warning {{'guarded_by' attribute only applies to}}
336 
gb_testfn(int y)337 int gb_testfn(int y){
338   int x GUARDED_BY(mu1) = y; // \
339     // expected-warning {{'guarded_by' attribute only applies to}}
340   return x;
341 }
342 
343 //2. Check argument parsing.
344 
345 // legal attribute arguments
346 int gb_var_arg_1 GUARDED_BY(muWrapper.mu);
347 int gb_var_arg_2 GUARDED_BY(muDoubleWrapper.muWrapper->mu);
348 int gb_var_arg_3 GUARDED_BY(muWrapper.getMu());
349 int gb_var_arg_4 GUARDED_BY(*muWrapper.getMuPointer());
350 int gb_var_arg_5 GUARDED_BY(&mu1);
351 int gb_var_arg_6 GUARDED_BY(muRef);
352 int gb_var_arg_7 GUARDED_BY(muDoubleWrapper.getWrapper()->getMu());
353 int gb_var_arg_8 GUARDED_BY(muPointer);
354 int gb_var_arg_9 GUARDED_BY(!&mu1);
355 int gb_var_arg_10 GUARDED_BY(!&*&mu1);
356 
357 // illegal attribute arguments
358 int gb_var_arg_bad_1 GUARDED_BY(1); // \
359   // expected-warning {{'guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
360 int gb_var_arg_bad_2 GUARDED_BY("mu"); // \
361   // expected-warning {{ignoring 'guarded_by' attribute because its argument is invalid}}
362 int gb_var_arg_bad_3 GUARDED_BY(muDoublePointer); // \
363   // expected-warning {{'guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
364 int gb_var_arg_bad_4 GUARDED_BY(umu); // \
365   // expected-warning {{'guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'UnlockableMu'}}
366 
367 //3.
368 // Thread Safety analysis tests
369 
370 
371 //-----------------------------------------//
372 //  Pt Guarded By Attribute (pgb)
373 //-----------------------------------------//
374 
375 #if !__has_attribute(pt_guarded_by)
376 #error "Should support pt_guarded_by attribute"
377 #endif
378 
379 //1. Check applied to the right types & argument number
380 
381 int *pgb_var_noargs __attribute__((pt_guarded_by)); // \
382   // expected-error {{'pt_guarded_by' attribute takes one argument}}
383 
384 int *pgb_ptr_var_arg PT_GUARDED_BY(mu1);
385 
386 int *pgb_ptr_var_args __attribute__((pt_guarded_by(mu1, mu2))); // \
387   // expected-error {{'pt_guarded_by' attribute takes one argument}}
388 
389 int pgb_var_args PT_GUARDED_BY(mu1); // \
390   // expected-warning {{'pt_guarded_by' only applies to pointer types; type here is 'int'}}
391 
392 class PGBFoo {
393  private:
394   int *pgb_field_noargs __attribute__((pt_guarded_by)); // \
395     // expected-error {{'pt_guarded_by' attribute takes one argument}}
396   int *pgb_field_args PT_GUARDED_BY(mu1);
397 };
398 
PT_GUARDED_BY(mu1)399 class PT_GUARDED_BY(mu1) PGB { // \
400   // expected-warning {{'pt_guarded_by' attribute only applies to non-static data members and global variables}}
401 };
402 
403 void pgb_function() PT_GUARDED_BY(mu1); // \
404   // expected-warning {{'pt_guarded_by' attribute only applies to}}
405 
406 void pgb_function_params(int gv_lvar PT_GUARDED_BY(mu1)); // \
407   // expected-warning {{'pt_guarded_by' attribute only applies to}}
408 
pgb_testfn(int y)409 void pgb_testfn(int y){
410   int *x PT_GUARDED_BY(mu1) = new int(0); // \
411     // expected-warning {{'pt_guarded_by' attribute only applies to}}
412   delete x;
413 }
414 
415 //2. Check argument parsing.
416 
417 // legal attribute arguments
418 int * pgb_var_arg_1 PT_GUARDED_BY(muWrapper.mu);
419 int * pgb_var_arg_2 PT_GUARDED_BY(muDoubleWrapper.muWrapper->mu);
420 int * pgb_var_arg_3 PT_GUARDED_BY(muWrapper.getMu());
421 int * pgb_var_arg_4 PT_GUARDED_BY(*muWrapper.getMuPointer());
422 int * pgb_var_arg_5 PT_GUARDED_BY(&mu1);
423 int * pgb_var_arg_6 PT_GUARDED_BY(muRef);
424 int * pgb_var_arg_7 PT_GUARDED_BY(muDoubleWrapper.getWrapper()->getMu());
425 int * pgb_var_arg_8 PT_GUARDED_BY(muPointer);
426 
427 
428 // illegal attribute arguments
429 int * pgb_var_arg_bad_1 PT_GUARDED_BY(1); // \
430   // expected-warning {{'pt_guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
431 int * pgb_var_arg_bad_2 PT_GUARDED_BY("mu"); // \
432   // expected-warning {{ignoring 'pt_guarded_by' attribute because its argument is invalid}}
433 int * pgb_var_arg_bad_3 PT_GUARDED_BY(muDoublePointer); // \
434   // expected-warning {{'pt_guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
435 int * pgb_var_arg_bad_4 PT_GUARDED_BY(umu); // \
436   // expected-warning {{'pt_guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute}}
437 
438 
439 //-----------------------------------------//
440 //  Acquired After (aa)
441 //-----------------------------------------//
442 
443 // FIXME: Would we like this attribute to take more than 1 arg?
444 
445 #if !__has_attribute(acquired_after)
446 #error "Should support acquired_after attribute"
447 #endif
448 
449 Mutex mu_aa ACQUIRED_AFTER(mu1);
450 
451 Mutex aa_var_noargs __attribute__((acquired_after)); // \
452   // expected-error {{'acquired_after' attribute takes at least 1 argument}}
453 
454 class AAFoo {
455  private:
456   Mutex aa_field_noargs __attribute__((acquired_after)); // \
457     // expected-error {{'acquired_after' attribute takes at least 1 argument}}
458   Mutex aa_field_args ACQUIRED_AFTER(mu1);
459 };
460 
ACQUIRED_AFTER(mu1)461 class ACQUIRED_AFTER(mu1) AA { // \
462   // expected-warning {{'acquired_after' attribute only applies to non-static data members and global variables}}
463 };
464 
465 void aa_function() ACQUIRED_AFTER(mu1); // \
466   // expected-warning {{'acquired_after' attribute only applies to}}
467 
468 void aa_function_params(int gv_lvar ACQUIRED_AFTER(mu1)); // \
469   // expected-warning {{'acquired_after' attribute only applies to}}
470 
aa_testfn(int y)471 void aa_testfn(int y){
472   Mutex x ACQUIRED_AFTER(mu1) = Mutex(); // \
473     // expected-warning {{'acquired_after' attribute only applies to}}
474 }
475 
476 //Check argument parsing.
477 
478 // legal attribute arguments
479 Mutex aa_var_arg_1 ACQUIRED_AFTER(muWrapper.mu);
480 Mutex aa_var_arg_2 ACQUIRED_AFTER(muDoubleWrapper.muWrapper->mu);
481 Mutex aa_var_arg_3 ACQUIRED_AFTER(muWrapper.getMu());
482 Mutex aa_var_arg_4 ACQUIRED_AFTER(*muWrapper.getMuPointer());
483 Mutex aa_var_arg_5 ACQUIRED_AFTER(&mu1);
484 Mutex aa_var_arg_6 ACQUIRED_AFTER(muRef);
485 Mutex aa_var_arg_7 ACQUIRED_AFTER(muDoubleWrapper.getWrapper()->getMu());
486 Mutex aa_var_arg_8 ACQUIRED_AFTER(muPointer);
487 
488 
489 // illegal attribute arguments
490 Mutex aa_var_arg_bad_1 ACQUIRED_AFTER(1); // \
491   // expected-warning {{'acquired_after' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
492 Mutex aa_var_arg_bad_2 ACQUIRED_AFTER("mu"); // \
493   // expected-warning {{ignoring 'acquired_after' attribute because its argument is invalid}}
494 Mutex aa_var_arg_bad_3 ACQUIRED_AFTER(muDoublePointer); // \
495   // expected-warning {{'acquired_after' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
496 Mutex aa_var_arg_bad_4 ACQUIRED_AFTER(umu); // \
497   // expected-warning {{'acquired_after' attribute requires arguments whose type is annotated with 'capability' attribute}}
498 UnlockableMu aa_var_arg_bad_5 ACQUIRED_AFTER(mu_aa); // \
499   // expected-warning {{'acquired_after' attribute can only be applied in a context annotated with 'capability("mutex")' attribute}}
500 
501 //-----------------------------------------//
502 //  Acquired Before (ab)
503 //-----------------------------------------//
504 
505 #if !__has_attribute(acquired_before)
506 #error "Should support acquired_before attribute"
507 #endif
508 
509 Mutex mu_ab ACQUIRED_BEFORE(mu1);
510 
511 Mutex ab_var_noargs __attribute__((acquired_before)); // \
512   // expected-error {{'acquired_before' attribute takes at least 1 argument}}
513 
514 class ABFoo {
515  private:
516   Mutex ab_field_noargs __attribute__((acquired_before)); // \
517     // expected-error {{'acquired_before' attribute takes at least 1 argument}}
518   Mutex ab_field_args ACQUIRED_BEFORE(mu1);
519 };
520 
ACQUIRED_BEFORE(mu1)521 class ACQUIRED_BEFORE(mu1) AB { // \
522   // expected-warning {{'acquired_before' attribute only applies to non-static data members and global variables}}
523 };
524 
525 void ab_function() ACQUIRED_BEFORE(mu1); // \
526   // expected-warning {{'acquired_before' attribute only applies to}}
527 
528 void ab_function_params(int gv_lvar ACQUIRED_BEFORE(mu1)); // \
529   // expected-warning {{'acquired_before' attribute only applies to}}
530 
ab_testfn(int y)531 void ab_testfn(int y){
532   Mutex x ACQUIRED_BEFORE(mu1) = Mutex(); // \
533     // expected-warning {{'acquired_before' attribute only applies to}}
534 }
535 
536 // Note: illegal int ab_int ACQUIRED_BEFORE(mu1) will
537 // be taken care of by warnings that ab__int is not lockable.
538 
539 //Check argument parsing.
540 
541 // legal attribute arguments
542 Mutex ab_var_arg_1 ACQUIRED_BEFORE(muWrapper.mu);
543 Mutex ab_var_arg_2 ACQUIRED_BEFORE(muDoubleWrapper.muWrapper->mu);
544 Mutex ab_var_arg_3 ACQUIRED_BEFORE(muWrapper.getMu());
545 Mutex ab_var_arg_4 ACQUIRED_BEFORE(*muWrapper.getMuPointer());
546 Mutex ab_var_arg_5 ACQUIRED_BEFORE(&mu1);
547 Mutex ab_var_arg_6 ACQUIRED_BEFORE(muRef);
548 Mutex ab_var_arg_7 ACQUIRED_BEFORE(muDoubleWrapper.getWrapper()->getMu());
549 Mutex ab_var_arg_8 ACQUIRED_BEFORE(muPointer);
550 
551 
552 // illegal attribute arguments
553 Mutex ab_var_arg_bad_1 ACQUIRED_BEFORE(1); // \
554   // expected-warning {{'acquired_before' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
555 Mutex ab_var_arg_bad_2 ACQUIRED_BEFORE("mu"); // \
556   // expected-warning {{ignoring 'acquired_before' attribute because its argument is invalid}}
557 Mutex ab_var_arg_bad_3 ACQUIRED_BEFORE(muDoublePointer); // \
558   // expected-warning {{'acquired_before' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
559 Mutex ab_var_arg_bad_4 ACQUIRED_BEFORE(umu); // \
560   // expected-warning {{'acquired_before' attribute requires arguments whose type is annotated with 'capability' attribute}}
561 UnlockableMu ab_var_arg_bad_5 ACQUIRED_BEFORE(mu_ab); // \
562   // expected-warning {{'acquired_before' attribute can only be applied in a context annotated with 'capability("mutex")' attribute}}
563 
564 
565 //-----------------------------------------//
566 //  Exclusive Lock Function (elf)
567 //-----------------------------------------//
568 
569 #if !__has_attribute(exclusive_lock_function)
570 #error "Should support exclusive_lock_function attribute"
571 #endif
572 
573 // takes zero or more arguments, all locks (vars/fields)
574 
575 void elf_function() EXCLUSIVE_LOCK_FUNCTION();
576 
577 void elf_function_args() EXCLUSIVE_LOCK_FUNCTION(mu1, mu2);
578 
579 int elf_testfn(int y) EXCLUSIVE_LOCK_FUNCTION();
580 
elf_testfn(int y)581 int elf_testfn(int y) {
582   int x EXCLUSIVE_LOCK_FUNCTION() = y; // \
583     // expected-warning {{'exclusive_lock_function' attribute only applies to functions}}
584   return x;
585 };
586 
587 int elf_test_var EXCLUSIVE_LOCK_FUNCTION(); // \
588   // expected-warning {{'exclusive_lock_function' attribute only applies to functions}}
589 
590 class ElfFoo {
591  private:
592   int test_field EXCLUSIVE_LOCK_FUNCTION(); // \
593     // expected-warning {{'exclusive_lock_function' attribute only applies to functions}}
594   void test_method() EXCLUSIVE_LOCK_FUNCTION();
595 };
596 
EXCLUSIVE_LOCK_FUNCTION()597 class EXCLUSIVE_LOCK_FUNCTION() ElfTestClass { // \
598   // expected-warning {{'exclusive_lock_function' attribute only applies to functions}}
599 };
600 
601 void elf_fun_params(int lvar EXCLUSIVE_LOCK_FUNCTION()); // \
602   // expected-warning {{'exclusive_lock_function' attribute only applies to functions}}
603 
604 // Check argument parsing.
605 
606 // legal attribute arguments
607 int elf_function_1() EXCLUSIVE_LOCK_FUNCTION(muWrapper.mu);
608 int elf_function_2() EXCLUSIVE_LOCK_FUNCTION(muDoubleWrapper.muWrapper->mu);
609 int elf_function_3() EXCLUSIVE_LOCK_FUNCTION(muWrapper.getMu());
610 int elf_function_4() EXCLUSIVE_LOCK_FUNCTION(*muWrapper.getMuPointer());
611 int elf_function_5() EXCLUSIVE_LOCK_FUNCTION(&mu1);
612 int elf_function_6() EXCLUSIVE_LOCK_FUNCTION(muRef);
613 int elf_function_7() EXCLUSIVE_LOCK_FUNCTION(muDoubleWrapper.getWrapper()->getMu());
614 int elf_function_8() EXCLUSIVE_LOCK_FUNCTION(muPointer);
615 int elf_function_9(Mutex x) EXCLUSIVE_LOCK_FUNCTION(1);
616 int elf_function_9(Mutex x, Mutex y) EXCLUSIVE_LOCK_FUNCTION(1,2);
617 
618 
619 // illegal attribute arguments
620 int elf_function_bad_2() EXCLUSIVE_LOCK_FUNCTION("mu"); // \
621   // expected-warning {{ignoring 'exclusive_lock_function' attribute because its argument is invalid}}
622 int elf_function_bad_3() EXCLUSIVE_LOCK_FUNCTION(muDoublePointer); // \
623   // expected-warning {{'exclusive_lock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
624 int elf_function_bad_4() EXCLUSIVE_LOCK_FUNCTION(umu); // \
625   // expected-warning {{'exclusive_lock_function' attribute requires arguments whose type is annotated with 'capability' attribute}}
626 
627 int elf_function_bad_1() EXCLUSIVE_LOCK_FUNCTION(1); // \
628   // expected-error {{'exclusive_lock_function' attribute parameter 1 is out of bounds: no parameters to index into}}
629 int elf_function_bad_5(Mutex x) EXCLUSIVE_LOCK_FUNCTION(0); // \
630   // expected-error {{'exclusive_lock_function' attribute parameter 1 is out of bounds: can only be 1, since there is one parameter}}
631 int elf_function_bad_6(Mutex x, Mutex y) EXCLUSIVE_LOCK_FUNCTION(0); // \
632   // expected-error {{'exclusive_lock_function' attribute parameter 1 is out of bounds: must be between 1 and 2}}
633 int elf_function_bad_7() EXCLUSIVE_LOCK_FUNCTION(0); // \
634   // expected-error {{'exclusive_lock_function' attribute parameter 1 is out of bounds: no parameters to index into}}
635 
636 
637 //-----------------------------------------//
638 //  Shared Lock Function (slf)
639 //-----------------------------------------//
640 
641 #if !__has_attribute(shared_lock_function)
642 #error "Should support shared_lock_function attribute"
643 #endif
644 
645 // takes zero or more arguments, all locks (vars/fields)
646 
647 void slf_function() SHARED_LOCK_FUNCTION();
648 
649 void slf_function_args() SHARED_LOCK_FUNCTION(mu1, mu2);
650 
651 int slf_testfn(int y) SHARED_LOCK_FUNCTION();
652 
slf_testfn(int y)653 int slf_testfn(int y) {
654   int x SHARED_LOCK_FUNCTION() = y; // \
655     // expected-warning {{'shared_lock_function' attribute only applies to functions}}
656   return x;
657 };
658 
659 int slf_test_var SHARED_LOCK_FUNCTION(); // \
660   // expected-warning {{'shared_lock_function' attribute only applies to functions}}
661 
662 void slf_fun_params(int lvar SHARED_LOCK_FUNCTION()); // \
663   // expected-warning {{'shared_lock_function' attribute only applies to functions}}
664 
665 class SlfFoo {
666  private:
667   int test_field SHARED_LOCK_FUNCTION(); // \
668     // expected-warning {{'shared_lock_function' attribute only applies to functions}}
669   void test_method() SHARED_LOCK_FUNCTION();
670 };
671 
SHARED_LOCK_FUNCTION()672 class SHARED_LOCK_FUNCTION() SlfTestClass { // \
673   // expected-warning {{'shared_lock_function' attribute only applies to functions}}
674 };
675 
676 // Check argument parsing.
677 
678 // legal attribute arguments
679 int slf_function_1() SHARED_LOCK_FUNCTION(muWrapper.mu);
680 int slf_function_2() SHARED_LOCK_FUNCTION(muDoubleWrapper.muWrapper->mu);
681 int slf_function_3() SHARED_LOCK_FUNCTION(muWrapper.getMu());
682 int slf_function_4() SHARED_LOCK_FUNCTION(*muWrapper.getMuPointer());
683 int slf_function_5() SHARED_LOCK_FUNCTION(&mu1);
684 int slf_function_6() SHARED_LOCK_FUNCTION(muRef);
685 int slf_function_7() SHARED_LOCK_FUNCTION(muDoubleWrapper.getWrapper()->getMu());
686 int slf_function_8() SHARED_LOCK_FUNCTION(muPointer);
687 int slf_function_9(Mutex x) SHARED_LOCK_FUNCTION(1);
688 int slf_function_9(Mutex x, Mutex y) SHARED_LOCK_FUNCTION(1,2);
689 
690 
691 // illegal attribute arguments
692 int slf_function_bad_2() SHARED_LOCK_FUNCTION("mu"); // \
693   // expected-warning {{ignoring 'shared_lock_function' attribute because its argument is invalid}}
694 int slf_function_bad_3() SHARED_LOCK_FUNCTION(muDoublePointer); // \
695   // expected-warning {{'shared_lock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
696 int slf_function_bad_4() SHARED_LOCK_FUNCTION(umu); // \
697   // expected-warning {{'shared_lock_function' attribute requires arguments whose type is annotated with 'capability' attribute}}
698 
699 int slf_function_bad_1() SHARED_LOCK_FUNCTION(1); // \
700   // expected-error {{'shared_lock_function' attribute parameter 1 is out of bounds: no parameters to index into}}
701 int slf_function_bad_5(Mutex x) SHARED_LOCK_FUNCTION(0); // \
702   // expected-error {{'shared_lock_function' attribute parameter 1 is out of bounds: can only be 1, since there is one parameter}}
703 int slf_function_bad_6(Mutex x, Mutex y) SHARED_LOCK_FUNCTION(0); // \
704   // expected-error {{'shared_lock_function' attribute parameter 1 is out of bounds: must be between 1 and 2}}
705 int slf_function_bad_7() SHARED_LOCK_FUNCTION(0); // \
706   // expected-error {{'shared_lock_function' attribute parameter 1 is out of bounds: no parameters to index into}}
707 
708 
709 //-----------------------------------------//
710 //  Exclusive TryLock Function (etf)
711 //-----------------------------------------//
712 
713 #if !__has_attribute(exclusive_trylock_function)
714 #error "Should support exclusive_trylock_function attribute"
715 #endif
716 
717 // takes a mandatory boolean or integer argument specifying the retval
718 // plus an optional list of locks (vars/fields)
719 
720 void etf_function() __attribute__((exclusive_trylock_function));  // \
721   // expected-error {{'exclusive_trylock_function' attribute takes at least 1 argument}}
722 
723 void etf_function_args() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu2);
724 
725 void etf_function_arg() EXCLUSIVE_TRYLOCK_FUNCTION(1);
726 
727 int etf_testfn(int y) EXCLUSIVE_TRYLOCK_FUNCTION(1);
728 
etf_testfn(int y)729 int etf_testfn(int y) {
730   int x EXCLUSIVE_TRYLOCK_FUNCTION(1) = y; // \
731     // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}}
732   return x;
733 };
734 
735 int etf_test_var EXCLUSIVE_TRYLOCK_FUNCTION(1); // \
736   // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}}
737 
738 class EtfFoo {
739  private:
740   int test_field EXCLUSIVE_TRYLOCK_FUNCTION(1); // \
741     // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}}
742   void test_method() EXCLUSIVE_TRYLOCK_FUNCTION(1);
743 };
744 
745 class EXCLUSIVE_TRYLOCK_FUNCTION(1) EtfTestClass { // \
746   // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}}
747 };
748 
749 void etf_fun_params(int lvar EXCLUSIVE_TRYLOCK_FUNCTION(1)); // \
750   // expected-warning {{'exclusive_trylock_function' attribute only applies to functions}}
751 
752 // Check argument parsing.
753 
754 // legal attribute arguments
755 int etf_function_1() EXCLUSIVE_TRYLOCK_FUNCTION(1, muWrapper.mu);
756 int etf_function_2() EXCLUSIVE_TRYLOCK_FUNCTION(1, muDoubleWrapper.muWrapper->mu);
757 int etf_function_3() EXCLUSIVE_TRYLOCK_FUNCTION(1, muWrapper.getMu());
758 int etf_function_4() EXCLUSIVE_TRYLOCK_FUNCTION(1, *muWrapper.getMuPointer());
759 int etf_function_5() EXCLUSIVE_TRYLOCK_FUNCTION(1, &mu1);
760 int etf_function_6() EXCLUSIVE_TRYLOCK_FUNCTION(1, muRef);
761 int etf_function_7() EXCLUSIVE_TRYLOCK_FUNCTION(1, muDoubleWrapper.getWrapper()->getMu());
762 int etf_functetfn_8() EXCLUSIVE_TRYLOCK_FUNCTION(1, muPointer);
763 int etf_function_9() EXCLUSIVE_TRYLOCK_FUNCTION(true);
764 
765 
766 // illegal attribute arguments
767 int etf_function_bad_1() EXCLUSIVE_TRYLOCK_FUNCTION(mu1); // \
768   // expected-error {{'exclusive_trylock_function' attribute requires parameter 1 to be int or bool}}
769 int etf_function_bad_2() EXCLUSIVE_TRYLOCK_FUNCTION("mu"); // \
770   // expected-error {{'exclusive_trylock_function' attribute requires parameter 1 to be int or bool}}
771 int etf_function_bad_3() EXCLUSIVE_TRYLOCK_FUNCTION(muDoublePointer); // \
772   // expected-error {{'exclusive_trylock_function' attribute requires parameter 1 to be int or bool}}
773 
774 int etf_function_bad_4() EXCLUSIVE_TRYLOCK_FUNCTION(1, "mu"); // \
775   // expected-warning {{ignoring 'exclusive_trylock_function' attribute because its argument is invalid}}
776 int etf_function_bad_5() EXCLUSIVE_TRYLOCK_FUNCTION(1, muDoublePointer); // \
777   // expected-warning {{'exclusive_trylock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
778 int etf_function_bad_6() EXCLUSIVE_TRYLOCK_FUNCTION(1, umu); // \
779   // expected-warning {{'exclusive_trylock_function' attribute requires arguments whose type is annotated with 'capability' attribute}}
780 
781 
782 //-----------------------------------------//
783 //  Shared TryLock Function (stf)
784 //-----------------------------------------//
785 
786 #if !__has_attribute(shared_trylock_function)
787 #error "Should support shared_trylock_function attribute"
788 #endif
789 
790 // takes a mandatory boolean or integer argument specifying the retval
791 // plus an optional list of locks (vars/fields)
792 
793 void stf_function() __attribute__((shared_trylock_function));  // \
794   // expected-error {{'shared_trylock_function' attribute takes at least 1 argument}}
795 
796 void stf_function_args() SHARED_TRYLOCK_FUNCTION(1, mu2);
797 
798 void stf_function_arg() SHARED_TRYLOCK_FUNCTION(1);
799 
800 int stf_testfn(int y) SHARED_TRYLOCK_FUNCTION(1);
801 
stf_testfn(int y)802 int stf_testfn(int y) {
803   int x SHARED_TRYLOCK_FUNCTION(1) = y; // \
804     // expected-warning {{'shared_trylock_function' attribute only applies to functions}}
805   return x;
806 };
807 
808 int stf_test_var SHARED_TRYLOCK_FUNCTION(1); // \
809   // expected-warning {{'shared_trylock_function' attribute only applies to functions}}
810 
811 void stf_fun_params(int lvar SHARED_TRYLOCK_FUNCTION(1)); // \
812   // expected-warning {{'shared_trylock_function' attribute only applies to functions}}
813 
814 
815 class StfFoo {
816  private:
817   int test_field SHARED_TRYLOCK_FUNCTION(1); // \
818     // expected-warning {{'shared_trylock_function' attribute only applies to functions}}
819   void test_method() SHARED_TRYLOCK_FUNCTION(1);
820 };
821 
822 class SHARED_TRYLOCK_FUNCTION(1) StfTestClass { // \
823     // expected-warning {{'shared_trylock_function' attribute only applies to functions}}
824 };
825 
826 // Check argument parsing.
827 
828 // legal attribute arguments
829 int stf_function_1() SHARED_TRYLOCK_FUNCTION(1, muWrapper.mu);
830 int stf_function_2() SHARED_TRYLOCK_FUNCTION(1, muDoubleWrapper.muWrapper->mu);
831 int stf_function_3() SHARED_TRYLOCK_FUNCTION(1, muWrapper.getMu());
832 int stf_function_4() SHARED_TRYLOCK_FUNCTION(1, *muWrapper.getMuPointer());
833 int stf_function_5() SHARED_TRYLOCK_FUNCTION(1, &mu1);
834 int stf_function_6() SHARED_TRYLOCK_FUNCTION(1, muRef);
835 int stf_function_7() SHARED_TRYLOCK_FUNCTION(1, muDoubleWrapper.getWrapper()->getMu());
836 int stf_function_8() SHARED_TRYLOCK_FUNCTION(1, muPointer);
837 int stf_function_9() SHARED_TRYLOCK_FUNCTION(true);
838 
839 
840 // illegal attribute arguments
841 int stf_function_bad_1() SHARED_TRYLOCK_FUNCTION(mu1); // \
842   // expected-error {{'shared_trylock_function' attribute requires parameter 1 to be int or bool}}
843 int stf_function_bad_2() SHARED_TRYLOCK_FUNCTION("mu"); // \
844   // expected-error {{'shared_trylock_function' attribute requires parameter 1 to be int or bool}}
845 int stf_function_bad_3() SHARED_TRYLOCK_FUNCTION(muDoublePointer); // \
846   // expected-error {{'shared_trylock_function' attribute requires parameter 1 to be int or bool}}
847 
848 int stf_function_bad_4() SHARED_TRYLOCK_FUNCTION(1, "mu"); // \
849   // expected-warning {{ignoring 'shared_trylock_function' attribute because its argument is invalid}}
850 int stf_function_bad_5() SHARED_TRYLOCK_FUNCTION(1, muDoublePointer); // \
851   // expected-warning {{'shared_trylock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
852 int stf_function_bad_6() SHARED_TRYLOCK_FUNCTION(1, umu); // \
853   // expected-warning {{'shared_trylock_function' attribute requires arguments whose type is annotated with 'capability' attribute}}
854 
855 
856 //-----------------------------------------//
857 //  Unlock Function (uf)
858 //-----------------------------------------//
859 
860 #if !__has_attribute(unlock_function)
861 #error "Should support unlock_function attribute"
862 #endif
863 
864 // takes zero or more arguments, all locks (vars/fields)
865 
866 void uf_function() UNLOCK_FUNCTION();
867 
868 void uf_function_args() UNLOCK_FUNCTION(mu1, mu2);
869 
870 int uf_testfn(int y) UNLOCK_FUNCTION();
871 
uf_testfn(int y)872 int uf_testfn(int y) {
873   int x UNLOCK_FUNCTION() = y; // \
874     // expected-warning {{'unlock_function' attribute only applies to functions}}
875   return x;
876 };
877 
878 int uf_test_var UNLOCK_FUNCTION(); // \
879   // expected-warning {{'unlock_function' attribute only applies to functions}}
880 
881 class UfFoo {
882  private:
883   int test_field UNLOCK_FUNCTION(); // \
884     // expected-warning {{'unlock_function' attribute only applies to functions}}
885   void test_method() UNLOCK_FUNCTION();
886 };
887 
888 class NO_THREAD_SAFETY_ANALYSIS UfTestClass { // \
889   // expected-warning {{'no_thread_safety_analysis' attribute only applies to functions}}
890 };
891 
892 void uf_fun_params(int lvar UNLOCK_FUNCTION()); // \
893   // expected-warning {{'unlock_function' attribute only applies to functions}}
894 
895 // Check argument parsing.
896 
897 // legal attribute arguments
898 int uf_function_1() UNLOCK_FUNCTION(muWrapper.mu);
899 int uf_function_2() UNLOCK_FUNCTION(muDoubleWrapper.muWrapper->mu);
900 int uf_function_3() UNLOCK_FUNCTION(muWrapper.getMu());
901 int uf_function_4() UNLOCK_FUNCTION(*muWrapper.getMuPointer());
902 int uf_function_5() UNLOCK_FUNCTION(&mu1);
903 int uf_function_6() UNLOCK_FUNCTION(muRef);
904 int uf_function_7() UNLOCK_FUNCTION(muDoubleWrapper.getWrapper()->getMu());
905 int uf_function_8() UNLOCK_FUNCTION(muPointer);
906 int uf_function_9(Mutex x) UNLOCK_FUNCTION(1);
907 int uf_function_9(Mutex x, Mutex y) UNLOCK_FUNCTION(1,2);
908 
909 
910 // illegal attribute arguments
911 int uf_function_bad_2() UNLOCK_FUNCTION("mu"); // \
912   // expected-warning {{ignoring 'unlock_function' attribute because its argument is invalid}}
913 int uf_function_bad_3() UNLOCK_FUNCTION(muDoublePointer); // \
914   // expected-warning {{'unlock_function' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
915 int uf_function_bad_4() UNLOCK_FUNCTION(umu); // \
916   // expected-warning {{'unlock_function' attribute requires arguments whose type is annotated with 'capability' attribute}}
917 
918 int uf_function_bad_1() UNLOCK_FUNCTION(1); // \
919   // expected-error {{'unlock_function' attribute parameter 1 is out of bounds: no parameters to index into}}
920 int uf_function_bad_5(Mutex x) UNLOCK_FUNCTION(0); // \
921   // expected-error {{'unlock_function' attribute parameter 1 is out of bounds: can only be 1, since there is one parameter}}
922 int uf_function_bad_6(Mutex x, Mutex y) UNLOCK_FUNCTION(0); // \
923   // expected-error {{'unlock_function' attribute parameter 1 is out of bounds: must be between 1 and 2}}
924 int uf_function_bad_7() UNLOCK_FUNCTION(0); // \
925   // expected-error {{'unlock_function' attribute parameter 1 is out of bounds: no parameters to index into}}
926 
927 
928 //-----------------------------------------//
929 //  Lock Returned (lr)
930 //-----------------------------------------//
931 
932 #if !__has_attribute(lock_returned)
933 #error "Should support lock_returned attribute"
934 #endif
935 
936 // Takes exactly one argument, a var/field
937 
938 void lr_function() __attribute__((lock_returned)); // \
939   // expected-error {{'lock_returned' attribute takes one argument}}
940 
941 void lr_function_arg() LOCK_RETURNED(mu1);
942 
943 void lr_function_args() __attribute__((lock_returned(mu1, mu2))); // \
944   // expected-error {{'lock_returned' attribute takes one argument}}
945 
946 int lr_testfn(int y) LOCK_RETURNED(mu1);
947 
lr_testfn(int y)948 int lr_testfn(int y) {
949   int x LOCK_RETURNED(mu1) = y; // \
950     // expected-warning {{'lock_returned' attribute only applies to functions}}
951   return x;
952 };
953 
954 int lr_test_var LOCK_RETURNED(mu1); // \
955   // expected-warning {{'lock_returned' attribute only applies to functions}}
956 
957 void lr_fun_params(int lvar LOCK_RETURNED(mu1)); // \
958   // expected-warning {{'lock_returned' attribute only applies to functions}}
959 
960 class LrFoo {
961  private:
962   int test_field LOCK_RETURNED(mu1); // \
963     // expected-warning {{'lock_returned' attribute only applies to functions}}
964   void test_method() LOCK_RETURNED(mu1);
965 };
966 
LOCK_RETURNED(mu1)967 class LOCK_RETURNED(mu1) LrTestClass { // \
968     // expected-warning {{'lock_returned' attribute only applies to functions}}
969 };
970 
971 // Check argument parsing.
972 
973 // legal attribute arguments
974 int lr_function_1() LOCK_RETURNED(muWrapper.mu);
975 int lr_function_2() LOCK_RETURNED(muDoubleWrapper.muWrapper->mu);
976 int lr_function_3() LOCK_RETURNED(muWrapper.getMu());
977 int lr_function_4() LOCK_RETURNED(*muWrapper.getMuPointer());
978 int lr_function_5() LOCK_RETURNED(&mu1);
979 int lr_function_6() LOCK_RETURNED(muRef);
980 int lr_function_7() LOCK_RETURNED(muDoubleWrapper.getWrapper()->getMu());
981 int lr_function_8() LOCK_RETURNED(muPointer);
982 
983 
984 // illegal attribute arguments
985 int lr_function_bad_1() LOCK_RETURNED(1); // \
986   // expected-warning {{'lock_returned' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
987 int lr_function_bad_2() LOCK_RETURNED("mu"); // \
988   // expected-warning {{ignoring 'lock_returned' attribute because its argument is invalid}}
989 int lr_function_bad_3() LOCK_RETURNED(muDoublePointer); // \
990   // expected-warning {{'lock_returned' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
991 int lr_function_bad_4() LOCK_RETURNED(umu); // \
992   // expected-warning {{'lock_returned' attribute requires arguments whose type is annotated with 'capability' attribute}}
993 
994 
995 
996 //-----------------------------------------//
997 //  Locks Excluded (le)
998 //-----------------------------------------//
999 
1000 #if !__has_attribute(locks_excluded)
1001 #error "Should support locks_excluded attribute"
1002 #endif
1003 
1004 // takes one or more arguments, all locks (vars/fields)
1005 
1006 void le_function() __attribute__((locks_excluded)); // \
1007   // expected-error {{'locks_excluded' attribute takes at least 1 argument}}
1008 
1009 void le_function_arg() LOCKS_EXCLUDED(mu1);
1010 
1011 void le_function_args() LOCKS_EXCLUDED(mu1, mu2);
1012 
1013 int le_testfn(int y) LOCKS_EXCLUDED(mu1);
1014 
le_testfn(int y)1015 int le_testfn(int y) {
1016   int x LOCKS_EXCLUDED(mu1) = y; // \
1017     // expected-warning {{'locks_excluded' attribute only applies to functions}}
1018   return x;
1019 };
1020 
1021 int le_test_var LOCKS_EXCLUDED(mu1); // \
1022   // expected-warning {{'locks_excluded' attribute only applies to functions}}
1023 
1024 void le_fun_params(int lvar LOCKS_EXCLUDED(mu1)); // \
1025   // expected-warning {{'locks_excluded' attribute only applies to functions}}
1026 
1027 class LeFoo {
1028  private:
1029   int test_field LOCKS_EXCLUDED(mu1); // \
1030     // expected-warning {{'locks_excluded' attribute only applies to functions}}
1031   void test_method() LOCKS_EXCLUDED(mu1);
1032 };
1033 
LOCKS_EXCLUDED(mu1)1034 class LOCKS_EXCLUDED(mu1) LeTestClass { // \
1035   // expected-warning {{'locks_excluded' attribute only applies to functions}}
1036 };
1037 
1038 // Check argument parsing.
1039 
1040 // legal attribute arguments
1041 int le_function_1() LOCKS_EXCLUDED(muWrapper.mu);
1042 int le_function_2() LOCKS_EXCLUDED(muDoubleWrapper.muWrapper->mu);
1043 int le_function_3() LOCKS_EXCLUDED(muWrapper.getMu());
1044 int le_function_4() LOCKS_EXCLUDED(*muWrapper.getMuPointer());
1045 int le_function_5() LOCKS_EXCLUDED(&mu1);
1046 int le_function_6() LOCKS_EXCLUDED(muRef);
1047 int le_function_7() LOCKS_EXCLUDED(muDoubleWrapper.getWrapper()->getMu());
1048 int le_function_8() LOCKS_EXCLUDED(muPointer);
1049 
1050 
1051 // illegal attribute arguments
1052 int le_function_bad_1() LOCKS_EXCLUDED(1); // \
1053   // expected-warning {{'locks_excluded' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
1054 int le_function_bad_2() LOCKS_EXCLUDED("mu"); // \
1055   // expected-warning {{ignoring 'locks_excluded' attribute because its argument is invalid}}
1056 int le_function_bad_3() LOCKS_EXCLUDED(muDoublePointer); // \
1057   // expected-warning {{'locks_excluded' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
1058 int le_function_bad_4() LOCKS_EXCLUDED(umu); // \
1059   // expected-warning {{'locks_excluded' attribute requires arguments whose type is annotated with 'capability' attribute}}
1060 
1061 
1062 
1063 //-----------------------------------------//
1064 //  Exclusive Locks Required (elr)
1065 //-----------------------------------------//
1066 
1067 #if !__has_attribute(exclusive_locks_required)
1068 #error "Should support exclusive_locks_required attribute"
1069 #endif
1070 
1071 // takes one or more arguments, all locks (vars/fields)
1072 
1073 void elr_function() __attribute__((exclusive_locks_required)); // \
1074   // expected-error {{'exclusive_locks_required' attribute takes at least 1 argument}}
1075 
1076 void elr_function_arg() EXCLUSIVE_LOCKS_REQUIRED(mu1);
1077 
1078 void elr_function_args() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2);
1079 
1080 int elr_testfn(int y) EXCLUSIVE_LOCKS_REQUIRED(mu1);
1081 
elr_testfn(int y)1082 int elr_testfn(int y) {
1083   int x EXCLUSIVE_LOCKS_REQUIRED(mu1) = y; // \
1084     // expected-warning {{'exclusive_locks_required' attribute only applies to functions}}
1085   return x;
1086 };
1087 
1088 int elr_test_var EXCLUSIVE_LOCKS_REQUIRED(mu1); // \
1089   // expected-warning {{'exclusive_locks_required' attribute only applies to functions}}
1090 
1091 void elr_fun_params(int lvar EXCLUSIVE_LOCKS_REQUIRED(mu1)); // \
1092   // expected-warning {{'exclusive_locks_required' attribute only applies to functions}}
1093 
1094 class ElrFoo {
1095  private:
1096   int test_field EXCLUSIVE_LOCKS_REQUIRED(mu1); // \
1097     // expected-warning {{'exclusive_locks_required' attribute only applies to functions}}
1098   void test_method() EXCLUSIVE_LOCKS_REQUIRED(mu1);
1099 };
1100 
EXCLUSIVE_LOCKS_REQUIRED(mu1)1101 class EXCLUSIVE_LOCKS_REQUIRED(mu1) ElrTestClass { // \
1102   // expected-warning {{'exclusive_locks_required' attribute only applies to functions}}
1103 };
1104 
1105 // Check argument parsing.
1106 
1107 // legal attribute arguments
1108 int elr_function_1() EXCLUSIVE_LOCKS_REQUIRED(muWrapper.mu);
1109 int elr_function_2() EXCLUSIVE_LOCKS_REQUIRED(muDoubleWrapper.muWrapper->mu);
1110 int elr_function_3() EXCLUSIVE_LOCKS_REQUIRED(muWrapper.getMu());
1111 int elr_function_4() EXCLUSIVE_LOCKS_REQUIRED(*muWrapper.getMuPointer());
1112 int elr_function_5() EXCLUSIVE_LOCKS_REQUIRED(&mu1);
1113 int elr_function_6() EXCLUSIVE_LOCKS_REQUIRED(muRef);
1114 int elr_function_7() EXCLUSIVE_LOCKS_REQUIRED(muDoubleWrapper.getWrapper()->getMu());
1115 int elr_function_8() EXCLUSIVE_LOCKS_REQUIRED(muPointer);
1116 
1117 
1118 // illegal attribute arguments
1119 int elr_function_bad_1() EXCLUSIVE_LOCKS_REQUIRED(1); // \
1120   // expected-warning {{'exclusive_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
1121 int elr_function_bad_2() EXCLUSIVE_LOCKS_REQUIRED("mu"); // \
1122   // expected-warning {{ignoring 'exclusive_locks_required' attribute because its argument is invalid}}
1123 int elr_function_bad_3() EXCLUSIVE_LOCKS_REQUIRED(muDoublePointer); // \
1124   // expected-warning {{'exclusive_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
1125 int elr_function_bad_4() EXCLUSIVE_LOCKS_REQUIRED(umu); // \
1126   // expected-warning {{'exclusive_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute}}
1127 
1128 
1129 
1130 
1131 //-----------------------------------------//
1132 //  Shared Locks Required (slr)
1133 //-----------------------------------------//
1134 
1135 #if !__has_attribute(shared_locks_required)
1136 #error "Should support shared_locks_required attribute"
1137 #endif
1138 
1139 // takes one or more arguments, all locks (vars/fields)
1140 
1141 void slr_function() __attribute__((shared_locks_required)); // \
1142   // expected-error {{'shared_locks_required' attribute takes at least 1 argument}}
1143 
1144 void slr_function_arg() SHARED_LOCKS_REQUIRED(mu1);
1145 
1146 void slr_function_args() SHARED_LOCKS_REQUIRED(mu1, mu2);
1147 
1148 int slr_testfn(int y) SHARED_LOCKS_REQUIRED(mu1);
1149 
slr_testfn(int y)1150 int slr_testfn(int y) {
1151   int x SHARED_LOCKS_REQUIRED(mu1) = y; // \
1152     // expected-warning {{'shared_locks_required' attribute only applies to functions}}
1153   return x;
1154 };
1155 
1156 int slr_test_var SHARED_LOCKS_REQUIRED(mu1); // \
1157   // expected-warning {{'shared_locks_required' attribute only applies to functions}}
1158 
1159 void slr_fun_params(int lvar SHARED_LOCKS_REQUIRED(mu1)); // \
1160   // expected-warning {{'shared_locks_required' attribute only applies to functions}}
1161 
1162 class SlrFoo {
1163  private:
1164   int test_field SHARED_LOCKS_REQUIRED(mu1); // \
1165     // expected-warning {{'shared_locks_required' attribute only applies to functions}}
1166   void test_method() SHARED_LOCKS_REQUIRED(mu1);
1167 };
1168 
SHARED_LOCKS_REQUIRED(mu1)1169 class SHARED_LOCKS_REQUIRED(mu1) SlrTestClass { // \
1170   // expected-warning {{'shared_locks_required' attribute only applies to functions}}
1171 };
1172 
1173 // Check argument parsing.
1174 
1175 // legal attribute arguments
1176 int slr_function_1() SHARED_LOCKS_REQUIRED(muWrapper.mu);
1177 int slr_function_2() SHARED_LOCKS_REQUIRED(muDoubleWrapper.muWrapper->mu);
1178 int slr_function_3() SHARED_LOCKS_REQUIRED(muWrapper.getMu());
1179 int slr_function_4() SHARED_LOCKS_REQUIRED(*muWrapper.getMuPointer());
1180 int slr_function_5() SHARED_LOCKS_REQUIRED(&mu1);
1181 int slr_function_6() SHARED_LOCKS_REQUIRED(muRef);
1182 int slr_function_7() SHARED_LOCKS_REQUIRED(muDoubleWrapper.getWrapper()->getMu());
1183 int slr_function_8() SHARED_LOCKS_REQUIRED(muPointer);
1184 
1185 
1186 // illegal attribute arguments
1187 int slr_function_bad_1() SHARED_LOCKS_REQUIRED(1); // \
1188   // expected-warning {{'shared_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
1189 int slr_function_bad_2() SHARED_LOCKS_REQUIRED("mu"); // \
1190   // expected-warning {{ignoring 'shared_locks_required' attribute because its argument is invalid}}
1191 int slr_function_bad_3() SHARED_LOCKS_REQUIRED(muDoublePointer); // \
1192   // expected-warning {{'shared_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'Mutex **'}}
1193 int slr_function_bad_4() SHARED_LOCKS_REQUIRED(umu); // \
1194   // expected-warning {{'shared_locks_required' attribute requires arguments whose type is annotated with 'capability' attribute}}
1195 
1196 
1197 //-----------------------------------------//
1198 //  Regression tests for unusual cases.
1199 //-----------------------------------------//
1200 
trivially_false_edges(bool b)1201 int trivially_false_edges(bool b) {
1202   // Create NULL (never taken) edges in CFG
1203   if (false) return 1;
1204   else       return 2;
1205 }
1206 
1207 // Possible Clang bug -- method pointer in template parameter
1208 class UnFoo {
1209 public:
1210   void foo();
1211 };
1212 
1213 template<void (UnFoo::*methptr)()>
1214 class MCaller {
1215 public:
call_method_ptr(UnFoo * f)1216   static void call_method_ptr(UnFoo *f) {
1217     // FIXME: Possible Clang bug:
1218     // getCalleeDecl() returns NULL in the following case:
1219     (f->*methptr)();
1220   }
1221 };
1222 
call_method_ptr_inst(UnFoo * f)1223 void call_method_ptr_inst(UnFoo* f) {
1224   MCaller<&UnFoo::foo>::call_method_ptr(f);
1225 }
1226 
1227 int temp;
empty_back_edge()1228 void empty_back_edge() {
1229   // Create a back edge to a block with with no statements
1230   for (;;) {
1231     ++temp;
1232     if (temp > 10) break;
1233   }
1234 }
1235 
1236 struct Foomger {
1237   void operator++();
1238 };
1239 
1240 struct Foomgoper {
1241   Foomger f;
1242 
1243   bool done();
invalid_back_edgeFoomgoper1244   void invalid_back_edge() {
1245     do {
1246       // FIXME: Possible Clang bug:
1247       // The first statement in this basic block has no source location
1248       ++f;
1249     } while (!done());
1250   }
1251 };
1252 
1253 
1254 //-----------------------------------------------------
1255 // Parsing of member variables and function parameters
1256 //------------------------------------------------------
1257 
1258 Mutex gmu;
1259 
1260 class StaticMu {
1261   static Mutex statmu;
1262 };
1263 
1264 class FooLate {
1265 public:
foo1()1266   void foo1()           EXCLUSIVE_LOCKS_REQUIRED(gmu)   { }
foo2()1267   void foo2()           EXCLUSIVE_LOCKS_REQUIRED(mu)    { }
foo3(Mutex * m)1268   void foo3(Mutex *m)   EXCLUSIVE_LOCKS_REQUIRED(m)     { }
foo3(FooLate * f)1269   void foo3(FooLate *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu) { }
1270   void foo4(FooLate *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu);
1271 
1272   static void foo5()    EXCLUSIVE_LOCKS_REQUIRED(mu);
1273 //FIXME: Bug 32066 - Error should be emitted irrespective of C++ dialect
1274 #if __cplusplus <= 199711L
1275   // expected-error@-3 {{invalid use of member 'mu' in static member function}}
1276 #endif
1277 
1278   template <class T>
foo6()1279   void foo6() EXCLUSIVE_LOCKS_REQUIRED(T::statmu) { }
1280 
1281   template <class T>
foo7(T * f)1282   void foo7(T* f) EXCLUSIVE_LOCKS_REQUIRED(f->mu) { }
1283 
1284   int a GUARDED_BY(gmu);
1285   int b GUARDED_BY(mu);
1286   int c GUARDED_BY(this->mu);
1287 
1288   Mutex mu;
1289 };
1290 
1291 //-------------------------
1292 // Empty argument lists
1293 //-------------------------
1294 
1295 class LOCKABLE EmptyArgListsTest {
lock()1296   void lock() EXCLUSIVE_LOCK_FUNCTION() { }
unlock()1297   void unlock() UNLOCK_FUNCTION() { }
1298 };
1299 
1300 
1301 namespace FunctionDefinitionParseTest {
1302 // Test parsing of attributes on function definitions.
1303 
1304 class Foo {
1305 public:
1306   Mutex mu_;
1307   void foo1();
1308   void foo2(Foo *f);
1309 };
1310 
1311 template <class T>
1312 class Bar {
1313 public:
1314   Mutex mu_;
1315   void bar();
1316 };
1317 
foo1()1318 void Foo::foo1()       EXCLUSIVE_LOCKS_REQUIRED(mu_) { }
foo2(Foo * f)1319 void Foo::foo2(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { }
1320 
1321 template <class T>
bar()1322 void Bar<T>::bar() EXCLUSIVE_LOCKS_REQUIRED(mu_) { }
1323 
baz(Foo * f)1324 void baz(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { }
1325 
1326 } // end namespace
1327 
1328 
1329 namespace TestMultiDecl {
1330 
1331 class Foo {
1332 public:
1333   int GUARDED_BY(mu_) a;
1334   int GUARDED_BY(mu_) b, c;
1335 
1336 private:
1337   Mutex mu_;
1338 };
1339 
1340 } // end namespace TestMultiDecl
1341 
1342 
1343 namespace NestedClassLateDecl {
1344 
1345 class Foo {
1346   class Bar {
1347     int a GUARDED_BY(mu);
1348     int b GUARDED_BY(fooMuStatic);
1349 
bar()1350     void bar()        EXCLUSIVE_LOCKS_REQUIRED(mu)       { a = 0;    }
bar2(Bar * b)1351     void bar2(Bar* b) EXCLUSIVE_LOCKS_REQUIRED(b->mu)    { b->a = 0; }
bar3(Foo * f)1352     void bar3(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(f->fooMu) { f->a = 0; }
1353 
1354     Mutex mu;
1355   };
1356 
1357   int a GUARDED_BY(fooMu);
1358   Mutex fooMu;
1359   static Mutex fooMuStatic;
1360 };
1361 
1362 }
1363 
1364 namespace PointerToMemberTest {
1365 
1366 // Empty string should be ignored.
1367 int  testEmptyAttribute GUARDED_BY("");
1368 void testEmptyAttributeFunction() EXCLUSIVE_LOCKS_REQUIRED("");
1369 
1370 class Graph {
1371 public:
1372   Mutex mu_;
1373 
1374   static Mutex* get_static_mu() LOCK_RETURNED(&Graph::mu_);
1375 };
1376 
1377 class Node {
1378 public:
1379   void foo() EXCLUSIVE_LOCKS_REQUIRED(&Graph::mu_);
1380   int a GUARDED_BY(&Graph::mu_);
1381 };
1382 
1383 }
1384 
1385 
1386 namespace SmartPointerTest {
1387 
1388 template<class T>
1389 class smart_ptr {
1390  public:
operator ->()1391   T* operator->() { return ptr_; }
operator *()1392   T& operator*()  { return ptr_; }
1393 
1394  private:
1395   T* ptr_;
1396 };
1397 
1398 
1399 Mutex gmu;
1400 smart_ptr<int> gdat PT_GUARDED_BY(gmu);
1401 
1402 
1403 class MyClass {
1404 public:
1405   Mutex mu_;
1406   smart_ptr<Mutex> smu_;
1407 
1408 
1409   smart_ptr<int> a PT_GUARDED_BY(mu_);
1410   int b            GUARDED_BY(smu_);
1411 };
1412 
1413 }
1414 
1415 
1416 namespace InheritanceTest {
1417 
1418 class LOCKABLE Base {
1419  public:
1420   void lock()   EXCLUSIVE_LOCK_FUNCTION();
1421   void unlock() UNLOCK_FUNCTION();
1422 };
1423 
1424 class Base2 { };
1425 
1426 class Derived1 : public Base { };
1427 
1428 class Derived2 : public Base2, public Derived1 { };
1429 
1430 class Derived3 : public Base2 { };
1431 
1432 class Foo {
1433   Derived1 mu1_;
1434   Derived2 mu2_;
1435   Derived3 mu3_;
1436   int a GUARDED_BY(mu1_);
1437   int b GUARDED_BY(mu2_);
1438   int c GUARDED_BY(mu3_);  // \
1439     // expected-warning {{'guarded_by' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'InheritanceTest::Derived3'}}
1440 
foo()1441   void foo() EXCLUSIVE_LOCKS_REQUIRED(mu1_, mu2_) {
1442     a = 0;
1443     b = 0;
1444   }
1445 };
1446 
1447 }
1448 
1449 
1450 namespace InvalidDeclTest {
1451 
1452 class Foo { };
1453 namespace {
bar(Mutex * mu)1454 void Foo::bar(Mutex* mu) LOCKS_EXCLUDED(mu) { } // \
1455    // expected-error   {{cannot define or redeclare 'bar' here because namespace '' does not enclose namespace 'Foo'}} \
1456    // expected-warning {{attribute locks_excluded ignored, because it is not attached to a declaration}}
1457 }
1458 
1459 } // end namespace InvalidDeclTest
1460 
1461 
1462 namespace StaticScopeTest {
1463 
1464 class FooStream;
1465 
1466 class Foo {
1467   mutable Mutex mu;
1468   int a GUARDED_BY(mu);
1469 
1470   static int si GUARDED_BY(mu);
1471 //FIXME: Bug 32066 - Error should be emitted irrespective of C++ dialect
1472 #if __cplusplus <= 199711L
1473   // expected-error@-3 {{invalid use of non-static data member 'mu'}}
1474 #endif
1475 
1476   static void foo() EXCLUSIVE_LOCKS_REQUIRED(mu);
1477 //FIXME: Bug 32066 - Error should be emitted irrespective of C++ dialect
1478 #if __cplusplus <= 199711L
1479   // expected-error@-3 {{invalid use of member 'mu' in static member function}}
1480 #endif
1481 
1482   friend FooStream& operator<<(FooStream& s, const Foo& f)
1483     EXCLUSIVE_LOCKS_REQUIRED(mu);
1484 //FIXME: Bug 32066 - Error should be emitted irrespective of C++ dialect
1485 #if __cplusplus <= 199711L
1486     // expected-error@-3 {{invalid use of non-static data member 'mu'}}
1487 #endif
1488 };
1489 
1490 
1491 } // end namespace StaticScopeTest
1492 
1493 
1494 namespace FunctionAttributesInsideClass_ICE_Test {
1495 
1496 class Foo {
1497 public:
1498   /*  Originally found when parsing foo() as an ordinary method after the
1499    *  the following:
1500 
1501   template <class T>
1502   void syntaxErrorMethod(int i) {
1503     if (i) {
1504       foo(
1505     }
1506   }
1507   */
1508 
method()1509   void method() {
1510     void foo() EXCLUSIVE_LOCKS_REQUIRED(mu); // \
1511       // expected-error {{use of undeclared identifier 'mu'}}
1512   }
1513 };
1514 
1515 }  // end namespace FunctionAttributesInsideClass_ICE_Test
1516 
1517 
1518 #ifdef CPP11
1519 namespace CRASH_POST_R301735 {
1520   class SomeClass {
1521    public:
foo()1522      void foo() {
1523        auto l = [this] { auto l = [] () EXCLUSIVE_LOCKS_REQUIRED(mu_) {}; };
1524      }
1525      Mutex mu_;
1526    };
1527 }
1528 #endif