citrun

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

action.h (1560B)


      1 #include "consumer.h"
      2 #include "log.h"
      3 
      4 #include <clang/Frontend/FrontendActions.h>
      5 #include <clang/Rewrite/Core/Rewriter.h>
      6 #include <clang/Tooling/Tooling.h>
      7 
      8 
      9 // For each source file provided to the tool, a new FrontendAction is created.
     10 class InstrumentAction : public clang::ASTFrontendAction
     11 {
     12 	void			 write_modified_src(clang::FileID const &);
     13 
     14 	clang::Rewriter		 m_TheRewriter;
     15 	RewriteASTConsumer	*m_InstrumentASTConsumer;
     16 	InstrumentLogger&	 m_log;
     17 	bool			 m_is_citruninst;
     18 	std::string		 m_compiler_file_name;
     19 
     20 public:
     21 	InstrumentAction(InstrumentLogger &log, bool citruninst,
     22 			std::string const &filename) :
     23 		m_log(log),
     24 		m_is_citruninst(citruninst),
     25 		m_compiler_file_name(filename)
     26 	{};
     27 
     28 	void EndSourceFileAction() override;
     29 	std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &, clang::StringRef) override;
     30 };
     31 
     32 //
     33 // Needed because we pass custom stuff down into the ASTFrontendAction
     34 //
     35 class InstrumentActionFactory : public clang::tooling::FrontendActionFactory
     36 {
     37 	InstrumentLogger&	 m_log;
     38 	bool			 m_is_citruninst;
     39 	std::vector<std::string> m_source_files;
     40 	int			 m_i;
     41 
     42 public:
     43 	InstrumentActionFactory(InstrumentLogger &log, bool citruninst,
     44 	    std::vector<std::string> const &src_files) :
     45 		m_log(log),
     46 		m_is_citruninst(citruninst),
     47 		m_source_files(src_files),
     48 		m_i(0)
     49 	{};
     50 
     51 	// clang::ASTFrontendAction *create() {
     52 	std::unique_ptr<clang::FrontendAction> create() {
     53 		return std::unique_ptr<clang::FrontendAction>(
     54 			new InstrumentAction(m_log, m_is_citruninst,
     55 			m_source_files[m_i++]));
     56 	}
     57 };