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
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2002-09-08 18:51:16 +00:00
|
|
|
#include "llvm/Value.h"
|
2004-07-04 12:19:56 +00:00
|
|
|
#include <iostream>
|
2002-09-08 18:51:16 +00:00
|
|
|
#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>
|
|
|
|
struct PrinterTrait {
|
|
|
|
static void print(const T* P) { std::cerr << P; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct PrinterTrait<Value> {
|
|
|
|
static void print(const Value* P) { std::cerr << *P; }
|
|
|
|
};
|
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
template <typename T>
|
|
|
|
struct LeakDetectorImpl {
|
|
|
|
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()) {
|
|
|
|
std::cerr
|
2004-07-15 01:29:12 +00:00
|
|
|
<< "Leaked " << Name << " objects found: " << Message << ":\n";
|
|
|
|
for (typename std::set<const T*>::iterator I = Ts.begin(),
|
|
|
|
E = Ts.end(); I != E; ++I) {
|
|
|
|
std::cerr << "\t";
|
|
|
|
PrinterTrait<T>::print(*I);
|
|
|
|
std::cerr << "\n";
|
|
|
|
}
|
2004-02-14 23:33:39 +00:00
|
|
|
std::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
|
|
|
};
|
|
|
|
|
2004-11-19 17:09:48 +00:00
|
|
|
LeakDetectorImpl<void> *Objects;
|
|
|
|
LeakDetectorImpl<Value> *LLVMObjects;
|
2002-09-08 18:51:16 +00:00
|
|
|
|
2004-11-19 17:09:48 +00:00
|
|
|
LeakDetectorImpl<void> &getObjects() {
|
|
|
|
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
|
|
|
|
2004-11-19 17:09:48 +00:00
|
|
|
LeakDetectorImpl<Value> &getLLVMObjects() {
|
|
|
|
if (LLVMObjects == 0)
|
|
|
|
LLVMObjects = new LeakDetectorImpl<Value>("LLVM");
|
|
|
|
return *LLVMObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearGarbage() {
|
|
|
|
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))
|
|
|
|
std::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
|
|
|
}
|