Skip to content

Commit e87cc8a

Browse files
Davide Bertolaadrian-prantl
authored andcommitted
add LLVMGetDINodeTag to C bindings
Differential Revision: https://reviews.llvm.org/D138415
1 parent 91a6df3 commit e87cc8a

File tree

6 files changed

+44
-1
lines changed

6 files changed

+44
-1
lines changed

llvm/include/llvm-c/DebugInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,12 @@ LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression(
11491149
unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
11501150
LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits);
11511151

1152+
1153+
/**
1154+
* Get the dwarf::Tag of a DINode
1155+
*/
1156+
uint16_t LLVMGetDINodeTag(LLVMMetadataRef MD);
1157+
11521158
/**
11531159
* Retrieves the \c DIVariable associated with this global variable expression.
11541160
* \param GVE The global variable expression.

llvm/lib/IR/DebugInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,10 @@ LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
15081508
return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
15091509
}
15101510

1511+
uint16_t LLVMGetDINodeTag(LLVMMetadataRef MD) {
1512+
return unwrapDI<DINode>(MD)->getTag();
1513+
}
1514+
15111515
const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
15121516
StringRef Str = unwrap<DIType>(DType)->getName();
15131517
*Length = Str.size();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
; RUN: llvm-c-test --get-di-tag < /dev/null
2+
; This used to trigger an assertion

llvm/tools/llvm-c-test/debuginfo.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
\*===----------------------------------------------------------------------===*/
1313

1414
#include "llvm-c-test.h"
15-
#include "llvm-c/Core.h"
1615
#include "llvm-c/DebugInfo.h"
1716
#include <stdio.h>
1817
#include <string.h>
18+
#include <assert.h>
1919

2020
static LLVMMetadataRef
2121
declare_objc_class(LLVMDIBuilderRef DIB, LLVMMetadataRef File) {
@@ -201,3 +201,29 @@ int llvm_test_dibuilder(void) {
201201

202202
return 0;
203203
}
204+
205+
int llvm_get_di_tag(void) {
206+
LLVMModuleRef m = LLVMModuleCreateWithName("Mod");
207+
LLVMContextRef context = LLVMGetModuleContext(m);
208+
209+
LLVMMetadataRef metas[] = {LLVMMDStringInContext2(context, "foo", 3)};
210+
LLVMMetadataRef md = LLVMMDNodeInContext2(context, metas, 1);
211+
uint16_t tag0 = LLVMGetDINodeTag(md);
212+
213+
assert(tag0 == 0);
214+
215+
const char *filename = "metadata.c";
216+
LLVMDIBuilderRef builder = LLVMCreateDIBuilder(m);
217+
LLVMMetadataRef file =
218+
LLVMDIBuilderCreateFile(builder, filename, strlen(filename), ".", 1);
219+
LLVMMetadataRef decl = LLVMDIBuilderCreateStructType(
220+
builder, file, "TestClass", 9, file, 42, 64, 0,
221+
LLVMDIFlagObjcClassComplete, NULL, NULL, 0, 0, NULL, NULL, 0);
222+
uint16_t tag1 = LLVMGetDINodeTag(decl);
223+
224+
assert(tag1 == 0x13);
225+
226+
LLVMDisposeModule(m);
227+
228+
return 0;
229+
}

llvm/tools/llvm-c-test/llvm-c-test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ int llvm_disassemble(void);
3737

3838
// debuginfo.c
3939
int llvm_test_dibuilder(void);
40+
int llvm_get_di_tag(void);
4041

4142
// metadata.c
4243
int llvm_add_named_metadata_operand(void);

llvm/tools/llvm-c-test/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ static void print_usage(void) {
4444
fprintf(stderr, " Read lines of triple, hex ascii machine code from stdin "
4545
"- print disassembly\n\n");
4646
fprintf(stderr, " * --calc\n");
47+
fprintf(stderr, " * --get-di-tag\n");
48+
fprintf(stderr, " Run test for getting MDNode dwarf tag\n");
4749
fprintf(stderr, " * --replace-md-operand\n");
4850
fprintf(stderr, " Run test for replacing MDNode operands\n");
4951
fprintf(stderr, " * --is-a-value-as-metadata\n");
@@ -96,6 +98,8 @@ int main(int argc, char **argv) {
9698
return llvm_add_named_metadata_operand();
9799
} else if (argc == 2 && !strcmp(argv[1], "--set-metadata")) {
98100
return llvm_set_metadata();
101+
} else if (argc == 2 && !strcmp(argv[1], "--get-di-tag")) {
102+
return llvm_get_di_tag();
99103
} else if (argc == 2 && !strcmp(argv[1], "--replace-md-operand")) {
100104
return llvm_replace_md_operand();
101105
} else if (argc == 2 && !strcmp(argv[1], "--is-a-value-as-metadata")) {

0 commit comments

Comments
 (0)