1 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LeakDetector class.
12 //===----------------------------------------------------------------------===//
14 #include "LLVMContextImpl.h"
15 #include "llvm/Support/LeakDetector.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/Streams.h"
20 #include "llvm/System/Mutex.h"
21 #include "llvm/System/Threading.h"
22 #include "llvm/Value.h"
25 static ManagedStatic<sys::SmartMutex<true> > ObjectsLock;
26 static ManagedStatic<LeakDetectorImpl<void> > Objects;
28 static void clearGarbage(LLVMContext &Context) {
30 Context.pImpl->LLVMObjects.clear();
33 void LeakDetector::addGarbageObjectImpl(void *Object) {
34 sys::SmartScopedLock<true> Lock(*ObjectsLock);
35 Objects->addGarbage(Object);
38 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
39 LLVMContextImpl *pImpl = Object->getContext().pImpl;
40 sys::SmartScopedLock<true> Lock(pImpl->LLVMObjectsLock);
41 pImpl->LLVMObjects.addGarbage(Object);
44 void LeakDetector::removeGarbageObjectImpl(void *Object) {
45 sys::SmartScopedLock<true> Lock(*ObjectsLock);
46 Objects->removeGarbage(Object);
49 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
50 LLVMContextImpl *pImpl = Object->getContext().pImpl;
51 sys::SmartScopedLock<true> Lock(pImpl->LLVMObjectsLock);
52 pImpl->LLVMObjects.removeGarbage(Object);
55 void LeakDetector::checkForGarbageImpl(LLVMContext &Context,
56 const std::string &Message) {
57 LLVMContextImpl *pImpl = Context.pImpl;
58 sys::SmartScopedLock<true> Lock(*ObjectsLock);
59 sys::SmartScopedLock<true> CLock(pImpl->LLVMObjectsLock);
61 Objects->setName("GENERIC");
62 pImpl->LLVMObjects.setName("LLVM");
64 // use non-short-circuit version so that both checks are performed
65 if (Objects->hasGarbage(Message) |
66 pImpl->LLVMObjects.hasGarbage(Message))
67 cerr << "\nThis is probably because you removed an object, but didn't "
68 << "delete it. Please check your code for memory leaks.\n";
70 // Clear out results so we don't get duplicate warnings on
72 clearGarbage(Context);