Chris Lattner | 213a9f9 | 2009-08-23 03:56:06 +0000 | [diff] [blame] | 1 | //===- LeaksContext.h - LeadDetector Implementation ------------*- C++ -*--===// |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines various helper methods and classes used by |
| 11 | // LLVMContextImpl for leaks detectors. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_LIB_IR_LEAKSCONTEXT_H |
| 16 | #define LLVM_LIB_IR_LEAKSCONTEXT_H |
Richard Smith | 8d039e4 | 2014-04-26 00:53:26 +0000 | [diff] [blame] | 17 | |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Value.h" |
Richard Smith | 8d039e4 | 2014-04-26 00:53:26 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 23 | |
| 24 | template <class T> |
| 25 | struct PrinterTrait { |
Chris Lattner | 213a9f9 | 2009-08-23 03:56:06 +0000 | [diff] [blame] | 26 | static void print(const T* P) { errs() << P; } |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | template<> |
| 30 | struct PrinterTrait<Value> { |
Chris Lattner | 213a9f9 | 2009-08-23 03:56:06 +0000 | [diff] [blame] | 31 | static void print(const Value* P) { errs() << *P; } |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | template <typename T> |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 35 | struct LeakDetectorImpl { |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 36 | explicit LeakDetectorImpl(const char* const name = "") : |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 37 | Cache(nullptr), Name(name) { } |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 38 | |
| 39 | void clear() { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 40 | Cache = nullptr; |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 41 | Ts.clear(); |
| 42 | } |
| 43 | |
| 44 | void setName(const char* n) { |
| 45 | Name = n; |
| 46 | } |
| 47 | |
| 48 | // Because the most common usage pattern, by far, is to add a |
| 49 | // garbage object, then remove it immediately, we optimize this |
| 50 | // case. When an object is added, it is not added to the set |
| 51 | // immediately, it is added to the CachedValue Value. If it is |
| 52 | // immediately removed, no set search need be performed. |
| 53 | void addGarbage(const T* o) { |
Rafael Espindola | 2c5792a | 2009-12-18 20:35:38 +0000 | [diff] [blame] | 54 | assert(Ts.count(o) == 0 && "Object already in set!"); |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 55 | if (Cache) { |
Rafael Espindola | 2c5792a | 2009-12-18 20:35:38 +0000 | [diff] [blame] | 56 | assert(Cache != o && "Object already in set!"); |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 57 | Ts.insert(Cache); |
| 58 | } |
| 59 | Cache = o; |
| 60 | } |
| 61 | |
| 62 | void removeGarbage(const T* o) { |
| 63 | if (o == Cache) |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 64 | Cache = nullptr; // Cache hit |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 65 | else |
| 66 | Ts.erase(o); |
| 67 | } |
| 68 | |
| 69 | bool hasGarbage(const std::string& Message) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 70 | addGarbage(nullptr); // Flush the Cache |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 71 | |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 72 | assert(!Cache && "No value should be cached anymore!"); |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 73 | |
| 74 | if (!Ts.empty()) { |
Chris Lattner | 213a9f9 | 2009-08-23 03:56:06 +0000 | [diff] [blame] | 75 | errs() << "Leaked " << Name << " objects found: " << Message << ":\n"; |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 76 | for (typename SmallPtrSet<const T*, 8>::iterator I = Ts.begin(), |
| 77 | E = Ts.end(); I != E; ++I) { |
Chris Lattner | 213a9f9 | 2009-08-23 03:56:06 +0000 | [diff] [blame] | 78 | errs() << '\t'; |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 79 | PrinterTrait<T>::print(*I); |
Chris Lattner | 213a9f9 | 2009-08-23 03:56:06 +0000 | [diff] [blame] | 80 | errs() << '\n'; |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 81 | } |
Chris Lattner | 213a9f9 | 2009-08-23 03:56:06 +0000 | [diff] [blame] | 82 | errs() << '\n'; |
Owen Anderson | 6d549d6 | 2009-08-19 17:07:46 +0000 | [diff] [blame] | 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | SmallPtrSet<const T*, 8> Ts; |
| 92 | const T* Cache; |
| 93 | const char* Name; |
| 94 | }; |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 95 | |
| 96 | } |
Richard Smith | 8d039e4 | 2014-04-26 00:53:26 +0000 | [diff] [blame] | 97 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 98 | #endif |