Skip to content

Commit 0d799d6

Browse files
committed
Add LLVMGlobalAddDebugInfo to Core.cpp
1 parent d02c85a commit 0d799d6

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

llvm/include/llvm-c/Core.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,14 @@ LLVM_C_ABI void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind);
27072707
*/
27082708
LLVM_C_ABI void LLVMGlobalClearMetadata(LLVMValueRef Global);
27092709

2710+
/**
2711+
* Add debuginfo metadata to this global.
2712+
*
2713+
* @see llvm::GlobalVariable::addDebugInfo()
2714+
*/
2715+
LLVM_C_ABI void LLVMGlobalAddDebugInfo(LLVMValueRef Global,
2716+
LLVMMetadataRef GVE);
2717+
27102718
/**
27112719
* Retrieves an array of metadata entries representing the metadata attached to
27122720
* this value. The caller is responsible for freeing this array by calling

llvm/lib/IR/Core.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,11 @@ void LLVMGlobalClearMetadata(LLVMValueRef Global) {
21942194
unwrap<GlobalObject>(Global)->clearMetadata();
21952195
}
21962196

2197+
void LLVMGlobalAddDebugInfo(LLVMValueRef Global, LLVMMetadataRef GVE) {
2198+
unwrap<GlobalVariable>(Global)->addDebugInfo(
2199+
unwrap<DIGlobalVariableExpression>(GVE));
2200+
}
2201+
21972202
/*--.. Operations on global variables ......................................--*/
21982203

21992204
LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
; RUN: llvm-c-test --add-globaldebuginfo < /dev/null
2+
; This used to trigger an assertion

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,37 @@ int llvm_di_type_get_name(void) {
416416

417417
return 0;
418418
}
419+
420+
int llvm_add_globaldebuginfo(void) {
421+
const char *Filename = "debuginfo.c";
422+
LLVMModuleRef M = LLVMModuleCreateWithName(Filename);
423+
LLVMDIBuilderRef Builder = LLVMCreateDIBuilder(M);
424+
LLVMMetadataRef File =
425+
LLVMDIBuilderCreateFile(Builder, Filename, strlen(Filename), ".", 1);
426+
427+
LLVMMetadataRef GlobalVarValueExpr =
428+
LLVMDIBuilderCreateConstantValueExpression(Builder, 0);
429+
LLVMMetadataRef Int64Ty =
430+
LLVMDIBuilderCreateBasicType(Builder, "Int64", 5, 64, 0, LLVMDIFlagZero);
431+
LLVMMetadataRef Int64TypeDef = LLVMDIBuilderCreateTypedef(
432+
Builder, Int64Ty, "int64_t", 7, File, 42, File, 0);
433+
434+
LLVMMetadataRef GVE = LLVMDIBuilderCreateGlobalVariableExpression(
435+
Builder, File, "global", 6, "", 0, File, 1, Int64TypeDef, true,
436+
GlobalVarValueExpr, NULL, 0);
437+
438+
LLVMTypeRef RecType =
439+
LLVMStructCreateNamed(LLVMGetModuleContext(M), "struct");
440+
LLVMValueRef Global = LLVMAddGlobal(M, RecType, "global");
441+
442+
LLVMGlobalAddDebugInfo(Global, GVE);
443+
size_t numEntries;
444+
LLVMValueMetadataEntry *ME = LLVMGlobalCopyAllMetadata(Global, &numEntries);
445+
assert(ME != NULL);
446+
assert(numEntries == 1);
447+
448+
LLVMDisposeDIBuilder(Builder);
449+
LLVMDisposeModule(M);
450+
451+
return 0;
452+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int llvm_add_named_metadata_operand(void);
4545
int llvm_set_metadata(void);
4646
int llvm_replace_md_operand(void);
4747
int llvm_is_a_value_as_metadata(void);
48+
int llvm_add_globaldebuginfo(void);
4849

4950
// object.c
5051
int llvm_object_list_sections(void);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ int main(int argc, char **argv) {
101101
return llvm_replace_md_operand();
102102
} else if (argc == 2 && !strcmp(argv[1], "--is-a-value-as-metadata")) {
103103
return llvm_is_a_value_as_metadata();
104+
} else if (argc == 2 && !strcmp(argv[1], "--add-globaldebuginfo")) {
105+
return llvm_add_globaldebuginfo();
104106
} else if (argc == 2 && !strcmp(argv[1], "--test-function-attributes")) {
105107
return llvm_test_function_attributes();
106108
} else if (argc == 2 && !strcmp(argv[1], "--test-callsite-attributes")) {

0 commit comments

Comments
 (0)