DI: Add Function::getSubprogram()
Add `Function::setSubprogram()` and `Function::getSubprogram()`,
convenience methods to forward to `setMetadata()` and `getMetadata()`,
respectively, and deal in `DISubprogram` instead of `MDNode`.
Also add a verifier check to enforce that `!dbg` attachments are always
subprograms.
Originally (when I had the llvm-dev discussion back in April) I thought
I'd store a pointer directly on `llvm::Function` for these attachments
-- we frequently have debug info, and that's much cheaper than using map
in the context if there are no other function-level attachments -- but
for now I'm just using the generic infrastructure. Let's add the extra
complexity only if this shows up in a profile.
llvm-svn: 246339
diff --git a/llvm/unittests/IR/MetadataTest.cpp b/llvm/unittests/IR/MetadataTest.cpp
index b3c26eb..6741c79 100644
--- a/llvm/unittests/IR/MetadataTest.cpp
+++ b/llvm/unittests/IR/MetadataTest.cpp
@@ -2273,4 +2273,16 @@
EXPECT_EQ(12304u, *F->getEntryCount());
}
+TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
+ Function *F = getFunction("foo");
+ DISubprogram *SP = getSubprogram();
+ F->setSubprogram(SP);
+
+ // Note that the static_cast confirms that F->getSubprogram() actually
+ // returns an DISubprogram.
+ EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
+ EXPECT_EQ(SP, F->getMetadata("dbg"));
+ EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
+}
+
}