16bc8f9a21cdbf3f043455ff78f9ece66f9f2c3d9Aaron Ballman// RUN: %clang_cc1 -fsyntax-only -verify %s
268fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian// rdar://9584012
368fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian
468fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahaniantypedef struct {
568fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian	char *str;
668fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian} Class;
768fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian
868fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahaniantypedef union {
968fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian	Class *object;
1068fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian} Instance __attribute__((transparent_union));
1168fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian
1268fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian__attribute__((nonnull(1))) void Class_init(Instance this, char *str) {
1368fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian	this.object->str = str;
1468fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian}
1568fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian
1668fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanianint main(void) {
1768fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian	Class *obj;
1868fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian	Class_init(0, "Hello World"); // expected-warning {{null passed to a callee which requires a non-null argument}}
1968fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian	Class_init(obj, "Hello World");
2068fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian}
2168fe96adf787abd1e98016da0f38e26644faf7b9Fariborz Jahanian
22b3d7efe0f95bc44f0e93602f4617a53ca3b66e3aAaron Ballmanvoid foo(const char *str) __attribute__((nonnull("foo"))); // expected-error{{'nonnull' attribute requires parameter 1 to be an integer constant}}
23651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid bar(int i) __attribute__((nonnull(1))); // expected-warning {{'nonnull' attribute only applies to pointer arguments}} expected-warning {{'nonnull' attribute applied to function with no pointer arguments}}
24651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
25651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid baz(__attribute__((nonnull)) const char *str);
26651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid baz2(__attribute__((nonnull(1))) const char *str); // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
27651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid baz3(__attribute__((nonnull)) int x); // expected-warning {{'nonnull' attribute only applies to pointer arguments}}
28651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
29651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid test_baz() {
30651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  baz(0); // expected-warning {{null passed to a callee which requires a non-null argument}}
31651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  baz2(0); // no-warning
32651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  baz3(0); // no-warning
33651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
34651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
35651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid test_void_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
36651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesint test_int_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
37651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid *test_ptr_returns_nonnull(void) __attribute__((returns_nonnull)); // no-warning
38651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
39651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesint i __attribute__((nonnull)); // expected-warning {{'nonnull' attribute only applies to functions, methods, and parameters}}
40651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesint j __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to functions and methods}}
41651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid *test_no_fn_proto() __attribute__((returns_nonnull));  // expected-warning {{'returns_nonnull' attribute only applies to functions and methods}}
42651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
43651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines__attribute__((returns_nonnull))
44651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid *test_bad_returns_null(void) {
45651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return 0; // expected-warning {{null returned from function that requires a non-null return value}}
46651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
47651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
48651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid PR18795(int (*g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) {
49651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  g(0); // expected-warning{{null passed to a callee which requires a non-null argument}}
50651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
51651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid PR18795_helper() {
52651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  PR18795(0); // expected-warning{{null passed to a callee which requires a non-null argument}}
53651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
54651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
55651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
56