这个问题特指LLVM 3.9。前面的问题:
以前,我已成功添加不带参数的函数属性,然后将它们应用于源代码中的函数,最后在LLVM IR中观察它们,如here所述。我尝试在llvm.org的llvm-dev列表中按照this thread中给出的建议添加一个带参数的函数属性。该线程还提供了如何访问参数here的建议。但是我无法在我的代码中使用该技术。
以下是我对代码所做的具体更改:
在$(LLVM_SRC)/tools/clang/include/clang/Basic/Attr.td
中,我添加了函数属性定义:
def DIMemberFunc : InheritableAttr {
let Spellings = [GNU<"dimemberfunc">];
let Subjects = SubjectList<[Function]>;
let Documentation = [Undocumented];
let Args = [DefaultIntArgument<"fid", 65535>];
}
在$(LLVM_SRC)/tools/clang/lib/CodeGen/CGCall.cpp
,我添加了一位经理:
void CodeGenModule::ConstructAttributeList(
StringRef Name, const CGFunctionInfo &FI, CGCalleeInfo CalleeInfo,
AttributeListType &PAL, unsigned &CallingConv, bool AttrOnCallSite){
llvm::AttrBuilder FuncAttrs;
llvm::AttrBuilder RetAttrs;
//...
if (TargetDecl) {
//...
if (TargetDecl->hasAttr<DIMemberFuncAttr>()){
auto myattribute = TargetDecl->getAttr<DIMemberFuncAttr>();
FuncAttrs.addAttribute("myattribute");
unsigned myargument = myattribute->getFid();
FuncAttrs.addAttribute("myargument", llvm::utostr(myargument));
}
//...
}
//...
}
在$(LLVM_SRC)/tools/clang/lib/Sema/SemaDeclAttr.cpp
中,我添加了一个处理函数:
static void handleDIMemberFuncAttr(Sema &S, Decl *D, const AttributeList &Attr){
uint32_t fid = DIMemberFuncAttr::DefaultFid;
if (Attr.getNumArgs() && !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), fid))
return;
D->addAttr(::new (S.Context)
DIMemberFuncAttr(Attr.getRange(), S.Context, fid
Attr.getAttributeSpellingListIndex()));
}
最后,在我的一个传球中,我有以下代码:
if (F.getAttributes().hasAttribute(AttributeSet::FunctionIndex, Attribute::DIMemberFunc)){
//Print Function
errs() << "Found member function named: ";
errs().write_escaped(F.getName()) << '\n';
//Print the DIMemberFunc argument value
int myfid;
if(F.hasFnAttribute("dimemberfunc")) {
myfid = F.getFnAttribute("dimemberfunc").getValueAsInt();
}
errs() << "\twith function ID: " << myfid << '\n';
}
当我运行传球时,我只获得了部分成功。即使我设置功能&#39;属性参数从1开始增加计数器,我仍然得到输出:
Found member function named: _ZN8simple_t8getValueEv
with function ID: 0
LLVM IR具有此功能的以下属性数据:
attributes #5 = { dimemberfunc nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "myargument"="2" "myattribute" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
有关如何直接从属性获取参数的任何见解,或者链接到比邮件列表帖子更好的文档,我们表示赞赏。