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