diagnose_if.cpp 18.2 KB
Newer Older
1
2
3
4
// RUN: %clang_cc1 %s -verify -fno-builtin -std=c++14

#define _diagnose_if(...) __attribute__((diagnose_if(__VA_ARGS__)))

Hans Wennborg's avatar
Hans Wennborg committed
5
6
using size_t = decltype(sizeof(int));

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
namespace type_dependent {
template <typename T>
void neverok() _diagnose_if(!T(), "oh no", "error") {} // expected-note 4{{from 'diagnose_if'}}

template <typename T>
void alwaysok() _diagnose_if(T(), "oh no", "error") {}

template <typename T>
void alwayswarn() _diagnose_if(!T(), "oh no", "warning") {} // expected-note 4{{from 'diagnose_if'}}

template <typename T>
void neverwarn() _diagnose_if(T(), "oh no", "warning") {}

void runAll() {
  alwaysok<int>();
  alwaysok<int>();

  {
    void (*pok)() = alwaysok<int>;
    pok = &alwaysok<int>;
  }

  neverok<int>(); // expected-error{{oh no}}
  neverok<short>(); // expected-error{{oh no}}

  {
    void (*pok)() = neverok<int>; // expected-error{{oh no}}
  }
  {
    void (*pok)();
    pok = &neverok<int>; // expected-error{{oh no}}
  }

  alwayswarn<int>(); // expected-warning{{oh no}}
  alwayswarn<short>(); // expected-warning{{oh no}}
  {
    void (*pok)() = alwayswarn<int>; // expected-warning{{oh no}}
    pok = &alwayswarn<int>; // expected-warning{{oh no}}
  }

  neverwarn<int>();
  neverwarn<short>();
  {
    void (*pok)() = neverwarn<int>;
    pok = &neverwarn<int>;
  }
}

template <typename T>
Hans Wennborg's avatar
Hans Wennborg committed
56
void errorIf(T a) _diagnose_if(T() != a, "oh no", "error") {} // expected-note{{from 'diagnose_if'}}
57
58

template <typename T>
Hans Wennborg's avatar
Hans Wennborg committed
59
void warnIf(T a) _diagnose_if(T() != a, "oh no", "warning") {} // expected-note{{from 'diagnose_if'}}
60
61
62

void runIf() {
  errorIf(0);
Hans Wennborg's avatar
Hans Wennborg committed
63
  errorIf(1); // expected-error{{oh no}}
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

  warnIf(0);
  warnIf(1); // expected-warning{{oh no}}
}
}

namespace value_dependent {
template <int N>
void neverok() _diagnose_if(N == 0 || N != 0, "oh no", "error") {} // expected-note 4{{from 'diagnose_if'}}

template <int N>
void alwaysok() _diagnose_if(N == 0 && N != 0, "oh no", "error") {}

template <int N>
void alwayswarn() _diagnose_if(N == 0 || N != 0, "oh no", "warning") {} // expected-note 4{{from 'diagnose_if'}}

template <int N>
void neverwarn() _diagnose_if(N == 0 && N != 0, "oh no", "warning") {}

void runAll() {
  alwaysok<0>();
  alwaysok<1>();

  {
    void (*pok)() = alwaysok<0>;
    pok = &alwaysok<0>;
  }

  neverok<0>(); // expected-error{{oh no}}
  neverok<1>(); // expected-error{{oh no}}

  {
    void (*pok)() = neverok<0>; // expected-error{{oh no}}
  }
  {
    void (*pok)();
    pok = &neverok<0>; // expected-error{{oh no}}
  }

  alwayswarn<0>(); // expected-warning{{oh no}}
  alwayswarn<1>(); // expected-warning{{oh no}}
  {
    void (*pok)() = alwayswarn<0>; // expected-warning{{oh no}}
    pok = &alwayswarn<0>; // expected-warning{{oh no}}
  }

  neverwarn<0>();
  neverwarn<1>();
  {
    void (*pok)() = neverwarn<0>;
    pok = &neverwarn<0>;
  }
}

template <int N>
Hans Wennborg's avatar
Hans Wennborg committed
119
void errorIf(int a) _diagnose_if(N != a, "oh no", "error") {} // expected-note{{from 'diagnose_if'}}
120
121

template <int N>
Hans Wennborg's avatar
Hans Wennborg committed
122
void warnIf(int a) _diagnose_if(N != a, "oh no", "warning") {} // expected-note{{from 'diagnose_if'}}
123
124
125

void runIf() {
  errorIf<0>(0);
Hans Wennborg's avatar
Hans Wennborg committed
126
  errorIf<0>(1); // expected-error{{oh no}}
127
128
129
130
131
132
133
134
135
136
137
138
139

  warnIf<0>(0);
  warnIf<0>(1); // expected-warning{{oh no}}
}
}

namespace no_overload_interaction {
void foo(int) _diagnose_if(1, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
void foo(short);

void bar(int);
void bar(short) _diagnose_if(1, "oh no", "error");

Hans Wennborg's avatar
Hans Wennborg committed
140
141
void fooArg(int a) _diagnose_if(a, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
void fooArg(short);
142
143
144
145
146
147
148
149

void barArg(int);
void barArg(short a) _diagnose_if(a, "oh no", "error");

void runAll() {
  foo(1); // expected-error{{oh no}}
  bar(1);

Hans Wennborg's avatar
Hans Wennborg committed
150
  fooArg(1); // expected-error{{oh no}}
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  barArg(1);

  auto p = foo; // expected-error{{incompatible initializer of type '<overloaded function type>'}}
}
}

namespace with_default_args {
void foo(int a = 0) _diagnose_if(a, "oh no", "warning"); // expected-note 1{{from 'diagnose_if'}}
void bar(int a = 1) _diagnose_if(a, "oh no", "warning"); // expected-note 2{{from 'diagnose_if'}}

void runAll() {
  foo();
  foo(0);
  foo(1); // expected-warning{{oh no}}

  bar(); // expected-warning{{oh no}}
  bar(0);
  bar(1); // expected-warning{{oh no}}
}
}

namespace naked_mem_expr {
struct Foo {
  void foo(int a) _diagnose_if(a, "should warn", "warning"); // expected-note{{from 'diagnose_if'}}
  void bar(int a) _diagnose_if(a, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
};

void runFoo() {
  Foo().foo(0);
  Foo().foo(1); // expected-warning{{should warn}}

  Foo().bar(0);
  Foo().bar(1); // expected-error{{oh no}}
}
}

namespace class_template {
template <typename T>
struct Errors {
  void foo(int i) _diagnose_if(i, "bad i", "error"); // expected-note{{from 'diagnose_if'}}
  void bar(int i) _diagnose_if(i != T(), "bad i", "error"); // expected-note{{from 'diagnose_if'}}

Hans Wennborg's avatar
Hans Wennborg committed
193
194
  void fooOvl(int i) _diagnose_if(i, "int bad i", "error"); // expected-note{{from 'diagnose_if'}}
  void fooOvl(short i) _diagnose_if(i, "short bad i", "error"); // expected-note{{from 'diagnose_if'}}
195

Hans Wennborg's avatar
Hans Wennborg committed
196
197
  void barOvl(int i) _diagnose_if(i != T(), "int bad i", "error"); // expected-note{{from 'diagnose_if'}}
  void barOvl(short i) _diagnose_if(i != T(), "short bad i", "error"); // expected-note{{from 'diagnose_if'}}
198
199
200
201
202
203
204
205
206
207
};

void runErrors() {
  Errors<int>().foo(0);
  Errors<int>().foo(1); // expected-error{{bad i}}

  Errors<int>().bar(0);
  Errors<int>().bar(1); // expected-error{{bad i}}

  Errors<int>().fooOvl(0);
Hans Wennborg's avatar
Hans Wennborg committed
208
  Errors<int>().fooOvl(1); // expected-error{{int bad i}}
209
  Errors<int>().fooOvl(short(0));
Hans Wennborg's avatar
Hans Wennborg committed
210
  Errors<int>().fooOvl(short(1)); // expected-error{{short bad i}}
211
212

  Errors<int>().barOvl(0);
Hans Wennborg's avatar
Hans Wennborg committed
213
  Errors<int>().barOvl(1); // expected-error{{int bad i}}
214
  Errors<int>().barOvl(short(0));
Hans Wennborg's avatar
Hans Wennborg committed
215
  Errors<int>().barOvl(short(1)); // expected-error{{short bad i}}
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
}

template <typename T>
struct Warnings {
  void foo(int i) _diagnose_if(i, "bad i", "warning"); // expected-note{{from 'diagnose_if'}}
  void bar(int i) _diagnose_if(i != T(), "bad i", "warning"); // expected-note{{from 'diagnose_if'}}

  void fooOvl(int i) _diagnose_if(i, "int bad i", "warning"); // expected-note{{from 'diagnose_if'}}
  void fooOvl(short i) _diagnose_if(i, "short bad i", "warning"); // expected-note{{from 'diagnose_if'}}

  void barOvl(int i) _diagnose_if(i != T(), "int bad i", "warning"); // expected-note{{from 'diagnose_if'}}
  void barOvl(short i) _diagnose_if(i != T(), "short bad i", "warning"); // expected-note{{from 'diagnose_if'}}
};

void runWarnings() {
  Warnings<int>().foo(0);
  Warnings<int>().foo(1); // expected-warning{{bad i}}

  Warnings<int>().bar(0);
  Warnings<int>().bar(1); // expected-warning{{bad i}}

  Warnings<int>().fooOvl(0);
  Warnings<int>().fooOvl(1); // expected-warning{{int bad i}}
  Warnings<int>().fooOvl(short(0));
  Warnings<int>().fooOvl(short(1)); // expected-warning{{short bad i}}

  Warnings<int>().barOvl(0);
  Warnings<int>().barOvl(1); // expected-warning{{int bad i}}
  Warnings<int>().barOvl(short(0));
  Warnings<int>().barOvl(short(1)); // expected-warning{{short bad i}}
}
}

namespace template_specialization {
template <typename T>
struct Foo {
  void foo() _diagnose_if(1, "override me", "error"); // expected-note{{from 'diagnose_if'}}
  void bar(int i) _diagnose_if(i, "bad i", "error"); // expected-note{{from 'diagnose_if'}}
  void baz(int i);
};

template <>
struct Foo<int> {
  void foo();
  void bar(int i);
  void baz(int i) _diagnose_if(i, "bad i", "error"); // expected-note{{from 'diagnose_if'}}
};

void runAll() {
  Foo<double>().foo(); // expected-error{{override me}}
  Foo<int>().foo();

  Foo<double>().bar(1); // expected-error{{bad i}}
  Foo<int>().bar(1);

  Foo<double>().baz(1);
  Foo<int>().baz(1); // expected-error{{bad i}}
}
}

namespace late_constexpr {
constexpr int foo();
constexpr int foo(int a);

Hans Wennborg's avatar
Hans Wennborg committed
280
281
void bar() _diagnose_if(foo(), "bad foo", "error"); // expected-note{{from 'diagnose_if'}}
void bar(int a) _diagnose_if(foo(a), "bad foo", "error"); // expected-note{{from 'diagnose_if'}}
282
283
284
285
286
287
288
289
290
291
292
293
294

void early() {
  bar();
  bar(0);
  bar(1);
}

constexpr int foo() { return 1; }
constexpr int foo(int a) { return a; }

void late() {
  bar(); // expected-error{{bad foo}}
  bar(0);
Hans Wennborg's avatar
Hans Wennborg committed
295
  bar(1); // expected-error{{bad foo}}
296
297
298
299
300
301
302
303
304
305
}
}

namespace late_parsed {
struct Foo {
  int i;
  constexpr Foo(int i): i(i) {}
  constexpr bool isFooable() const { return i; }

  void go() const _diagnose_if(isFooable(), "oh no", "error") {} // expected-note{{from 'diagnose_if'}}
Hans Wennborg's avatar
Hans Wennborg committed
306
  operator int() const _diagnose_if(isFooable(), "oh no", "error") { return 1; } // expected-note{{from 'diagnose_if'}}
307

Hans Wennborg's avatar
Hans Wennborg committed
308
  void go2() const _diagnose_if(isFooable(), "oh no", "error") // expected-note{{from 'diagnose_if'}}
309
      __attribute__((enable_if(true, ""))) {}
Hans Wennborg's avatar
Hans Wennborg committed
310
  void go2() const _diagnose_if(isFooable(), "oh no", "error") {}
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

  constexpr int go3() const _diagnose_if(isFooable(), "oh no", "error")
      __attribute__((enable_if(true, ""))) {
    return 1;
  }

  constexpr int go4() const _diagnose_if(isFooable(), "oh no", "error") {
    return 1;
  }
  constexpr int go4() const _diagnose_if(isFooable(), "oh no", "error")
      __attribute__((enable_if(true, ""))) {
    return 1;
  }

  // We hope to support emitting these errors in the future. For now, though...
  constexpr int runGo() const {
    return go3() + go4();
  }
};

Hans Wennborg's avatar
Hans Wennborg committed
331
void go(const Foo &f) _diagnose_if(f.isFooable(), "oh no", "error") {} // expected-note{{from 'diagnose_if'}}
332
333
334
335
336
337

void run() {
  Foo(0).go();
  Foo(1).go(); // expected-error{{oh no}}

  (void)int(Foo(0));
Hans Wennborg's avatar
Hans Wennborg committed
338
  (void)int(Foo(1)); // expected-error{{oh no}}
339
340

  Foo(0).go2();
Hans Wennborg's avatar
Hans Wennborg committed
341
  Foo(1).go2(); // expected-error{{oh no}}
342
343

  go(Foo(0));
Hans Wennborg's avatar
Hans Wennborg committed
344
  go(Foo(1)); // expected-error{{oh no}}
345
346
347
348
349
350
351
352
353
}
}

namespace member_templates {
struct Foo {
  int i;
  constexpr Foo(int i): i(i) {}
  constexpr bool bad() const { return i; }

Hans Wennborg's avatar
Hans Wennborg committed
354
  template <typename T> T getVal() _diagnose_if(bad(), "oh no", "error") { // expected-note{{from 'diagnose_if'}}
355
356
357
358
    return T();
  }

  template <typename T>
Hans Wennborg's avatar
Hans Wennborg committed
359
  constexpr T getVal2() const _diagnose_if(bad(), "oh no", "error") { // expected-note{{from 'diagnose_if'}}
360
361
362
363
    return T();
  }

  template <typename T>
Hans Wennborg's avatar
Hans Wennborg committed
364
  constexpr operator T() const _diagnose_if(bad(), "oh no", "error") { // expected-note{{from 'diagnose_if'}}
365
366
367
368
369
370
371
372
373
    return T();
  }

  // We hope to support emitting these errors in the future.
  int run() { return getVal<int>() + getVal2<int>() + int(*this); }
};

void run() {
  Foo(0).getVal<int>();
Hans Wennborg's avatar
Hans Wennborg committed
374
  Foo(1).getVal<int>(); // expected-error{{oh no}}
375
376

  Foo(0).getVal2<int>();
Hans Wennborg's avatar
Hans Wennborg committed
377
  Foo(1).getVal2<int>(); // expected-error{{oh no}}
378
379

  (void)int(Foo(0));
Hans Wennborg's avatar
Hans Wennborg committed
380
  (void)int(Foo(1)); // expected-error{{oh no}}
381
382
383
384
385
386
387
388
389
}
}

namespace special_member_operators {
struct Bar { int j; };
struct Foo {
  int i;
  constexpr Foo(int i): i(i) {}
  constexpr bool bad() const { return i; }
Hans Wennborg's avatar
Hans Wennborg committed
390
  const Bar *operator->() const _diagnose_if(bad(), "oh no", "error") { // expected-note{{from 'diagnose_if'}}
391
392
    return nullptr;
  }
Hans Wennborg's avatar
Hans Wennborg committed
393
  void operator()() const _diagnose_if(bad(), "oh no", "error") {} // expected-note{{from 'diagnose_if'}}
394
395
396
397
398
399
};

struct ParenOverload {
  int i;
  constexpr ParenOverload(int i): i(i) {}
  constexpr bool bad() const { return i; }
Hans Wennborg's avatar
Hans Wennborg committed
400
401
  void operator()(double) const _diagnose_if(bad(), "oh no", "error") {} // expected-note{{from 'diagnose_if'}}
  void operator()(int) const _diagnose_if(bad(), "oh no", "error") {} // expected-note{{from 'diagnose_if'}}
402
403
404
405
406
407
408
};

struct ParenTemplate {
  int i;
  constexpr ParenTemplate(int i): i(i) {}
  constexpr bool bad() const { return i; }
  template <typename T>
Hans Wennborg's avatar
Hans Wennborg committed
409
  void operator()(T) const _diagnose_if(bad(), "oh no", "error") {} // expected-note 2{{from 'diagnose_if'}}
410
411
412
413
};

void run() {
  (void)Foo(0)->j;
Hans Wennborg's avatar
Hans Wennborg committed
414
  (void)Foo(1)->j; // expected-error{{oh no}}
415
416

  Foo(0)();
Hans Wennborg's avatar
Hans Wennborg committed
417
  Foo(1)(); // expected-error{{oh no}}
418
419
420
421

  ParenOverload(0)(1);
  ParenOverload(0)(1.);

Hans Wennborg's avatar
Hans Wennborg committed
422
423
  ParenOverload(1)(1); // expected-error{{oh no}}
  ParenOverload(1)(1.); // expected-error{{oh no}}
424
425
426
427

  ParenTemplate(0)(1);
  ParenTemplate(0)(1.);

Hans Wennborg's avatar
Hans Wennborg committed
428
429
  ParenTemplate(1)(1); // expected-error{{oh no}}
  ParenTemplate(1)(1.); // expected-error{{oh no}}
430
431
432
}

void runLambda() {
Hans Wennborg's avatar
Hans Wennborg committed
433
  auto L1 = [](int i) _diagnose_if(i, "oh no", "error") {}; // expected-note{{from 'diagnose_if'}}
434
  L1(0);
Hans Wennborg's avatar
Hans Wennborg committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  L1(1); // expected-error{{oh no}}
}

struct Brackets {
  int i;
  constexpr Brackets(int i): i(i) {}
  void operator[](int) _diagnose_if(i == 1, "oh no", "warning") // expected-note{{from 'diagnose_if'}}
    _diagnose_if(i == 2, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
};

void runBrackets(int i) {
  Brackets{0}[i];
  Brackets{1}[i]; // expected-warning{{oh no}}
  Brackets{2}[i]; // expected-error{{oh no}}
}

struct Unary {
  int i;
  constexpr Unary(int i): i(i) {}
  void operator+() _diagnose_if(i == 1, "oh no", "warning") // expected-note{{from 'diagnose_if'}}
    _diagnose_if(i == 2, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
};

void runUnary() {
  +Unary{0};
  +Unary{1}; // expected-warning{{oh no}}
  +Unary{2}; // expected-error{{oh no}}
}

struct PostInc {
  void operator++(int i) _diagnose_if(i == 1, "oh no", "warning") // expected-note{{from 'diagnose_if'}}
    _diagnose_if(i == 2, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
};

void runPostInc() {
  PostInc{}++;
  PostInc{}.operator++(1); // expected-warning{{oh no}}
  PostInc{}.operator++(2); // expected-error{{oh no}}
473
474
475
476
477
478
479
480
}
}

namespace ctors {
struct Foo {
  int I;
  constexpr Foo(int I): I(I) {}

Hans Wennborg's avatar
Hans Wennborg committed
481
482
  constexpr const Foo &operator=(const Foo &) const
      _diagnose_if(I, "oh no", "error") {  // expected-note{{from 'diagnose_if'}}
483
484
485
    return *this;
  }

Hans Wennborg's avatar
Hans Wennborg committed
486
487
  constexpr const Foo &operator=(const Foo &&) const
      _diagnose_if(I, "oh no", "error") { // expected-note{{from 'diagnose_if'}}
488
489
490
491
    return *this;
  }
};

Hans Wennborg's avatar
Hans Wennborg committed
492
493
494
495
496
497
struct Bar {
  int I;
  constexpr Bar(int I) _diagnose_if(I == 1, "oh no", "warning") // expected-note{{from 'diagnose_if'}}
    _diagnose_if(I == 2, "oh no", "error"): I(I) {} // expected-note{{from 'diagnose_if'}}
};

498
499
500
501
void run() {
  constexpr Foo F{0};
  constexpr Foo F2{1};

Hans Wennborg's avatar
Hans Wennborg committed
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
  F2 = F; // expected-error{{oh no}}
  F2 = Foo{2}; // expected-error{{oh no}}

  Bar{0};
  Bar{1}; // expected-warning{{oh no}}
  Bar{2}; // expected-error{{oh no}}
}
}

namespace ref_init {
struct Bar {};
struct Baz {};
struct Foo {
  int i;
  constexpr Foo(int i): i(i) {}
  operator const Bar &() const _diagnose_if(i, "oh no", "warning"); // expected-note{{from 'diagnose_if'}}
  operator const Baz &() const _diagnose_if(i, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
};
void fooBar(const Bar &b);
void fooBaz(const Baz &b);

void run() {
  fooBar(Foo{0});
  fooBar(Foo{1}); // expected-warning{{oh no}}
  fooBaz(Foo{0});
  fooBaz(Foo{1}); // expected-error{{oh no}}
}
}

namespace udl {
void operator""_fn(char c)_diagnose_if(c == 1, "oh no", "warning") // expected-note{{from 'diagnose_if'}}
    _diagnose_if(c == 2, "oh no", "error"); // expected-note{{from 'diagnose_if'}}

void run() {
  '\0'_fn;
  '\1'_fn; // expected-warning{{oh no}}
  '\2'_fn; // expected-error{{oh no}}
}
}

namespace PR31638 {
struct String {
  String(char const* __s) _diagnose_if(__s == nullptr, "oh no ptr", "warning"); // expected-note{{from 'diagnose_if'}}
  String(int __s) _diagnose_if(__s != 0, "oh no int", "warning"); // expected-note{{from 'diagnose_if'}}
};

void run() {
  String s(nullptr); // expected-warning{{oh no ptr}}
  String ss(42); // expected-warning{{oh no int}}
}
}

namespace PR31639 {
struct Foo {
  Foo(int I) __attribute__((diagnose_if(I, "oh no", "error"))); // expected-note{{from 'diagnose_if'}}
};

void bar() { Foo f(1); } // expected-error{{oh no}}
}

namespace user_defined_conversion {
struct Foo {
  int i;
  constexpr Foo(int i): i(i) {}
  operator size_t() const _diagnose_if(i == 1, "oh no", "warning") // expected-note{{from 'diagnose_if'}}
      _diagnose_if(i == 2, "oh no", "error"); // expected-note{{from 'diagnose_if'}}
};

void run() {
  // `new T[N]`, where N is implicitly convertible to size_t, calls
  // PerformImplicitConversion directly. This lets us test the diagnostic logic
  // in PerformImplicitConversion.
  new int[Foo{0}];
  new int[Foo{1}]; // expected-warning{{oh no}}
  new int[Foo{2}]; // expected-error{{oh no}}
}
}

namespace std {
  template <typename T>
  struct initializer_list {
    const T *ptr;
    size_t elems;

    constexpr size_t size() const { return elems; }
  };
}

namespace initializer_lists {
struct Foo {
  Foo(std::initializer_list<int> l)
    _diagnose_if(l.size() == 1, "oh no", "warning") // expected-note{{from 'diagnose_if'}}
    _diagnose_if(l.size() == 2, "oh no", "error") {} // expected-note{{from 'diagnose_if'}}
};

void run() {
  Foo{std::initializer_list<int>{}};
  Foo{std::initializer_list<int>{1}}; // expected-warning{{oh no}}
  Foo{std::initializer_list<int>{1, 2}}; // expected-error{{oh no}}
  Foo{std::initializer_list<int>{1, 2, 3}};
}
}

namespace range_for_loop {
  namespace adl {
    struct Foo {
      int i;
      constexpr Foo(int i): i(i) {}
    };
    void **begin(const Foo &f) _diagnose_if(f.i, "oh no", "warning");
    void **end(const Foo &f) _diagnose_if(f.i, "oh no", "warning");

    struct Bar {
      int i;
      constexpr Bar(int i): i(i) {}
    };
    void **begin(const Bar &b) _diagnose_if(b.i, "oh no", "error");
    void **end(const Bar &b) _diagnose_if(b.i, "oh no", "error");
  }

  void run() {
    for (void *p : adl::Foo(0)) {}
    // FIXME: This should emit diagnostics. It seems that our constexpr
    // evaluator isn't able to evaluate `adl::Foo(1)` as a constant, though.
    for (void *p : adl::Foo(1)) {}

    for (void *p : adl::Bar(0)) {}
    // FIXME: Same thing.
    for (void *p : adl::Bar(1)) {}
  }
}

namespace operator_new {
struct Foo {
  int j;
  static void *operator new(size_t i) _diagnose_if(i, "oh no", "warning");
};

struct Bar {
  int j;
  static void *operator new(size_t i) _diagnose_if(!i, "oh no", "warning");
};

void run() {
  // FIXME: This should emit a diagnostic.
  new Foo();
  // This is here because we sometimes pass a dummy argument `operator new`. We
  // should ignore this, rather than complaining about it.
  new Bar();
}
}

namespace contextual_implicit_conv {
struct Foo {
  int i;
  constexpr Foo(int i): i(i) {}
  constexpr operator int() const _diagnose_if(i == 1, "oh no", "warning") // expected-note{{from 'diagnose_if'}}
      _diagnose_if(i == 2, "oh no", "error") { // expected-note{{from 'diagnose_if'}}
    return i;
  }
};

void run() {
  switch (constexpr Foo i = 0) { default: break; }
  switch (constexpr Foo i = 1) { default: break; } // expected-warning{{oh no}}
  switch (constexpr Foo i = 2) { default: break; } // expected-error{{oh no}}
668
669
}
}