我正在使用标签bar
注释功能参数,如下所示。
int foo (char* s __attribute__((annotate("bar")))) {
...
}
接下来,我正在运行一个函数传递。如何确定给定的函数参数是否使用标签bar
?
答案 0 :(得分:0)
您必须阅读llvm.var.annotation和llvm.dbg.declare内在函数。
更具体地说,这是您的代码生成的llvm-ir:
@.str = private unnamed_addr constant [4 x i8] c"bar\00", section "llvm.metadata"
@.str.1 = private unnamed_addr constant [75 x i8] c"/tmp/compiler-explorer-compiler117030-12962-1rhu4lb.ojfaiz4cxr/example.cpp\00", section "llvm.metadata"
; Function Attrs: nounwind uwtable
define i32 @foo(char*)(i8*) #0 !dbg !6 {
%2 = alloca i8*, align 8
store i8* %0, i8** %2, align 8
call void @llvm.dbg.declare(metadata i8** %2, metadata !12, metadata !13), !dbg !14
%3 = bitcast i8** %2 to i8*
call void @llvm.var.annotation(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.1, i32 0, i32 0), i32 1)
ret i32 0, !dbg !15
}
!6 = distinct !DISubprogram(name: "foo", linkageName: "foo(char*)", scope: !1, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
!7 = !DISubroutineType(types: !8)
!8 = !{!9, !10}
!9 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64, align: 64)
!11 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char)
!12 = !DILocalVariable(name: "s", arg: 1, scope: !6, file: !1, line: 1, type: !10)
dbg.declare指令告诉你%2实际上是函数的第一个参数(名为s)。
%3是%2的bitcast,所以基本上是别名。
llvm.var.annotation指令告诉你%2用常量字符串@str注释,其值为" bar"。