Alexander Kornienko | 36155dd | 2014-03-21 00:07:27 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wthread-safety -verify %s |
| 2 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 3 | typedef int __attribute__((capability("role"))) ThreadRole; |
Alexander Kornienko | 36155dd | 2014-03-21 00:07:27 +0000 | [diff] [blame] | 4 | struct __attribute__((shared_capability("mutex"))) Mutex {}; |
| 5 | struct NotACapability {}; |
| 6 | |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 7 | // Put capability attributes on unions |
| 8 | union __attribute__((capability("mutex"))) MutexUnion { int a; char* b; }; |
| 9 | typedef union { int a; char* b; } __attribute__((capability("mutex"))) MutexUnion2; |
| 10 | |
Alexander Kornienko | 36155dd | 2014-03-21 00:07:27 +0000 | [diff] [blame] | 11 | // Test an invalid capability name |
| 12 | struct __attribute__((capability("wrong"))) IncorrectName {}; // expected-warning {{invalid capability name 'wrong'; capability name must be 'mutex' or 'role'}} |
| 13 | |
Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 14 | int Test1 __attribute__((capability("test1"))); // expected-error {{'capability' attribute only applies to structs, unions, classes, and typedefs}} |
| 15 | int Test2 __attribute__((shared_capability("test2"))); // expected-error {{'shared_capability' attribute only applies to structs, unions, classes, and typedefs}} |
Alexander Kornienko | 36155dd | 2014-03-21 00:07:27 +0000 | [diff] [blame] | 16 | int Test3 __attribute__((acquire_capability("test3"))); // expected-warning {{'acquire_capability' attribute only applies to functions}} |
| 17 | int Test4 __attribute__((try_acquire_capability("test4"))); // expected-error {{'try_acquire_capability' attribute only applies to functions}} |
| 18 | int Test5 __attribute__((release_capability("test5"))); // expected-warning {{'release_capability' attribute only applies to functions}} |
| 19 | |
| 20 | struct __attribute__((capability(12))) Test3 {}; // expected-error {{'capability' attribute requires a string}} |
| 21 | struct __attribute__((shared_capability(Test2))) Test4 {}; // expected-error {{'shared_capability' attribute requires a string}} |
| 22 | |
| 23 | struct __attribute__((capability)) Test5 {}; // expected-error {{'capability' attribute takes one argument}} |
| 24 | struct __attribute__((shared_capability("test1", 12))) Test6 {}; // expected-error {{'shared_capability' attribute takes one argument}} |
| 25 | |
| 26 | struct NotACapability BadCapability; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 27 | ThreadRole GUI, Worker; |
Alexander Kornienko | 36155dd | 2014-03-21 00:07:27 +0000 | [diff] [blame] | 28 | void Func1(void) __attribute__((requires_capability(GUI))) {} |
| 29 | void Func2(void) __attribute__((requires_shared_capability(Worker))) {} |
| 30 | |
| 31 | void Func3(void) __attribute__((requires_capability)) {} // expected-error {{'requires_capability' attribute takes at least 1 argument}} |
| 32 | void Func4(void) __attribute__((requires_shared_capability)) {} // expected-error {{'requires_shared_capability' attribute takes at least 1 argument}} |
| 33 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 34 | void Func5(void) __attribute__((requires_capability(1))) {} // expected-warning {{'requires_capability' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} |
Alexander Kornienko | 36155dd | 2014-03-21 00:07:27 +0000 | [diff] [blame] | 35 | void Func6(void) __attribute__((requires_shared_capability(BadCapability))) {} // expected-warning {{'requires_shared_capability' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'struct NotACapability'}} |
| 36 | |
| 37 | void Func7(void) __attribute__((assert_capability(GUI))) {} |
| 38 | void Func8(void) __attribute__((assert_shared_capability(GUI))) {} |
| 39 | |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 40 | void Func9(void) __attribute__((assert_capability())) {} // expected-warning {{'assert_capability' attribute without capability arguments can only be applied to non-static methods of a class}} |
| 41 | void Func10(void) __attribute__((assert_shared_capability())) {} // expected-warning {{'assert_shared_capability' attribute without capability arguments can only be applied to non-static methods of a class}} |
| 42 | |
Alexander Kornienko | 36155dd | 2014-03-21 00:07:27 +0000 | [diff] [blame] | 43 | void Func11(void) __attribute__((acquire_capability(GUI))) {} |
| 44 | void Func12(void) __attribute__((acquire_shared_capability(GUI))) {} |
| 45 | |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 46 | void Func13(void) __attribute__((acquire_capability())) {} // expected-warning {{'acquire_capability' attribute without capability arguments can only be applied to non-static methods of a class}} |
| 47 | void Func14(void) __attribute__((acquire_shared_capability())) {} // expected-warning {{'acquire_shared_capability' attribute without capability arguments can only be applied to non-static methods of a class}} |
| 48 | |
Alexander Kornienko | 36155dd | 2014-03-21 00:07:27 +0000 | [diff] [blame] | 49 | void Func15(void) __attribute__((release_capability(GUI))) {} |
| 50 | void Func16(void) __attribute__((release_shared_capability(GUI))) {} |
| 51 | void Func17(void) __attribute__((release_generic_capability(GUI))) {} |
| 52 | |
Aaron Puchert | 7ba1ab7 | 2018-09-20 00:39:27 +0000 | [diff] [blame] | 53 | void Func18(void) __attribute__((release_capability())) {} // expected-warning {{'release_capability' attribute without capability arguments can only be applied to non-static methods of a class}} |
| 54 | void Func19(void) __attribute__((release_shared_capability())) {} // expected-warning {{'release_shared_capability' attribute without capability arguments can only be applied to non-static methods of a class}} |
| 55 | void Func20(void) __attribute__((release_generic_capability())) {} // expected-warning {{'release_generic_capability' attribute without capability arguments can only be applied to non-static methods of a class}} |
| 56 | |
| 57 | void Func21(void) __attribute__((try_acquire_capability(1))) {} // expected-warning {{'try_acquire_capability' attribute without capability arguments can only be applied to non-static methods of a class}} |
| 58 | void Func22(void) __attribute__((try_acquire_shared_capability(1))) {} // expected-warning {{'try_acquire_shared_capability' attribute without capability arguments can only be applied to non-static methods of a class}} |
Alexander Kornienko | 36155dd | 2014-03-21 00:07:27 +0000 | [diff] [blame] | 59 | |
| 60 | void Func23(void) __attribute__((try_acquire_capability(1, GUI))) {} |
| 61 | void Func24(void) __attribute__((try_acquire_shared_capability(1, GUI))) {} |
| 62 | |
| 63 | void Func25(void) __attribute__((try_acquire_capability())) {} // expected-error {{'try_acquire_capability' attribute takes at least 1 argument}} |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 64 | void Func26(void) __attribute__((try_acquire_shared_capability())) {} // expected-error {{'try_acquire_shared_capability' attribute takes at least 1 argument}} |
| 65 | |
| 66 | // Test that boolean logic works with capability attributes |
| 67 | void Func27(void) __attribute__((requires_capability(!GUI))); |
| 68 | void Func28(void) __attribute__((requires_capability(GUI && Worker))); |
| 69 | void Func29(void) __attribute__((requires_capability(GUI || Worker))); |
| 70 | void Func30(void) __attribute__((requires_capability((Worker || Worker) && !GUI))); |
| 71 | |
| 72 | int AlsoNotACapability; |
| 73 | void Func31(void) __attribute__((requires_capability(GUI && AlsoNotACapability))); // expected-warning {{'requires_capability' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} |