[llvm-commits] [llvm] r97763 - in /llvm/trunk: lib/VMCore/LLVMContextImpl.h unittests/VMCore/MetadataTest.cpp

Jeffrey Yasskin jyasskin at google.com
Thu Mar 4 15:24:20 PST 2010


Author: jyasskin
Date: Thu Mar  4 17:24:19 2010
New Revision: 97763

URL: http://llvm.org/viewvc/llvm-project?rev=97763&view=rev
Log:
Stop leaking MDStrings.

Modified:
    llvm/trunk/lib/VMCore/LLVMContextImpl.h
    llvm/trunk/unittests/VMCore/MetadataTest.cpp

Modified: llvm/trunk/lib/VMCore/LLVMContextImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/LLVMContextImpl.h?rev=97763&r1=97762&r2=97763&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/LLVMContextImpl.h (original)
+++ llvm/trunk/lib/VMCore/LLVMContextImpl.h Thu Mar  4 17:24:19 2010
@@ -246,6 +246,11 @@
       MDNode *N = &(*MDNodeSet.begin());
       N->destroy();
     }
+    // Destroy MDStrings.
+    for (StringMap<MDString*>::iterator I = MDStringCache.begin(),
+           E = MDStringCache.end(); I != E; ++I) {
+      delete I->second;
+    }
   }
 };
 

Modified: llvm/trunk/unittests/VMCore/MetadataTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/VMCore/MetadataTest.cpp?rev=97763&r1=97762&r2=97763&view=diff
==============================================================================
--- llvm/trunk/unittests/VMCore/MetadataTest.cpp (original)
+++ llvm/trunk/unittests/VMCore/MetadataTest.cpp Thu Mar  4 17:24:19 2010
@@ -20,11 +20,15 @@
 
 namespace {
 
-LLVMContext &Context = getGlobalContext();
+class MetadataTest : public testing::Test {
+protected:
+  LLVMContext Context;
+};
+typedef MetadataTest MDStringTest;
 
 // Test that construction of MDString with different value produces different
 // MDString objects, even with the same string pointer and nulls in the string.
-TEST(MDStringTest, CreateDifferent) {
+TEST_F(MDStringTest, CreateDifferent) {
   char x[3] = { 'f', 0, 'A' };
   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
   x[2] = 'B';
@@ -34,7 +38,7 @@
 
 // Test that creation of MDStrings with the same string contents produces the
 // same MDString object, even with different pointers.
-TEST(MDStringTest, CreateSame) {
+TEST_F(MDStringTest, CreateSame) {
   char x[4] = { 'a', 'b', 'c', 'X' };
   char y[4] = { 'a', 'b', 'c', 'Y' };
 
@@ -44,7 +48,7 @@
 }
 
 // Test that MDString prints out the string we fed it.
-TEST(MDStringTest, PrintingSimple) {
+TEST_F(MDStringTest, PrintingSimple) {
   char *str = new char[13];
   strncpy(str, "testing 1 2 3", 13);
   MDString *s = MDString::get(Context, StringRef(str, 13));
@@ -58,7 +62,7 @@
 }
 
 // Test printing of MDString with non-printable characters.
-TEST(MDStringTest, PrintingComplex) {
+TEST_F(MDStringTest, PrintingComplex) {
   char str[5] = {0, '\n', '"', '\\', -1};
   MDString *s = MDString::get(Context, StringRef(str+0, 5));
   std::string Str;
@@ -67,8 +71,10 @@
   EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
 }
 
+typedef MetadataTest MDNodeTest;
+
 // Test the two constructors, and containing other Constants.
-TEST(MDNodeTest, Simple) {
+TEST_F(MDNodeTest, Simple) {
   char x[3] = { 'a', 'b', 'c' };
   char y[3] = { '1', '2', '3' };
 
@@ -101,7 +107,7 @@
   EXPECT_EQ(n1, n2->getOperand(0));
 }
 
-TEST(MDNodeTest, Delete) {
+TEST_F(MDNodeTest, Delete) {
   Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
   Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
 
@@ -115,8 +121,9 @@
 }
 
 TEST(NamedMDNodeTest, Search) {
-  Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
-  Constant *C2 = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 2);
+  LLVMContext Context;
+  Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
+  Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2);
 
   Value *const V = C;
   Value *const V2 = C2;
@@ -125,9 +132,9 @@
 
   MDNode *Nodes[2] = { n, n2 };
 
-  Module *M = new Module("MyModule", getGlobalContext());
+  Module *M = new Module("MyModule", Context);
   const char *Name = "llvm.NMD1";
-  NamedMDNode *NMD = NamedMDNode::Create(getGlobalContext(), Name, &Nodes[0], 2, M);
+  NamedMDNode *NMD = NamedMDNode::Create(Context, Name, &Nodes[0], 2, M);
   std::string Str;
   raw_string_ostream oss(Str);
   NMD->print(oss);





More information about the llvm-commits mailing list