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/Support/LeakDetector.h"
00015 #include "llvm/ADT/SmallPtrSet.h"
00016 #include "llvm/Support/Compiler.h"
00017 #include "llvm/Support/Streams.h"
00018 #include "llvm/Value.h"
00019 using namespace llvm;
00020 
00021 namespace {
00022   template <class T>
00023   struct VISIBILITY_HIDDEN PrinterTrait {
00024     static void print(const T* P) { cerr << P; }
00025   };
00026 
00027   template<>
00028   struct VISIBILITY_HIDDEN PrinterTrait<Value> {
00029     static void print(const Value* P) { cerr << *P; }
00030   };
00031 
00032   template <typename T>
00033   struct VISIBILITY_HIDDEN LeakDetectorImpl {
00034     explicit LeakDetectorImpl(const char* const name) : Cache(0), Name(name) { }
00035 
00036     // Because the most common usage pattern, by far, is to add a
00037     // garbage object, then remove it immediately, we optimize this
00038     // case.  When an object is added, it is not added to the set
00039     // immediately, it is added to the CachedValue Value.  If it is
00040     // immediately removed, no set search need be performed.
00041     void addGarbage(const T* o) {
00042       if (Cache) {
00043         assert(Ts.count(Cache) == 0 && "Object already in set!");
00044         Ts.insert(Cache);
00045       }
00046       Cache = o;
00047     }
00048 
00049     void removeGarbage(const T* o) {
00050       if (o == Cache)
00051         Cache = 0; // Cache hit
00052       else
00053         Ts.erase(o);
00054     }
00055 
00056     bool hasGarbage(const std::string& Message) {
00057       addGarbage(0); // Flush the Cache
00058 
00059       assert(Cache == 0 && "No value should be cached anymore!");
00060 
00061       if (!Ts.empty()) {
00062         cerr << "Leaked " << Name << " objects found: " << Message << ":\n";
00063         for (typename SmallPtrSet<const T*, 8>::iterator I = Ts.begin(),
00064                E = Ts.end(); I != E; ++I) {
00065           cerr << "\t";
00066           PrinterTrait<T>::print(*I);
00067           cerr << "\n";
00068         }
00069         cerr << '\n';
00070 
00071         return true;
00072       }
00073       return false;
00074     }
00075 
00076   private:
00077     SmallPtrSet<const T*, 8> Ts;
00078     const T* Cache;
00079     const char* const Name;
00080   };
00081 
00082   static LeakDetectorImpl<void>  *Objects;
00083   static LeakDetectorImpl<Value> *LLVMObjects;
00084 
00085   static LeakDetectorImpl<void> &getObjects() {
00086     if (Objects == 0)
00087       Objects = new LeakDetectorImpl<void>("GENERIC");
00088     return *Objects;
00089   }
00090 
00091   static LeakDetectorImpl<Value> &getLLVMObjects() {
00092     if (LLVMObjects == 0)
00093       LLVMObjects = new LeakDetectorImpl<Value>("LLVM");
00094     return *LLVMObjects;
00095   }
00096 
00097   static void clearGarbage() {
00098     delete Objects;
00099     delete LLVMObjects;
00100     Objects = 0;
00101     LLVMObjects = 0;
00102   }
00103 }
00104 
00105 void LeakDetector::addGarbageObjectImpl(void *Object) {
00106   getObjects().addGarbage(Object);
00107 }
00108 
00109 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
00110   getLLVMObjects().addGarbage(Object);
00111 }
00112 
00113 void LeakDetector::removeGarbageObjectImpl(void *Object) {
00114   getObjects().removeGarbage(Object);
00115 }
00116 
00117 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
00118   getLLVMObjects().removeGarbage(Object);
00119 }
00120 
00121 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
00122   // use non-short-circuit version so that both checks are performed
00123   if (getObjects().hasGarbage(Message) |
00124       getLLVMObjects().hasGarbage(Message))
00125     cerr << "\nThis is probably because you removed an object, but didn't "
00126          << "delete it.  Please check your code for memory leaks.\n";
00127 
00128   // Clear out results so we don't get duplicate warnings on
00129   // next call...
00130   clearGarbage();
00131 }