1

I want to find out type informations of every functions using by Clang libtool.

However, VisitReturnStmt sometimes cannot find any return statements. Also, class type return(eg. ASTConsumer * in "CreateASTConsumer" method) is converted to "int *" type. (another case: bool -> _Bool)

How can I find out real return types for every functions?

Thanks in advance for your help.

The tool source and the input cpp source are same as follows.

    #include "clang/Driver/Options.h"
    #include "clang/AST/AST.h"
    #include "clang/AST/ASTContext.h"
    #include "clang/AST/ASTConsumer.h"
    #include "clang/AST/RecursiveASTVisitor.h"
    #include "clang/Frontend/ASTConsumers.h"
    #include "clang/Frontend/FrontendActions.h"
    #include "clang/Frontend/CompilerInstance.h"
    #include "clang/Tooling/CommonOptionsParser.h"
    #include "clang/Tooling/Tooling.h"

    using namespace std;
    using namespace clang;
    using namespace clang::driver;
    using namespace clang::tooling;
    using namespace llvm;

    Rewriter TheRewriter;

    class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> {
    private:
        ASTContext *astContext; // used for getting additional AST info

    public:
        explicit ExampleVisitor(CompilerInstance *CI) 
          : astContext(&(CI->getASTContext())) // initialize private members
        {
          TheRewriter.setSourceMgr(astContext->getSourceManager(), astContext->getLangOpts());
        }

        virtual bool VisitReturnStmt(ReturnStmt *ReturnStatement) {

            ReturnStatement->getRetValue()->dump(TheRewriter.getSourceMgr());
            return true;
        }

        virtual bool VisitStmt(Stmt *S) {
            S->dump(TheRewriter.getSourceMgr());
            return true;
        }
    };



    class ExampleASTConsumer : public ASTConsumer {
    private:
        ExampleVisitor *visitor; // doesn't have to be private

    public:
        // override the constructor in order to pass CI
        explicit ExampleASTConsumer(CompilerInstance *CI)
            : visitor(new ExampleVisitor(CI)) // initialize the visitor
        { }

        // override this to call our ExampleVisitor on the entire source file
        virtual void HandleTranslationUnit(ASTContext &Context) {
            /* we can use ASTContext to get the TranslationUnitDecl, which is
                 a single Decl that collectively represents the entire source file */
            visitor->TraverseDecl(Context.getTranslationUnitDecl());
        }

    };



    class ExampleFrontendAction : public ASTFrontendAction {
    public:
        virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, StringRef file) {
            return new ExampleASTConsumer(&CI); // pass CI pointer to ASTConsumer
        }
    };



    int main(int argc, const char **argv) {
        // parse the command-line args passed to your code
        CommonOptionsParser op(argc, argv);
        // create a new Clang Tool instance (a LibTooling environment)
        ClangTool Tool(op.getCompilations(), op.getSourcePathList());

        // run the Clang Tool, creating a new FrontendAction (explained below)
        int result = Tool.run(newFrontendActionFactory<ExampleFrontendAction>());
        return result;
    }
1
  • The returnstmt problem is solved. The solution is that all include paths are needed to run the libtool.
    – happybrown
    Feb 7, 2014 at 6:16

1 Answer 1

2

If I'm interpreting the clang docs correctly

Note that GCC allows return with no argument in a function declared to return a value, and it allows returning a value in functions declared to return void. We explicitly model this in the AST, which means you can't depend on the return type of the function and the presence of an argument.

this implies that you can't reliably infer the return type of a function from its return statement.

If you want to find the return type of a function, you could visit FunctionDecl nodes and call FunctionDecl::getReturnType() on them

1
  • Thank you for your response. I already tried to find the return type by getReturnType(). Stmt *FuncBody = f->getBody(); QualType QT = f->getResultType(); std::string TypeStr = QT.getAsString(); the result shows that "CreateASTConsumer" is "int *".
    – happybrown
    Feb 3, 2014 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.