[PATCH] D99289: Verify that MDNodes belong to the same context as the Module.

Nick Lewycky via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 24 12:38:26 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG80f6c99a78ac: Verify that MDNodes belong to the same context as the Module. (authored by nicholas).

Changed prior to commit:
  https://reviews.llvm.org/D99289?vs=333079&id=333091#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99289/new/

https://reviews.llvm.org/D99289

Files:
  llvm/lib/IR/Verifier.cpp
  llvm/unittests/IR/VerifierTest.cpp


Index: llvm/unittests/IR/VerifierTest.cpp
===================================================================
--- llvm/unittests/IR/VerifierTest.cpp
+++ llvm/unittests/IR/VerifierTest.cpp
@@ -238,5 +238,20 @@
   }
 }
 
+TEST(VerifierTest, MDNodeWrongContext) {
+  LLVMContext C1, C2;
+  auto *Node = MDNode::get(C1, None);
+
+  Module M("M", C2);
+  auto *NamedNode = M.getOrInsertNamedMetadata("test");
+  NamedNode->addOperand(Node);
+
+  std::string Error;
+  raw_string_ostream ErrorOS(Error);
+  EXPECT_TRUE(verifyModule(M, &ErrorOS));
+  EXPECT_TRUE(StringRef(ErrorOS.str())
+                  .startswith("MDNode context does not match Module context!"));
+}
+
 } // end anonymous namespace
 } // end namespace llvm
Index: llvm/lib/IR/Verifier.cpp
===================================================================
--- llvm/lib/IR/Verifier.cpp
+++ llvm/lib/IR/Verifier.cpp
@@ -813,6 +813,9 @@
   if (!MDNodes.insert(&MD).second)
     return;
 
+  Assert(&MD.getContext() == &Context,
+         "MDNode context does not match Module context!", &MD);
+
   switch (MD.getMetadataID()) {
   default:
     llvm_unreachable("Invalid MDNode subclass");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99289.333091.patch
Type: text/x-patch
Size: 1154 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210324/35a76da7/attachment.bin>


More information about the llvm-commits mailing list