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