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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-19 19:07:46 +02:00
|
|
|
#include "LLVMContextImpl.h"
|
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"
|
2009-06-17 23:56:05 +02:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-08-19 19:07:46 +02:00
|
|
|
#include "llvm/System/Mutex.h"
|
2009-06-18 18:54:52 +02:00
|
|
|
#include "llvm/System/Threading.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
|
|
|
|
2009-08-19 19:07:46 +02:00
|
|
|
static ManagedStatic<sys::SmartMutex<true> > ObjectsLock;
|
|
|
|
static ManagedStatic<LeakDetectorImpl<void> > Objects;
|
2004-07-15 03:29:12 +02:00
|
|
|
|
2009-08-19 19:07:46 +02:00
|
|
|
static void clearGarbage(LLVMContext &Context) {
|
|
|
|
Objects->clear();
|
|
|
|
Context.pImpl->LLVMObjects.clear();
|
2002-09-08 20:51:16 +02:00
|
|
|
}
|
2004-02-15 00:33:39 +01:00
|
|
|
|
|
|
|
void LeakDetector::addGarbageObjectImpl(void *Object) {
|
2009-08-19 19:07:46 +02:00
|
|
|
sys::SmartScopedLock<true> Lock(*ObjectsLock);
|
2009-06-17 23:56:05 +02:00
|
|
|
Objects->addGarbage(Object);
|
2004-02-15 00:33:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::addGarbageObjectImpl(const Value *Object) {
|
2009-08-19 19:07:46 +02:00
|
|
|
LLVMContextImpl *pImpl = Object->getContext().pImpl;
|
|
|
|
pImpl->LLVMObjects.addGarbage(Object);
|
2004-02-15 00:33:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::removeGarbageObjectImpl(void *Object) {
|
2009-08-19 19:07:46 +02:00
|
|
|
sys::SmartScopedLock<true> Lock(*ObjectsLock);
|
2009-06-17 23:56:05 +02:00
|
|
|
Objects->removeGarbage(Object);
|
2004-02-15 00:33:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
|
2009-08-19 19:07:46 +02:00
|
|
|
LLVMContextImpl *pImpl = Object->getContext().pImpl;
|
|
|
|
pImpl->LLVMObjects.removeGarbage(Object);
|
2004-02-15 00:33:39 +01:00
|
|
|
}
|
|
|
|
|
2009-08-19 19:07:46 +02:00
|
|
|
void LeakDetector::checkForGarbageImpl(LLVMContext &Context,
|
|
|
|
const std::string &Message) {
|
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
|
|
|
sys::SmartScopedLock<true> Lock(*ObjectsLock);
|
|
|
|
|
2009-06-17 23:56:05 +02:00
|
|
|
Objects->setName("GENERIC");
|
2009-08-19 19:07:46 +02:00
|
|
|
pImpl->LLVMObjects.setName("LLVM");
|
2009-06-17 23:56:05 +02:00
|
|
|
|
2004-02-15 00:33:39 +01:00
|
|
|
// use non-short-circuit version so that both checks are performed
|
2009-06-17 23:56:05 +02:00
|
|
|
if (Objects->hasGarbage(Message) |
|
2009-08-19 19:07:46 +02:00
|
|
|
pImpl->LLVMObjects.hasGarbage(Message))
|
2009-08-23 13:37:21 +02:00
|
|
|
errs() << "\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...
|
2009-08-19 19:07:46 +02:00
|
|
|
clearGarbage(Context);
|
2004-02-15 00:33:39 +01:00
|
|
|
}
|