xref: /llvm-project/clang/test/Sema/annotate-type.c (revision c57b9f5a138d9a7ce0744e4fdb85ff31dc5f2cac)
1874217f9SNikolas Klauser // RUN: %clang_cc1 %s -fsyntax-only -verify
2*c57b9f5aSTimm Baeder // RUN: %clang_cc1 %s -fsyntax-only -verify -fexperimental-new-constant-interpreter
3665da187SMartin Boehme 
4665da187SMartin Boehme const char *some_function();
5665da187SMartin Boehme 
6665da187SMartin Boehme void foo(float *[[clang::annotate_type("foo")]] a) {
7665da187SMartin Boehme   int [[clang::annotate_type("bar")]] x1;
8665da187SMartin Boehme   int *[[clang::annotate_type("bar")]] x2;
9665da187SMartin Boehme   int *[[clang::annotate_type("bar", 1)]] x3;
10665da187SMartin Boehme   int *[[clang::annotate_type("bar", 1 + 2)]] x4;
11665da187SMartin Boehme   struct {} [[clang::annotate_type("foo")]] x5;
12665da187SMartin Boehme   int [[clang::annotate_type("int")]] *[[clang::annotate_type("ptr")]] array[10] [[clang::annotate_type("arr")]];
13665da187SMartin Boehme 
14665da187SMartin Boehme   typedef int [[clang::annotate_type("bar")]] my_typedef;
15665da187SMartin Boehme 
16665da187SMartin Boehme   // GNU spelling is not supported
17665da187SMartin Boehme   int __attribute__((annotate_type("bar"))) y1;  // expected-warning {{unknown attribute 'annotate_type' ignored}}
18665da187SMartin Boehme   int *__attribute__((annotate_type("bar"))) y2; // expected-warning {{unknown attribute 'annotate_type' ignored}}
19665da187SMartin Boehme 
20665da187SMartin Boehme   // Various error cases
218c7b64b5SMartin Boehme   [[clang::annotate_type("bar")]] int *z1; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
228c7b64b5SMartin Boehme   int *z2 [[clang::annotate_type("bar")]]; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
23665da187SMartin Boehme   [[clang::annotate_type("bar")]]; // expected-error {{'annotate_type' attribute cannot be applied to a statement}}
2498322d3eSCorentin Jabot   int *[[clang::annotate_type(1)]] z3; // expected-error {{expected string literal as argument of 'annotate_type' attribute}}
25665da187SMartin Boehme   int *[[clang::annotate_type()]] z4; // expected-error {{'annotate_type' attribute takes at least 1 argument}}
26665da187SMartin Boehme   int *[[clang::annotate_type]] z5; // expected-error {{'annotate_type' attribute takes at least 1 argument}}
2798322d3eSCorentin Jabot   int *[[clang::annotate_type(some_function())]] z6; // expected-error {{expected string literal as argument of 'annotate_type' attribute}}
28665da187SMartin Boehme   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}}
29665da187SMartin Boehme   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}}
30665da187SMartin Boehme   int *[[clang::annotate_type("bar", int)]] z9; // expected-error {{expected expression}}
31665da187SMartin Boehme }
32665da187SMartin Boehme // More error cases: Prohibit adding the attribute to declarations.
33665da187SMartin Boehme // Different declarations hit different code paths, so they need separate tests.
348c7b64b5SMartin Boehme [[clang::annotate_type("bar")]] int *global; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
358c7b64b5SMartin Boehme [[clang::annotate_type("bar")]] void annotated_function(); // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
368c7b64b5SMartin Boehme void g([[clang::annotate_type("bar")]] int); // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
378c7b64b5SMartin Boehme struct [[clang::annotate_type("foo")]] S; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
388c7b64b5SMartin Boehme struct [[clang::annotate_type("foo")]] S{ // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
398c7b64b5SMartin Boehme   [[clang::annotate_type("foo")]] int member; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
408c7b64b5SMartin Boehme   [[clang::annotate_type("foo")]] union { // expected-error {{an attribute list cannot appear here}}
41665da187SMartin Boehme     int i;
42665da187SMartin Boehme     float f;
43665da187SMartin Boehme   };
44665da187SMartin Boehme };
45