Skip to content

Commit

Permalink
[LLVM-C] Use unwrapDI in LLVMDITypeGetName
Browse files Browse the repository at this point in the history
This function otherwise crashes. This behavior has been incorrect since
its introduction in 260b581 (D46627).

Reviewed By: scott.linder

Differential Revision: https://reviews.llvm.org/D154630
  • Loading branch information
tamird authored and slinder1 committed Jul 7, 2023
1 parent 9306352 commit eb98aba
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion llvm/lib/IR/DebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/Bindings/llvm-c/di-type-get-name.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
; RUN: llvm-c-test --di-type-get-name < /dev/null
; This used to trigger an assertion
25 changes: 25 additions & 0 deletions llvm/tools/llvm-c-test/debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions llvm/tools/llvm-c-test/llvm-c-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions llvm/tools/llvm-c-test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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")) {
Expand Down

0 comments on commit eb98aba

Please sign in to comment.