LeakDetector.cpp revision e81561909d128c6e2d8033cb5465a49b2596b26a
1//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the LeakDetector class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/Support/LeakDetector.h" 15#include "llvm/Support/Compiler.h" 16#include "llvm/Support/Streams.h" 17#include "llvm/Value.h" 18#include <set> 19using namespace llvm; 20 21namespace { 22 template <class T> 23 struct VISIBILITY_HIDDEN PrinterTrait { 24 static void print(const T* P) { cerr << P; } 25 }; 26 27 template<> 28 struct VISIBILITY_HIDDEN PrinterTrait<Value> { 29 static void print(const Value* P) { cerr << *P; } 30 }; 31 32 template <typename T> 33 struct VISIBILITY_HIDDEN LeakDetectorImpl { 34 LeakDetectorImpl(const char* const name) : Cache(0), Name(name) { } 35 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); 45 } 46 Cache = o; 47 } 48 49 void removeGarbage(const T* o) { 50 if (o == Cache) 51 Cache = 0; // Cache hit 52 else 53 Ts.erase(o); 54 } 55 56 bool hasGarbage(const std::string& Message) { 57 addGarbage(0); // Flush the Cache 58 59 assert(Cache == 0 && "No value should be cached anymore!"); 60 61 if (!Ts.empty()) { 62 cerr << "Leaked " << Name << " objects found: " << Message << ":\n"; 63 for (typename std::set<const T*>::iterator I = Ts.begin(), 64 E = Ts.end(); I != E; ++I) { 65 cerr << "\t"; 66 PrinterTrait<T>::print(*I); 67 cerr << "\n"; 68 } 69 cerr << '\n'; 70 71 return true; 72 } 73 return false; 74 } 75 76 private: 77 std::set<const T*> Ts; 78 const T* Cache; 79 const char* const Name; 80 }; 81 82 LeakDetectorImpl<void> *Objects; 83 LeakDetectorImpl<Value> *LLVMObjects; 84 85 LeakDetectorImpl<void> &getObjects() { 86 if (Objects == 0) 87 Objects = new LeakDetectorImpl<void>("GENERIC"); 88 return *Objects; 89 } 90 91 LeakDetectorImpl<Value> &getLLVMObjects() { 92 if (LLVMObjects == 0) 93 LLVMObjects = new LeakDetectorImpl<Value>("LLVM"); 94 return *LLVMObjects; 95 } 96 97 void clearGarbage() { 98 delete Objects; 99 delete LLVMObjects; 100 Objects = 0; 101 LLVMObjects = 0; 102 } 103} 104 105void LeakDetector::addGarbageObjectImpl(void *Object) { 106 getObjects().addGarbage(Object); 107} 108 109void LeakDetector::addGarbageObjectImpl(const Value *Object) { 110 getLLVMObjects().addGarbage(Object); 111} 112 113void LeakDetector::removeGarbageObjectImpl(void *Object) { 114 getObjects().removeGarbage(Object); 115} 116 117void LeakDetector::removeGarbageObjectImpl(const Value *Object) { 118 getLLVMObjects().removeGarbage(Object); 119} 120 121void 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"; 127 128 // Clear out results so we don't get duplicate warnings on 129 // next call... 130 clearGarbage(); 131} 132