Clang 铿锵重写:找到调用表达式的顶部
我正在做一些源代码中的铿锵重写。我想在语句前插入一行。例如,如果我从if语句内部调用,如下所示:Clang 铿锵重写:找到调用表达式的顶部,clang,llvm,Clang,Llvm,我正在做一些源代码中的铿锵重写。我想在语句前插入一行。例如,如果我从if语句内部调用,如下所示: if (cmp(a,b) > 0 && cmp(c,d) < 0){ //do something } //there are call inside if (cmp(a,b) > 0 && cmp(c,d) < 0){ //do something } for (int a = 0; a < range(s); a++){ //do
if (cmp(a,b) > 0 && cmp(c,d) < 0){
//do something
}
//there are call inside
if (cmp(a,b) > 0 && cmp(c,d) < 0){
//do something
}
for (int a = 0; a < range(s); a++){
//do something
}
// add something here
if (cmp(a,b)){
//do something
}
if(cmp(a,b)>0&&cmp(c,d)<0){
//做点什么
}
我想在if语句之前重写一条注释,如下所示:
if (cmp(a,b) > 0 && cmp(c,d) < 0){
//do something
}
//there are call inside
if (cmp(a,b) > 0 && cmp(c,d) < 0){
//do something
}
for (int a = 0; a < range(s); a++){
//do something
}
// add something here
if (cmp(a,b)){
//do something
}
//里面有调用
如果(cmp(a,b)>0&&cmp(c,d)<0){
//做点什么
}
到目前为止,我所能做的是,我可以使用VisitCallExpr()检测调用。但是我怎么才能在if语句之前找到位置来写评论呢。
注意:此处的if语句也可以用于,如下所示:
if (cmp(a,b) > 0 && cmp(c,d) < 0){
//do something
}
//there are call inside
if (cmp(a,b) > 0 && cmp(c,d) < 0){
//do something
}
for (int a = 0; a < range(s); a++){
//do something
}
// add something here
if (cmp(a,b)){
//do something
}
for(int a=0;a
有人能帮我找到一种方法吗。我已经找到了一种方法,尽管它不是一种通用的方法。关于这个问题,我提出了另一个问题。但是,如果我们只考虑这里的特定情况如下:
if (cmp(a,b) > 0 && cmp(c,d) < 0){
//do something
}
//there are call inside
if (cmp(a,b) > 0 && cmp(c,d) < 0){
//do something
}
for (int a = 0; a < range(s); a++){
//do something
}
// add something here
if (cmp(a,b)){
//do something
}
我们可以使用一个简单的AST匹配器:
Matcher.addMatcher(ifStmt(hasCondition(callExpr()))
.bind("call_from_if"),&handleIFCALL);
处理程序的定义应如下所示:
class IFCALL : public MatchFinder::MatchCallback {
public:
IFCALL(Rewriter &Rewrite) : Rewrite(Rewrite) {}
virtual void run(const MatchFinder::MatchResult &Result) {
const IfStmt *statement =
Result.Nodes.getNodeAs<clang::IfStmt>("call_from_if");
SourceLocation ST = statement->getLocStart();
std::stringstream SSBefore;
SSBefore << "\n \\\\ if statement have call inside \n";
Rewrite.InsertText(ST, SSBefore.str(), true, true);
}
}
private:
Rewriter &Rewrite;
};
类IFCALL:public MatchFinder::MatchCallback{
公众:
IFCALL(重写和重写):重写(重写){
虚拟无效运行(常量MatchFinder::MatchResult和Result){
常量IfStmt*语句=
Result.Nodes.getNodeAs(“call_from_if”);
SourceLocation ST=语句->getLocStart();
std::stringstream-SSBefore;
以前