Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 1 | //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the LeakDetector class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 14 | #include "llvm/Support/LeakDetector.h" |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Streams.h" |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 17 | #include "llvm/Value.h" |
| 18 | #include <set> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 21 | namespace { |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 22 | template <class T> |
Chris Lattner | 9ef7e06 | 2006-06-28 22:57:00 +0000 | [diff] [blame] | 23 | struct VISIBILITY_HIDDEN PrinterTrait { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 24 | static void print(const T* P) { cerr << P; } |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | template<> |
Chris Lattner | 9ef7e06 | 2006-06-28 22:57:00 +0000 | [diff] [blame] | 28 | struct VISIBILITY_HIDDEN PrinterTrait<Value> { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 29 | static void print(const Value* P) { cerr << *P; } |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 32 | template <typename T> |
Chris Lattner | 9ef7e06 | 2006-06-28 22:57:00 +0000 | [diff] [blame] | 33 | struct VISIBILITY_HIDDEN LeakDetectorImpl { |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 34 | LeakDetectorImpl(const char* const name) : Cache(0), Name(name) { } |
Chris Lattner | 3f6962e | 2002-09-19 19:22:11 +0000 | [diff] [blame] | 35 | |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 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 |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 53 | Ts.erase(o); |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 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()) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 62 | cerr << "Leaked " << Name << " objects found: " << Message << ":\n"; |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 63 | for (typename std::set<const T*>::iterator I = Ts.begin(), |
| 64 | E = Ts.end(); I != E; ++I) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 65 | cerr << "\t"; |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 66 | PrinterTrait<T>::print(*I); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 67 | cerr << "\n"; |
Chris Lattner | ce36d55 | 2004-07-15 01:29:12 +0000 | [diff] [blame] | 68 | } |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 69 | cerr << '\n'; |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 70 | |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | std::set<const T*> Ts; |
Chris Lattner | 8709927 | 2004-02-15 23:33:48 +0000 | [diff] [blame] | 78 | const T* Cache; |
| 79 | const char* const Name; |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Chris Lattner | 9945826 | 2004-11-19 17:09:48 +0000 | [diff] [blame] | 82 | LeakDetectorImpl<void> *Objects; |
| 83 | LeakDetectorImpl<Value> *LLVMObjects; |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 9945826 | 2004-11-19 17:09:48 +0000 | [diff] [blame] | 85 | LeakDetectorImpl<void> &getObjects() { |
| 86 | if (Objects == 0) |
| 87 | Objects = new LeakDetectorImpl<void>("GENERIC"); |
| 88 | return *Objects; |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Chris Lattner | 9945826 | 2004-11-19 17:09:48 +0000 | [diff] [blame] | 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; |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 105 | void LeakDetector::addGarbageObjectImpl(void *Object) { |
| 106 | getObjects().addGarbage(Object); |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void LeakDetector::addGarbageObjectImpl(const Value *Object) { |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 110 | getLLVMObjects().addGarbage(Object); |
| 111 | } |
| 112 | |
| 113 | void LeakDetector::removeGarbageObjectImpl(void *Object) { |
| 114 | getObjects().removeGarbage(Object); |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void LeakDetector::removeGarbageObjectImpl(const Value *Object) { |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 118 | getLLVMObjects().removeGarbage(Object); |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void LeakDetector::checkForGarbageImpl(const std::string &Message) { |
Alkis Evlogimenos | b663d76 | 2004-02-14 23:33:39 +0000 | [diff] [blame] | 122 | // use non-short-circuit version so that both checks are performed |
| 123 | if (getObjects().hasGarbage(Message) | |
| 124 | getLLVMObjects().hasGarbage(Message)) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 125 | cerr << "\nThis is probably because you removed an object, but didn't " |
| 126 | << "delete it. Please check your code for memory leaks.\n"; |
Chris Lattner | 9945826 | 2004-11-19 17:09:48 +0000 | [diff] [blame] | 127 | |
| 128 | // Clear out results so we don't get duplicate warnings on |
| 129 | // next call... |
| 130 | clearGarbage(); |
Chris Lattner | de69a4c | 2002-09-08 18:51:16 +0000 | [diff] [blame] | 131 | } |