citrun

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

commit 165c6db967ee6001c29d80ac20f5341c0a9eee68
parent ce5762df302d58a99431ee3450ce26cc07b0512e
Author: Kyle Milz <kyle@0x30.net>
Date:   Mon,  8 Aug 2016 22:14:42 -0600

src: rename ast visitor

Diffstat:
Msrc/Jamfile | 2+-
Msrc/inst_action.h | 2+-
Dsrc/inst_ast_visitor.cc | 135-------------------------------------------------------------------------------
Asrc/inst_visitor.cc | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/inst_ast_visitor.h -> src/inst_visitor.h | 0
5 files changed, 137 insertions(+), 137 deletions(-)

diff --git a/src/Jamfile b/src/Jamfile @@ -55,7 +55,7 @@ InstallBin $(PREFIX)/bin : citrun-gl ; INST_SRCS = inst_main.cc inst_action.cc - inst_ast_visitor.cc ; + inst_visitor.cc ; Stringize runtime_h.h : $(TOP)/lib/runtime.h ; diff --git a/src/inst_action.h b/src/inst_action.h @@ -3,7 +3,7 @@ #include <clang/Rewrite/Core/Rewriter.h> #include <clang/Tooling/Tooling.h> -#include "inst_ast_visitor.h" +#include "inst_visitor.h" class RewriteASTConsumer : public clang::ASTConsumer { public: diff --git a/src/inst_ast_visitor.cc b/src/inst_ast_visitor.cc @@ -1,135 +0,0 @@ -// -// Copyright (c) 2016 Kyle Milz <kyle@0x30.net> -// -// Permission to use, copy, modify, and distribute this software for any -// purpose with or without fee is hereby granted, provided that the above -// copyright notice and this permission notice appear in all copies. -// -// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -// -#include <clang/AST/AST.h> -#include <clang/Lex/Lexer.h> -#include <sstream> -#include <string> - -#include "inst_ast_visitor.h" - -bool -RewriteASTVisitor::VisitVarDecl(clang::VarDecl *d) -{ - return true; -} - -bool -RewriteASTVisitor::VisitStmt(clang::Stmt *s) -{ - m_counters[8]++; - - if (clang::isa<clang::IfStmt>(s)) { - s = clang::cast<clang::IfStmt>(s)->getCond(); - if (modify_stmt(s) == false) - return true; - m_counters[2]++; - } - else if (clang::isa<clang::ForStmt>(s)) { - s = clang::cast<clang::ForStmt>(s)->getCond(); - if (modify_stmt(s) == false) - return true; - m_counters[3]++; - } - else if (clang::isa<clang::WhileStmt>(s)) { - s = clang::cast<clang::WhileStmt>(s)->getCond(); - if (modify_stmt(s) == false) - return true; - m_counters[4]++; - } - else if (clang::isa<clang::SwitchStmt>(s)) { - s = clang::cast<clang::SwitchStmt>(s)->getCond(); - if (modify_stmt(s) == false) - return true; - m_counters[5]++; - } - else if (clang::isa<clang::ReturnStmt>(s)) { - s = clang::cast<clang::ReturnStmt>(s)->getRetValue(); - if (modify_stmt(s) == false) - return true; - m_counters[6]++; - } - /* - else if (isa<BreakStmt>(s) || isa<ContinueStmt>(s) || - || isa<SwitchCase>(s)) { - } - else if (clang::isa<clang::DeclStmt>(s)) { - } - */ - else if (clang::isa<clang::CallExpr>(s)) { - if (modify_stmt(s) == false) - return true; - m_counters[7]++; - } - - return true; -} - -bool -RewriteASTVisitor::modify_stmt(clang::Stmt *s) -{ - if (s == NULL) - return false; - - std::stringstream ss; - ss << "(++_citrun_lines[" - << m_SM.getPresumedLineNumber(s->getLocStart()) - 1 - << "], "; - if (m_TheRewriter.InsertTextBefore(s->getLocStart(), ss.str())) - // writing failed, don't attempt to add ")" - return false; - m_TheRewriter.InsertTextAfter(real_loc_end(s), ")"); - - return true; -} - -bool -RewriteASTVisitor::VisitFunctionDecl(clang::FunctionDecl *f) -{ - // Only function definitions (with bodies), not declarations. - if (f->hasBody() == 0) - return true; - - std::stringstream rewrite_text; - - // main() is a special case because it must start the runtime thread. - clang::DeclarationName DeclName = f->getNameInfo().getName(); - if (DeclName.getAsString() == "main") { - m_counters[0]++; - rewrite_text << "citrun_start();"; - } - - clang::Stmt *FuncBody = f->getBody(); - clang::SourceLocation curly_brace(FuncBody->getLocStart().getLocWithOffset(1)); - - // Animate function calls by firing the entire declaration. - int decl_start = m_SM.getPresumedLineNumber(f->getLocStart()); - int decl_end = m_SM.getPresumedLineNumber(curly_brace); - for (int i = decl_start; i <= decl_end; i++) - rewrite_text << "++_citrun_lines[" << i - 1 << "];"; - - // Rewrite the function source right after the beginning curly brace. - m_TheRewriter.InsertTextBefore(curly_brace, rewrite_text.str()); - - m_counters[1]++; - return true; -} - -clang::SourceLocation -RewriteASTVisitor::real_loc_end(clang::Stmt *d) -{ - clang::SourceLocation _e(d->getLocEnd()); - return clang::SourceLocation(clang::Lexer::getLocForEndOfToken(_e, 0, m_SM, m_lopt)); -} diff --git a/src/inst_visitor.cc b/src/inst_visitor.cc @@ -0,0 +1,135 @@ +// +// Copyright (c) 2016 Kyle Milz <kyle@0x30.net> +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +// +#include <clang/AST/AST.h> +#include <clang/Lex/Lexer.h> +#include <sstream> +#include <string> + +#include "inst_visitor.h" + +bool +RewriteASTVisitor::VisitVarDecl(clang::VarDecl *d) +{ + return true; +} + +bool +RewriteASTVisitor::VisitStmt(clang::Stmt *s) +{ + m_counters[8]++; + + if (clang::isa<clang::IfStmt>(s)) { + s = clang::cast<clang::IfStmt>(s)->getCond(); + if (modify_stmt(s) == false) + return true; + m_counters[2]++; + } + else if (clang::isa<clang::ForStmt>(s)) { + s = clang::cast<clang::ForStmt>(s)->getCond(); + if (modify_stmt(s) == false) + return true; + m_counters[3]++; + } + else if (clang::isa<clang::WhileStmt>(s)) { + s = clang::cast<clang::WhileStmt>(s)->getCond(); + if (modify_stmt(s) == false) + return true; + m_counters[4]++; + } + else if (clang::isa<clang::SwitchStmt>(s)) { + s = clang::cast<clang::SwitchStmt>(s)->getCond(); + if (modify_stmt(s) == false) + return true; + m_counters[5]++; + } + else if (clang::isa<clang::ReturnStmt>(s)) { + s = clang::cast<clang::ReturnStmt>(s)->getRetValue(); + if (modify_stmt(s) == false) + return true; + m_counters[6]++; + } + /* + else if (isa<BreakStmt>(s) || isa<ContinueStmt>(s) || + || isa<SwitchCase>(s)) { + } + else if (clang::isa<clang::DeclStmt>(s)) { + } + */ + else if (clang::isa<clang::CallExpr>(s)) { + if (modify_stmt(s) == false) + return true; + m_counters[7]++; + } + + return true; +} + +bool +RewriteASTVisitor::modify_stmt(clang::Stmt *s) +{ + if (s == NULL) + return false; + + std::stringstream ss; + ss << "(++_citrun_lines[" + << m_SM.getPresumedLineNumber(s->getLocStart()) - 1 + << "], "; + if (m_TheRewriter.InsertTextBefore(s->getLocStart(), ss.str())) + // writing failed, don't attempt to add ")" + return false; + m_TheRewriter.InsertTextAfter(real_loc_end(s), ")"); + + return true; +} + +bool +RewriteASTVisitor::VisitFunctionDecl(clang::FunctionDecl *f) +{ + // Only function definitions (with bodies), not declarations. + if (f->hasBody() == 0) + return true; + + std::stringstream rewrite_text; + + // main() is a special case because it must start the runtime thread. + clang::DeclarationName DeclName = f->getNameInfo().getName(); + if (DeclName.getAsString() == "main") { + m_counters[0]++; + rewrite_text << "citrun_start();"; + } + + clang::Stmt *FuncBody = f->getBody(); + clang::SourceLocation curly_brace(FuncBody->getLocStart().getLocWithOffset(1)); + + // Animate function calls by firing the entire declaration. + int decl_start = m_SM.getPresumedLineNumber(f->getLocStart()); + int decl_end = m_SM.getPresumedLineNumber(curly_brace); + for (int i = decl_start; i <= decl_end; i++) + rewrite_text << "++_citrun_lines[" << i - 1 << "];"; + + // Rewrite the function source right after the beginning curly brace. + m_TheRewriter.InsertTextBefore(curly_brace, rewrite_text.str()); + + m_counters[1]++; + return true; +} + +clang::SourceLocation +RewriteASTVisitor::real_loc_end(clang::Stmt *d) +{ + clang::SourceLocation _e(d->getLocEnd()); + return clang::SourceLocation(clang::Lexer::getLocForEndOfToken(_e, 0, m_SM, m_lopt)); +} diff --git a/src/inst_ast_visitor.h b/src/inst_visitor.h