2002-09-08 20:51:16 +02:00
|
|
|
//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-08 20:51:16 +02:00
|
|
|
//
|
|
|
|
// This file implements the LeakDetector class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-28 03:09:03 +01:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2008-08-14 22:40:10 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2006-12-07 02:30:32 +01:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-11-28 03:09:03 +01:00
|
|
|
#include "llvm/Support/Streams.h"
|
2002-09-08 20:51:16 +02:00
|
|
|
#include "llvm/Value.h"
|
2003-12-14 22:35:53 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2004-02-15 00:33:39 +01:00
|
|
|
namespace {
|
2004-07-15 03:29:12 +02:00
|
|
|
template <class T>
|
2006-06-29 00:57:00 +02:00
|
|
|
struct VISIBILITY_HIDDEN PrinterTrait {
|
2006-12-07 02:30:32 +01:00
|
|
|
static void print(const T* P) { cerr << P; }
|
2004-07-15 03:29:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2006-06-29 00:57:00 +02:00
|
|
|
struct VISIBILITY_HIDDEN PrinterTrait<Value> {
|
2006-12-07 02:30:32 +01:00
|
|
|
static void print(const Value* P) { cerr << *P; }
|
2004-07-15 03:29:12 +02:00
|
|
|
};
|
|
|
|
|
2004-02-15 00:33:39 +01:00
|
|
|
template <typename T>
|
2006-06-29 00:57:00 +02:00
|
|
|
struct VISIBILITY_HIDDEN LeakDetectorImpl {
|
2008-07-07 20:00:37 +02:00
|
|
|
explicit LeakDetectorImpl(const char* const name) : Cache(0), Name(name) { }
|
2002-09-08 20:51:16 +02:00
|
|
|
|
2004-02-15 00:33:39 +01: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 20:51:16 +02:00
|
|
|
|
2004-02-15 00:33:39 +01:00
|
|
|
void removeGarbage(const T* o) {
|
|
|
|
if (o == Cache)
|
|
|
|
Cache = 0; // Cache hit
|
|
|
|
else
|
2004-07-15 03:29:12 +02:00
|
|
|
Ts.erase(o);
|
2004-02-15 00:33:39 +01:00
|
|
|
}
|
2002-09-08 20:51:16 +02:00
|
|
|
|
2004-02-15 00:33:39 +01:00
|
|
|
bool hasGarbage(const std::string& Message) {
|
|
|
|
addGarbage(0); // Flush the Cache
|
2002-09-19 21:22:11 +02:00
|
|
|
|
2004-02-15 00:33:39 +01:00
|
|
|
assert(Cache == 0 && "No value should be cached anymore!");
|
2002-09-19 21:22:11 +02:00
|
|
|
|
2004-02-15 00:33:39 +01:00
|
|
|
if (!Ts.empty()) {
|
2006-12-07 02:30:32 +01:00
|
|
|
cerr << "Leaked " << Name << " objects found: " << Message << ":\n";
|
2008-08-14 22:40:10 +02:00
|
|
|
for (typename SmallPtrSet<const T*, 8>::iterator I = Ts.begin(),
|
2004-07-15 03:29:12 +02:00
|
|
|
E = Ts.end(); I != E; ++I) {
|
2006-12-07 02:30:32 +01:00
|
|
|
cerr << "\t";
|
2004-07-15 03:29:12 +02:00
|
|
|
PrinterTrait<T>::print(*I);
|
2006-12-07 02:30:32 +01:00
|
|
|
cerr << "\n";
|
2004-07-15 03:29:12 +02:00
|
|
|
}
|
2006-12-07 02:30:32 +01:00
|
|
|
cerr << '\n';
|
2002-09-08 20:51:16 +02:00
|
|
|
|
2004-02-15 00:33:39 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2002-09-08 20:51:16 +02:00
|
|
|
}
|
|
|
|
|
2004-02-15 00:33:39 +01:00
|
|
|
private:
|
2008-08-14 22:40:10 +02:00
|
|
|
SmallPtrSet<const T*, 8> Ts;
|
2004-02-16 00:33:48 +01:00
|
|
|
const T* Cache;
|
|
|
|
const char* const Name;
|
2004-02-15 00:33:39 +01:00
|
|
|
};
|
|
|
|
|
2008-04-10 23:11:47 +02:00
|
|
|
static LeakDetectorImpl<void> *Objects;
|
|
|
|
static LeakDetectorImpl<Value> *LLVMObjects;
|
2002-09-08 20:51:16 +02:00
|
|
|
|
2008-04-10 23:11:47 +02:00
|
|
|
static LeakDetectorImpl<void> &getObjects() {
|
2004-11-19 18:09:48 +01:00
|
|
|
if (Objects == 0)
|
|
|
|
Objects = new LeakDetectorImpl<void>("GENERIC");
|
|
|
|
return *Objects;
|
2004-02-15 00:33:39 +01:00
|
|
|
}
|
2002-09-08 20:51:16 +02:00
|
|
|
|
2008-04-10 23:11:47 +02:00
|
|
|
static LeakDetectorImpl<Value> &getLLVMObjects() {
|
2004-11-19 18:09:48 +01:00
|
|
|
if (LLVMObjects == 0)
|
|
|
|
LLVMObjects = new LeakDetectorImpl<Value>("LLVM");
|
|
|
|
return *LLVMObjects;
|
|
|
|
}
|
|
|
|
|
2008-04-10 23:11:47 +02:00
|
|
|
static void clearGarbage() {
|
2004-11-19 18:09:48 +01:00
|
|
|
delete Objects;
|
|
|
|
delete LLVMObjects;
|
|
|
|
Objects = 0;
|
|
|
|
LLVMObjects = 0;
|
2002-09-08 20:51:16 +02:00
|
|
|
}
|
|
|
|
}
|
2004-02-15 00:33:39 +01: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 02:30:32 +01: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 18:09:48 +01:00
|
|
|
|
|
|
|
// Clear out results so we don't get duplicate warnings on
|
|
|
|
// next call...
|
|
|
|
clearGarbage();
|
2004-02-15 00:33:39 +01:00
|
|
|
}
|