diff options
Diffstat (limited to 'test/Analysis/initializer.cpp')
-rw-r--r-- | test/Analysis/initializer.cpp | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/test/Analysis/initializer.cpp b/test/Analysis/initializer.cpp index 656a8bf8eed8..d43c8cfd9a0a 100644 --- a/test/Analysis/initializer.cpp +++ b/test/Analysis/initializer.cpp @@ -1,4 +1,8 @@ -// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store region -cfg-add-initializers -verify %s +// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -analyzer-store region -cfg-add-implicit-dtors -std=c++11 -verify %s + +// We don't inline constructors unless we have destructors turned on. + +void clang_analyzer_eval(bool); class A { int x; @@ -7,8 +11,68 @@ public: }; A::A() : x(0) { - if (x != 0) { - int *p = 0; - *p = 0; // no-warning - } + clang_analyzer_eval(x == 0); // expected-warning{{TRUE}} +} + + +class DirectMember { + int x; +public: + DirectMember(int value) : x(value) {} + + int getX() { return x; } +}; + +void testDirectMember() { + DirectMember obj(3); + clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}} +} + + +class IndirectMember { + struct { + int x; + }; +public: + IndirectMember(int value) : x(value) {} + + int getX() { return x; } +}; + +void testIndirectMember() { + IndirectMember obj(3); + clang_analyzer_eval(obj.getX() == 3); // expected-warning{{TRUE}} +} + + +struct DelegatingConstructor { + int x; + DelegatingConstructor(int y) { x = y; } + DelegatingConstructor() : DelegatingConstructor(42) {} +}; + +void testDelegatingConstructor() { + DelegatingConstructor obj; + clang_analyzer_eval(obj.x == 42); // expected-warning{{TRUE}} +} + + +// ------------------------------------ +// False negatives +// ------------------------------------ + +struct RefWrapper { + RefWrapper(int *p) : x(*p) {} + RefWrapper(int &r) : x(r) {} + int &x; +}; + +void testReferenceMember() { + int *p = 0; + RefWrapper X(p); // should warn in the constructor +} + +void testReferenceMember2() { + int *p = 0; + RefWrapper X(*p); // should warn here } |