2002-09-09 02:51:16 +08:00
|
|
|
//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-09 02:51:16 +08:00
|
|
|
//
|
|
|
|
// This file implements the LeakDetector class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-20 01:07:46 +08:00
|
|
|
#include "LLVMContextImpl.h"
|
2006-11-28 10:09:03 +08:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2008-08-15 04:40:10 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2006-12-07 09:30:32 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-06-18 05:56:05 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
|
|
|
#include "llvm/Support/Threading.h"
|
2002-09-09 02:51:16 +08:00
|
|
|
#include "llvm/Value.h"
|
2003-12-15 05:35:53 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2009-08-20 01:07:46 +08:00
|
|
|
static ManagedStatic<sys::SmartMutex<true> > ObjectsLock;
|
|
|
|
static ManagedStatic<LeakDetectorImpl<void> > Objects;
|
2004-07-15 09:29:12 +08:00
|
|
|
|
2009-08-20 01:07:46 +08:00
|
|
|
static void clearGarbage(LLVMContext &Context) {
|
|
|
|
Objects->clear();
|
|
|
|
Context.pImpl->LLVMObjects.clear();
|
2002-09-09 02:51:16 +08:00
|
|
|
}
|
2004-02-15 07:33:39 +08:00
|
|
|
|
|
|
|
void LeakDetector::addGarbageObjectImpl(void *Object) {
|
2009-08-20 01:07:46 +08:00
|
|
|
sys::SmartScopedLock<true> Lock(*ObjectsLock);
|
2009-06-18 05:56:05 +08:00
|
|
|
Objects->addGarbage(Object);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::addGarbageObjectImpl(const Value *Object) {
|
2009-08-20 01:07:46 +08:00
|
|
|
LLVMContextImpl *pImpl = Object->getContext().pImpl;
|
|
|
|
pImpl->LLVMObjects.addGarbage(Object);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::removeGarbageObjectImpl(void *Object) {
|
2009-08-20 01:07:46 +08:00
|
|
|
sys::SmartScopedLock<true> Lock(*ObjectsLock);
|
2009-06-18 05:56:05 +08:00
|
|
|
Objects->removeGarbage(Object);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
|
2009-08-20 01:07:46 +08:00
|
|
|
LLVMContextImpl *pImpl = Object->getContext().pImpl;
|
|
|
|
pImpl->LLVMObjects.removeGarbage(Object);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|
|
|
|
|
2009-08-20 01:07:46 +08:00
|
|
|
void LeakDetector::checkForGarbageImpl(LLVMContext &Context,
|
|
|
|
const std::string &Message) {
|
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
|
|
|
sys::SmartScopedLock<true> Lock(*ObjectsLock);
|
|
|
|
|
2009-06-18 05:56:05 +08:00
|
|
|
Objects->setName("GENERIC");
|
2009-08-20 01:07:46 +08:00
|
|
|
pImpl->LLVMObjects.setName("LLVM");
|
2009-06-18 05:56:05 +08:00
|
|
|
|
2004-02-15 07:33:39 +08:00
|
|
|
// use non-short-circuit version so that both checks are performed
|
2009-06-18 05:56:05 +08:00
|
|
|
if (Objects->hasGarbage(Message) |
|
2009-08-20 01:07:46 +08:00
|
|
|
pImpl->LLVMObjects.hasGarbage(Message))
|
2009-08-23 19:37:21 +08: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-20 01:09:48 +08:00
|
|
|
|
|
|
|
// Clear out results so we don't get duplicate warnings on
|
|
|
|
// next call...
|
2009-08-20 01:07:46 +08:00
|
|
|
clearGarbage(Context);
|
2004-02-15 07:33:39 +08:00
|
|
|
}
|