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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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/Support/Visibility.h"
00016 #include "llvm/Value.h"
00017 #include <iostream>
00018 #include <set>
00019 using namespace llvm;
00020 
00021 namespace {
00022   template <class T>
00023   struct VISIBILITY_HIDDEN PrinterTrait {
00024     static void print(const T* P) { std::cerr << P; }
00025   };
00026 
00027   template<>
00028   struct VISIBILITY_HIDDEN PrinterTrait<Value> {
00029     static void print(const Value* P) { std::cerr << *P; }
00030   };
00031 
00032   template <typename T>
00033   struct VISIBILITY_HIDDEN LeakDetectorImpl {
00034     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         std::cerr
00063             << "Leaked " << Name << " objects found: " << Message << ":\n";
00064         for (typename std::set<const T*>::iterator I = Ts.begin(),
00065                E = Ts.end(); I != E; ++I) {
00066           std::cerr << "\t";
00067           PrinterTrait<T>::print(*I);
00068           std::cerr << "\n";
00069         }
00070         std::cerr << '\n';
00071 
00072         return true;
00073       }
00074       return false;
00075     }
00076 
00077   private:
00078     std::set<const T*> Ts;
00079     const T* Cache;
00080     const char* const Name;
00081   };
00082 
00083   LeakDetectorImpl<void>  *Objects;
00084   LeakDetectorImpl<Value> *LLVMObjects;
00085 
00086   LeakDetectorImpl<void> &getObjects() {
00087     if (Objects == 0)
00088       Objects = new LeakDetectorImpl<void>("GENERIC");
00089     return *Objects;
00090   }
00091 
00092   LeakDetectorImpl<Value> &getLLVMObjects() {
00093     if (LLVMObjects == 0)
00094       LLVMObjects = new LeakDetectorImpl<Value>("LLVM");
00095     return *LLVMObjects;
00096   }
00097 
00098   void clearGarbage() {
00099     delete Objects;
00100     delete LLVMObjects;
00101     Objects = 0;
00102     LLVMObjects = 0;
00103   }
00104 }
00105 
00106 void LeakDetector::addGarbageObjectImpl(void *Object) {
00107   getObjects().addGarbage(Object);
00108 }
00109 
00110 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
00111   getLLVMObjects().addGarbage(Object);
00112 }
00113 
00114 void LeakDetector::removeGarbageObjectImpl(void *Object) {
00115   getObjects().removeGarbage(Object);
00116 }
00117 
00118 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
00119   getLLVMObjects().removeGarbage(Object);
00120 }
00121 
00122 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
00123   // use non-short-circuit version so that both checks are performed
00124   if (getObjects().hasGarbage(Message) |
00125       getLLVMObjects().hasGarbage(Message))
00126     std::cerr << "\nThis is probably because you removed an object, but didn't "
00127                  "delete it.  Please check your code for memory leaks.\n";
00128 
00129   // Clear out results so we don't get duplicate warnings on
00130   // next call...
00131   clearGarbage();
00132 }