How to detect all return statements by Clang Libtooling?

Hi all,

I¡¯m trying to use clang libtooling for detecting all return statements.

I used the ¡°VisitReturnStmt¡± function but sometimes it is not working as follow case.

//target code

class ExampleFrontendAction : public ASTFrontendAction {

public:

virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, StringRef file) {

return new ExampleASTConsumer(&CI); // pass CI pointer to ASTConsumer

}

};

//target code end

//results

CXXMethodDecl 0x2468cb0 </project/llvm/Debug+Asserts/bin/a.cpp:107:5, line:115:5> CreateASTConsumer ‘int *(int &, int)’

-ParmVarDecl 0x2468b60 <line:107:44, col:62> CI ‘int &’ invalid

-ParmVarDecl 0x2468bd0 <col:66, col:76> file ‘int’ invalid

`-CompoundStmt 0x2469138 <col:82, line:115:5> invalid

As you can see, we cannot find any ReturnStmt. (It is included in CompoundStmt. Also the CompoundStmt is a leaf statement.)

How can I detect this case? Please help me!

Thanks in advance.

Additional Question.

Why does clang return ¡°int *¡± instead of ¡°ASTConsumer *¡± in the case of CreateASTConsumer method when I use getReturnType() in VisitFunctionDecl()?

Any ideas?

Brown.

Hi all,

I’m trying to use clang libtooling for detecting all return statements.

I used the “VisitReturnStmt” function but sometimes it is not working as
follow case.

//target code

class ExampleFrontendAction : public ASTFrontendAction {

public:

    virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, StringRef
file) {

        return new ExampleASTConsumer(&CI); // pass CI pointer to
ASTConsumer

    }

};

//target code end

//results

CXXMethodDecl 0x2468cb0 </project/llvm/Debug+Asserts/bin/a.cpp:107:5,
line:115:5> CreateASTConsumer 'int *(int &, int)'

>-ParmVarDecl 0x2468b60 <line:107:44, col:62> CI 'int &' invalid

>-ParmVarDecl 0x2468bd0 <col:66, col:76> file 'int' invalid

`-CompoundStmt 0x2469138 <col:82, line:115:5> invalid

As you can see, we cannot find any ReturnStmt. (It is included in
CompoundStmt. Also the CompoundStmt is a leaf statement.)

How can I detect this case? Please help me!

Thanks in advance.

Additional Question.

Why does clang return “int *” instead of “ASTConsumer *” in the case of
CreateASTConsumer method when I use getReturnType() in VisitFunctionDecl()?

My guess is that the headers are not correctly found; are you sure you're
using a compilation database that works?

Wow!

Thanks Klimek! It works. (Your insight is definitely correct.)

Can I ask an another question?

When I set all includes paths completely, all return statements could be detected.

However, so many functions are detected due to included header files.

If I want to detect only functions in a target source file, what should I do?

Thanks

Wow!

Thanks Klimek! It works. (Your insight is definitely correct.)

Can I ask an another question?

When I set all includes paths completely, all return statements could be
detected.

However, so many functions are detected due to included header files.

If I want to detect only functions in a target source file, what should I
do?

What I do is match on all nodes, and then filter the results based on the
SourceLocation afterwards...