xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage-warning-unevaluated-context.cpp (revision de8b2f0c69c8bc13a248691ef9ad9a1c10d81392)
1 // RUN: %clang_cc1 -std=c++20  -Wno-all -Wunsafe-buffer-usage \
2 // RUN:            -fsafe-buffer-usage-suggestions \
3 // RUN:            -fblocks -verify %s
4 
5 // RUN: %clang -x c++ -frtti -fsyntax-only -fblocks %s 2>&1 | FileCheck --allow-empty %s
6 // RUN: %clang_cc1 -std=c++11 -fblocks %s 2>&1 | FileCheck --allow-empty %s
7 // RUN: %clang_cc1 -std=c++20 -fblocks %s 2>&1 | FileCheck --allow-empty %s
8 // CHECK-NOT: [-Wunsafe-buffer-usage]
9 
10 
11 namespace std {
12   class type_info;
13   class bad_cast;
14   class bad_typeid;
15 }
16 using size_t = __typeof(sizeof(int));
17 void *malloc(size_t);
18 
foo(int v)19 void foo(int v) {
20 }
21 
foo(int * p)22 void foo(int *p){}
23 
24 void foo(int, int);
25 
uneval_context_fix()26 void uneval_context_fix() {
27   auto p = new int[10]; // expected-warning{{'p' is an unsafe pointer used for buffer access}}
28 
29   // Warn on the following DREs
30   _Generic(1, int: p[2], float: 3); // expected-note{{used in buffer access here}}
31 
32   // Do not warn for following DREs
33   auto q = new int[10];
34   foo(sizeof(q[1]), // no-note
35       sizeof(decltype(q[1]))); // no-note
36   __typeof(q[5]) x; // no-note
37   int *r = (int *)malloc(sizeof(q[5])); // no-note
38   int y = sizeof(q[5]); // no-note
39   __is_pod(__typeof(q[5])); // no-note
40   __is_trivially_constructible(__typeof(q[5]), decltype(q[5])); // no-note
41   _Generic(q[1], int: 2, float: 3); // no-note
42   _Generic(1, int: 2, float: q[3]); // no-note
43   decltype(q[2]) var = y; // no-note
44   noexcept(q[2]); // no-note
45   typeid(q[3]); // no-note
46 }
47