diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp --- a/llvm/lib/IR/DebugInfo.cpp +++ b/llvm/lib/IR/DebugInfo.cpp @@ -1487,7 +1487,7 @@ } const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) { - StringRef Str = unwrap(DType)->getName(); + StringRef Str = unwrapDI(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 --- /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/test/Bindings/llvm-c/get-md-tag.ll b/llvm/test/Bindings/llvm-c/get-di-tag.ll rename from llvm/test/Bindings/llvm-c/get-md-tag.ll rename to llvm/test/Bindings/llvm-c/get-di-tag.ll diff --git a/llvm/tools/llvm-c-test/debuginfo.c b/llvm/tools/llvm-c-test/debuginfo.c --- a/llvm/tools/llvm-c-test/debuginfo.c +++ b/llvm/tools/llvm-c-test/debuginfo.c @@ -13,9 +13,10 @@ #include "llvm-c-test.h" #include "llvm-c/DebugInfo.h" + +#include #include #include -#include static LLVMMetadataRef declare_objc_class(LLVMDIBuilderRef DIB, LLVMMetadataRef File) { @@ -203,30 +204,56 @@ } int llvm_get_di_tag(void) { - LLVMModuleRef m = LLVMModuleCreateWithName("Mod"); - LLVMContextRef context = LLVMGetModuleContext(m); - - LLVMMetadataRef metas[] = {LLVMMDStringInContext2(context, "foo", 3)}; - LLVMMetadataRef md = LLVMMDNodeInContext2(context, metas, 1); - uint16_t tag0 = LLVMGetDINodeTag(md); - - assert(tag0 == 0); - (void)tag0; - - const char *filename = "metadata.c"; - LLVMDIBuilderRef builder = LLVMCreateDIBuilder(m); - LLVMMetadataRef file = - LLVMDIBuilderCreateFile(builder, filename, strlen(filename), ".", 1); - LLVMMetadataRef decl = LLVMDIBuilderCreateStructType( - builder, file, "TestClass", 9, file, 42, 64, 0, + LLVMModuleRef M = LLVMModuleCreateWithName("Mod"); + LLVMContextRef Context = LLVMGetModuleContext(M); + + const char String[] = "foo"; + LLVMMetadataRef StringMD = + LLVMMDStringInContext2(Context, String, strlen(String)); + LLVMMetadataRef NodeMD = LLVMMDNodeInContext2(Context, &StringMD, 1); + assert(LLVMGetDINodeTag(NodeMD) == 0); + (void)NodeMD; + + 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); - uint16_t tag1 = LLVMGetDINodeTag(decl); + assert(LLVMGetDINodeTag(Struct) == 0x13); + (void)Struct; - assert(tag1 == 0x13); - (void)tag1; + LLVMDisposeDIBuilder(Builder); + LLVMDisposeModule(M); - LLVMDisposeDIBuilder(builder); - LLVMDisposeModule(m); + 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); + assert(LLVMGetDINodeTag(Struct) == 0x13); + + 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 --- a/llvm/tools/llvm-c-test/llvm-c-test.h +++ b/llvm/tools/llvm-c-test/llvm-c-test.h @@ -38,6 +38,7 @@ // 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 --- a/llvm/tools/llvm-c-test/main.c +++ b/llvm/tools/llvm-c-test/main.c @@ -44,16 +44,18 @@ fprintf(stderr, " Read lines of triple, hex ascii machine code from stdin " "- print disassembly\n\n"); fprintf(stderr, " * --calc\n"); + fprintf( + stderr, + " 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"); fprintf(stderr, " Run test for checking if LLVMValueRef is a ValueAsMetadata\n"); - fprintf( - stderr, - " Read lines of name, rpn from stdin - print generated module\n\n"); fprintf(stderr, " * --echo\n"); fprintf(stderr, " Read bitcode file from stdin - print it back out\n\n"); fprintf(stderr, " * --echo --opaque-pointers\n"); @@ -96,6 +98,8 @@ 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")) { diff --git a/llvm/tools/llvm-c-test/metadata.c b/llvm/tools/llvm-c-test/metadata.c --- a/llvm/tools/llvm-c-test/metadata.c +++ b/llvm/tools/llvm-c-test/metadata.c @@ -13,70 +13,88 @@ \*===----------------------------------------------------------------------===*/ #include "llvm-c-test.h" +#include "llvm-c/Types.h" #include #include int llvm_add_named_metadata_operand(void) { - LLVMModuleRef m = LLVMModuleCreateWithName("Mod"); - LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) }; + LLVMModuleRef M = LLVMModuleCreateWithName("Mod"); + LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0); // This used to trigger an assertion - LLVMAddNamedMetadataOperand(m, "name", LLVMMDNode(values, 1)); + LLVMAddNamedMetadataOperand(M, "name", LLVMMDNode(&Int, 1)); - LLVMDisposeModule(m); + LLVMDisposeModule(M); return 0; } int llvm_set_metadata(void) { - LLVMBuilderRef b = LLVMCreateBuilder(); - LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) }; + LLVMBuilderRef Builder = LLVMCreateBuilder(); // This used to trigger an assertion - LLVMValueRef ret = LLVMBuildRetVoid(b); - LLVMSetMetadata(ret, LLVMGetMDKindID("kind", 4), LLVMMDNode(values, 1)); + LLVMValueRef Return = LLVMBuildRetVoid(Builder); - LLVMDisposeBuilder(b); - LLVMDeleteInstruction(ret); + const char Name[] = "kind"; + LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0); + LLVMSetMetadata(Return, LLVMGetMDKindID(Name, strlen(Name)), + LLVMMDNode(&Int, 1)); + + LLVMDisposeBuilder(Builder); + LLVMDeleteInstruction(Return); return 0; } int llvm_replace_md_operand(void) { - LLVMModuleRef m = LLVMModuleCreateWithName("Mod"); - LLVMContextRef context = LLVMGetModuleContext(m); - unsigned int tmp = 0; + LLVMModuleRef M = LLVMModuleCreateWithName("Mod"); + LLVMContextRef Context = LLVMGetModuleContext(M); + + const char String1[] = "foo"; + LLVMMetadataRef String1MD = + LLVMMDStringInContext2(Context, String1, strlen(String1)); + LLVMMetadataRef NodeMD = LLVMMDNodeInContext2(Context, &String1MD, 1); + LLVMValueRef Value = LLVMMetadataAsValue(Context, NodeMD); - LLVMMetadataRef metas[] = {LLVMMDStringInContext2(context, "foo", 3)}; - LLVMValueRef md = - LLVMMetadataAsValue(context, LLVMMDNodeInContext2(context, metas, 1)); + const char String2[] = "bar"; + LLVMMetadataRef String2MD = + LLVMMDStringInContext2(Context, String2, strlen(String2)); + LLVMReplaceMDNodeOperandWith(Value, 0, String2MD); - LLVMReplaceMDNodeOperandWith(md, 0, - LLVMMDStringInContext2(context, "bar", 3)); + LLVMValueRef Operand = LLVMGetOperand(Value, 0); - assert(!strncmp(LLVMGetMDString(LLVMGetOperand(md, 0), &tmp), "bar", 0)); - (void)tmp; + unsigned int Len; + const char *String = LLVMGetMDString(Operand, &Len); + assert(Len == strlen(String2)); + assert(strncmp(String, String2, Len) == 0); + (void)String; - LLVMDisposeModule(m); + LLVMDisposeModule(M); return 0; } int llvm_is_a_value_as_metadata(void) { - LLVMModuleRef m = LLVMModuleCreateWithName("Mod"); - LLVMContextRef context = LLVMGetModuleContext(m); - - LLVMValueRef values[] = {LLVMConstInt(LLVMInt32Type(), 0, 0)}; - LLVMValueRef md = LLVMMDNode(values, 1); - assert(LLVMIsAValueAsMetadata(md) == md); - (void)md; - - LLVMMetadataRef metas[] = {LLVMMDStringInContext2(context, "foo", 3)}; - LLVMValueRef md2 = - LLVMMetadataAsValue(context, LLVMMDNodeInContext2(context, metas, 1)); - assert(LLVMIsAValueAsMetadata(md2) == NULL); - (void)md2; + LLVMModuleRef M = LLVMModuleCreateWithName("Mod"); + LLVMContextRef Context = LLVMGetModuleContext(M); + + { + LLVMValueRef Int = LLVMConstInt(LLVMInt32Type(), 0, 0); + LLVMValueRef NodeMD = LLVMMDNode(&Int, 1); + assert(LLVMIsAValueAsMetadata(NodeMD) == NodeMD); + (void)NodeMD; + } + + { + const char String[] = "foo"; + LLVMMetadataRef StringMD = + LLVMMDStringInContext2(Context, String, strlen(String)); + LLVMMetadataRef NodeMD = LLVMMDNodeInContext2(Context, &StringMD, 1); + LLVMValueRef Value = LLVMMetadataAsValue(Context, NodeMD); + assert(LLVMIsAValueAsMetadata(Value) == NULL); + (void)Value; + } return 0; }