new.cpp revision e38c1c2c449529e60f48e740cb8662e68e5a5330
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -verify %s
2
3void clang_analyzer_eval(bool);
4
5typedef typeof(sizeof(int)) size_t;
6extern "C" void *malloc(size_t);
7
8// This is the standard placement new.
9inline void* operator new(size_t, void* __p) throw()
10{
11  return __p;
12}
13
14void *testPlacementNew() {
15  int *x = (int *)malloc(sizeof(int));
16  *x = 1;
17  clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}};
18
19  void *y = new (x) int;
20  clang_analyzer_eval(x == y); // expected-warning{{TRUE}};
21  clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}};
22
23  return y;
24}
25
26void *operator new(size_t, size_t, int *);
27void *testCustomNew() {
28  int x[1] = {1};
29  clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}};
30
31  void *y = new (0, x) int;
32  clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}};
33
34  return y; // no-warning
35}
36
37