citrun

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

commit 9c02de4dafed7c4034fb6b4ec839c55ea1ece8d8
parent 8a28dea73f04a121926044430b04ebbd8878aa58
Author: kyle <kyle@getaddrinfo.net>
Date:   Sat,  2 Apr 2016 10:17:16 -0600

move instrument/ into src/

Diffstat:
M.gitignore | 2--
MJamfile | 2+-
MJamrules | 6+++---
Dinstrument/Jamfile | 12------------
Dinstrument/compilers/c++ | 2--
Dinstrument/compilers/cc | 2--
Dinstrument/compilers/g++ | 2--
Dinstrument/compilers/gcc | 2--
Dinstrument/instrument_action.h | 42------------------------------------------
Dinstrument/rewrite_ast_visitor.cc | 94-------------------------------------------------------------------------------
Asrc/Jamfile | 13+++++++++++++
Rinstrument/scv_instrument.1 -> src/citrun_instrument.1 | 0
Asrc/compilers/c++ | 2++
Asrc/compilers/cc | 2++
Asrc/compilers/g++ | 2++
Asrc/compilers/gcc | 2++
Rinstrument/instrument_action.cc -> src/instrument_action.cc | 0
Asrc/instrument_action.h | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/instrument_ast_visitor.cc | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rinstrument/rewrite_ast_visitor.h -> src/instrument_ast_visitor.h | 0
Rinstrument/main.cc -> src/instrument_main.cc | 0
Rinstrument/runtime_h.h -> src/runtime_h.h | 0
22 files changed, 161 insertions(+), 162 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,4 +1,2 @@ *.o *.swp -scv_instrument -scv_viewer diff --git a/Jamfile b/Jamfile @@ -1,6 +1,6 @@ SubDir TOP ; -SubInclude TOP instrument ; SubInclude TOP lib ; +SubInclude TOP src ; SubInclude TOP viewer ; SubInclude TOP wrap ; diff --git a/Jamrules b/Jamrules @@ -10,7 +10,7 @@ if $(OS) = OPENBSD { # Link directly against libestdc++ from ports LINKLIBS on scv_viewer = -lestdc++ -lm -lglut ; - LINKLIBS on scv_instrument = + LINKLIBS on citrun_instrument = -lestdc++ -Wl,--start-group -lclangAST @@ -41,7 +41,7 @@ if $(OS) = MACOSX { # We must specify that _scv_node0 will be available at runtime LINKFLAGS on libscv.$(SHLIB_SUF) = -Wl,-U,__scv_node0 ; - LINKLIBS on scv_instrument = + LINKLIBS on citrun_instrument = -lc++ -lclangAST -lclangAnalysis @@ -67,7 +67,7 @@ if $(OS) = LINUX { # Link directly against libestdc++ from ports LINKLIBS on scv_viewer = -lstdc++ -lm -lglut ; - LINKLIBS on scv_instrument = + LINKLIBS on citrun_instrument = -Wl,--start-group -lclangAST -lclangAnalysis diff --git a/instrument/Jamfile b/instrument/Jamfile @@ -1,12 +0,0 @@ -SubDir TOP instrument ; - -SUBDIRC++FLAGS = -std=c++11 `llvm-config --cxxflags` ; -LINKFLAGS on scv_instrument = `llvm-config --ldflags` ; -LINKLIBS on scv_instrument += `llvm-config --libs bitreader mcparser transformutils option --system-libs` ; - -Main scv_instrument : main.cc - instrument_action.cc - rewrite_ast_visitor.cc ; - -InstallBin $(PREFIX)/bin : scv_instrument ; -InstallMan $(PREFIX)/man : scv_instrument.1 ; diff --git a/instrument/compilers/c++ b/instrument/compilers/c++ @@ -1 +0,0 @@ -../scv_instrument -\ No newline at end of file diff --git a/instrument/compilers/cc b/instrument/compilers/cc @@ -1 +0,0 @@ -../scv_instrument -\ No newline at end of file diff --git a/instrument/compilers/g++ b/instrument/compilers/g++ @@ -1 +0,0 @@ -../scv_instrument -\ No newline at end of file diff --git a/instrument/compilers/gcc b/instrument/compilers/gcc @@ -1 +0,0 @@ -../scv_instrument -\ No newline at end of file diff --git a/instrument/instrument_action.h b/instrument/instrument_action.h @@ -1,42 +0,0 @@ -#include <clang/AST/ASTConsumer.h> -#include <clang/Frontend/FrontendActions.h> -#include <clang/Rewrite/Core/Rewriter.h> - -#include "rewrite_ast_visitor.h" - -class RewriteASTConsumer : public clang::ASTConsumer { -public: - RewriteASTConsumer(clang::Rewriter &R) : Visitor(R) {} - - // Override the method that gets called for each parsed top-level - // declaration. - bool HandleTopLevelDecl(clang::DeclGroupRef DR) override { - for (auto &b : DR) { - // Traverse the declaration using our AST visitor. - Visitor.TraverseDecl(b); - // b->dump(); - } - return true; - } - - RewriteASTVisitor& get_visitor() { return Visitor; }; -private: - RewriteASTVisitor Visitor; -}; - -// For each source file provided to the tool, a new FrontendAction is created. -class InstrumentAction : public clang::ASTFrontendAction { -public: - InstrumentAction() {}; - - void EndSourceFileAction() override; -#if LLVM_VER > 35 - std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &, clang::StringRef) override; -#else - clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &, clang::StringRef) override; -#endif - -private: - clang::Rewriter TheRewriter; - RewriteASTConsumer *InstrumentASTConsumer; -}; diff --git a/instrument/rewrite_ast_visitor.cc b/instrument/rewrite_ast_visitor.cc @@ -1,94 +0,0 @@ -#include <sstream> -#include <string> - -#include <clang/AST/AST.h> -#include <clang/Lex/Lexer.h> - -#include "rewrite_ast_visitor.h" - -bool -RewriteASTVisitor::VisitVarDecl(clang::VarDecl *d) -{ - return true; -} - -bool -RewriteASTVisitor::VisitStmt(clang::Stmt *s) -{ - std::stringstream ss; - unsigned line = SM.getPresumedLineNumber(s->getLocStart()); - clang::Stmt *stmt_to_inst = NULL; - - if (clang::isa<clang::IfStmt>(s)) { - stmt_to_inst = clang::cast<clang::IfStmt>(s)->getCond(); - } - else if (clang::isa<clang::ForStmt>(s)) { - stmt_to_inst = clang::cast<clang::ForStmt>(s)->getCond(); - } - else if (clang::isa<clang::WhileStmt>(s)) { - stmt_to_inst = clang::cast<clang::WhileStmt>(s)->getCond(); - } - else if (clang::isa<clang::SwitchStmt>(s)) { - stmt_to_inst = clang::cast<clang::SwitchStmt>(s)->getCond(); - } - else if (clang::isa<clang::ReturnStmt>(s)) { - stmt_to_inst = clang::cast<clang::ReturnStmt>(s)->getRetValue(); - } - /* - 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)) { - stmt_to_inst = s; - } - - if (stmt_to_inst == NULL) - return true; - - ss << "(++_scv_lines[" << line << "], "; - if (TheRewriter.InsertTextBefore(stmt_to_inst->getLocStart(), ss.str())) - // writing failed, don't attempt to add ")" - return true; - - TheRewriter.InsertTextAfter(real_loc_end(stmt_to_inst), ")"); - ++rewrite_count; - - return true; -} - -bool -RewriteASTVisitor::VisitFunctionDecl(clang::FunctionDecl *f) -{ - // Only function definitions (with bodies), not declarations. - if (f->hasBody() == 0) - return true; - - clang::Stmt *FuncBody = f->getBody(); - - clang::DeclarationName DeclName = f->getNameInfo().getName(); - std::string FuncName = DeclName.getAsString(); - - if (FuncName.compare("main") != 0) - // Function is not main - return true; - - std::stringstream ss; - // On some platforms we need to depend directly on a symbol provided by - // the runtime. Normally this isn't needed because the runtime only - // depends on symbols in the isntrumented application. - ss << "libscv_init();"; - clang::SourceLocation curly_brace(FuncBody->getLocStart().getLocWithOffset(1)); - TheRewriter.InsertTextBefore(curly_brace, ss.str()); - - return true; -} - -clang::SourceLocation -RewriteASTVisitor::real_loc_end(clang::Stmt *d) -{ - clang::SourceLocation _e(d->getLocEnd()); - return clang::SourceLocation(clang::Lexer::getLocForEndOfToken(_e, 0, SM, lopt)); -} diff --git a/src/Jamfile b/src/Jamfile @@ -0,0 +1,13 @@ +SubDir TOP src ; + +SUBDIRC++FLAGS = -std=c++11 `llvm-config --cxxflags` ; +LINKFLAGS on citrun_instrument = `llvm-config --ldflags` ; +LINKLIBS on citrun_instrument += `llvm-config --libs bitreader mcparser transformutils option --system-libs` ; + +Main citrun_instrument : + instrument_main.cc + instrument_action.cc + instrument_ast_visitor.cc ; + +InstallBin $(PREFIX)/bin : citrun_instrument ; +InstallMan $(PREFIX)/man : citrun_instrument.1 ; diff --git a/instrument/scv_instrument.1 b/src/citrun_instrument.1 diff --git a/src/compilers/c++ b/src/compilers/c++ @@ -0,0 +1 @@ +../scv_instrument +\ No newline at end of file diff --git a/src/compilers/cc b/src/compilers/cc @@ -0,0 +1 @@ +../scv_instrument +\ No newline at end of file diff --git a/src/compilers/g++ b/src/compilers/g++ @@ -0,0 +1 @@ +../scv_instrument +\ No newline at end of file diff --git a/src/compilers/gcc b/src/compilers/gcc @@ -0,0 +1 @@ +../scv_instrument +\ No newline at end of file diff --git a/instrument/instrument_action.cc b/src/instrument_action.cc diff --git a/src/instrument_action.h b/src/instrument_action.h @@ -0,0 +1,42 @@ +#include <clang/AST/ASTConsumer.h> +#include <clang/Frontend/FrontendActions.h> +#include <clang/Rewrite/Core/Rewriter.h> + +#include "instrument_ast_visitor.h" + +class RewriteASTConsumer : public clang::ASTConsumer { +public: + RewriteASTConsumer(clang::Rewriter &R) : Visitor(R) {} + + // Override the method that gets called for each parsed top-level + // declaration. + bool HandleTopLevelDecl(clang::DeclGroupRef DR) override { + for (auto &b : DR) { + // Traverse the declaration using our AST visitor. + Visitor.TraverseDecl(b); + // b->dump(); + } + return true; + } + + RewriteASTVisitor& get_visitor() { return Visitor; }; +private: + RewriteASTVisitor Visitor; +}; + +// For each source file provided to the tool, a new FrontendAction is created. +class InstrumentAction : public clang::ASTFrontendAction { +public: + InstrumentAction() {}; + + void EndSourceFileAction() override; +#if LLVM_VER > 35 + std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &, clang::StringRef) override; +#else + clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &, clang::StringRef) override; +#endif + +private: + clang::Rewriter TheRewriter; + RewriteASTConsumer *InstrumentASTConsumer; +}; diff --git a/src/instrument_ast_visitor.cc b/src/instrument_ast_visitor.cc @@ -0,0 +1,94 @@ +#include <sstream> +#include <string> + +#include <clang/AST/AST.h> +#include <clang/Lex/Lexer.h> + +#include "instrument_ast_visitor.h" + +bool +RewriteASTVisitor::VisitVarDecl(clang::VarDecl *d) +{ + return true; +} + +bool +RewriteASTVisitor::VisitStmt(clang::Stmt *s) +{ + std::stringstream ss; + unsigned line = SM.getPresumedLineNumber(s->getLocStart()); + clang::Stmt *stmt_to_inst = NULL; + + if (clang::isa<clang::IfStmt>(s)) { + stmt_to_inst = clang::cast<clang::IfStmt>(s)->getCond(); + } + else if (clang::isa<clang::ForStmt>(s)) { + stmt_to_inst = clang::cast<clang::ForStmt>(s)->getCond(); + } + else if (clang::isa<clang::WhileStmt>(s)) { + stmt_to_inst = clang::cast<clang::WhileStmt>(s)->getCond(); + } + else if (clang::isa<clang::SwitchStmt>(s)) { + stmt_to_inst = clang::cast<clang::SwitchStmt>(s)->getCond(); + } + else if (clang::isa<clang::ReturnStmt>(s)) { + stmt_to_inst = clang::cast<clang::ReturnStmt>(s)->getRetValue(); + } + /* + 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)) { + stmt_to_inst = s; + } + + if (stmt_to_inst == NULL) + return true; + + ss << "(++_scv_lines[" << line << "], "; + if (TheRewriter.InsertTextBefore(stmt_to_inst->getLocStart(), ss.str())) + // writing failed, don't attempt to add ")" + return true; + + TheRewriter.InsertTextAfter(real_loc_end(stmt_to_inst), ")"); + ++rewrite_count; + + return true; +} + +bool +RewriteASTVisitor::VisitFunctionDecl(clang::FunctionDecl *f) +{ + // Only function definitions (with bodies), not declarations. + if (f->hasBody() == 0) + return true; + + clang::Stmt *FuncBody = f->getBody(); + + clang::DeclarationName DeclName = f->getNameInfo().getName(); + std::string FuncName = DeclName.getAsString(); + + if (FuncName.compare("main") != 0) + // Function is not main + return true; + + std::stringstream ss; + // On some platforms we need to depend directly on a symbol provided by + // the runtime. Normally this isn't needed because the runtime only + // depends on symbols in the isntrumented application. + ss << "libscv_init();"; + clang::SourceLocation curly_brace(FuncBody->getLocStart().getLocWithOffset(1)); + TheRewriter.InsertTextBefore(curly_brace, ss.str()); + + return true; +} + +clang::SourceLocation +RewriteASTVisitor::real_loc_end(clang::Stmt *d) +{ + clang::SourceLocation _e(d->getLocEnd()); + return clang::SourceLocation(clang::Lexer::getLocForEndOfToken(_e, 0, SM, lopt)); +} diff --git a/instrument/rewrite_ast_visitor.h b/src/instrument_ast_visitor.h diff --git a/instrument/main.cc b/src/instrument_main.cc diff --git a/instrument/runtime_h.h b/src/runtime_h.h