Jordan Rose | e38c1c2 | 2012-06-20 01:32:01 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -verify %s |
Zhongxing Xu | 40ab43b | 2010-04-20 05:48:57 +0000 | [diff] [blame] | 2 | |
Jordan Rose | e38c1c2 | 2012-06-20 01:32:01 +0000 | [diff] [blame^] | 3 | void clang_analyzer_eval(bool); |
| 4 | |
| 5 | typedef typeof(sizeof(int)) size_t; |
| 6 | extern "C" void *malloc(size_t); |
| 7 | |
| 8 | // This is the standard placement new. |
| 9 | inline void* operator new(size_t, void* __p) throw() |
| 10 | { |
| 11 | return __p; |
Zhongxing Xu | 48fb322 | 2010-04-21 02:22:25 +0000 | [diff] [blame] | 12 | } |
Zhongxing Xu | 40ab43b | 2010-04-20 05:48:57 +0000 | [diff] [blame] | 13 | |
Jordan Rose | e38c1c2 | 2012-06-20 01:32:01 +0000 | [diff] [blame^] | 14 | void *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 | |
| 26 | void *operator new(size_t, size_t, int *); |
| 27 | void *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 |
Zhongxing Xu | 40ab43b | 2010-04-20 05:48:57 +0000 | [diff] [blame] | 35 | } |
| 36 | |