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/System/Mutex.h"
20 #include "llvm/System/Threading.h"
21 #include "llvm/Value.h"
24 static ManagedStatic
<sys::SmartMutex
<true> > ObjectsLock
;
25 static ManagedStatic
<LeakDetectorImpl
<void> > Objects
;
27 static void clearGarbage(LLVMContext
&Context
) {
29 Context
.pImpl
->LLVMObjects
.clear();
32 void LeakDetector::addGarbageObjectImpl(void *Object
) {
33 sys::SmartScopedLock
<true> Lock(*ObjectsLock
);
34 Objects
->addGarbage(Object
);
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
);
43 void LeakDetector::removeGarbageObjectImpl(void *Object
) {
44 sys::SmartScopedLock
<true> Lock(*ObjectsLock
);
45 Objects
->removeGarbage(Object
);
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
);
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
);
60 Objects
->setName("GENERIC");
61 pImpl
->LLVMObjects
.setName("LLVM");
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";
69 // Clear out results so we don't get duplicate warnings on
71 clearGarbage(Context
);