The noreturn GCC extension is now supported.
[oota-llvm.git] / lib / VMCore / LeakDetector.cpp
1 //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LeakDetector class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/LeakDetector.h"
15 #include "llvm/Value.h"
16 #include <iostream>
17 #include <set>
18 using namespace llvm;
19
20 namespace {
21   template <class T>
22   struct PrinterTrait {
23     static void print(const T* P) { std::cerr << P; }
24   };
25
26   template<>
27   struct PrinterTrait<Value> {
28     static void print(const Value* P) { std::cerr << *P; }
29   };
30
31   template <typename T>
32   struct LeakDetectorImpl {
33     LeakDetectorImpl(const char* const name) : Cache(0), Name(name) { }
34
35     // Because the most common usage pattern, by far, is to add a
36     // garbage object, then remove it immediately, we optimize this
37     // case.  When an object is added, it is not added to the set
38     // immediately, it is added to the CachedValue Value.  If it is
39     // immediately removed, no set search need be performed.
40     void addGarbage(const T* o) {
41       if (Cache) {
42         assert(Ts.count(Cache) == 0 && "Object already in set!");
43         Ts.insert(Cache);
44       }
45       Cache = o;
46     }
47
48     void removeGarbage(const T* o) {
49       if (o == Cache)
50         Cache = 0; // Cache hit
51       else
52         Ts.erase(o);
53     }
54
55     bool hasGarbage(const std::string& Message) {
56       addGarbage(0); // Flush the Cache
57
58       assert(Cache == 0 && "No value should be cached anymore!");
59
60       if (!Ts.empty()) {
61         std::cerr
62             << "Leaked " << Name << " objects found: " << Message << ":\n";
63         for (typename std::set<const T*>::iterator I = Ts.begin(),
64                E = Ts.end(); I != E; ++I) {
65           std::cerr << "\t";
66           PrinterTrait<T>::print(*I);
67           std::cerr << "\n";
68         }
69         std::cerr << '\n';
70
71         return true;
72       }
73       return false;
74     }
75
76   private:
77     std::set<const T*> Ts;
78     const T* Cache;
79     const char* const Name;
80   };
81
82   LeakDetectorImpl<void>  *Objects;
83   LeakDetectorImpl<Value> *LLVMObjects;
84
85   LeakDetectorImpl<void> &getObjects() {
86     if (Objects == 0)
87       Objects = new LeakDetectorImpl<void>("GENERIC");
88     return *Objects;
89   }
90
91   LeakDetectorImpl<Value> &getLLVMObjects() {
92     if (LLVMObjects == 0)
93       LLVMObjects = new LeakDetectorImpl<Value>("LLVM");
94     return *LLVMObjects;
95   }
96
97   void clearGarbage() {
98     delete Objects;
99     delete LLVMObjects;
100     Objects = 0;
101     LLVMObjects = 0;
102   }
103 }
104
105 void LeakDetector::addGarbageObjectImpl(void *Object) {
106   getObjects().addGarbage(Object);
107 }
108
109 void LeakDetector::addGarbageObjectImpl(const Value *Object) {
110   getLLVMObjects().addGarbage(Object);
111 }
112
113 void LeakDetector::removeGarbageObjectImpl(void *Object) {
114   getObjects().removeGarbage(Object);
115 }
116
117 void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
118   getLLVMObjects().removeGarbage(Object);
119 }
120
121 void LeakDetector::checkForGarbageImpl(const std::string &Message) {
122   // use non-short-circuit version so that both checks are performed
123   if (getObjects().hasGarbage(Message) |
124       getLLVMObjects().hasGarbage(Message))
125     std::cerr << "\nThis is probably because you removed an object, but didn't "
126                  "delete it.  Please check your code for memory leaks.\n";
127
128   // Clear out results so we don't get duplicate warnings on
129   // next call...
130   clearGarbage();
131 }