blob: 158415870250c10159dd1275937223c4ebb310c0 [file] [log] [blame]
Yuchen Wuc3e64242013-12-05 22:02:29 +00001//===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Devang Patel37140652011-09-28 18:50:00 +000010// llvm-cov is a command line tools to analyze and report coverage information.
11//
Devang Patel37140652011-09-28 18:50:00 +000012//===----------------------------------------------------------------------===//
13
Alex Lorenze82d89c2014-08-22 22:56:03 +000014#include "llvm/ADT/StringRef.h"
Justin Bogner533e2612014-10-30 20:29:48 +000015#include "llvm/ADT/StringSwitch.h"
Justin Bogner733013c2015-06-03 02:48:09 +000016#include "llvm/Support/CommandLine.h"
Richard Smith2ad6d482016-06-09 00:53:21 +000017#include "llvm/Support/ManagedStatic.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000018#include "llvm/Support/Path.h"
Richard Smith2ad6d482016-06-09 00:53:21 +000019#include "llvm/Support/PrettyStackTrace.h"
Justin Bogner5a07bb82015-03-24 23:34:36 +000020#include "llvm/Support/Process.h"
Richard Smith2ad6d482016-06-09 00:53:21 +000021#include "llvm/Support/Signals.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000022#include "llvm/Support/raw_ostream.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000023#include <string>
24
25using namespace llvm;
26
27/// \brief The main entry point for the 'show' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000028int showMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000029
30/// \brief The main entry point for the 'report' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000031int reportMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000032
Vedant Kumar7101d732016-07-26 22:50:58 +000033/// \brief The main entry point for the 'export' subcommand.
34int exportMain(int argc, const char *argv[]);
35
Alex Lorenze82d89c2014-08-22 22:56:03 +000036/// \brief The main entry point for the 'convert-for-testing' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000037int convertForTestingMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000038
39/// \brief The main entry point for the gcov compatible coverage tool.
Justin Bognerd249a3b2014-10-30 20:57:49 +000040int gcovMain(int argc, const char *argv[]);
Devang Patel37140652011-09-28 18:50:00 +000041
Justin Bogner533e2612014-10-30 20:29:48 +000042/// \brief Top level help.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000043static int helpMain(int argc, const char *argv[]) {
Vedant Kumar7101d732016-07-26 22:50:58 +000044 errs() << "Usage: llvm-cov {export|gcov|report|show} [OPTION]...\n\n"
Justin Bogner5a07bb82015-03-24 23:34:36 +000045 << "Shows code coverage information.\n\n"
46 << "Subcommands:\n"
Vedant Kumar7101d732016-07-26 22:50:58 +000047 << " export: Export instrprof file to structured format.\n"
Justin Bogner5a07bb82015-03-24 23:34:36 +000048 << " gcov: Work with the gcov format.\n"
Vedant Kumar7101d732016-07-26 22:50:58 +000049 << " report: Summarize instrprof style coverage information.\n"
50 << " show: Annotate source files using instrprof style coverage.\n";
51
Justin Bogner533e2612014-10-30 20:29:48 +000052 return 0;
53}
54
Justin Bogner733013c2015-06-03 02:48:09 +000055/// \brief Top level version information.
56static int versionMain(int argc, const char *argv[]) {
57 cl::PrintVersionMessage();
58 return 0;
59}
60
Alex Lorenz2b5d03a2014-07-28 18:03:51 +000061int main(int argc, const char **argv) {
Richard Smith2ad6d482016-06-09 00:53:21 +000062 // Print a stack trace if we signal out.
63 sys::PrintStackTraceOnErrorSignal(argv[0]);
64 PrettyStackTraceProgram X(argc, argv);
65 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
66
Alex Lorenze82d89c2014-08-22 22:56:03 +000067 // If argv[0] is or ends with 'gcov', always be gcov compatible
68 if (sys::path::stem(argv[0]).endswith_lower("gcov"))
Justin Bognerd249a3b2014-10-30 20:57:49 +000069 return gcovMain(argc, argv);
Alex Lorenze82d89c2014-08-22 22:56:03 +000070
71 // Check if we are invoking a specific tool command.
72 if (argc > 1) {
Justin Bognerd249a3b2014-10-30 20:57:49 +000073 typedef int (*MainFunction)(int, const char *[]);
74 MainFunction Func = StringSwitch<MainFunction>(argv[1])
75 .Case("convert-for-testing", convertForTestingMain)
Vedant Kumar7101d732016-07-26 22:50:58 +000076 .Case("export", exportMain)
Justin Bognerd249a3b2014-10-30 20:57:49 +000077 .Case("gcov", gcovMain)
78 .Case("report", reportMain)
79 .Case("show", showMain)
80 .Cases("-h", "-help", "--help", helpMain)
Justin Bogner733013c2015-06-03 02:48:09 +000081 .Cases("-version", "--version", versionMain)
Justin Bognerd249a3b2014-10-30 20:57:49 +000082 .Default(nullptr);
Alex Lorenze82d89c2014-08-22 22:56:03 +000083
Justin Bognerd249a3b2014-10-30 20:57:49 +000084 if (Func) {
Patrik Hagglund07ccb102014-09-18 11:52:57 +000085 std::string Invocation = std::string(argv[0]) + " " + argv[1];
Alex Lorenze82d89c2014-08-22 22:56:03 +000086 argv[1] = Invocation.c_str();
Justin Bognerd249a3b2014-10-30 20:57:49 +000087 return Func(argc - 1, argv + 1);
Alex Lorenze82d89c2014-08-22 22:56:03 +000088 }
89 }
90
Justin Bogner5a07bb82015-03-24 23:34:36 +000091 if (argc > 1) {
92 if (sys::Process::StandardErrHasColors())
93 errs().changeColor(raw_ostream::RED);
94 errs() << "Unrecognized command: " << argv[1] << ".\n\n";
95 if (sys::Process::StandardErrHasColors())
96 errs().resetColor();
97 }
98 helpMain(argc, argv);
99 return 1;
Devang Patel37140652011-09-28 18:50:00 +0000100}