1// RUN: %clang_cc1 -fsyntax-only -Wthread-safety -verify %s
2
3typedef int __attribute__((capability("role"))) ThreadRole;
4struct __attribute__((shared_capability("mutex"))) Mutex {};
5struct NotACapability {};
6
7// Put capability attributes on unions
8union __attribute__((capability("mutex"))) MutexUnion { int a; char* b; };
9typedef union { int a; char* b; } __attribute__((capability("mutex"))) MutexUnion2;
10
11// Test an invalid capability name
12struct __attribute__((capability("wrong"))) IncorrectName {}; // expected-warning {{invalid capability name 'wrong'; capability name must be 'mutex' or 'role'}}
13
14int Test1 __attribute__((capability("test1")));  // expected-error {{'capability' attribute only applies to structs, unions, and typedefs}}
15int Test2 __attribute__((shared_capability("test2"))); // expected-error {{'shared_capability' attribute only applies to structs, unions, and typedefs}}
16int Test3 __attribute__((acquire_capability("test3")));  // expected-warning {{'acquire_capability' attribute only applies to functions}}
17int Test4 __attribute__((try_acquire_capability("test4"))); // expected-error {{'try_acquire_capability' attribute only applies to functions}}
18int Test5 __attribute__((release_capability("test5"))); // expected-warning {{'release_capability' attribute only applies to functions}}
19
20struct __attribute__((capability(12))) Test3 {}; // expected-error {{'capability' attribute requires a string}}
21struct __attribute__((shared_capability(Test2))) Test4 {}; // expected-error {{'shared_capability' attribute requires a string}}
22
23struct __attribute__((capability)) Test5 {}; // expected-error {{'capability' attribute takes one argument}}
24struct __attribute__((shared_capability("test1", 12))) Test6 {}; // expected-error {{'shared_capability' attribute takes one argument}}
25
26struct NotACapability BadCapability;
27ThreadRole GUI, Worker;
28void Func1(void) __attribute__((requires_capability(GUI))) {}
29void Func2(void) __attribute__((requires_shared_capability(Worker))) {}
30
31void Func3(void) __attribute__((requires_capability)) {}  // expected-error {{'requires_capability' attribute takes at least 1 argument}}
32void Func4(void) __attribute__((requires_shared_capability)) {}  // expected-error {{'requires_shared_capability' attribute takes at least 1 argument}}
33
34void Func5(void) __attribute__((requires_capability(1))) {}  // expected-warning {{'requires_capability' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
35void 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
37void Func7(void) __attribute__((assert_capability(GUI))) {}
38void Func8(void) __attribute__((assert_shared_capability(GUI))) {}
39
40void Func9(void) __attribute__((assert_capability())) {} // expected-error {{'assert_capability' attribute takes one argument}}
41void Func10(void) __attribute__((assert_shared_capability())) {} // expected-error {{'assert_shared_capability' attribute takes one argument}}
42
43void Func11(void) __attribute__((acquire_capability(GUI))) {}
44void Func12(void) __attribute__((acquire_shared_capability(GUI))) {}
45
46void Func15(void) __attribute__((release_capability(GUI))) {}
47void Func16(void) __attribute__((release_shared_capability(GUI))) {}
48void Func17(void) __attribute__((release_generic_capability(GUI))) {}
49
50void Func21(void) __attribute__((try_acquire_capability(1))) {}
51void Func22(void) __attribute__((try_acquire_shared_capability(1))) {}
52
53void Func23(void) __attribute__((try_acquire_capability(1, GUI))) {}
54void Func24(void) __attribute__((try_acquire_shared_capability(1, GUI))) {}
55
56void Func25(void) __attribute__((try_acquire_capability())) {} // expected-error {{'try_acquire_capability' attribute takes at least 1 argument}}
57void Func26(void) __attribute__((try_acquire_shared_capability())) {} // expected-error {{'try_acquire_shared_capability' attribute takes at least 1 argument}}
58
59// Test that boolean logic works with capability attributes
60void Func27(void) __attribute__((requires_capability(!GUI)));
61void Func28(void) __attribute__((requires_capability(GUI && Worker)));
62void Func29(void) __attribute__((requires_capability(GUI || Worker)));
63void Func30(void) __attribute__((requires_capability((Worker || Worker) && !GUI)));
64
65int AlsoNotACapability;
66void 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'}}
67