[llvm] r233322 - Verifier: Check accessors of MDLocation

Duncan P. N. Exon Smith dexonsmith at apple.com
Thu Mar 26 15:05:04 PDT 2015


Author: dexonsmith
Date: Thu Mar 26 17:05:04 2015
New Revision: 233322

URL: http://llvm.org/viewvc/llvm-project?rev=233322&view=rev
Log:
Verifier: Check accessors of MDLocation

Check accessors of `MDLocation`, and change them to `cast<>` down to the
right types.  Also add type-safe factory functions.

All the callers that handle broken code need to use the new versions of
the accessors (`getRawScope()` instead of `getScope()`) that still
return `Metadata*`.  This is also necessary for things like
`MDNodeKeyImpl<MDLocation>` (in LLVMContextImpl.h) that need to unique
the nodes when their operands might still be forward references of the
wrong type.

In the `Value` hierarchy, consumers that handle broken code use
`getOperand()` directly.  However, debug info nodes have a ton of
operands, and their order (even their existence) isn't stable yet.  It's
safer and more maintainable to add an explicit "raw" accessor on the
class itself.

Modified:
    llvm/trunk/include/llvm/IR/DebugInfoMetadata.h
    llvm/trunk/lib/AsmParser/LLParser.cpp
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/lib/IR/AsmWriter.cpp
    llvm/trunk/lib/IR/LLVMContextImpl.h
    llvm/trunk/lib/IR/Verifier.cpp
    llvm/trunk/test/Assembler/mdlocation.ll
    llvm/trunk/test/Assembler/metadata.ll
    llvm/trunk/test/Linker/Inputs/mdlocation.ll
    llvm/trunk/test/Linker/mdlocation.ll
    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=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/DebugInfoMetadata.h (original)
+++ llvm/trunk/include/llvm/IR/DebugInfoMetadata.h Thu Mar 26 17:05:04 2015
@@ -882,6 +882,13 @@ class MDLocation : public MDNode {
                              unsigned Column, Metadata *Scope,
                              Metadata *InlinedAt, StorageType Storage,
                              bool ShouldCreate = true);
+  static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
+                             unsigned Column, MDLocalScope *Scope,
+                             MDLocation *InlinedAt, StorageType Storage,
+                             bool ShouldCreate = true) {
+    return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
+                   static_cast<Metadata *>(InlinedAt), Storage, ShouldCreate);
+  }
 
   TempMDLocation cloneImpl() const {
     return getTemporary(getContext(), getLine(), getColumn(), getScope(),
@@ -896,14 +903,25 @@ public:
                     (unsigned Line, unsigned Column, Metadata *Scope,
                      Metadata *InlinedAt = nullptr),
                     (Line, Column, Scope, InlinedAt))
+  DEFINE_MDNODE_GET(MDLocation,
+                    (unsigned Line, unsigned Column, MDLocalScope *Scope,
+                     MDLocation *InlinedAt = nullptr),
+                    (Line, Column, Scope, InlinedAt))
 
   /// \brief Return a (temporary) clone of this.
   TempMDLocation clone() const { return cloneImpl(); }
 
   unsigned getLine() const { return SubclassData32; }
   unsigned getColumn() const { return SubclassData16; }
-  Metadata *getScope() const { return getOperand(0); }
-  Metadata *getInlinedAt() const {
+  MDLocalScope *getScope() const {
+    return cast_or_null<MDLocalScope>(getRawScope());
+  }
+  MDLocation *getInlinedAt() const {
+    return cast_or_null<MDLocation>(getRawInlinedAt());
+  }
+
+  Metadata *getRawScope() const { return getOperand(0); }
+  Metadata *getRawInlinedAt() const {
     if (getNumOperands() == 2)
       return getOperand(1);
     return nullptr;

Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLParser.cpp Thu Mar 26 17:05:04 2015
@@ -3348,8 +3348,8 @@ bool LLParser::ParseMDLocation(MDNode *&
   PARSE_MD_FIELDS();
 #undef VISIT_MD_FIELDS
 
-  auto get = (IsDistinct ? MDLocation::getDistinct : MDLocation::get);
-  Result = get(Context, line.Val, column.Val, scope.Val, inlinedAt.Val);
+  Result = GET_OR_DISTINCT(
+      MDLocation, (Context, line.Val, column.Val, scope.Val, inlinedAt.Val));
   return false;
 }
 

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Thu Mar 26 17:05:04 2015
@@ -1691,14 +1691,15 @@ std::error_code BitcodeReader::ParseMeta
       if (Record.size() != 5)
         return Error("Invalid record");
 
-      auto get = Record[0] ? MDLocation::getDistinct : MDLocation::get;
       unsigned Line = Record[1];
       unsigned Column = Record[2];
       MDNode *Scope = cast<MDNode>(MDValueList.getValueFwdRef(Record[3]));
       Metadata *InlinedAt =
           Record[4] ? MDValueList.getValueFwdRef(Record[4] - 1) : nullptr;
-      MDValueList.AssignValue(get(Context, Line, Column, Scope, InlinedAt),
-                              NextMDValueNo++);
+      MDValueList.AssignValue(
+          GET_OR_DISTINCT(MDLocation, Record[0],
+                          (Context, Line, Column, Scope, InlinedAt)),
+          NextMDValueNo++);
       break;
     }
     case bitc::METADATA_GENERIC_DEBUG: {

Modified: llvm/trunk/lib/IR/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/AsmWriter.cpp?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/lib/IR/AsmWriter.cpp (original)
+++ llvm/trunk/lib/IR/AsmWriter.cpp Thu Mar 26 17:05:04 2015
@@ -1419,11 +1419,10 @@ static void writeMDLocation(raw_ostream
   if (DL->getColumn())
     Out << FS << "column: " << DL->getColumn();
   Out << FS << "scope: ";
-  WriteAsOperandInternal(Out, DL->getScope(), TypePrinter, Machine, Context);
-  if (DL->getInlinedAt()) {
+  WriteAsOperandInternal(Out, DL->getRawScope(), TypePrinter, Machine, Context);
+  if (auto *IA = DL->getRawInlinedAt()) {
     Out << FS << "inlinedAt: ";
-    WriteAsOperandInternal(Out, DL->getInlinedAt(), TypePrinter, Machine,
-                           Context);
+    WriteAsOperandInternal(Out, IA, TypePrinter, Machine, Context);
   }
   Out << ")";
 }

Modified: llvm/trunk/lib/IR/LLVMContextImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/LLVMContextImpl.h?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/lib/IR/LLVMContextImpl.h (original)
+++ llvm/trunk/lib/IR/LLVMContextImpl.h Thu Mar 26 17:05:04 2015
@@ -240,12 +240,12 @@ template <> struct MDNodeKeyImpl<MDLocat
       : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
 
   MDNodeKeyImpl(const MDLocation *L)
-      : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()),
-        InlinedAt(L->getInlinedAt()) {}
+      : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()),
+        InlinedAt(L->getRawInlinedAt()) {}
 
   bool isKeyOf(const MDLocation *RHS) const {
     return Line == RHS->getLine() && Column == RHS->getColumn() &&
-           Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
+           Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt();
   }
   unsigned getHashValue() const {
     return hash_combine(Line, Column, Scope, InlinedAt);

Modified: llvm/trunk/lib/IR/Verifier.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Verifier.cpp?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Verifier.cpp (original)
+++ llvm/trunk/lib/IR/Verifier.cpp Thu Mar 26 17:05:04 2015
@@ -659,8 +659,9 @@ void Verifier::visitMetadataAsValue(cons
 }
 
 void Verifier::visitMDLocation(const MDLocation &N) {
-  Assert(N.getScope(), "location requires a valid scope", &N);
-  if (auto *IA = N.getInlinedAt())
+  Assert(N.getRawScope() && isa<MDLocalScope>(N.getRawScope()),
+         "location requires a valid scope", &N, N.getRawScope());
+  if (auto *IA = N.getRawInlinedAt())
     Assert(isa<MDLocation>(IA), "inlined-at should be a location", &N, IA);
 }
 

Modified: llvm/trunk/test/Assembler/mdlocation.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/mdlocation.ll?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/test/Assembler/mdlocation.ll (original)
+++ llvm/trunk/test/Assembler/mdlocation.ll Thu Mar 26 17:05:04 2015
@@ -4,8 +4,8 @@
 ; CHECK: !named = !{!0, !1, !1, !2, !2, !3, !3, !4}
 !named = !{!0, !1, !2, !3, !4, !5, !6, !7}
 
-; CHECK: !0 = !{}
-!0 = !{}
+; CHECK: !0 = !MDSubprogram(
+!0 = !MDSubprogram()
 
 ; CHECK-NEXT: !1 = !MDLocation(line: 3, column: 7, scope: !0)
 !1 = !MDLocation(line: 3, column: 7, scope: !0)

Modified: llvm/trunk/test/Assembler/metadata.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/metadata.ll?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/test/Assembler/metadata.ll (original)
+++ llvm/trunk/test/Assembler/metadata.ll Thu Mar 26 17:05:04 2015
@@ -12,7 +12,7 @@ define void @test() {
 }
 
 !0 = !MDLocation(line: 662302, column: 26, scope: !1)
-!1 = !{i32 4, !"foo"}
+!1 = !MDSubprogram(name: "foo")
 
 declare void @llvm.dbg.func.start(metadata) nounwind readnone
 

Modified: llvm/trunk/test/Linker/Inputs/mdlocation.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/Inputs/mdlocation.ll?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/test/Linker/Inputs/mdlocation.ll (original)
+++ llvm/trunk/test/Linker/Inputs/mdlocation.ll Thu Mar 26 17:05:04 2015
@@ -1,10 +1,10 @@
 !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}
 
-!0 = !{} ; Use this as a scope.
+!0 = !MDSubprogram() ; Use this as a scope.
 !1 = !MDLocation(line: 3, column: 7, scope: !0)
 !2 = !MDLocation(line: 3, column: 7, scope: !0, inlinedAt: !1)
 !3 = !MDLocation(line: 3, column: 7, scope: !0, inlinedAt: !2)
-!4 = distinct !{} ; Test actual remapping.
+!4 = distinct !MDSubprogram() ; Test actual remapping.
 !5 = !MDLocation(line: 3, column: 7, scope: !4)
 !6 = !MDLocation(line: 3, column: 7, scope: !4, inlinedAt: !5)
 !7 = !MDLocation(line: 3, column: 7, scope: !4, inlinedAt: !6)

Modified: llvm/trunk/test/Linker/mdlocation.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/mdlocation.ll?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/test/Linker/mdlocation.ll (original)
+++ llvm/trunk/test/Linker/mdlocation.ll Thu Mar 26 17:05:04 2015
@@ -5,27 +5,27 @@
 ; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !0, !1, !2, !3, !10, !11, !12, !13, !14, !15}
 !named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}
 
-; CHECK:      !0 = !{}
+; CHECK:      !0 = !MDSubprogram(
 ; CHECK-NEXT: !1 = !MDLocation(line: 3, column: 7, scope: !0)
 ; CHECK-NEXT: !2 = !MDLocation(line: 3, column: 7, scope: !0, inlinedAt: !1)
 ; CHECK-NEXT: !3 = !MDLocation(line: 3, column: 7, scope: !0, inlinedAt: !2)
-; CHECK-NEXT: !4 = distinct !{}
+; CHECK-NEXT: !4 = distinct !MDSubprogram(
 ; CHECK-NEXT: !5 = !MDLocation(line: 3, column: 7, scope: !4)
 ; CHECK-NEXT: !6 = !MDLocation(line: 3, column: 7, scope: !4, inlinedAt: !5)
 ; CHECK-NEXT: !7 = !MDLocation(line: 3, column: 7, scope: !4, inlinedAt: !6)
 ; CHECK-NEXT: !8 = distinct !MDLocation(line: 3, column: 7, scope: !0)
 ; CHECK-NEXT: !9 = distinct !MDLocation(line: 3, column: 7, scope: !0, inlinedAt: !8)
-; CHECK-NEXT: !10 = distinct !{}
+; CHECK-NEXT: !10 = distinct !MDSubprogram(
 ; CHECK-NEXT: !11 = !MDLocation(line: 3, column: 7, scope: !10)
 ; CHECK-NEXT: !12 = !MDLocation(line: 3, column: 7, scope: !10, inlinedAt: !11)
 ; CHECK-NEXT: !13 = !MDLocation(line: 3, column: 7, scope: !10, inlinedAt: !12)
 ; CHECK-NEXT: !14 = distinct !MDLocation(line: 3, column: 7, scope: !0)
 ; CHECK-NEXT: !15 = distinct !MDLocation(line: 3, column: 7, scope: !0, inlinedAt: !14)
-!0 = !{} ; Use this as a scope.
+!0 = !MDSubprogram() ; Use this as a scope.
 !1 = !MDLocation(line: 3, column: 7, scope: !0)
 !2 = !MDLocation(line: 3, column: 7, scope: !0, inlinedAt: !1)
 !3 = !MDLocation(line: 3, column: 7, scope: !0, inlinedAt: !2)
-!4 = distinct !{} ; Test actual remapping.
+!4 = distinct !MDSubprogram() ; Test actual remapping.
 !5 = !MDLocation(line: 3, column: 7, scope: !4)
 !6 = !MDLocation(line: 3, column: 7, scope: !4, inlinedAt: !5)
 !7 = !MDLocation(line: 3, column: 7, scope: !4, inlinedAt: !6)

Modified: llvm/trunk/unittests/IR/MetadataTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/MetadataTest.cpp?rev=233322&r1=233321&r2=233322&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/MetadataTest.cpp (original)
+++ llvm/trunk/unittests/IR/MetadataTest.cpp Thu Mar 26 17:05:04 2015
@@ -69,6 +69,12 @@ protected:
     Metadata *MDs[] = {MD1, MD2};
     return MDNode::get(Context, MDs);
   }
+
+  MDSubprogram *getSubprogram() {
+    return MDSubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
+                                     nullptr, false, false, 0, nullptr, 0, 0, 0,
+                                     0);
+  }
 };
 typedef MetadataTest MDStringTest;
 
@@ -671,7 +677,7 @@ TEST_F(MDNodeTest, deleteTemporaryWithTr
 typedef MetadataTest MDLocationTest;
 
 TEST_F(MDLocationTest, Overflow) {
-  MDNode *N = MDNode::get(Context, None);
+  MDSubprogram *N = getSubprogram();
   {
     MDLocation *L = MDLocation::get(Context, 2, 7, N);
     EXPECT_EQ(2u, L->getLine());
@@ -696,7 +702,7 @@ TEST_F(MDLocationTest, Overflow) {
 }
 
 TEST_F(MDLocationTest, getDistinct) {
-  MDNode *N = MDNode::get(Context, None);
+  MDNode *N = getSubprogram();
   MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
   EXPECT_TRUE(L0->isDistinct());
   MDLocation *L1 = MDLocation::get(Context, 2, 7, N);





More information about the llvm-commits mailing list