2002-09-08 18:51:16 +00:00
|
|
|
//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-08 18:51:16 +00:00
|
|
|
//
|
|
|
|
// This file implements the LeakDetector class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Support/LeakDetector.h"
|
|
|
|
#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
|
|
|
// Clear out results so we don't get duplicate warnings on
|
|
|
|
// next call...
|
|
|
|
Ts.clear();
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
typedef LeakDetectorImpl<void> Objects;
|
|
|
|
typedef LeakDetectorImpl<Value> LLVMObjects;
|
2002-09-08 18:51:16 +00:00
|
|
|
|
2004-02-14 23:33:39 +00:00
|
|
|
Objects& getObjects() {
|
2004-02-15 23:33:48 +00:00
|
|
|
static Objects *o = 0;
|
|
|
|
if (o == 0)
|
|
|
|
o = new Objects("GENERIC");
|
|
|
|
return *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
|
|
|
LLVMObjects& getLLVMObjects() {
|
2004-02-15 23:33:48 +00:00
|
|
|
static LLVMObjects *o = 0;
|
|
|
|
if (o == 0)
|
|
|
|
o = new LLVMObjects("LLVM");
|
|
|
|
return *o;
|
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";
|
|
|
|
}
|