Skip to content

Commit 448146d

Browse files
authored
[llvm-c] Add bindings for DbgRecord (#166383)
In the LLVM-C library, there is currently no way to get information about a DbgRecord - which is the new way to attach debug information to llvm instructions. We can only iterate on debug records with LLVMGetFirstDbgRecord/ LLVMGetLastDbgRecord/LLVMGetNextDbgRecord, but there is no way to read information. This PR adds utility functions to read DbgRecord information.
1 parent f6cf44a commit 448146d

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

llvm/include/llvm-c/Core.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,13 @@ enum {
531531
*/
532532
typedef unsigned LLVMGEPNoWrapFlags;
533533

534+
typedef enum {
535+
LLVMDbgRecordLabel,
536+
LLVMDbgRecordDeclare,
537+
LLVMDbgRecordValue,
538+
LLVMDbgRecordAssign,
539+
} LLVMDbgRecordKind;
540+
534541
/**
535542
* @}
536543
*/
@@ -3896,6 +3903,37 @@ LLVM_C_ABI LLVMDbgRecordRef LLVMGetNextDbgRecord(LLVMDbgRecordRef DbgRecord);
38963903
LLVM_C_ABI LLVMDbgRecordRef
38973904
LLVMGetPreviousDbgRecord(LLVMDbgRecordRef DbgRecord);
38983905

3906+
/**
3907+
* Get the debug location attached to the debug record.
3908+
*
3909+
* @see llvm::DbgRecord::getDebugLoc()
3910+
*/
3911+
LLVMMetadataRef LLVMDbgRecordGetDebugLoc(LLVMDbgRecordRef Rec);
3912+
3913+
LLVMDbgRecordKind LLVMDbgRecordGetKind(LLVMDbgRecordRef Rec);
3914+
3915+
/**
3916+
* Get the value of the DbgVariableRecord.
3917+
*
3918+
* @see llvm::DbgVariableRecord::getValue()
3919+
*/
3920+
LLVMValueRef LLVMDbgVariableRecordGetValue(LLVMDbgRecordRef Rec,
3921+
unsigned OpIdx);
3922+
3923+
/**
3924+
* Get the debug info variable of the DbgVariableRecord.
3925+
*
3926+
* @see llvm::DbgVariableRecord::getVariable()
3927+
*/
3928+
LLVMMetadataRef LLVMDbgVariableRecordGetVariable(LLVMDbgRecordRef Rec);
3929+
3930+
/**
3931+
* Get the debug info expression of the DbgVariableRecord.
3932+
*
3933+
* @see llvm::DbgVariableRecord::getExpression()
3934+
*/
3935+
LLVMMetadataRef LLVMDbgVariableRecordGetExpression(LLVMDbgRecordRef Rec);
3936+
38993937
/**
39003938
* @defgroup LLVMCCoreValueInstructionCall Call Sites and Invocations
39013939
*

llvm/lib/IR/Core.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3036,6 +3036,37 @@ LLVMDbgRecordRef LLVMGetPreviousDbgRecord(LLVMDbgRecordRef Rec) {
30363036
return wrap(&*--I);
30373037
}
30383038

3039+
LLVMMetadataRef LLVMDbgRecordGetDebugLoc(LLVMDbgRecordRef Rec) {
3040+
return wrap(unwrap<DbgRecord>(Rec)->getDebugLoc().getAsMDNode());
3041+
}
3042+
3043+
LLVMDbgRecordKind LLVMDbgRecordGetKind(LLVMDbgRecordRef Rec) {
3044+
DbgRecord *Record = unwrap<DbgRecord>(Rec);
3045+
if (isa<DbgLabelRecord>(Record))
3046+
return LLVMDbgRecordLabel;
3047+
DbgVariableRecord *VariableRecord = dyn_cast<DbgVariableRecord>(Record);
3048+
assert(VariableRecord && "unexpected record");
3049+
if (VariableRecord->isDbgDeclare())
3050+
return LLVMDbgRecordDeclare;
3051+
if (VariableRecord->isDbgValue())
3052+
return LLVMDbgRecordValue;
3053+
assert(VariableRecord->isDbgAssign() && "unexpected record");
3054+
return LLVMDbgRecordAssign;
3055+
}
3056+
3057+
LLVMValueRef LLVMDbgVariableRecordGetValue(LLVMDbgRecordRef Rec,
3058+
unsigned OpIdx) {
3059+
return wrap(unwrap<DbgVariableRecord>(Rec)->getValue(OpIdx));
3060+
}
3061+
3062+
LLVMMetadataRef LLVMDbgVariableRecordGetVariable(LLVMDbgRecordRef Rec) {
3063+
return wrap(unwrap<DbgVariableRecord>(Rec)->getRawVariable());
3064+
}
3065+
3066+
LLVMMetadataRef LLVMDbgVariableRecordGetExpression(LLVMDbgRecordRef Rec) {
3067+
return wrap(unwrap<DbgVariableRecord>(Rec)->getRawExpression());
3068+
}
3069+
30393070
unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
30403071
if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
30413072
return FPI->arg_size();

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,43 @@ int llvm_test_dibuilder(void) {
364364
assert(AddDbgRecordUnderTheRange == NULL);
365365
(void)AddDbgRecordUnderTheRange;
366366

367+
// Test that we can read the first debug record.
368+
LLVMMetadataRef AddDbgRecordFirstDebugLoc =
369+
LLVMDbgRecordGetDebugLoc(AddDbgRecordFirst);
370+
(void)AddDbgRecordFirstDebugLoc;
371+
assert(LLVMDILocationGetLine(AddDbgRecordFirstDebugLoc) == 43);
372+
assert(LLVMDbgRecordGetKind(AddDbgRecordFirst) == LLVMDbgRecordValue);
373+
LLVMValueRef AddDbgRecordFirstValue =
374+
LLVMDbgVariableRecordGetValue(AddDbgRecordFirst, 0);
375+
(void)AddDbgRecordFirstValue;
376+
assert(LLVMGetValueKind(AddDbgRecordFirstValue) == LLVMConstantIntValueKind);
377+
assert(LLVMConstIntGetZExtValue(AddDbgRecordFirstValue) == 0);
378+
LLVMMetadataRef AddDbgRecordFirstVariable =
379+
LLVMDbgVariableRecordGetVariable(AddDbgRecordFirst);
380+
(void)AddDbgRecordFirstVariable;
381+
assert(LLVMGetMetadataKind(AddDbgRecordFirstVariable) ==
382+
LLVMDILocalVariableMetadataKind);
383+
// TODO: For now, there is no way to get the name.
384+
LLVMMetadataRef AddDbgRecordFirstVariableScope =
385+
LLVMDIVariableGetScope(AddDbgRecordFirstVariable);
386+
(void)AddDbgRecordFirstVariableScope;
387+
assert(LLVMGetMetadataKind(AddDbgRecordFirstVariableScope) ==
388+
LLVMDILexicalBlockMetadataKind);
389+
LLVMMetadataRef AddDbgRecordFirstVariableFile =
390+
LLVMDIScopeGetFile(AddDbgRecordFirstVariableScope);
391+
(void)AddDbgRecordFirstVariableFile;
392+
assert(LLVMGetMetadataKind(AddDbgRecordFirstVariableFile) ==
393+
LLVMDIFileMetadataKind);
394+
unsigned FileLen = 0;
395+
assert(strcmp(LLVMDIFileGetFilename(AddDbgRecordFirstVariableFile, &FileLen),
396+
"debuginfo.c") == 0);
397+
(void)FileLen;
398+
LLVMMetadataRef AddDbgRecordFirstExpr =
399+
LLVMDbgVariableRecordGetExpression(AddDbgRecordFirst);
400+
assert(LLVMGetMetadataKind(AddDbgRecordFirstExpr) ==
401+
LLVMDIExpressionMetadataKind);
402+
(void)AddDbgRecordFirstExpr;
403+
367404
char *MStr = LLVMPrintModuleToString(M);
368405
puts(MStr);
369406
LLVMDisposeMessage(MStr);

0 commit comments

Comments
 (0)