From 169d2bd06003c39970bc94c99669a34b61bb7e45 Mon Sep 17 00:00:00 2001 From: dim Date: Mon, 8 Apr 2013 18:41:23 +0000 Subject: Vendor import of llvm trunk r178860: http://llvm.org/svn/llvm-project/llvm/trunk@178860 --- lib/IR/LeakDetector.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 lib/IR/LeakDetector.cpp (limited to 'lib/IR/LeakDetector.cpp') diff --git a/lib/IR/LeakDetector.cpp b/lib/IR/LeakDetector.cpp new file mode 100644 index 0000000..835e5e6 --- /dev/null +++ b/lib/IR/LeakDetector.cpp @@ -0,0 +1,69 @@ +//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the LeakDetector class. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/LeakDetector.h" +#include "LLVMContextImpl.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/IR/Value.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/Mutex.h" +#include "llvm/Support/Threading.h" +using namespace llvm; + +static ManagedStatic > ObjectsLock; +static ManagedStatic > Objects; + +static void clearGarbage(LLVMContext &Context) { + Objects->clear(); + Context.pImpl->LLVMObjects.clear(); +} + +void LeakDetector::addGarbageObjectImpl(void *Object) { + sys::SmartScopedLock Lock(*ObjectsLock); + Objects->addGarbage(Object); +} + +void LeakDetector::addGarbageObjectImpl(const Value *Object) { + LLVMContextImpl *pImpl = Object->getContext().pImpl; + pImpl->LLVMObjects.addGarbage(Object); +} + +void LeakDetector::removeGarbageObjectImpl(void *Object) { + sys::SmartScopedLock Lock(*ObjectsLock); + Objects->removeGarbage(Object); +} + +void LeakDetector::removeGarbageObjectImpl(const Value *Object) { + LLVMContextImpl *pImpl = Object->getContext().pImpl; + pImpl->LLVMObjects.removeGarbage(Object); +} + +void LeakDetector::checkForGarbageImpl(LLVMContext &Context, + const std::string &Message) { + LLVMContextImpl *pImpl = Context.pImpl; + sys::SmartScopedLock Lock(*ObjectsLock); + + Objects->setName("GENERIC"); + pImpl->LLVMObjects.setName("LLVM"); + + // use non-short-circuit version so that both checks are performed + if (Objects->hasGarbage(Message) | + pImpl->LLVMObjects.hasGarbage(Message)) + errs() << "\nThis is probably because you removed an object, but didn't " + << "delete it. Please check your code for memory leaks.\n"; + + // Clear out results so we don't get duplicate warnings on + // next call... + clearGarbage(Context); +} -- cgit v1.1