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