Advertisement
saheel1511

Libtooling example (complete cpp file)

Dec 4th, 2014
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  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.  
  13. #include "clang/Analysis/Analyses/LiveVariables.h"
  14. #include "clang/Analysis/CFG.h"
  15. #include "clang/Analysis/AnalysisContext.h"
  16.  
  17. using namespace std;
  18. using namespace clang;
  19. using namespace clang::driver;
  20. using namespace clang::tooling;
  21. using namespace llvm;
  22.  
  23. Rewriter rewriter;
  24. int numFunctions = 0;
  25. int numVarDecl = 0;
  26.  
  27. class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> {
  28. private:
  29.     ASTContext *astContext; // used for getting additional AST info
  30.  
  31. public:
  32.     // initialize private members
  33.     explicit ExampleVisitor(CompilerInstance *CI) : astContext(&(CI->getASTContext())) {
  34.         rewriter.setSourceMgr(astContext->getSourceManager(), astContext->getLangOpts());
  35.     }
  36.  
  37.     virtual bool VisitDecl(Decl *D) {
  38.         errs() << numVarDecl++ << "\n";
  39.        
  40.         // if (D->hasBody()) {
  41.            
  42.         //     Stmt *S = D->getBody();
  43.         //     errs() << S << "\n";
  44.         //     S->getLocStart().print(errs(), astContext->getSourceManager());
  45.         //     errs() << "\n";
  46.        
  47.         // }
  48.        
  49.         D->getLocStart().print(errs(), astContext->getSourceManager());
  50.         errs() << "\n";
  51.        
  52.         return true;
  53.     }
  54.  
  55.  
  56.     virtual bool VisitFunctionDecl(FunctionDecl *func) {
  57.         errs() << "Inside " << ++numFunctions << " " << func->getNameInfo().getName().getAsString() << "\n";
  58.  
  59.         // FIXME: This detects CFGBlocks in given source file
  60.         // correctly, but does not give correct liveness values
  61.         clang::AnalysisDeclContextManager *ADCM = new clang::AnalysisDeclContextManager(false, true, true, true, true, true);
  62.         clang::AnalysisDeclContext *func_ADC = ADCM->getContext(func);
  63.         clang::LiveVariables *func_LV = clang::LiveVariables::computeLiveness(*func_ADC, false);
  64.         clang::LiveVariables::Observer *obs = new clang::LiveVariables::Observer();
  65.         func_LV->runOnAllBlocks(*obs);
  66.         func_LV->dumpBlockLiveness((func_ADC->getASTContext()).getSourceManager());
  67.  
  68.  
  69.         return true;
  70.     }
  71.    
  72. };
  73.  
  74. class ExampleASTConsumer : public ASTConsumer {
  75. private:
  76.     ExampleVisitor *visitor; // doesn't have to be private
  77.  
  78. public:
  79.     // override the constructor in order to pass CI
  80.     explicit ExampleASTConsumer(CompilerInstance *CI)
  81.         : visitor(new ExampleVisitor(CI)) // initialize the visitor
  82.         { }
  83.  
  84.     // override this to call our ExampleVisitor on the entire source file
  85.     virtual void HandleTranslationUnit(ASTContext &Context) {
  86.         /* we can use ASTContext to get the TranslationUnitDecl, which is
  87.            a single Decl that collectively represents the entire source file */
  88.         visitor->TraverseDecl(Context.getTranslationUnitDecl());
  89.     }
  90. };
  91.  
  92. class ExampleFrontendAction : public ASTFrontendAction {
  93. public:
  94.     virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, StringRef file) {
  95.         return std::unique_ptr<ASTConsumer>(new ExampleASTConsumer(&CI)); // pass CI pointer to ASTConsumer
  96.     }
  97. };
  98.  
  99. int main(int argc, const char **argv) {
  100.     // parse the command-line args passed to your code
  101.     // CommonOptionsParser op(argc, argv);        
  102.     cl::OptionCategory my_tool_category("my tool option");
  103.     clang::tooling::CommonOptionsParser op(argc, argv, my_tool_category);
  104.  
  105.     // create a new Clang Tool instance (a LibTooling environment)
  106.     ClangTool Tool(op.getCompilations(), op.getSourcePathList());
  107.  
  108.     // run the Clang Tool, creating a new FrontendAction (explained below)
  109.     int result = Tool.run(newFrontendActionFactory<ExampleFrontendAction>().get());
  110.  
  111.     return result;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement