summaryrefslogtreecommitdiffstats
path: root/clang/test/SemaCXX/attr-likelihood.cpp
blob: 642d62fa898268c8e6fa6d14d76639836d542115 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// RUN: %clang_cc1 %s -std=c++17 -fsyntax-only -verify
// RUN: %clang_cc1 %s -DPEDANTIC -pedantic -fsyntax-only -verify

#if PEDANTIC
void g() {
  if (true)
    [[likely]] {} // expected-warning {{use of the 'likely' attribute is a C++20 extension}}
  else
    [[unlikely]] {} // expected-warning {{use of the 'unlikely' attribute is a C++20 extension}}
}
#else
void a() {
  if (true)
    [[likely]]; // expected-warning {{conflicting attributes 'likely' are ignored}}
  else
    [[likely]]; // expected-note {{conflicting attribute is here}}
}

void b() {
  if (true)
    [[unlikely]]; // expected-warning {{conflicting attributes 'unlikely' are ignored}}
  else
    [[unlikely]]; // expected-note {{conflicting attribute is here}}
}

void c() {
  if (true)
    [[likely]];
}

void d() {
  if (true)
    [[unlikely]];
}

void g() {
  if (true)
    [[likely]] {}
  else
    [[unlikely]] {}
}

void h() {
  if (true)
    [[likely]] {}
  else {
  }
}

void i() {
  if (true)
    [[unlikely]] {}
  else {
  }
}

void j() {
  if (true) {
  } else
    [[likely]] {}
}

void k() {
  if (true) {
  } else
    [[likely]] {}
}

void l() {
  if (true)
    [[likely]] {}
  else
    [[unlikely]] if (false) [[likely]] {}
}

void m() {
  [[likely]] int x = 42; // expected-error {{'likely' attribute cannot be applied to a declaration}}

  if (x)
    [[unlikely]] {}
  if (x) {
    [[unlikely]];
  }
  switch (x) {
  case 1:
    [[likely]] {}
    break;
    [[likely]] case 2 : case 3 : {}
    break;
  }

  do {
    [[unlikely]];
  } while (x);
  do
    [[unlikely]] {}
  while (x);
  do { // expected-note {{to match this 'do'}}
  }
  [[unlikely]] while (x); // expected-error {{expected 'while' in do/while loop}}
  for (;;)
    [[unlikely]] {}
  for (;;) {
    [[unlikely]];
  }
  while (x)
    [[unlikely]] {}
  while (x) {
    [[unlikely]];
  }

  switch (x)
    [[unlikely]] {}

  if (x)
    goto lbl;

  // FIXME: allow the attribute on the label
  [[unlikely]] lbl : // expected-error {{'unlikely' attribute cannot be applied to a declaration}}
                     [[likely]] x = x + 1;

  [[likely]]++ x;
}

void n() [[likely]] // expected-error {{'likely' attribute cannot be applied to types}}
{
  try
    [[likely]] {} // expected-error {{expected '{'}}
  catch (...) [[likely]] { // expected-error {{expected expression}}
  }
}

void o()
{
  // expected-warning@+2 {{attribute 'likely' has no effect when annotating an 'if constexpr' statement}}
  // expected-note@+1 {{annotating the 'if constexpr' statement here}}
  if constexpr (true) [[likely]];

  // expected-note@+1 {{annotating the 'if constexpr' statement here}}
  if constexpr (true) {
  // expected-warning@+1 {{attribute 'unlikely' has no effect when annotating an 'if constexpr' statement}}
  } else [[unlikely]];

  // Annotating both branches with conflicting likelihoods generates no diagnostic regarding the conflict.
  // expected-warning@+2 {{attribute 'likely' has no effect when annotating an 'if constexpr' statement}}
  // expected-note@+1 2 {{annotating the 'if constexpr' statement here}}
  if constexpr (true) [[likely]] {
  // expected-warning@+1 {{attribute 'likely' has no effect when annotating an 'if constexpr' statement}}
  } else [[likely]];

  if (1) [[likely, unlikely]] { // expected-error {{'unlikely' and 'likely' attributes are not compatible}} \
                                // expected-note {{conflicting attribute is here}}
  } else [[unlikely]][[likely]] { // expected-error {{'likely' and 'unlikely' attributes are not compatible}} \
                                  // expected-note {{conflicting attribute is here}}
  }
}

constexpr int constexpr_function() {
  [[likely]] return 0;
}
static_assert(constexpr_function() == 0);

constexpr double pow(double x, long long n) noexcept {
    if (n > 0) [[likely]]
        return x * pow(x, n - 1);
    else [[unlikely]]
        return 1;
}
constexpr long long fact(long long n) noexcept {
    if (n > 1) [[likely]]
        return n * fact(n - 1);
    else [[unlikely]]
        return 1;
}

#endif