Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / new-ctor-null-throw.cpp
blob6c17cebcb31bc4a5087efdc44d63f03d39880ffe
1 // RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=core \
2 // RUN: -analyzer-config suppress-null-return-paths=false \
3 // RUN: -verify %s
4 // RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=core \
5 // RUN: -DSUPPRESSED \
6 // RUN: -verify %s
8 void clang_analyzer_eval(bool);
10 typedef __typeof__(sizeof(int)) size_t;
13 // These are ill-formed. One cannot return nullptr from a throwing version of an
14 // operator new.
15 void *operator new(size_t size) {
16 return nullptr;
17 // expected-warning@-1 {{'operator new' should not return a null pointer unless it is declared 'throw()' or 'noexcept'}}
18 // expected-warning@-2 {{null returned from function that requires a non-null return value}}
20 void *operator new[](size_t size) {
21 return nullptr;
22 // expected-warning@-1 {{'operator new[]' should not return a null pointer unless it is declared 'throw()' or 'noexcept'}}
23 // expected-warning@-2 {{null returned from function that requires a non-null return value}}
26 struct S {
27 int x;
28 S() : x(1) {}
29 ~S() {}
30 int getX() const { return x; }
33 void testArrays() {
34 S *s = new S[10]; // no-crash
35 s[0].x = 2;
36 #ifndef SUPPRESSED
37 // expected-warning@-2 {{Dereference of null pointer}}
38 #endif
41 void testCtor() {
42 S *s = new S();
43 s->x = 13;
44 #ifndef SUPPRESSED
45 // expected-warning@-2 {{Access to field 'x' results in a dereference of a null pointer (loaded from variable 's')}}
46 #endif
49 void testMethod() {
50 S *s = new S();
51 const int X = s->getX();
52 #ifndef SUPPRESSED
53 // expected-warning@-2 {{Called C++ object pointer is null}}
54 #endif