From: Douglas Gregor <dgregor@apple.com>
Date: Mon, 31 Jan 2011 22:04:05 +0000 (+0000)
Subject: Teach Diagnostic::setClient() to free the existing, owned
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4f5e21e24fb9e6ec473a13f83b5c9a2c41501a70;p=clang

Teach Diagnostic::setClient() to free the existing, owned
client. Fixes a libclang leak.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124614 91177308-0d34-0410-b5e6-96231b3b80d8
---

diff --git a/include/clang/Basic/Diagnostic.h b/include/clang/Basic/Diagnostic.h
index fd34fdec1e..19e7c91f53 100644
--- a/include/clang/Basic/Diagnostic.h
+++ b/include/clang/Basic/Diagnostic.h
@@ -335,10 +335,7 @@ public:
   ///
   /// \param ShouldOwnClient true if the diagnostic object should take
   /// ownership of \c client.
-  void setClient(DiagnosticClient *client, bool ShouldOwnClient = true) {
-    Client = client;
-    OwnsDiagClient = ShouldOwnClient;
-  }
+  void setClient(DiagnosticClient *client, bool ShouldOwnClient = true);
 
   /// setErrorLimit - Specify a limit for the number of errors we should
   /// emit before giving up.  Zero disables the limit.
diff --git a/lib/Basic/Diagnostic.cpp b/lib/Basic/Diagnostic.cpp
index 9a263c1d0b..31e33315cc 100644
--- a/lib/Basic/Diagnostic.cpp
+++ b/lib/Basic/Diagnostic.cpp
@@ -62,6 +62,13 @@ Diagnostic::~Diagnostic() {
     delete Client;
 }
 
+void Diagnostic::setClient(DiagnosticClient *client, bool ShouldOwnClient) {
+  if (OwnsDiagClient && Client)
+    delete Client;
+  
+  Client = client;
+  OwnsDiagClient = ShouldOwnClient;
+}
 
 void Diagnostic::pushMappings(SourceLocation Loc) {
   DiagStateOnPushStack.push_back(GetCurDiagState());
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 808f4e9617..8c87d37651 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -827,6 +827,8 @@ static int perform_file_scan(const char *ast_file, const char *source_file,
   }
 
   fclose(fp);
+  clang_disposeTranslationUnit(TU);
+  clang_disposeIndex(Idx);
   return 0;
 }