Quotes should be printed before private prefix; some code clean up.
[llvm/msp430.git] / lib / VMCore / LeakDetector.cpp
blob1bf9171553837c7fc0e8d0aaa78d9908c2fa9f08
1 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
19 using namespace llvm;
21 namespace {
22 template <class T>
23 struct VISIBILITY_HIDDEN PrinterTrait {
24 static void print(const T* P) { cerr << P; }
27 template<>
28 struct VISIBILITY_HIDDEN PrinterTrait<Value> {
29 static void print(const Value* P) { cerr << *P; }
32 template <typename T>
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) {
42 if (Cache) {
43 assert(Ts.count(Cache) == 0 && "Object already in set!");
44 Ts.insert(Cache);
46 Cache = o;
49 void removeGarbage(const T* o) {
50 if (o == Cache)
51 Cache = 0; // Cache hit
52 else
53 Ts.erase(o);
56 bool hasGarbage(const std::string& Message) {
57 addGarbage(0); // Flush the Cache
59 assert(Cache == 0 && "No value should be cached anymore!");
61 if (!Ts.empty()) {
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) {
65 cerr << "\t";
66 PrinterTrait<T>::print(*I);
67 cerr << "\n";
69 cerr << '\n';
71 return true;
73 return false;
76 private:
77 SmallPtrSet<const T*, 8> Ts;
78 const T* Cache;
79 const char* const Name;
82 static LeakDetectorImpl<void> *Objects;
83 static LeakDetectorImpl<Value> *LLVMObjects;
85 static LeakDetectorImpl<void> &getObjects() {
86 if (Objects == 0)
87 Objects = new LeakDetectorImpl<void>("GENERIC");
88 return *Objects;
91 static LeakDetectorImpl<Value> &getLLVMObjects() {
92 if (LLVMObjects == 0)
93 LLVMObjects = new LeakDetectorImpl<Value>("LLVM");
94 return *LLVMObjects;
97 static void clearGarbage() {
98 delete Objects;
99 delete LLVMObjects;
100 Objects = 0;
101 LLVMObjects = 0;
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
129 // next call...
130 clearGarbage();