36 changes: 35 additions & 1 deletion llvm/tools/llvm-cov/CodeCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
using namespace llvm;
using namespace coverage;

void exportCoverageDataToJson(StringRef ObjectFilename,
const coverage::CoverageMapping &CoverageMapping,
raw_ostream &OS);

namespace {
/// \brief The implementation of the coverage tool.
class CodeCoverageTool {
Expand All @@ -46,7 +50,9 @@ class CodeCoverageTool {
/// \brief The show command.
Show,
/// \brief The report command.
Report
Report,
/// \brief The export command.
Export
};

/// \brief Print the error message to the error output stream.
Expand Down Expand Up @@ -94,6 +100,9 @@ class CodeCoverageTool {
int report(int argc, const char **argv,
CommandLineParserType commandLineParser);

int export_(int argc, const char **argv,
CommandLineParserType commandLineParser);

std::string ObjectFilename;
CoverageViewOptions ViewOpts;
std::string PGOFilename;
Expand Down Expand Up @@ -534,6 +543,8 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
return show(argc, argv, commandLineParser);
case Report:
return report(argc, argv, commandLineParser);
case Export:
return export_(argc, argv, commandLineParser);
}
return 0;
}
Expand Down Expand Up @@ -694,6 +705,24 @@ int CodeCoverageTool::report(int argc, const char **argv,
return 0;
}

int CodeCoverageTool::export_(int argc, const char **argv,
CommandLineParserType commandLineParser) {

auto Err = commandLineParser(argc, argv);
if (Err)
return Err;

auto Coverage = load();
if (!Coverage) {
error("Could not load coverage information");
return 1;
}

exportCoverageDataToJson(ObjectFilename, *Coverage.get(), outs());

return 0;
}

int showMain(int argc, const char *argv[]) {
CodeCoverageTool Tool;
return Tool.run(CodeCoverageTool::Show, argc, argv);
Expand All @@ -703,3 +732,8 @@ int reportMain(int argc, const char *argv[]) {
CodeCoverageTool Tool;
return Tool.run(CodeCoverageTool::Report, argc, argv);
}

int exportMain(int argc, const char *argv[]) {
CodeCoverageTool Tool;
return Tool.run(CodeCoverageTool::Export, argc, argv);
}
418 changes: 418 additions & 0 deletions llvm/tools/llvm-cov/CoverageExporterJson.cpp

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions llvm/tools/llvm-cov/llvm-cov.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ int showMain(int argc, const char *argv[]);
/// \brief The main entry point for the 'report' subcommand.
int reportMain(int argc, const char *argv[]);

/// \brief The main entry point for the 'export' subcommand.
int exportMain(int argc, const char *argv[]);

/// \brief The main entry point for the 'convert-for-testing' subcommand.
int convertForTestingMain(int argc, const char *argv[]);

Expand All @@ -38,12 +41,14 @@ int gcovMain(int argc, const char *argv[]);

/// \brief Top level help.
static int helpMain(int argc, const char *argv[]) {
errs() << "Usage: llvm-cov {gcov|report|show} [OPTION]...\n\n"
errs() << "Usage: llvm-cov {export|gcov|report|show} [OPTION]...\n\n"
<< "Shows code coverage information.\n\n"
<< "Subcommands:\n"
<< " export: Export instrprof file to structured format.\n"
<< " gcov: Work with the gcov format.\n"
<< " show: Annotate source files using instrprof style coverage.\n"
<< " report: Summarize instrprof style coverage information.\n";
<< " report: Summarize instrprof style coverage information.\n"
<< " show: Annotate source files using instrprof style coverage.\n";

return 0;
}

Expand All @@ -68,6 +73,7 @@ int main(int argc, const char **argv) {
typedef int (*MainFunction)(int, const char *[]);
MainFunction Func = StringSwitch<MainFunction>(argv[1])
.Case("convert-for-testing", convertForTestingMain)
.Case("export", exportMain)
.Case("gcov", gcovMain)
.Case("report", reportMain)
.Case("show", showMain)
Expand Down