teak-llvm/clang/test/Analysis/NewDeleteLeaks-PR19102.cpp
Artem Dergachev 0c79eab03d [analyzer] Suppress "this" pointer escape during construction.
Pointer escape event notifies checkers that a pointer can no longer be reliably
tracked by the analyzer. For example, if a pointer is passed into a function
that has no body available, or written into a global, MallocChecker would
no longer report memory leaks for such pointer.

In case of operator new() under -analyzer-config c++-allocator-inlining=true,
MallocChecker would start tracking the pointer allocated by operator new()
only to immediately meet a pointer escape event notifying the checker that the
pointer has escaped into a constructor (assuming that the body of the
constructor is not available) and immediately stop tracking it. Even though
it is theoretically possible for such constructor to put "this" into
a global container that would later be freed, we prefer to preserve the old
behavior of MallocChecker, i.e. a memory leak warning, in order to
be able to find any memory leaks in C++ at all. In fact, c++-allocator-inlining
*reduces* the amount of false positives coming from this-pointers escaping in
constructors, because it'd be able to inline constructors in some cases.

With other checkers working similarly, we simply suppress the escape event for
this-value of the constructor, regardless of analyzer options.

Differential Revision: https://reviews.llvm.org/D41797
rdar://problem/12180598

llvm-svn: 322795
2018-01-18 00:44:41 +00:00

45 lines
1.3 KiB
C++

// RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDeleteLeaks -verify %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDeleteLeaks -analyzer-config c++-allocator-inlining=true -verify %s
class A0 {};
class A1 {
public:
A1(int);
};
struct S{
int i;
};
class A2 {
public:
A2();
A2(S);
A2(int*);
A2(S*);
A2(S&, int);
A2(int, S**);
};
void test() {
new int; // expected-warning@+1 {{Potential memory leak}}
new A0; // expected-warning@+1 {{Potential memory leak}}
new A1(0); // expected-warning@+1 {{Potential memory leak}}
new A2; // expected-warning@+1 {{Potential memory leak}}
S s;
s.i = 1;
S* ps = new S;
new A2(s); // expected-warning@+1 {{Potential memory leak}}
new A2(&(s.i)); // expected-warning@+1 {{Potential memory leak}}
new A2(ps); // no warning
new A2(*ps, 1); // no warning
new A2(1, &ps); // no warning
// Tests to ensure that leaks are reported for consumed news no matter what the arguments are.
A2 *a2p1 = new A2; // expected-warning@+1 {{Potential leak of memory}}
A2 *a2p2 = new A2(ps); // expected-warning@+1 {{Potential leak of memory}}
A2 *a2p3 = new A2(*ps, 1); // expected-warning@+1 {{Potential leak of memory}}
A2 *a2p4 = new A2(1, &ps); // expected-warning@+1 {{Potential leak of memory}}
}