new-with-exceptions.cpp revision b59b580a57a36df9d146473098d14c64508ff319
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -analyzer-store region -std=c++11 -fexceptions -fcxx-exceptions -verify -DEXCEPTIONS %s
2// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -analyzer-store region -std=c++11 -verify %s
3
4void clang_analyzer_eval(bool);
5
6typedef __typeof__(sizeof(int)) size_t;
7extern "C" void *malloc(size_t);
8
9// This is the standard placement new.
10inline void* operator new(size_t, void* __p) throw()
11{
12  return __p;
13}
14
15struct NoThrow {
16  void *operator new(size_t) throw();
17};
18
19struct NoExcept {
20  void *operator new(size_t) noexcept;
21};
22
23struct DefaultThrow {
24  void *operator new(size_t);
25};
26
27struct ExplicitThrow {
28  void *operator new(size_t) throw(int);
29};
30
31void testNew() {
32  clang_analyzer_eval(new NoThrow); // expected-warning{{UNKNOWN}}
33  clang_analyzer_eval(new NoExcept); // expected-warning{{UNKNOWN}}
34
35  clang_analyzer_eval(new DefaultThrow);
36  clang_analyzer_eval(new ExplicitThrow);
37#ifdef EXCEPTIONS
38  // expected-warning@-3 {{TRUE}}
39  // expected-warning@-3 {{TRUE}}
40#else
41  // expected-warning@-6 {{UNKNOWN}}
42  // expected-warning@-6 {{UNKNOWN}}
43#endif
44}
45
46void testNewArray() {
47  clang_analyzer_eval(new NoThrow[2]);
48  clang_analyzer_eval(new NoExcept[2]);
49  clang_analyzer_eval(new DefaultThrow[2]);
50  clang_analyzer_eval(new ExplicitThrow[2]);
51#ifdef EXCEPTIONS
52  // expected-warning@-5 {{TRUE}}
53  // expected-warning@-5 {{TRUE}}
54  // expected-warning@-5 {{TRUE}}
55  // expected-warning@-5 {{TRUE}}
56#else
57  // expected-warning@-10 {{UNKNOWN}}
58  // expected-warning@-10 {{UNKNOWN}}
59  // expected-warning@-10 {{UNKNOWN}}
60  // expected-warning@-10 {{UNKNOWN}}
61#endif
62}
63
64extern void *operator new[](size_t, int) noexcept;
65
66void testNewArrayNoThrow() {
67  clang_analyzer_eval(new (1) NoThrow[2]); // expected-warning{{UNKNOWN}}
68  clang_analyzer_eval(new (1) NoExcept[2]); // expected-warning{{UNKNOWN}}
69  clang_analyzer_eval(new (1) DefaultThrow[2]); // expected-warning{{UNKNOWN}}
70  clang_analyzer_eval(new (1) ExplicitThrow[2]); // expected-warning{{UNKNOWN}}
71}
72