ClangASTMatchers:如何从函数声明中找到函数体?

ClangASTMatchers:如何从函数声明中找到函数体?,clang,clang++,clang-ast-matchers,Clang,Clang++,Clang Ast Matchers,我试图编写一个简单的叮当声检查程序,它将检查多次调用fopen()的构造函数。我的目标是在第二次调用fopen()时发现潜在的内存泄漏 class Dummy_file { FILE *f1_; FILE *f2_; public: Dummy_file(const char* f1_name, const char* f2_name, const char * mode){ f1_ = fopen(f1_name, mode); f2_ =

我试图编写一个简单的叮当声检查程序,它将检查多次调用
fopen()
的构造函数。我的目标是在第二次调用
fopen()
时发现潜在的内存泄漏

class Dummy_file
{
  FILE *f1_;
  FILE *f2_;
  public:
    Dummy_file(const char* f1_name, const char* f2_name, const char * mode){
        f1_ = fopen(f1_name, mode);
        f2_ = fopen(f2_name, mode);
    }
    ~Dummy_file(){
        fclose(f1_);
        fclose(f2_);
    }
};
用这个

callExpr(callee(functionDecl(hasName("fopen")))).bind("fopencalls")
能够找到所有
fopen()
调用

但我无法找到使用此命令的
cxconstructordecl

cxxConstructorDecl(has(callExpr(callee(functionDecl(hasName("fopen")))))).bind("ctr")
我很怀疑,因为我使用的是
cxconstructordecl
我的过滤器没有应用于构造函数主体。 那么,如何从函数声明中找到函数体呢?

简要说明 您应该使用
hasgendant
matcher而不是
has
matcher。当
has
仅检查被测试节点的立即子节点是否匹配时,
hasDegenant
匹配任何子节点

在这里,您可以看到您的示例:

  |-CXXConstructorDecl <line:8:3, line:11:3> line:8:3 Dummy_file 'void (const char *, const char *, const char *)'
  | |-ParmVarDecl <col:14, col:26> col:26 used f1_name 'const char *'
  | |-ParmVarDecl <col:35, col:47> col:47 used f2_name 'const char *'
  | |-ParmVarDecl <col:56, col:68> col:68 used mode 'const char *'
  | `-CompoundStmt <col:74, line:11:3>
  |   |-BinaryOperator <line:9:5, col:30> 'FILE *' lvalue '='
  |   | |-MemberExpr <col:5> 'FILE *' lvalue ->f1_ 0x55d36491a230
  |   | | `-CXXThisExpr <col:5> 'Dummy_file *' this
  |   | `-CallExpr <col:11, col:30> 'FILE *'
  |   |   |-ImplicitCastExpr <col:11> 'FILE *(*)(const char *__restrict, const char *__restrict)' <FunctionToPointerDecay>
  |   |   | `-DeclRefExpr <col:11> 'FILE *(const char *__restrict, const char *__restrict)' lvalue Function 0x55d3648fa220 'fopen' 'FILE *(const char *__restrict, const char *__restrict)'
  |   |   |-ImplicitCastExpr <col:17> 'const char *' <LValueToRValue>
  |   |   | `-DeclRefExpr <col:17> 'const char *' lvalue ParmVar 0x55d36491a310 'f1_name' 'const char *'
  |   |   `-ImplicitCastExpr <col:26> 'const char *' <LValueToRValue>
  |   |     `-DeclRefExpr <col:26> 'const char *' lvalue ParmVar 0x55d36491a400 'mode' 'const char *'
我希望这能回答你的问题

clang-query> match cxxConstructorDecl(hasDescendant(callExpr(callee(functionDecl(hasName("fopen")))).bind("fopencall"))).bind("ctr")

Match #1:

$TEST_DIR/test.cpp:8:3: note: "ctr" binds here
  Dummy_file(const char *f1_name, const char *f2_name, const char *mode) {
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$TEST_DIR/test.cpp:9:11: note: "fopencall" binds here
    f1_ = fopen(f1_name, mode);
          ^~~~~~~~~~~~~~~~~~~~
$TEST_DIR/test.cpp:8:3: note: "root" binds here
  Dummy_file(const char *f1_name, const char *f2_name, const char *mode) {
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 match.