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