File: annotate-type.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (65 lines) | stat: -rw-r--r-- 3,609 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// RUN: %clang_cc1 %s -std=c++17 -fsyntax-only -fcxx-exceptions -verify

struct S1 {
  void f() [[clang::annotate_type("foo")]];
  [[clang::annotate_type("foo")]] void g(); // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
};

template <typename T1, typename T2> struct is_same {
  static constexpr bool value = false;
};

template <typename T1> struct is_same<T1, T1> {
  static constexpr bool value = true;
};

static_assert(is_same<int, int [[clang::annotate_type("foo")]]>::value);
static_assert(is_same<int [[clang::annotate_type("foo")]],
                      int [[clang::annotate_type("bar")]]>::value);
static_assert(is_same<int *, int *[[clang::annotate_type("foo")]]>::value);

// Cannot overload on types that only differ by `annotate_type` attribute.
void f(int) {} // expected-note {{previous definition is here}}
void f(int [[clang::annotate_type("foo")]]) {} // expected-error {{redefinition of 'f'}}

// Cannot specialize on types that only differ by `annotate_type` attribute.
template <class T> struct S2 {};

template <> struct S2<int> {}; // expected-note {{previous definition is here}}

template <>
struct S2<int [[clang::annotate_type("foo")]]> {}; // expected-error {{redefinition of 'S2<int>'}}

// Test that the attribute supports parameter pack expansion.
template <int... Is> void variadic_func_template() {
  int [[clang::annotate_type("foo", Is...)]] val;
}
int f2() { variadic_func_template<1, 2, 3>(); }

// Make sure we correctly diagnose wrong number of arguments for
// [[clang::annotate_type]] inside a template argument.
template <typename Ty> void func_template();
void f3() {
  func_template<int [[clang::annotate_type()]]>(); // expected-error {{'annotate_type' attribute takes at least 1 argument}}
}

// More error cases: Prohibit adding the attribute to declarations.
// Different declarations hit different code paths, so they need separate tests.
namespace [[clang::annotate_type("foo")]] my_namespace {} // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
struct [[clang::annotate_type("foo")]] S3; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
struct [[clang::annotate_type("foo")]] S3{ // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
  [[clang::annotate_type("foo")]] int member; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
};
void f4() {
  for ([[clang::annotate_type("foo")]] int i = 0; i < 42; ++i) {} // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
  for (; [[clang::annotate_type("foo")]] bool b = false;) {} // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
  while ([[clang::annotate_type("foo")]] bool b = false) {} // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
  if ([[clang::annotate_type("foo")]] bool b = false) {} // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
  try {
  } catch ([[clang::annotate_type("foo")]] int i) { // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
  }
}
template <class T>
[[clang::annotate_type("foo")]] T var_template; // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}
[[clang::annotate_type("foo")]] extern "C" int extern_c_func(); // expected-error {{an attribute list cannot appear here}}
extern "C" [[clang::annotate_type("foo")]] int extern_c_func(); // expected-error {{'annotate_type' attribute cannot be applied to a declaration}}