【问题标题】:BinaryOperator doesn't work when comes to a=function(b,c)?当涉及 a=function(b,c) 时,BinaryOperator 不起作用?
【发布时间】:2017-11-11 11:34:51
【问题描述】:

我想识别像int a = function(b,c)这样的Expression,所以我把代码写成followers:

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) 打印出所有 stmts 的类型。

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) 的类型没有打印出来。我对结果很困惑。我的代码中是否有错误或其他问题?

【问题讨论】:

    标签: clang llvm clang++ llvm-clang clang-static-analyzer


    【解决方案1】:

    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;
    }
    

    【讨论】:

    • 其实我不知道代码,但是当我使用这个link中的代码时,我可以得到每个CallExpr的名称
    猜你喜欢
    • 2018-08-11
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2020-07-27
    • 2012-04-17
    相关资源
    最近更新 更多