blob: 80021020ca66cc4c80b1fee5cb7c8939603b6920 [file] [log] [blame]
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +00001// RUN: %clang_cc1 -fsyntax-only -verify -Wconsumed -std=c++11 %s
2
DeLesley Hutchins66540852013-10-04 21:28:06 +00003#define CALLABLE_WHEN(...) __attribute__ ((callable_when(__VA_ARGS__)))
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +00004#define CONSUMABLE(state) __attribute__ ((consumable(state)))
5#define SET_TYPESTATE(state) __attribute__ ((set_typestate(state)))
6#define RETURN_TYPESTATE(state) __attribute__ ((return_typestate(state)))
7#define TESTS_TYPESTATE(state) __attribute__ ((tests_typestate(state)))
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +00008
DeLesley Hutchins50949202013-09-03 22:35:53 +00009// FIXME: This test is here because the warning is issued by the Consumed
10// analysis, not SemaDeclAttr. The analysis won't run after an error
11// has been issued. Once the attribute propagation for template
12// instantiation has been fixed, this can be moved somewhere else and the
13// definition can be removed.
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +000014int returnTypestateForUnconsumable() RETURN_TYPESTATE(consumed); // expected-warning {{return state set for an unconsumable type 'int'}}
15int returnTypestateForUnconsumable() {
16 return 0;
17}
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +000018
19class AttrTester0 {
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +000020 void consumes() __attribute__ ((set_typestate())); // expected-error {{attribute takes one argument}}
DeLesley Hutchins1bf63432013-10-11 22:30:48 +000021 bool testsUnconsumed() __attribute__ ((tests_typestate())); // expected-error {{attribute takes one argument}}
DeLesley Hutchins66540852013-10-04 21:28:06 +000022 void callableWhen() __attribute__ ((callable_when())); // expected-error {{attribute takes at least 1 argument}}
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +000023};
24
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +000025int var0 SET_TYPESTATE(consumed); // expected-warning {{'set_typestate' attribute only applies to methods}}
DeLesley Hutchins1bf63432013-10-11 22:30:48 +000026int var1 TESTS_TYPESTATE(consumed); // expected-warning {{'tests_typestate' attribute only applies to methods}}
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +000027int var2 CALLABLE_WHEN("consumed"); // expected-warning {{'callable_when' attribute only applies to methods}}
David Blaikiea33ab602013-09-06 01:28:43 +000028int var3 CONSUMABLE(consumed); // expected-warning {{'consumable' attribute only applies to classes}}
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +000029int var4 RETURN_TYPESTATE(consumed); // expected-warning {{'return_typestate' attribute only applies to functions}}
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +000030
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +000031void function0() SET_TYPESTATE(consumed); // expected-warning {{'set_typestate' attribute only applies to methods}}
DeLesley Hutchins1bf63432013-10-11 22:30:48 +000032void function1() TESTS_TYPESTATE(consumed); // expected-warning {{'tests_typestate' attribute only applies to methods}}
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +000033void function2() CALLABLE_WHEN("consumed"); // expected-warning {{'callable_when' attribute only applies to methods}}
David Blaikiea33ab602013-09-06 01:28:43 +000034void function3() CONSUMABLE(consumed); // expected-warning {{'consumable' attribute only applies to classes}}
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +000035
David Blaikiea33ab602013-09-06 01:28:43 +000036class CONSUMABLE(unknown) AttrTester1 {
DeLesley Hutchins66540852013-10-04 21:28:06 +000037 void callableWhen0() CALLABLE_WHEN("unconsumed");
38 void callableWhen1() CALLABLE_WHEN(42); // expected-error {{'callable_when' attribute requires a string}}
39 void callableWhen2() CALLABLE_WHEN("foo"); // expected-warning {{'callable_when' attribute argument not supported: foo}}
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +000040 void consumes() SET_TYPESTATE(consumed);
DeLesley Hutchins1bf63432013-10-11 22:30:48 +000041 bool testsUnconsumed() TESTS_TYPESTATE(consumed);
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +000042};
43
Aaron Ballmand0686072013-09-11 19:47:58 +000044AttrTester1 returnTypestateTester0() RETURN_TYPESTATE(not_a_state); // expected-warning {{'return_typestate' attribute argument not supported: 'not_a_state'}}
DeLesley Hutchins0e8534e2013-09-03 20:11:38 +000045AttrTester1 returnTypestateTester1() RETURN_TYPESTATE(42); // expected-error {{'return_typestate' attribute requires an identifier}}
46
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +000047class AttrTester2 {
DeLesley Hutchins66540852013-10-04 21:28:06 +000048 void callableWhen() CALLABLE_WHEN("unconsumed"); // expected-warning {{consumed analysis attribute is attached to member of class 'AttrTester2' which isn't marked as consumable}}
DeLesley Hutchinsf30e1942013-10-11 23:03:26 +000049 void consumes() SET_TYPESTATE(consumed); // expected-warning {{consumed analysis attribute is attached to member of class 'AttrTester2' which isn't marked as consumable}}
DeLesley Hutchins1bf63432013-10-11 22:30:48 +000050 bool testsUnconsumed() TESTS_TYPESTATE(consumed); // expected-warning {{consumed analysis attribute is attached to member of class 'AttrTester2' which isn't marked as consumable}}
DeLesley Hutchinsdf7bef02013-08-12 21:20:55 +000051};
David Blaikiea33ab602013-09-06 01:28:43 +000052
53class CONSUMABLE(42) AttrTester3; // expected-error {{'consumable' attribute requires an identifier}}