LLVM API Documentation

LeakDetector.cpp
Go to the documentation of this file.
00001 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the LeakDetector class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/IR/LeakDetector.h"
00015 #include "LLVMContextImpl.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include "llvm/IR/Value.h"
00018 #include "llvm/Support/Compiler.h"
00019 #include "llvm/Support/ManagedStatic.h"
00020 #include "llvm/Support/Mutex.h"
00021 #include "llvm/Support/Threading.h"
00022 using namespace llvm;
00023 
00024 static ManagedStatic<sys::SmartMutex<true> > ObjectsLock;
00025 static ManagedStatic<LeakDetectorImpl<void> > Objects;
00026 
00027 static void clearGarbage(LLVMContext &Context) {
00028   Objects->clear();
00029   Context.pImpl->LLVMObjects.clear();
00030 }
00031 
00032 void LeakDetector::addGarbageObjectImpl(void *Object) {
00033   sys::SmartScopedLock<true> Lock(*ObjectsLock);
00034   Objects->addGarbage(Object);
00035 }
00036 
00037 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
00038   LLVMContextImpl *pImpl = Object->getContext().pImpl;
00039   pImpl->LLVMObjects.addGarbage(Object);
00040 }
00041 
00042 void LeakDetector::removeGarbageObjectImpl(void *Object) {
00043   sys::SmartScopedLock<true> Lock(*ObjectsLock);
00044   Objects->removeGarbage(Object);
00045 }
00046 
00047 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
00048   LLVMContextImpl *pImpl = Object->getContext().pImpl;
00049   pImpl->LLVMObjects.removeGarbage(Object);
00050 }
00051 
00052 void LeakDetector::checkForGarbageImpl(LLVMContext &Context, 
00053                                        const std::string &Message) {
00054   LLVMContextImpl *pImpl = Context.pImpl;
00055   sys::SmartScopedLock<true> Lock(*ObjectsLock);
00056   
00057   Objects->setName("GENERIC");
00058   pImpl->LLVMObjects.setName("LLVM");
00059   
00060   // use non-short-circuit version so that both checks are performed
00061   if (Objects->hasGarbage(Message) |
00062       pImpl->LLVMObjects.hasGarbage(Message))
00063     errs() << "\nThis is probably because you removed an object, but didn't "
00064            << "delete it.  Please check your code for memory leaks.\n";
00065 
00066   // Clear out results so we don't get duplicate warnings on
00067   // next call...
00068   clearGarbage(Context);
00069 }