Skip to content

Commit

Permalink
AsmWriter: Handle broken metadata nodes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dexonsmith committed Mar 16, 2015
1 parent 763e186 commit bee74a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions unittests/IR/MetadataTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit bee74a3

Please sign in to comment.