<div dir="ltr"><div><div><div>HI there:<br><br></div>My clang is 3.4.1 . I have a very simple libtooling code which just visit the functiondecl.<br></div>but   function "bool VisitFunctionDecl(FunctionDecl *func)" is never called in line 35, who can answer me?<br><br></div><div>please see the following output, even i can output the AST in line 53.<br></div><br>===============================================================================<br> llvm/tools/clang/tools/example$ ./Example test.c --<br>TranslationUnitDecl 0x3225610 <<invalid sloc>><look for declarations><br>|-TypedefDecl 0x3225b20 <<invalid sloc>> __int128_t '__int128'<look for declarations><br>|-TypedefDecl 0x3225b80 <<invalid sloc>> __uint128_t 'unsigned __int128'<look for declarations><br>|-TypedefDecl 0x3225ed0 <<invalid sloc>> __builtin_va_list '__va_list_tag [1]'<look for declarations><br>`-FunctionDecl 0x3225f70 </home/xiaohui/llvm/llvm/tools/clang/tools/example/test.c:1:1, line:2:1> do_math 'void ()'<br>  `-CompoundStmt 0x3226018 <line:1:16, line:2:1><br><br>Found 0 functions.<br><br><div><div>===============================================================================<br><br><div>1 #include "clang/Driver/Options.h"<br>  2 #include "clang/AST/AST.h"<br>  3 #include "clang/AST/ASTContext.h"<br>  4 #include "clang/AST/ASTConsumer.h"<br>  5 #include "clang/AST/RecursiveASTVisitor.h"<br>  6 #include "clang/Frontend/ASTConsumers.h"<br>  7 #include "clang/Frontend/FrontendActions.h"<br>  8 #include "clang/Frontend/CompilerInstance.h"<br>  9 #include "clang/Tooling/CommonOptionsParser.h"<br> 10 #include "clang/Tooling/Tooling.h"<br> 11 #include "clang/Rewrite/Core/Rewriter.h"<br> 12 #include "llvm/Support/raw_ostream.h"<br> 13 <br> 14 using namespace std;<br> 15 using namespace clang;<br> 16 using namespace clang::driver;<br> 17 using namespace clang::tooling;<br> 18 using namespace llvm;<br> 19 <br> 20 Rewriter rewriter;<br> 21 int numFunctions = 0;<br> 22 <br> 23 <br> 24 class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> {<br> 25 private:<br> 26     ASTContext *astContext; // used for getting additional AST info<br> 27 <br> 28 public:<br> 29     explicit ExampleVisitor(CompilerInstance *CI)<br> 30       : astContext(&(CI->getASTContext())) // initialize private members<br> 31     {<br> 32         rewriter.setSourceMgr(astContext->getSourceManager(), astContext->getLangOpts());<br> 33     }<br> 34 <br> 35      bool VisitFunctionDecl(FunctionDecl *func) {<br> 36         numFunctions++;<br> 37         return true;<br> 38     }<br>39 };<br> 40 <br> 41 class ExampleASTConsumer : public ASTConsumer {<br> 42 private:<br> 43     ExampleVisitor *visitor; // doesn't have to be private<br> 44 <br> 45 public:<br> 46     // override the constructor in order to pass CI<br> 47     explicit ExampleASTConsumer(CompilerInstance *CI)<br> 48         : visitor(new ExampleVisitor(CI)) // initialize the visitor<br> 49     { }<br> 50 <br> 51     // override this to call our ExampleVisitor on the entire source file<br> 52     virtual void HandleTranslationUnit(ASTContext &Context) {<br> 53            Context.getTranslationUnitDecl()->dump();<br> 54 <br> 55       visitor->TraverseDecl(Context.getTranslationUnitDecl());<br> 56     }<br> 57 <br> 58 };<br> 59 <br> 60 class ExampleFrontendAction : public ASTFrontendAction {<br> 61 public:<br> 62     virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, StringRef file) {<br> 63         return new ExampleASTConsumer(&CI); // pass CI pointer to ASTConsumer<br> 64     }<br> 65 };<br> 66 <br> 67 int main(int argc, const char **argv) {<br> 68     CommonOptionsParser op(argc, argv);<br> 69     ClangTool Tool(op.getCompilations(), op.getSourcePathList());<br> 70 <br> 71     int result = Tool.run(newFrontendActionFactory<ExampleFrontendAction>());<br> 72 <br> 73     errs() << "\nFound " << numFunctions << " functions.\n\n";<br> 74     return result;<br> 75 }<br><br></div></div></div></div>