/3rd_party/llvm/lib/VMCore/LeakDetector.cpp

https://code.google.com/p/softart/ · C++ · 69 lines · 43 code · 11 blank · 15 comment · 1 complexity · 4730eeb7bcaf07c78d7a2fa021e7bb31 MD5 · raw file

  1. //===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===//
  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 implements the LeakDetector class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "LLVMContextImpl.h"
  14. #include "llvm/Support/LeakDetector.h"
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/Support/Compiler.h"
  17. #include "llvm/Support/ManagedStatic.h"
  18. #include "llvm/Support/Mutex.h"
  19. #include "llvm/Support/Threading.h"
  20. #include "llvm/Value.h"
  21. using namespace llvm;
  22. static ManagedStatic<sys::SmartMutex<true> > ObjectsLock;
  23. static ManagedStatic<LeakDetectorImpl<void> > Objects;
  24. static void clearGarbage(LLVMContext &Context) {
  25. Objects->clear();
  26. Context.pImpl->LLVMObjects.clear();
  27. }
  28. void LeakDetector::addGarbageObjectImpl(void *Object) {
  29. sys::SmartScopedLock<true> Lock(*ObjectsLock);
  30. Objects->addGarbage(Object);
  31. }
  32. void LeakDetector::addGarbageObjectImpl(const Value *Object) {
  33. LLVMContextImpl *pImpl = Object->getContext().pImpl;
  34. pImpl->LLVMObjects.addGarbage(Object);
  35. }
  36. void LeakDetector::removeGarbageObjectImpl(void *Object) {
  37. sys::SmartScopedLock<true> Lock(*ObjectsLock);
  38. Objects->removeGarbage(Object);
  39. }
  40. void LeakDetector::removeGarbageObjectImpl(const Value *Object) {
  41. LLVMContextImpl *pImpl = Object->getContext().pImpl;
  42. pImpl->LLVMObjects.removeGarbage(Object);
  43. }
  44. void LeakDetector::checkForGarbageImpl(LLVMContext &Context,
  45. const std::string &Message) {
  46. LLVMContextImpl *pImpl = Context.pImpl;
  47. sys::SmartScopedLock<true> Lock(*ObjectsLock);
  48. Objects->setName("GENERIC");
  49. pImpl->LLVMObjects.setName("LLVM");
  50. // use non-short-circuit version so that both checks are performed
  51. if (Objects->hasGarbage(Message) |
  52. pImpl->LLVMObjects.hasGarbage(Message))
  53. errs() << "\nThis is probably because you removed an object, but didn't "
  54. << "delete it. Please check your code for memory leaks.\n";
  55. // Clear out results so we don't get duplicate warnings on
  56. // next call...
  57. clearGarbage(Context);
  58. }