Fortran: Fix PR 47485.
[gcc.git] / gcc / testsuite / g++.dg / analyzer / new-2.C
blob391d159a53aed3e39188f7e4244773f10fe482c1
1 // { dg-additional-options "-O0 -fno-analyzer-suppress-followups -fexceptions" }
2 #include <new>
4 struct A
6   int x;
7   int y;
8 };
10 void test_spurious_null_warning_throwing ()
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" } */
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 } */
23   delete a;
24   delete y;
25   delete x;
26   delete[] arr;
29 void test_default_initialization ()
31     int *y = ::new int;
32     int *x = ::new int (); /* { dg-bogus "dereference of possibly-NULL 'operator new" } */
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 } */
39     delete x;
40     delete y;
43 /* From clang core.uninitialized.NewArraySize
44 new[] should not be called with an undefined size argument */
46 void test_garbage_new_array ()
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 */
54 void test_nonthrowing ()
56   int* x = new(std::nothrow) int;
57   int* y = new(std::nothrow) int();
58   int* arr = new(std::nothrow) int[10];
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 } */
67   delete y;
68   delete x;
69   delete[] arr;