Use covariant return types for Instruction::clone, and eliminate
[oota-llvm.git] / lib / VMCore / LeakDetector.cpp
1 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LeakDetector class.
11 //
12 //===----------------------------------------------------------------------===//
13
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/System/Mutex.h"
20 #include "llvm/System/Threading.h"
21 #include "llvm/Value.h"
22 using namespace llvm;
23
24 static ManagedStatic<sys::SmartMutex<true> > ObjectsLock;
25 static ManagedStatic<LeakDetectorImpl<void> > Objects;
26
27 static void clearGarbage(LLVMContext &Context) {
28   Objects->clear();
29   Context.pImpl->LLVMObjects.clear();
30 }
31
32 void LeakDetector::addGarbageObjectImpl(void *Object) {
33   sys::SmartScopedLock<true> Lock(*ObjectsLock);
34   Objects->addGarbage(Object);
35 }
36
37 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
38   LLVMContextImpl *pImpl = Object->getContext().pImpl;
39   sys::SmartScopedLock<true> Lock(pImpl->LLVMObjectsLock);
40   pImpl->LLVMObjects.addGarbage(Object);
41 }
42
43 void LeakDetector::removeGarbageObjectImpl(void *Object) {
44   sys::SmartScopedLock<true> Lock(*ObjectsLock);
45   Objects->removeGarbage(Object);
46 }
47
48 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
49   LLVMContextImpl *pImpl = Object->getContext().pImpl;
50   sys::SmartScopedLock<true> Lock(pImpl->LLVMObjectsLock);
51   pImpl->LLVMObjects.removeGarbage(Object);
52 }
53
54 void LeakDetector::checkForGarbageImpl(LLVMContext &Context, 
55                                        const std::string &Message) {
56   LLVMContextImpl *pImpl = Context.pImpl;
57   sys::SmartScopedLock<true> Lock(*ObjectsLock);
58   sys::SmartScopedLock<true> CLock(pImpl->LLVMObjectsLock);
59   
60   Objects->setName("GENERIC");
61   pImpl->LLVMObjects.setName("LLVM");
62   
63   // use non-short-circuit version so that both checks are performed
64   if (Objects->hasGarbage(Message) |
65       pImpl->LLVMObjects.hasGarbage(Message))
66     errs() << "\nThis is probably because you removed an object, but didn't "
67            << "delete it.  Please check your code for memory leaks.\n";
68
69   // Clear out results so we don't get duplicate warnings on
70   // next call...
71   clearGarbage(Context);
72 }