[cfe-dev] libtooling fails to traverse TranslationUnitDecl

xiaohui chen xchen198812 at gmail.com
Sun Dec 28 22:21:47 PST 2014


sorry, i  forget to attach my test.c file
===================
void do_math() {
}


On Sun, Dec 28, 2014 at 11:46 PM, xiaohui chen <xchen198812 at gmail.com>
wrote:

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


More information about the cfe-dev mailing list