Switch DIDescriptor to use a TrackingVH. - This makes it much safer to work with...
authorDaniel Dunbar <daniel@zuster.org>
Tue, 22 Sep 2009 02:03:18 +0000 (02:03 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Tue, 22 Sep 2009 02:03:18 +0000 (02:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82507 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/DebugInfo.h
lib/Analysis/DebugInfo.cpp

index f211be2d49c822a3bbb1f5d64bd48c07422ca4bc..35f6787f02e25996484694c1b98484ef466c233f 100644 (file)
@@ -24,6 +24,7 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/Support/Dwarf.h"
+#include "llvm/Support/ValueHandle.h"
 
 namespace llvm {
   class BasicBlock;
@@ -45,7 +46,7 @@ namespace llvm {
 
   class DIDescriptor {
   protected:
-    MDNode *DbgNode;
+    TrackingVH<MDNode> DbgNode;
 
     /// DIDescriptor constructor.  If the specified node is non-null, check
     /// to make sure that the tag in the descriptor matches 'RequiredTag'.  If
index a072161896418dd0cf1d84eb5223bfbde1773473..531f609fd18870532ead2e82375eec331fa2c8f6 100644 (file)
@@ -267,8 +267,17 @@ void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
     return;
 
   assert (!D.isNull() && "Can not replace with null");
-  DbgNode->replaceAllUsesWith(D.getNode());
-  delete DbgNode;
+
+  // Since we use a TrackingVH for the node, its easy for clients to manufacture
+  // legitimate situations where they want to replaceAllUsesWith() on something
+  // which, due to uniquing, has merged with the source. We shield clients from
+  // this detail by allowing a value to be replaced with replaceAllUsesWith()
+  // itself.
+  if (getNode() != D.getNode()) {
+    MDNode *Node = DbgNode;
+    Node->replaceAllUsesWith(D.getNode());
+    delete Node;
+  }
 }
 
 /// Verify - Verify that a compile unit is well formed.
@@ -395,7 +404,7 @@ bool DISubprogram::describes(const Function *F) {
 /// dump - Print descriptor.
 void DIDescriptor::dump() const {
   errs() << "[" << dwarf::TagString(getTag()) << "] ";
-  errs().write_hex((intptr_t)DbgNode) << ']';
+  errs().write_hex((intptr_t) &*DbgNode) << ']';
 }
 
 /// dump - Print compile unit.