[cfe-dev] libtooling fails to traverse TranslationUnitDecl

xiaohui chen xchen198812 at gmail.com
Sun Jan 4 10:48:47 PST 2015


yes, it is weird. ok, anyway i will try to upgrade. thank you!

On Sun, Jan 4, 2015 at 2:16 AM, Manasij Mukherjee <manasij7479 at gmail.com>
wrote:

> Your code works in clang 3.5 after making the API changes.
> Not sure why it does not in your version.
> Maybe upgrade ?
>
> On Sat, Jan 3, 2015 at 8:46 PM, Manuel Klimek <klimek at google.com> wrote:
>
>> At a first glance this looks like it should work.
>>
>> On Mon Dec 29 2014 at 7:26:26 AM xiaohui chen <xchen198812 at gmail.com>
>> wrote:
>>
>>> 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 }
>>>>
>>>>
>>> _______________________________________________
>>> cfe-dev mailing list
>>> cfe-dev at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150104/e71853a6/attachment.html>


More information about the cfe-dev mailing list