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