[llvm] r230871 - IR: Specialize MDScope::getFile() for MDFile

Duncan P. N. Exon Smith dexonsmith at apple.com
Sat Feb 28 13:47:02 PST 2015


Author: dexonsmith
Date: Sat Feb 28 15:47:02 2015
New Revision: 230871

URL: http://llvm.org/viewvc/llvm-project?rev=230871&view=rev
Log:
IR: Specialize MDScope::getFile() for MDFile

Fix `MDScope::getFile()` so that it correctly returns a valid `MDFile`
even when it's an instance of `MDFile`.  This logic is necessary because
of r230057.  I'm working on moving the new hierarchy into place
out-of-tree (on track to commit Monday morning, BTW), and this was
exposed by a few failing tests.

Modified:
    llvm/trunk/include/llvm/IR/DebugInfoMetadata.h
    llvm/trunk/unittests/IR/MetadataTest.cpp

Modified: llvm/trunk/include/llvm/IR/DebugInfoMetadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/DebugInfoMetadata.h?rev=230871&r1=230870&r2=230871&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/DebugInfoMetadata.h (original)
+++ llvm/trunk/include/llvm/IR/DebugInfoMetadata.h Sat Feb 28 15:47:02 2015
@@ -330,7 +330,15 @@ protected:
   ~MDScope() {}
 
 public:
-  Metadata *getFile() const { return getOperand(0); }
+  /// \brief Return the underlying file.
+  ///
+  /// An \a MDFile is an \a MDScope, but it doesn't point at a separate file
+  /// (it\em is the file).  If \c this is an \a MDFile, we need to return \c
+  /// this.  Otherwise, return the first operand, which is where all other
+  /// subclasses store their file pointer.
+  Metadata *getFile() const {
+    return isa<MDFile>(this) ? const_cast<MDScope *>(this) : getOperand(0);
+  }
 
   static bool classof(const Metadata *MD) {
     switch (MD->getMetadataID()) {

Modified: llvm/trunk/unittests/IR/MetadataTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/MetadataTest.cpp?rev=230871&r1=230870&r2=230871&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/MetadataTest.cpp (original)
+++ llvm/trunk/unittests/IR/MetadataTest.cpp Sat Feb 28 15:47:02 2015
@@ -1041,6 +1041,12 @@ TEST_F(MDFileTest, get) {
   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
 }
 
+TEST_F(MDFileTest, ScopeGetFile) {
+  // Ensure that MDScope::getFile() returns itself.
+  MDScope *N = MDFile::get(Context, "file", "dir");
+  EXPECT_EQ(N, N->getFile());
+}
+
 typedef MetadataTest MDCompileUnitTest;
 
 TEST_F(MDCompileUnitTest, get) {





More information about the llvm-commits mailing list