2002-09-08 18:51:16 +00:00
|
|
|
//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-08 18:51:16 +00:00
|
|
|
//
|
|
|
|
// This file implements the LeakDetector class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-28 02:09:03 +00:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2006-12-07 01:30:32 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-11-28 02:09:03 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2002-09-08 18:51:16 +00:00
|
|
|
#include "llvm/Value.h"
|
|
|
|
#include <set>
|
2003-12-14 21:35:53 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
namespace {
|
2004-07-15 01:29:12 +00:00
|
|
|
template <class T>
|
2006-06-28 22:57:00 +00:00
|
|
|
struct VISIBILITY_HIDDEN PrinterTrait {
|
2006-12-07 01:30:32 +00:00
|
|
|
static void print(const T* P) { cerr << P; }
|
2004-07-15 01:29:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2006-06-28 22:57:00 +00:00
|
|
|
struct VISIBILITY_HIDDEN PrinterTrait<Value> {
|
2006-12-07 01:30:32 +00:00
|
|
|
static void print(const Value* P) { cerr << *P; }
|
2004-07-15 01:29:12 +00:00
|
|
|
};
|
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
template <typename T>
|
2006-06-28 22:57:00 +00:00
|
|
|
struct VISIBILITY_HIDDEN LeakDetectorImpl {
|
2004-02-14 23:33:39 +00:00
|
|
|
LeakDetectorImpl(const char* const name) : Cache(0), Name(name) { }
|
2002-09-08 18:51:16 +00:00
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
// Because the most common usage pattern, by far, is to add a
|
|
|
|
// garbage object, then remove it immediately, we optimize this
|
|
|
|
// case. When an object is added, it is not added to the set
|
|
|
|
// immediately, it is added to the CachedValue Value. If it is
|
|
|
|
// immediately removed, no set search need be performed.
|
|
|
|
void addGarbage(const T* o) {
|
|
|
|
if (Cache) {
|
|
|
|
assert(Ts.count(Cache) == 0 && "Object already in set!");
|
|
|
|
Ts.insert(Cache);
|
|
|
|
}
|
|
|
|
Cache = o;
|
|
|
|
}
|
2002-09-08 18:51:16 +00:00
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
void removeGarbage(const T* o) {
|
|
|
|
if (o == Cache)
|
|
|
|
Cache = 0; // Cache hit
|
|
|
|
else
|
2004-07-15 01:29:12 +00:00
|
|
|
Ts.erase(o);
|
2004-02-14 23:33:39 +00:00
|
|
|
}
|
2002-09-08 18:51:16 +00:00
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
bool hasGarbage(const std::string& Message) {
|
|
|
|
addGarbage(0); // Flush the Cache
|
2002-09-19 19:22:11 +00:00
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
assert(Cache == 0 && "No value should be cached anymore!");
|
2002-09-19 19:22:11 +00:00
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
if (!Ts.empty()) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "Leaked " << Name << " objects found: " << Message << ":\n";
|
2004-07-15 01:29:12 +00:00
|
|
|
for (typename std::set<const T*>::iterator I = Ts.begin(),
|
|
|
|
E = Ts.end(); I != E; ++I) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "\t";
|
2004-07-15 01:29:12 +00:00
|
|
|
PrinterTrait<T>::print(*I);
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "\n";
|
2004-07-15 01:29:12 +00:00
|
|
|
}
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << '\n';
|
2002-09-08 18:51:16 +00:00
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2002-09-08 18:51:16 +00:00
|
|
|
}
|
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
private:
|
|
|
|
std::set<const T*> Ts;
|
2004-02-15 23:33:48 +00:00
|
|
|
const T* Cache;
|
|
|
|
const char* const Name;
|
2004-02-14 23:33:39 +00:00
|
|
|
};
|
|
|
|
|
2008-04-10 21:11:47 +00:00
|
|
|
static LeakDetectorImpl<void> *Objects;
|
|
|
|
static LeakDetectorImpl<Value> *LLVMObjects;
|
2002-09-08 18:51:16 +00:00
|
|
|
|
2008-04-10 21:11:47 +00:00
|
|
|
static LeakDetectorImpl<void> &getObjects() {
|
2004-11-19 17:09:48 +00:00
|
|
|
if (Objects == 0)
|
|
|
|
Objects = new LeakDetectorImpl<void>("GENERIC");
|
|
|
|
return *Objects;
|
2004-02-14 23:33:39 +00:00
|
|
|
}
|
2002-09-08 18:51:16 +00:00
|
|
|
|
2008-04-10 21:11:47 +00:00
|
|
|
static LeakDetectorImpl<Value> &getLLVMObjects() {
|
2004-11-19 17:09:48 +00:00
|
|
|
if (LLVMObjects == 0)
|
|
|
|
LLVMObjects = new LeakDetectorImpl<Value>("LLVM");
|
|
|
|
return *LLVMObjects;
|
|
|
|
}
|
|
|
|
|
2008-04-10 21:11:47 +00:00
|
|
|
static void clearGarbage() {
|
2004-11-19 17:09:48 +00:00
|
|
|
delete Objects;
|
|
|
|
delete LLVMObjects;
|
|
|
|
Objects = 0;
|
|
|
|
LLVMObjects = 0;
|
2002-09-08 18:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
2004-02-14 23:33:39 +00:00
|
|
|
|
|
|
|
void LeakDetector::addGarbageObjectImpl(void *Object) {
|
|
|
|
getObjects().addGarbage(Object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::addGarbageObjectImpl(const Value *Object) {
|
|
|
|
getLLVMObjects().addGarbage(Object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::removeGarbageObjectImpl(void *Object) {
|
|
|
|
getObjects().removeGarbage(Object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
|
|
|
|
getLLVMObjects().removeGarbage(Object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::checkForGarbageImpl(const std::string &Message) {
|
|
|
|
// use non-short-circuit version so that both checks are performed
|
|
|
|
if (getObjects().hasGarbage(Message) |
|
|
|
|
getLLVMObjects().hasGarbage(Message))
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "\nThis is probably because you removed an object, but didn't "
|
|
|
|
<< "delete it. Please check your code for memory leaks.\n";
|
2004-11-19 17:09:48 +00:00
|
|
|
|
|
|
|
// Clear out results so we don't get duplicate warnings on
|
|
|
|
// next call...
|
|
|
|
clearGarbage();
|
2004-02-14 23:33:39 +00:00
|
|
|
}
|