Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,14 @@ LLVM_C_ABI void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind);
*/
LLVM_C_ABI void LLVMGlobalClearMetadata(LLVMValueRef Global);

/**
* Add debuginfo metadata to this global.
*
* @see llvm::GlobalVariable::addDebugInfo()
*/
LLVM_C_ABI void LLVMGlobalAddDebugInfo(LLVMValueRef Global,
LLVMMetadataRef GVE);

/**
* Retrieves an array of metadata entries representing the metadata attached to
* this value. The caller is responsible for freeing this array by calling
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,11 @@ void LLVMGlobalClearMetadata(LLVMValueRef Global) {
unwrap<GlobalObject>(Global)->clearMetadata();
}

void LLVMGlobalAddDebugInfo(LLVMValueRef Global, LLVMMetadataRef GVE) {
unwrap<GlobalVariable>(Global)->addDebugInfo(
unwrap<DIGlobalVariableExpression>(GVE));
}

/*--.. Operations on global variables ......................................--*/

LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/Bindings/llvm-c/add_globaldebuginfo.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
; RUN: llvm-c-test --add-globaldebuginfo < /dev/null
; This used to trigger an assertion
34 changes: 34 additions & 0 deletions llvm/tools/llvm-c-test/debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,37 @@ int llvm_di_type_get_name(void) {

return 0;
}

int llvm_add_globaldebuginfo(void) {
const char *Filename = "debuginfo.c";
LLVMModuleRef M = LLVMModuleCreateWithName(Filename);
LLVMDIBuilderRef Builder = LLVMCreateDIBuilder(M);
LLVMMetadataRef File =
LLVMDIBuilderCreateFile(Builder, Filename, strlen(Filename), ".", 1);

LLVMMetadataRef GlobalVarValueExpr =
LLVMDIBuilderCreateConstantValueExpression(Builder, 0);
LLVMMetadataRef Int64Ty =
LLVMDIBuilderCreateBasicType(Builder, "Int64", 5, 64, 0, LLVMDIFlagZero);
LLVMMetadataRef Int64TypeDef = LLVMDIBuilderCreateTypedef(
Builder, Int64Ty, "int64_t", 7, File, 42, File, 0);

LLVMMetadataRef GVE = LLVMDIBuilderCreateGlobalVariableExpression(
Builder, File, "global", 6, "", 0, File, 1, Int64TypeDef, true,
GlobalVarValueExpr, NULL, 0);

LLVMTypeRef RecType =
LLVMStructCreateNamed(LLVMGetModuleContext(M), "struct");
LLVMValueRef Global = LLVMAddGlobal(M, RecType, "global");

LLVMGlobalAddDebugInfo(Global, GVE);
size_t numEntries;
LLVMValueMetadataEntry *ME = LLVMGlobalCopyAllMetadata(Global, &numEntries);
Copy link
Collaborator

@mikaelholmen mikaelholmen Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If compiling without asserts you get

../tools/llvm-c-test/debuginfo.c:447:27: error: unused variable 'ME' [-Werror,-Wunused-variable]
  447 |   LLVMValueMetadataEntry *ME = LLVMGlobalCopyAllMetadata(Global, &numEntries);
      |                           ^~
1 error generated.

here.

assert(ME != NULL);
assert(numEntries == 1);

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 @@ -45,6 +45,7 @@ int llvm_add_named_metadata_operand(void);
int llvm_set_metadata(void);
int llvm_replace_md_operand(void);
int llvm_is_a_value_as_metadata(void);
int llvm_add_globaldebuginfo(void);

// object.c
int llvm_object_list_sections(void);
Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/llvm-c-test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ int main(int argc, char **argv) {
return llvm_replace_md_operand();
} else if (argc == 2 && !strcmp(argv[1], "--is-a-value-as-metadata")) {
return llvm_is_a_value_as_metadata();
} else if (argc == 2 && !strcmp(argv[1], "--add-globaldebuginfo")) {
return llvm_add_globaldebuginfo();
} else if (argc == 2 && !strcmp(argv[1], "--test-function-attributes")) {
return llvm_test_function_attributes();
} else if (argc == 2 && !strcmp(argv[1], "--test-callsite-attributes")) {
Expand Down
Loading