AsmWriter: Handle broken metadata nodes
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Mon, 16 Mar 2015 21:21:10 +0000 (21:21 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Mon, 16 Mar 2015 21:21:10 +0000 (21:21 +0000)
Print out temporary `MDNode`s so we don't crash in the verifier (or
during `dump()` output).

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

lib/IR/AsmWriter.cpp
unittests/IR/MetadataTest.cpp

index ea6822052d01d1f8caaacead9e622ca09f863f32..fb23c7e436ac7e924c763a5cfabea4f950670d03 100644 (file)
@@ -1916,10 +1916,10 @@ static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
                                     TypePrinting *TypePrinter,
                                     SlotTracker *Machine,
                                     const Module *Context) {
-  assert(!Node->isTemporary() && "Unexpected forward declaration");
-
   if (Node->isDistinct())
     Out << "distinct ";
+  else if (Node->isTemporary())
+    Out << "<temporary!> "; // Handle broken code.
 
   switch (Node->getMetadataID()) {
   default:
index 12fa392312bb556b1d48861cd1acf5463cce36ba..70ce2946af1aeb5ed11ba93f52e1f5c30b5e8ee2 100644 (file)
@@ -251,6 +251,22 @@ TEST_F(MDNodeTest, Print) {
     EXPECT_EQ(Expected_, Actual_);                                             \
   } while (false)
 
+TEST_F(MDNodeTest, PrintTemporary) {
+  MDNode *Arg = getNode();
+  TempMDNode Temp = MDNode::getTemporary(Context, Arg);
+  MDNode *N = getNode(Temp.get());
+  Module M("test", Context);
+  NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
+  NMD->addOperand(N);
+
+  EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
+  EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
+  EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
+
+  // Cleanup.
+  Temp->replaceAllUsesWith(Arg);
+}
+
 TEST_F(MDNodeTest, PrintFromModule) {
   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
   MDString *S = MDString::get(Context, "foo");