[llvm] eb98aba - [LLVM-C] Use unwrapDI in LLVMDITypeGetName

Scott Linder via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 7 09:51:28 PDT 2023


Author: Tamir Duberstein
Date: 2023-07-07T16:51:20Z
New Revision: eb98abab2c83c9c101c4749c93836108657d6164

URL: https://github.com/llvm/llvm-project/commit/eb98abab2c83c9c101c4749c93836108657d6164
DIFF: https://github.com/llvm/llvm-project/commit/eb98abab2c83c9c101c4749c93836108657d6164.diff

LOG: [LLVM-C] Use unwrapDI in LLVMDITypeGetName

This function otherwise crashes. This behavior has been incorrect since
its introduction in 260b581498bed0b847e7e086852e0082d266711d (D46627).

Reviewed By: scott.linder

Differential Revision: https://reviews.llvm.org/D154630

Added: 
    llvm/test/Bindings/llvm-c/di-type-get-name.ll

Modified: 
    llvm/lib/IR/DebugInfo.cpp
    llvm/tools/llvm-c-test/debuginfo.c
    llvm/tools/llvm-c-test/llvm-c-test.h
    llvm/tools/llvm-c-test/main.c

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp
index 02374480589e22..48b5501c55ba47 100644
--- a/llvm/lib/IR/DebugInfo.cpp
+++ b/llvm/lib/IR/DebugInfo.cpp
@@ -1487,7 +1487,7 @@ uint16_t LLVMGetDINodeTag(LLVMMetadataRef MD) {
 }
 
 const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
-  StringRef Str = unwrap<DIType>(DType)->getName();
+  StringRef Str = unwrapDI<DIType>(DType)->getName();
   *Length = Str.size();
   return Str.data();
 }

diff  --git a/llvm/test/Bindings/llvm-c/di-type-get-name.ll b/llvm/test/Bindings/llvm-c/di-type-get-name.ll
new file mode 100644
index 00000000000000..5872a85bed7ed4
--- /dev/null
+++ b/llvm/test/Bindings/llvm-c/di-type-get-name.ll
@@ -0,0 +1,2 @@
+; RUN: llvm-c-test --di-type-get-name < /dev/null
+; This used to trigger an assertion

diff  --git a/llvm/tools/llvm-c-test/debuginfo.c b/llvm/tools/llvm-c-test/debuginfo.c
index 5f36f8e618661b..a3e41be12e95d4 100644
--- a/llvm/tools/llvm-c-test/debuginfo.c
+++ b/llvm/tools/llvm-c-test/debuginfo.c
@@ -231,3 +231,28 @@ int llvm_get_di_tag(void) {
 
   return 0;
 }
+
+int llvm_di_type_get_name(void) {
+  LLVMModuleRef M = LLVMModuleCreateWithName("Mod");
+
+  LLVMDIBuilderRef Builder = LLVMCreateDIBuilder(M);
+  const char Filename[] = "metadata.c";
+  const char Directory[] = ".";
+  LLVMMetadataRef File = LLVMDIBuilderCreateFile(
+      Builder, Filename, strlen(Filename), Directory, strlen(Directory));
+  const char Name[] = "TestClass";
+  LLVMMetadataRef Struct = LLVMDIBuilderCreateStructType(
+      Builder, File, Name, strlen(Name), File, 42, 64, 0,
+      LLVMDIFlagObjcClassComplete, NULL, NULL, 0, 0, NULL, NULL, 0);
+
+  size_t Len;
+  const char *TypeName = LLVMDITypeGetName(Struct, &Len);
+  assert(Len == strlen(Name));
+  assert(strncmp(TypeName, Name, Len) == 0);
+  (void)TypeName;
+
+  LLVMDisposeDIBuilder(Builder);
+  LLVMDisposeModule(M);
+
+  return 0;
+}

diff  --git a/llvm/tools/llvm-c-test/llvm-c-test.h b/llvm/tools/llvm-c-test/llvm-c-test.h
index 15b8573a9bfd97..00566660257e07 100644
--- a/llvm/tools/llvm-c-test/llvm-c-test.h
+++ b/llvm/tools/llvm-c-test/llvm-c-test.h
@@ -38,6 +38,7 @@ int llvm_disassemble(void);
 // debuginfo.c
 int llvm_test_dibuilder(void);
 int llvm_get_di_tag(void);
+int llvm_di_type_get_name(void);
 
 // metadata.c
 int llvm_add_named_metadata_operand(void);

diff  --git a/llvm/tools/llvm-c-test/main.c b/llvm/tools/llvm-c-test/main.c
index dc46b7410daff8..85bbc71d59ae84 100644
--- a/llvm/tools/llvm-c-test/main.c
+++ b/llvm/tools/llvm-c-test/main.c
@@ -49,6 +49,8 @@ static void print_usage(void) {
       "    Read lines of name, rpn from stdin - print generated module\n\n");
   fprintf(stderr, "  * --get-di-tag\n");
   fprintf(stderr, "    Run test for getting MDNode dwarf tag\n");
+  fprintf(stderr, "  * --di-type-get-name\n");
+  fprintf(stderr, "    Run test for getting MDNode type name\n");
   fprintf(stderr, "  * --replace-md-operand\n");
   fprintf(stderr, "    Run test for replacing MDNode operands\n");
   fprintf(stderr, "  * --is-a-value-as-metadata\n");
@@ -96,6 +98,8 @@ int main(int argc, char **argv) {
     return llvm_set_metadata();
   } else if (argc == 2 && !strcmp(argv[1], "--get-di-tag")) {
     return llvm_get_di_tag();
+  } else if (argc == 2 && !strcmp(argv[1], "--di-type-get-name")) {
+    return llvm_di_type_get_name();
   } else if (argc == 2 && !strcmp(argv[1], "--replace-md-operand")) {
     return llvm_replace_md_operand();
   } else if (argc == 2 && !strcmp(argv[1], "--is-a-value-as-metadata")) {


        


More information about the llvm-commits mailing list