Debian Codegraph @

gcc/testsuite/g++.dg/analyzer/new-2.C

raw
1 // { dg-additional-options "-O0 -fno-analyzer-suppress-followups -fexceptions" }
2 #include <new>
3
4 struct A
5 {
6 int x;
7 int y;
8 };
9
10 void test_spurious_null_warning_throwing ()
11 {
12 int *x = new int; /* { dg-bogus "dereference of possibly-NULL" } */
13 int *y = new int (); /* { dg-bogus "dereference of possibly-NULL" "non-throwing" } */
14 int *arr = new int[3]; /* { dg-bogus "dereference of possibly-NULL" } */
15 A *a = new A (); /* { dg-bogus "dereference of possibly-NULL" "throwing new cannot be null" } */
16
17 int z = *y + 2;
18 z = *x + 4; /* { dg-bogus "dereference of possibly-NULL 'x'" } */
19 /* { dg-warning "use of uninitialized value '\\*x'" "" { target *-*-* } .-1 } */
20 z = arr[0] + 4; /* { dg-bogus "dereference of possibly-NULL" } */
21 /* { dg-warning "use of uninitialized value '\\*arr'" "" { target *-*-* } .-1 } */
22
23 delete a;
24 delete y;
25 delete x;
26 delete[] arr;
27 }
28
29 void test_default_initialization ()
30 {
31 int *y = ::new int;
32 int *x = ::new int (); /* { dg-bogus "dereference of possibly-NULL 'operator new" } */
33
34 int b = *x + 3; /* { dg-bogus "dereference of possibly-NULL" } */
35 /* { dg-bogus "use of uninitialized ‘*x’" "" { target *-*-* } .-1 } */
36 int a = *y + 2; /* { dg-bogus "dereference of possibly-NULL 'y'" } */
37 /* { dg-warning "use of uninitialized value '\\*y'" "no default init" { target *-*-* } .-1 } */
38
39 delete x;
40 delete y;
41 }
42
43 /* From clang core.uninitialized.NewArraySize
44 new[] should not be called with an undefined size argument */
45
46 void test_garbage_new_array ()
47 {
48 int n;
49 int *arr = ::new int[n]; /* { dg-warning "use of uninitialized value 'n'" } */
50 arr[0] = 7;
51 ::delete[] arr; /* no warnings emitted here either */
52 }
53
54 void test_nonthrowing ()
55 {
56 int* x = new(std::nothrow) int;
57 int* y = new(std::nothrow) int();
58 int* arr = new(std::nothrow) int[10];
59
60 int z = *y + 2; /* { dg-warning "dereference of NULL 'y'" } */
61 /* { dg-bogus "use of uninitialized value '\\*y'" "" { target *-*-* } .-1 } */
62 z = *x + 4; /* { dg-warning "dereference of possibly-NULL 'x'" } */
63 /* { dg-warning "use of uninitialized value '\\*x'" "" { target *-*-* } .-1 } */
64 z = arr[0] + 4; /* { dg-warning "dereference of possibly-NULL 'arr'" } */
65 /* { dg-warning "use of uninitialized value '\\*arr'" "" { target *-*-* } .-1 } */
66
67 delete y;
68 delete x;
69 delete[] arr;
70 }