citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 17e9c959ca325b46bd4f9b7627105417773a7482
parent c9c9d395529302f551732d7cc6551ce42a4a6831
Author: Kyle Milz <kyle@0x30.net>
Date:   Tue, 19 Sep 2017 01:13:44 -0600

bin: split out ASTConsumer class into its own file

Diffstat:
Mbin/inst_action.h | 2+-
Abin/inst_consumer.h | 27+++++++++++++++++++++++++++
Mbin/inst_visitor.cc | 21+++++++++++++++++++++
Mbin/inst_visitor.h | 69+++++++++++++++------------------------------------------------------
4 files changed, 64 insertions(+), 55 deletions(-)

diff --git a/bin/inst_action.h b/bin/inst_action.h @@ -1,4 +1,4 @@ -#include "inst_visitor.h" +#include "inst_consumer.h" #include "inst_log.h" #include <clang/Frontend/FrontendActions.h> diff --git a/bin/inst_consumer.h b/bin/inst_consumer.h @@ -0,0 +1,27 @@ +#include "inst_visitor.h" + +#include <clang/AST/ASTConsumer.h> +#include <clang/Rewrite/Core/Rewriter.h> + + +class RewriteASTConsumer : public clang::ASTConsumer +{ + RewriteASTVisitor Visitor; + +public: + explicit RewriteASTConsumer(clang::Rewriter &R) : + Visitor(R) {} + + // Override the method that gets called for each parsed top-level + // declaration. + virtual bool HandleTopLevelDecl(clang::DeclGroupRef DR) { + for (auto &b : DR) { + // Traverse the declaration using our AST visitor. + Visitor.TraverseDecl(b); + // b->dump(); + } + return true; + } + + RewriteASTVisitor &get_visitor() { return Visitor; }; +}; diff --git a/bin/inst_visitor.cc b/bin/inst_visitor.cc @@ -21,6 +21,27 @@ #include <string> +RewriteASTVisitor::RewriteASTVisitor(clang::Rewriter &R) : + m_TheRewriter(R), + m_SM(R.getSourceMgr()), + m_lopt(R.getLangOpts()), + m_counters(), + m_counter_descr( {{ + "Function definitions", + "If statements", + "For loops", + "While loops", + "Do while loops", + "Switch statements", + "Return statement values", + "Call expressions", + "Total statements", + "Binary operators", + "Errors rewriting source code" + }} ) +{ +} + bool RewriteASTVisitor::TraverseStmt(clang::Stmt *s) { diff --git a/bin/inst_visitor.h b/bin/inst_visitor.h @@ -1,5 +1,4 @@ #include <array> -#include <clang/AST/ASTConsumer.h> #include <clang/AST/RecursiveASTVisitor.h> #include <clang/Rewrite/Core/Rewriter.h> @@ -28,63 +27,25 @@ class RewriteASTVisitor : public clang::RecursiveASTVisitor<RewriteASTVisitor> clang::LangOptions m_lopt; public: - explicit RewriteASTVisitor(clang::Rewriter &R) : - m_TheRewriter(R), - m_SM(R.getSourceMgr()), - m_lopt(R.getLangOpts()), - m_counters(), - m_counter_descr( {{ - "Function definitions", - "If statements", - "For loops", - "While loops", - "Do while loops", - "Switch statements", - "Return statement values", - "Call expressions", - "Total statements", - "Binary operators", - "Errors rewriting source code" - }} ) - {} + explicit RewriteASTVisitor(clang::Rewriter &R); + virtual ~RewriteASTVisitor() {}; - virtual bool TraverseStmt(clang::Stmt *); - virtual bool TraverseDecl(clang::Decl *); + virtual bool TraverseStmt(clang::Stmt *); + virtual bool TraverseDecl(clang::Decl *); - bool VisitVarDecl(clang::VarDecl *d); - bool VisitStmt(clang::Stmt *s); - bool VisitFunctionDecl(clang::FunctionDecl *f); + bool VisitVarDecl(clang::VarDecl *d); + bool VisitStmt(clang::Stmt *s); + bool VisitFunctionDecl(clang::FunctionDecl *f); - bool VisitIfStmt(clang::IfStmt *); - bool VisitForStmt(clang::ForStmt *); - bool VisitWhileStmt(clang::WhileStmt *); - bool VisitDoStmt(clang::DoStmt *); - bool VisitSwitchStmt(clang::SwitchStmt *); - bool VisitReturnStmt(clang::ReturnStmt *); - bool VisitCallExpr(clang::CallExpr *); - bool VisitBinaryOperator(clang::BinaryOperator *); + bool VisitIfStmt(clang::IfStmt *); + bool VisitForStmt(clang::ForStmt *); + bool VisitWhileStmt(clang::WhileStmt *); + bool VisitDoStmt(clang::DoStmt *); + bool VisitSwitchStmt(clang::SwitchStmt *); + bool VisitReturnStmt(clang::ReturnStmt *); + bool VisitCallExpr(clang::CallExpr *); + bool VisitBinaryOperator(clang::BinaryOperator *); std::array<int, NCOUNTERS> m_counters; std::array<std::string, NCOUNTERS> m_counter_descr; }; - -class RewriteASTConsumer : public clang::ASTConsumer -{ - RewriteASTVisitor Visitor; - -public: - explicit RewriteASTConsumer(clang::Rewriter &R) : Visitor(R) {} - - // Override the method that gets called for each parsed top-level - // declaration. - virtual bool HandleTopLevelDecl(clang::DeclGroupRef DR) { - for (auto &b : DR) { - // Traverse the declaration using our AST visitor. - Visitor.TraverseDecl(b); - // b->dump(); - } - return true; - } - - RewriteASTVisitor& get_visitor() { return Visitor; }; -};