xref: /llvm-project/clang/test/Sema/annotate-type.c (revision 8c7b64b5ae2a09027c38db969a04fc9ddd0cd6bb)
1 // RUN: %clang_cc1 %s -fsyntax-only -fdouble-square-bracket-attributes -verify
2 
3 const char *some_function();
4 
5 void foo(float *[[clang::annotate_type("foo")]] a) {
6   int [[clang::annotate_type("bar")]] x1;
7   int *[[clang::annotate_type("bar")]] x2;
8   int *[[clang::annotate_type("bar", 1)]] x3;
9   int *[[clang::annotate_type("bar", 1 + 2)]] x4;
10   struct {} [[clang::annotate_type("foo")]] x5;
11   int [[clang::annotate_type("int")]] *[[clang::annotate_type("ptr")]] array[10] [[clang::annotate_type("arr")]];
12 
13   typedef int [[clang::annotate_type("bar")]] my_typedef;
14 
15   // GNU spelling is not supported
16   int __attribute__((annotate_type("bar"))) y1;  // expected-warning {{unknown attribute 'annotate_type' ignored}}
17   int *__attribute__((annotate_type("bar"))) y2; // expected-warning {{unknown attribute 'annotate_type' ignored}}
18 
19   // Various error cases
20   [[clang::annotate_type("bar")]] int *z1; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
21   int *z2 [[clang::annotate_type("bar")]]; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
22   [[clang::annotate_type("bar")]]; // expected-error {{'annotate_type' attribute cannot be applied to a statement}}
23   int *[[clang::annotate_type(1)]] z3; // expected-error {{'annotate_type' attribute requires a string}}
24   int *[[clang::annotate_type()]] z4; // expected-error {{'annotate_type' attribute takes at least 1 argument}}
25   int *[[clang::annotate_type]] z5; // expected-error {{'annotate_type' attribute takes at least 1 argument}}
26   int *[[clang::annotate_type(some_function())]] z6; // expected-error {{'annotate_type' attribute requires a string}}
27   int *[[clang::annotate_type("bar", some_function())]] z7; // expected-error {{'annotate_type' attribute requires parameter 1 to be a constant expression}} expected-note{{subexpression not valid in a constant expression}}
28   int *[[clang::annotate_type("bar", z7)]] z8; // expected-error {{'annotate_type' attribute requires parameter 1 to be a constant expression}} expected-note{{subexpression not valid in a constant expression}}
29   int *[[clang::annotate_type("bar", int)]] z9; // expected-error {{expected expression}}
30 }
31 // More error cases: Prohibit adding the attribute to declarations.
32 // Different declarations hit different code paths, so they need separate tests.
33 [[clang::annotate_type("bar")]] int *global; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
34 [[clang::annotate_type("bar")]] void annotated_function(); // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
35 void g([[clang::annotate_type("bar")]] int); // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
36 struct [[clang::annotate_type("foo")]] S; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
37 struct [[clang::annotate_type("foo")]] S{ // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
38   [[clang::annotate_type("foo")]] int member; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
39   [[clang::annotate_type("foo")]] union { // expected-error {{an attribute list cannot appear here}}
40     int i;
41     float f;
42   };
43 };
44