1 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LeakDetector class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/LeakDetector.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Streams.h"
18 #include "llvm/Value.h"
23 struct VISIBILITY_HIDDEN PrinterTrait
{
24 static void print(const T
* P
) { cerr
<< P
; }
28 struct VISIBILITY_HIDDEN PrinterTrait
<Value
> {
29 static void print(const Value
* P
) { cerr
<< *P
; }
33 struct VISIBILITY_HIDDEN LeakDetectorImpl
{
34 explicit LeakDetectorImpl(const char* const name
) : Cache(0), Name(name
) { }
36 // Because the most common usage pattern, by far, is to add a
37 // garbage object, then remove it immediately, we optimize this
38 // case. When an object is added, it is not added to the set
39 // immediately, it is added to the CachedValue Value. If it is
40 // immediately removed, no set search need be performed.
41 void addGarbage(const T
* o
) {
43 assert(Ts
.count(Cache
) == 0 && "Object already in set!");
49 void removeGarbage(const T
* o
) {
51 Cache
= 0; // Cache hit
56 bool hasGarbage(const std::string
& Message
) {
57 addGarbage(0); // Flush the Cache
59 assert(Cache
== 0 && "No value should be cached anymore!");
62 cerr
<< "Leaked " << Name
<< " objects found: " << Message
<< ":\n";
63 for (typename SmallPtrSet
<const T
*, 8>::iterator I
= Ts
.begin(),
64 E
= Ts
.end(); I
!= E
; ++I
) {
66 PrinterTrait
<T
>::print(*I
);
77 SmallPtrSet
<const T
*, 8> Ts
;
79 const char* const Name
;
82 static LeakDetectorImpl
<void> *Objects
;
83 static LeakDetectorImpl
<Value
> *LLVMObjects
;
85 static LeakDetectorImpl
<void> &getObjects() {
87 Objects
= new LeakDetectorImpl
<void>("GENERIC");
91 static LeakDetectorImpl
<Value
> &getLLVMObjects() {
93 LLVMObjects
= new LeakDetectorImpl
<Value
>("LLVM");
97 static void clearGarbage() {
105 void LeakDetector::addGarbageObjectImpl(void *Object
) {
106 getObjects().addGarbage(Object
);
109 void LeakDetector::addGarbageObjectImpl(const Value
*Object
) {
110 getLLVMObjects().addGarbage(Object
);
113 void LeakDetector::removeGarbageObjectImpl(void *Object
) {
114 getObjects().removeGarbage(Object
);
117 void LeakDetector::removeGarbageObjectImpl(const Value
*Object
) {
118 getLLVMObjects().removeGarbage(Object
);
121 void LeakDetector::checkForGarbageImpl(const std::string
&Message
) {
122 // use non-short-circuit version so that both checks are performed
123 if (getObjects().hasGarbage(Message
) |
124 getLLVMObjects().hasGarbage(Message
))
125 cerr
<< "\nThis is probably because you removed an object, but didn't "
126 << "delete it. Please check your code for memory leaks.\n";
128 // Clear out results so we don't get duplicate warnings on