IR: Specialize MDScope::getFile() for MDFile
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Sat, 28 Feb 2015 21:47:02 +0000 (21:47 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Sat, 28 Feb 2015 21:47:02 +0000 (21:47 +0000)
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.

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

include/llvm/IR/DebugInfoMetadata.h
unittests/IR/MetadataTest.cpp

index 4534a14925775fc49995fa136423e22f2f063ba5..1f47f8a1cc3f35f41161a5a4098f8254d37e8099 100644 (file)
@@ -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()) {
index 6f2372d58cb81ff262736ce905d6304fbf1ec798..4de6ecc0f9b0c331932a97f911199308df175f59 100644 (file)
@@ -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) {