attr-sentinel.c revision 8eb662ed5e04bd0f494c7dbefacb7d45660ab9fa
1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1  -fsyntax-only -verify %s
2236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian
3236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian#define NULL (void*)0
4236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian
5236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian#define ATTR __attribute__ ((__sentinel__))
6236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian
78eb662ed5e04bd0f494c7dbefacb7d45660ab9faJohn McCallvoid foo1 (int x, ...) ATTR; // expected-note 2 {{function has been explicitly marked sentinel here}}
8236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanianvoid foo5 (int x, ...) __attribute__ ((__sentinel__(1))); // expected-note {{function has been explicitly marked sentinel here}}
9236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanianvoid foo6 (int x, ...) __attribute__ ((__sentinel__(5))); // expected-note {{function has been explicitly marked sentinel here}}
10236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanianvoid foo7 (int x, ...) __attribute__ ((__sentinel__(0))); // expected-note {{function has been explicitly marked sentinel here}}
11236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanianvoid foo10 (int x, ...) __attribute__ ((__sentinel__(1,1)));
12236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanianvoid foo12 (int x, ... ) ATTR; // expected-note {{function has been explicitly marked sentinel here}}
13236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian
14b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattnervoid test1() {
15236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian  foo1(1, NULL); // OK
16236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian  foo1(1, 0) ; // expected-warning {{missing sentinel in function call}}
17236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian  foo5(1, NULL, 2);  // OK
18236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian  foo5(1,2,NULL, 1); // OK
191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  foo5(1, NULL, 2, 1); // expected-warning {{missing sentinel in function call}}
20236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian
21236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian  foo6(1,2,3,4,5,6,7); // expected-warning {{missing sentinel in function call}}
22236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian  foo6(1,NULL,3,4,5,6,7); // OK
23236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian  foo7(1); // expected-warning {{not enough variable arguments in 'foo7' declaration to fit a sentinel}}
24236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian  foo7(1, NULL); // OK
25236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian
26236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian  foo12(1); // expected-warning {{not enough variable arguments in 'foo12' declaration to fit a sentinel}}
278eb662ed5e04bd0f494c7dbefacb7d45660ab9faJohn McCall
288eb662ed5e04bd0f494c7dbefacb7d45660ab9faJohn McCall  // PR 5685
298eb662ed5e04bd0f494c7dbefacb7d45660ab9faJohn McCall  struct A {};
308eb662ed5e04bd0f494c7dbefacb7d45660ab9faJohn McCall  struct A a, b, c;
318eb662ed5e04bd0f494c7dbefacb7d45660ab9faJohn McCall  foo1(3, &a, &b, &c); // expected-warning {{missing sentinel in function call}}
328eb662ed5e04bd0f494c7dbefacb7d45660ab9faJohn McCall  foo1(3, &a, &b, &c, (struct A*) 0);
33236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian}
34236673e8fbd391cc7827efcaa64320344900d348Fariborz Jahanian
35b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner
36b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner
37b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattnervoid (*e) (int arg, const char * format, ...) __attribute__ ((__sentinel__ (1,1)));
38b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner
39b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattnervoid test2() {
40b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner  void (*b) (int arg, const char * format, ...) __attribute__ ((__sentinel__));  // expected-note {{function has been explicitly marked sentinel here}}
41b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner  void (*z) (int arg, const char * format, ...) __attribute__ ((__sentinel__ (2))); // expected-note {{function has been explicitly marked sentinel here}}
42b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner
43b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner
44b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner  void (*y) (int arg, const char * format, ...) __attribute__ ((__sentinel__ (5))); // expected-note {{function has been explicitly marked sentinel here}}
45b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner
46b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner  b(1, "%s", (void*)0); // OK
47b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner  b(1, "%s", 0);  // expected-warning {{missing sentinel in function call}}
48b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner  z(1, "%s",4 ,1,0);  // expected-warning {{missing sentinel in function call}}
49b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner  z(1, "%s", (void*)0, 1, 0); // OK
50b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner
51b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner  y(1, "%s", 1,2,3,4,5,6,7);  // expected-warning {{missing sentinel in function call}}
52b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner
53b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner  y(1, "%s", (void*)0,3,4,5,6,7); // OK
54b1332d7a5228600ae86276b12883ec0ac9500ec1Chris Lattner}
55