2017-01-30 78 views
0

我使用標籤bar來標註功能參數,如下所示。如何確定函數參數是否註釋?

int foo (char* s __attribute__((annotate("bar")))) { 
    ... 
} 

接下來,我正在運行函數傳遞。如何確定給定的函數參數是否使用標籤bar註釋?

回答

0

您將不得不閱讀llvm.var.annotationllvm.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」。