我想识别像 int a = function(b,c)
这样的表达式,所以我把代码写成了粉丝:
void foo(int* a, int *b) {
int x;
int m;
int z;
int *p;
if (a[0] > 1) {
b[0] = 2;
z=10;
x = function( sizeof(char));
}
m = function( sizeof(char));
bar(x,m);
}
void bar(float x, float y);
int function(int size){
return size;
}
而且我使用clang -Xclang -ast-dump -fsyntax-only cfunc_with_if.c
来获取代码的AST:
从结果中我发现 int a = function(b,c)
的AST节点类型是 BinaryOperator 。为了验证这一点,我使用VisitStmt(Stmt *s)
打印出所有帖子'类型。
bool VisitStmt(Stmt *s) {
if(isa<Stmt>(s)) {
Stmt *Statement = dyn_cast<Stmt>(s);
//Statement->dump();
std::string st(Statement->getStmtClassName());
st = st + "\n";
TheRewriter.InsertText(Statement->getLocStart(), st, true, true);
}
return true;
}
但结果太奇怪了。关于 int a = function(b,c)
的类型没有打印。我对结果很困惑。我的代码中还有其他错误吗?
答案 0 :(得分:0)
bar(x,m);
也没有输出。当工具编译正在分析的代码时是否有任何错误?如上所述,由于x = function( sizeof(char));
尚未声明,因此代码无法在function
进行编译。即使编译因错误而失败,libtool工具仍然可以至少部分运行,结果很奇怪。
编辑添加:如果您在此代码上运行该工具会发生什么?
void bar(float x, float y);
int function(int size);
void foo(int* a, int *b) {
int x;
int m;
int z;
int *p;
if (a[0] > 1) {
b[0] = 2;
z=10;
x = function( sizeof(char));
}
m = function( sizeof(char));
bar(x,m);
}
void bar(float x, float y);
int function(int size){
return size;
}