• [^] # Re: Superbe article !

    Posté par (site web personnel) . En réponse à la dépêche LLVM 3.3 et Clang 3.3. Évalué à 10.

    Voilà:

    #include <clang/Frontend/FrontendAction.h>
    #include <clang/Tooling/Tooling.h>
    #include <clang/AST/ASTConsumer.h>
    #include <clang/AST/Decl.h>
    #include <iostream>
    struct MyASTConsumer : clang::ASTConsumer {
     void HandleTagDeclDefinition(clang::TagDecl* D) override {
     // if it's a struct or a class
     if (D->isRecord())
     std::cout << "Found a class: " << D->getNameAsString() << std::endl;
     }
    };
    struct MyAction : clang::ASTFrontendAction {
     virtual clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &CI,
     llvm::StringRef) override {
     return new MyASTConsumer();
     }
    };
    int main(int argc, const char **argv)
    {
     std::vector<std::string> Argv;
     // Just forward all the arguments to the tool
     for (int I = 0 ; I < argc; ++I)
     Argv.push_back(argv[I]);
     clang::FileManager FM({"."});
     clang::tooling::ToolInvocation Inv(Argv, new MyAction, &FM);
     return !Inv.run();
    }
    
    

    Ça s'utilise comme ça:

    ./a.out mylib.h -c -x c++ -I/usr/include
    
    

    Et ça affiche toute les classes (y compris toutes celle de la stdlib)