Jordan Rose | ee04959 | 2012-08-21 21:44:07 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -analyzer-ipa=inlining -cfg-add-implicit-dtors -std=c++11 -verify %s |
Jordan Rose | 3a0a9e3 | 2012-07-26 20:04:21 +0000 | [diff] [blame] | 2 | |
Jordan Rose | e1ce783 | 2012-07-31 18:04:59 +0000 | [diff] [blame] | 3 | // We don't inline constructors unless we have destructors turned on. |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 4 | |
| 5 | void clang_analyzer_eval(bool); |
Zhongxing Xu | 9dc84c9 | 2010-11-16 07:52:17 +0000 | [diff] [blame] | 6 | |
| 7 | class A { |
| 8 | int x; |
| 9 | public: |
| 10 | A(); |
| 11 | }; |
| 12 | |
| 13 | A::A() : x(0) { |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 14 | clang_analyzer_eval(x == 0); // expected-warning{{TRUE}} |
Zhongxing Xu | 9dc84c9 | 2010-11-16 07:52:17 +0000 | [diff] [blame] | 15 | } |
Jordan Rose | 3a0a9e3 | 2012-07-26 20:04:21 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | class DirectMember { |
| 19 | int x; |
| 20 | public: |
| 21 | DirectMember(int value) : x(value) {} |
| 22 | |
| 23 | int getX() { return x; } |
| 24 | }; |
| 25 | |
| 26 | void testDirectMember() { |
| 27 | DirectMember obj(3); |
| 28 | clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}} |
| 29 | } |
| 30 | |
| 31 | |
| 32 | class IndirectMember { |
| 33 | struct { |
| 34 | int x; |
| 35 | }; |
| 36 | public: |
| 37 | IndirectMember(int value) : x(value) {} |
| 38 | |
| 39 | int getX() { return x; } |
| 40 | }; |
| 41 | |
| 42 | void testIndirectMember() { |
| 43 | IndirectMember obj(3); |
| 44 | clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}} |
| 45 | } |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 46 | |
| 47 | |
Jordan Rose | 563ea23 | 2012-08-03 23:31:15 +0000 | [diff] [blame] | 48 | struct DelegatingConstructor { |
| 49 | int x; |
| 50 | DelegatingConstructor(int y) { x = y; } |
| 51 | DelegatingConstructor() : DelegatingConstructor(42) {} |
| 52 | }; |
| 53 | |
| 54 | void testDelegatingConstructor() { |
| 55 | DelegatingConstructor obj; |
| 56 | clang_analyzer_eval(obj.x == 42); // expected-warning{{TRUE}} |
| 57 | } |
| 58 | |
| 59 | |
Jordan Rose | 9f3b9d5 | 2012-08-02 21:33:42 +0000 | [diff] [blame] | 60 | // ------------------------------------ |
| 61 | // False negatives |
| 62 | // ------------------------------------ |
| 63 | |
| 64 | struct RefWrapper { |
| 65 | RefWrapper(int *p) : x(*p) {} |
| 66 | RefWrapper(int &r) : x(r) {} |
| 67 | int &x; |
| 68 | }; |
| 69 | |
| 70 | void testReferenceMember() { |
| 71 | int *p = 0; |
| 72 | RefWrapper X(p); // should warn in the constructor |
| 73 | } |
| 74 | |
| 75 | void testReferenceMember2() { |
| 76 | int *p = 0; |
| 77 | RefWrapper X(*p); // should warn here |
| 78 | } |