commit b4935642b56ff23dabf1b976fc24c7ef3c60470b
parent 5006d79ca8ad225b0da277ec910239d4aab16ee7
Author: Kyle Milz <kyle@0x30.net>
Date: Fri, 12 Aug 2016 19:56:10 -0600
src: add copy constructor to log
Diffstat:
5 files changed, 46 insertions(+), 42 deletions(-)
diff --git a/src/inst_action.cc b/src/inst_action.cc
@@ -42,14 +42,14 @@ InstrumentAction::write_modified_src(clang::FileID const &fid)
if (m_is_citruninst) {
out_file += ".citrun";
- *m_log << "Writing modified source to '"
+ m_log << "Writing modified source to '"
<< out_file << "'.\n";
}
std::error_code ec;
llvm::raw_fd_ostream output(out_file, ec, llvm::sys::fs::F_None);
if (ec.value()) {
- *m_log << "Error writing modified source: "
+ m_log << "Error writing modified source: "
<< ec.message() << "\n";
warnx("'%s': %s", out_file.c_str(), ec.message().c_str());
return;
@@ -57,7 +57,7 @@ InstrumentAction::write_modified_src(clang::FileID const &fid)
// Write the instrumented source file
m_TheRewriter.getEditBuffer(fid).write(output);
- *m_log << "Modified source written successfully.\n";
+ m_log << "Modified source written successfully.\n";
}
void
@@ -102,14 +102,14 @@ InstrumentAction::EndSourceFileAction()
unsigned header_sz = count(header.begin(), header.end(), '\n');
if (!m_is_citruninst && m_TheRewriter.InsertTextAfter(start, header)) {
- *m_log << "Failed inserting " << header_sz
+ m_log << "Failed inserting " << header_sz
<< " lines of instrumentation preabmle.";
return;
}
- *m_log << "Instrumentation of '" << m_compiler_file_name << "' finished:\n";
- *m_log << " " << num_lines << " Lines of source code\n";
- *m_log << " " << header_sz << " Lines of instrumentation header\n";
+ m_log << "Instrumentation of '" << m_compiler_file_name << "' finished:\n";
+ m_log << " " << num_lines << " Lines of source code\n";
+ m_log << " " << header_sz << " Lines of instrumentation header\n";
//
// Write out statistics from the AST visitor.
@@ -118,7 +118,7 @@ InstrumentAction::EndSourceFileAction()
for (int i = 0; i < NCOUNTERS; ++i) {
if (v.m_counters[i] == 0)
continue;
- *m_log << " " << v.m_counters[i] << " "
+ m_log << " " << v.m_counters[i] << " "
<< v.m_counter_descr[i] << "\n";
}
diff --git a/src/inst_action.h b/src/inst_action.h
@@ -30,7 +30,7 @@ private:
// For each source file provided to the tool, a new FrontendAction is created.
class InstrumentAction : public clang::ASTFrontendAction {
public:
- InstrumentAction(InstrumentLogger *log, bool citruninst,
+ InstrumentAction(InstrumentLogger &log, bool citruninst,
std::string const &filename) :
m_log(log),
m_is_citruninst(citruninst),
@@ -45,7 +45,7 @@ private:
clang::Rewriter m_TheRewriter;
RewriteASTConsumer *m_InstrumentASTConsumer;
- InstrumentLogger *m_log;
+ InstrumentLogger m_log;
bool m_is_citruninst;
std::string m_compiler_file_name;
};
diff --git a/src/inst_log.h b/src/inst_log.h
@@ -11,6 +11,11 @@ public:
m_pid(getpid()),
m_needs_prefix(true)
{};
+ InstrumentLogger(InstrumentLogger &o) :
+ m_pid(o.m_pid),
+ m_output(o.m_output),
+ m_needs_prefix(o.m_needs_prefix)
+ {}
//~InstrumentLogger()
//{ llvm::errs() << "~InstrumentLogger()\n"; };
@@ -52,6 +57,7 @@ public:
pid_t m_pid;
llvm::raw_ostream *m_output;
+ bool m_needs_prefix;
private:
void print_prefix() {
@@ -65,8 +71,6 @@ private:
if (std::find(rhs.begin(), rhs.end(), '\n') != rhs.end())
m_needs_prefix = true;
};
-
- bool m_needs_prefix;
};
#endif // _INST_LOG_H_
diff --git a/src/inst_main.cc b/src/inst_main.cc
@@ -129,14 +129,14 @@ main(int argc, char *argv[])
if (is_citruninst == false)
clean_PATH();
- CitrunInst main(argc, argv, &llog, is_citruninst);
+ CitrunInst main(argc, argv, llog, is_citruninst);
main.process_cmdline();
if (main.instrument())
return 1;
return main.compile_modified();
}
-CitrunInst::CitrunInst(int argc, char *argv[], InstrumentLogger *l, bool is_citruninst) :
+CitrunInst::CitrunInst(int argc, char *argv[], InstrumentLogger &l, bool is_citruninst) :
m_args(argv, argv + argc),
m_log(l),
m_is_citruninst(is_citruninst)
@@ -189,7 +189,7 @@ CitrunInst::save_if_srcfile(char *arg)
ends_with(arg, ".cpp") || ends_with(arg, ".cxx")) {
m_source_files.push_back(arg);
- *m_log << "Found source file '" << arg << "'.\n";
+ m_log << "Found source file '" << arg << "'.\n";
if (m_is_citruninst)
// In this mode the modified source file is written to a
@@ -211,15 +211,15 @@ CitrunInst::process_cmdline()
bool object_arg = false;
bool compile_arg = false;
- *m_log << "Command line is '";
+ m_log << "Command line is '";
for (auto &arg : m_args)
- *m_log << arg << " ";
- *m_log << "'.\n";
+ m_log << arg << " ";
+ m_log << "'.\n";
for (auto &arg : m_args) {
if (std::strcmp(arg, "-E") == 0) {
- *m_log << "Preprocessor argument found\n";
+ m_log << "Preprocessor argument found\n";
exec_compiler();
}
else if (std::strcmp(arg, "-o") == 0)
@@ -230,7 +230,7 @@ CitrunInst::process_cmdline()
save_if_srcfile(arg);
}
- *m_log << "Object arg = " << object_arg << ", "
+ m_log << "Object arg = " << object_arg << ", "
<< "compile arg = " << compile_arg << "\n";
bool linking = false;
@@ -244,24 +244,24 @@ CitrunInst::process_cmdline()
linking = true;
if (linking) {
- *m_log << "Link detected, adding '";
+ m_log << "Link detected, adding '";
#ifndef __APPLE__
// OSX always links this.
m_args.push_back(const_cast<char *>("-pthread"));
- *m_log << m_args.back() << " ";
+ m_log << m_args.back() << " ";
#endif
#ifdef CITRUN_COVERAGE
// Needed because libcitrun.a will be instrumented with gcov.
m_args.push_back(const_cast<char *>("-coverage"));
#endif
m_args.push_back(const_cast<char *>(STR(CITRUN_SHARE) "/libcitrun.a"));
- *m_log << m_args.back() << "' to command line.\n";
+ m_log << m_args.back() << "' to command line.\n";
}
if (m_source_files.size() != 0)
return;
- *m_log << "No source files found. Executing command line.\n";
+ m_log << "No source files found. Executing command line.\n";
exec_compiler();
}
@@ -280,10 +280,10 @@ CitrunInst::instrument()
clang_argv.insert(clang_argv.end(), m_args.begin(), m_args.end());
#if defined(__OpenBSD__)
clang_argv.push_back("-I/usr/local/lib/clang/3.8.1/include");
- *m_log << "Added clangtool argument '" << clang_argv.back() << "'.\n";
+ m_log << "Added clangtool argument '" << clang_argv.back() << "'.\n";
#elif defined(__APPLE__)
clang_argv.push_back("-I/opt/local/libexec/llvm-3.8/lib/clang/3.8.1/include");
- *m_log << "Added clangtool argument '" << clang_argv.back() << "'.\n";
+ m_log << "Added clangtool argument '" << clang_argv.back() << "'.\n";
#endif
int clang_argc = clang_argv.size();
@@ -295,15 +295,15 @@ CitrunInst::instrument()
clang::DiagnosticOptions diags;
clang::TextDiagnosticPrinter *log;
- log = new clang::TextDiagnosticPrinter(*m_log->m_output, &diags, false);
- log->setPrefix(std::to_string(m_log->m_pid));
+ log = new clang::TextDiagnosticPrinter(*m_log.m_output, &diags, false);
+ log->setPrefix(std::to_string(m_log.m_pid));
Tool.setDiagnosticConsumer(log);
std::unique_ptr<InstrumentActionFactory> f =
llvm::make_unique<InstrumentActionFactory>(m_log, m_is_citruninst, m_source_files);
int ret = Tool.run(f.get());
- *m_log << "Instrumentation " << (ret ? "failed.\n" : "successful.\n");
+ m_log << "Instrumentation " << (ret ? "failed.\n" : "successful.\n");
if (m_is_citruninst)
// Nothing left to do if we're in this mode.
@@ -321,11 +321,11 @@ CitrunInst::try_unmodified_compile()
int ret = fork_compiler();
if (ret == 0) {
- *m_log << "But the native compile succeeded!\n";
+ m_log << "But the native compile succeeded!\n";
return 1;
}
- *m_log << "And the native compile failed.\n";
+ m_log << "And the native compile failed.\n";
return ret;
}
@@ -333,7 +333,7 @@ void
CitrunInst::restore_original_src()
{
for (auto &tmp_file : m_temp_file_map) {
- *m_log << "Restored '" << tmp_file.first << "'.\n";
+ m_log << "Restored '" << tmp_file.first << "'.\n";
copy_file(tmp_file.first, tmp_file.second);
unlink(tmp_file.second.c_str());
@@ -344,10 +344,10 @@ void
CitrunInst::exec_compiler()
{
// XXX: Need to destroy log here.
- m_log->m_output->flush();
+ m_log.m_output->flush();
if (m_is_citruninst) {
- *m_log << "Running as citrun-inst, not re-exec()'ing\n";
+ m_log << "Running as citrun-inst, not re-exec()'ing\n";
exit(0);
}
@@ -360,7 +360,7 @@ int
CitrunInst::fork_compiler()
{
// Otherwise we'll get two copies of buffers after fork().
- m_log->m_output->flush();
+ m_log.m_output->flush();
pid_t child_pid;
if ((child_pid = fork()) < 0)
@@ -370,7 +370,7 @@ CitrunInst::fork_compiler()
// In child.
exec_compiler();
- *m_log << "Forked '" << m_args[0] << "' "
+ m_log << "Forked '" << m_args[0] << "' "
<< "pid is '" << child_pid << "'.\n";
int status;
@@ -382,14 +382,14 @@ CitrunInst::fork_compiler()
if (WIFEXITED(status))
exit = WEXITSTATUS(status);
- *m_log << "'" << child_pid << "' exited " << exit << ".\n";
+ m_log << "'" << child_pid << "' exited " << exit << ".\n";
return exit;
}
int
CitrunInst::compile_modified()
{
- *m_log << "Running native compiler on modified source code.\n";
+ m_log << "Running native compiler on modified source code.\n";
int ret = fork_compiler();
restore_original_src();
diff --git a/src/inst_main.h b/src/inst_main.h
@@ -5,7 +5,7 @@
class CitrunInst {
public:
- CitrunInst(int, char *argv[], InstrumentLogger *, bool);
+ CitrunInst(int, char *argv[], InstrumentLogger &, bool);
void clean_PATH();
void process_cmdline();
@@ -20,7 +20,7 @@ private:
int try_unmodified_compile();
std::vector<char *> m_args;
- InstrumentLogger *m_log;
+ InstrumentLogger m_log;
bool m_is_citruninst;
std::vector<std::string> m_source_files;
std::map<std::string, std::string> m_temp_file_map;
@@ -31,7 +31,7 @@ private:
//
class InstrumentActionFactory : public clang::tooling::FrontendActionFactory {
public:
- InstrumentActionFactory(InstrumentLogger *log, bool citruninst,
+ InstrumentActionFactory(InstrumentLogger &log, bool citruninst,
std::vector<std::string> const &src_files) :
m_log(log),
m_is_citruninst(citruninst),
@@ -44,7 +44,7 @@ public:
}
private:
- InstrumentLogger *m_log;
+ InstrumentLogger m_log;
bool m_is_citruninst;
std::vector<std::string> m_source_files;
int m_i;