blob: 4c3b574451c35058f9bb935f1c087b184695456d [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"
Rui Ueyama197194b2018-04-13 18:26:06 +000017#include "llvm/Support/InitLLVM.h"
Richard Smith2ad6d482016-06-09 00:53:21 +000018#include "llvm/Support/ManagedStatic.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000019#include "llvm/Support/Path.h"
Justin Bogner5a07bb82015-03-24 23:34:36 +000020#include "llvm/Support/Process.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000021#include "llvm/Support/raw_ostream.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000022#include <string>
23
24using namespace llvm;
25
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000026/// The main entry point for the 'show' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000027int showMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000028
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000029/// The main entry point for the 'report' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000030int reportMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000031
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000032/// The main entry point for the 'export' subcommand.
Vedant Kumar7101d732016-07-26 22:50:58 +000033int exportMain(int argc, const char *argv[]);
34
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000035/// The main entry point for the 'convert-for-testing' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000036int convertForTestingMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000037
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000038/// The main entry point for the gcov compatible coverage tool.
Justin Bognerd249a3b2014-10-30 20:57:49 +000039int gcovMain(int argc, const char *argv[]);
Devang Patel37140652011-09-28 18:50:00 +000040
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000041/// Top level help.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000042static int helpMain(int argc, const char *argv[]) {
Vedant Kumar7101d732016-07-26 22:50:58 +000043 errs() << "Usage: llvm-cov {export|gcov|report|show} [OPTION]...\n\n"
Justin Bogner5a07bb82015-03-24 23:34:36 +000044 << "Shows code coverage information.\n\n"
45 << "Subcommands:\n"
Vedant Kumar7101d732016-07-26 22:50:58 +000046 << " export: Export instrprof file to structured format.\n"
Justin Bogner5a07bb82015-03-24 23:34:36 +000047 << " gcov: Work with the gcov format.\n"
Vedant Kumar7101d732016-07-26 22:50:58 +000048 << " report: Summarize instrprof style coverage information.\n"
49 << " show: Annotate source files using instrprof style coverage.\n";
50
Justin Bogner533e2612014-10-30 20:29:48 +000051 return 0;
52}
53
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000054/// Top level version information.
Justin Bogner733013c2015-06-03 02:48:09 +000055static int versionMain(int argc, const char *argv[]) {
56 cl::PrintVersionMessage();
57 return 0;
58}
59
Alex Lorenz2b5d03a2014-07-28 18:03:51 +000060int main(int argc, const char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +000061 InitLLVM X(argc, argv);
Richard Smith2ad6d482016-06-09 00:53:21 +000062
Alex Lorenze82d89c2014-08-22 22:56:03 +000063 // If argv[0] is or ends with 'gcov', always be gcov compatible
64 if (sys::path::stem(argv[0]).endswith_lower("gcov"))
Justin Bognerd249a3b2014-10-30 20:57:49 +000065 return gcovMain(argc, argv);
Alex Lorenze82d89c2014-08-22 22:56:03 +000066
67 // Check if we are invoking a specific tool command.
68 if (argc > 1) {
Justin Bognerd249a3b2014-10-30 20:57:49 +000069 typedef int (*MainFunction)(int, const char *[]);
70 MainFunction Func = StringSwitch<MainFunction>(argv[1])
71 .Case("convert-for-testing", convertForTestingMain)
Vedant Kumar7101d732016-07-26 22:50:58 +000072 .Case("export", exportMain)
Justin Bognerd249a3b2014-10-30 20:57:49 +000073 .Case("gcov", gcovMain)
74 .Case("report", reportMain)
75 .Case("show", showMain)
76 .Cases("-h", "-help", "--help", helpMain)
Justin Bogner733013c2015-06-03 02:48:09 +000077 .Cases("-version", "--version", versionMain)
Justin Bognerd249a3b2014-10-30 20:57:49 +000078 .Default(nullptr);
Alex Lorenze82d89c2014-08-22 22:56:03 +000079
Justin Bognerd249a3b2014-10-30 20:57:49 +000080 if (Func) {
Patrik Hagglund07ccb102014-09-18 11:52:57 +000081 std::string Invocation = std::string(argv[0]) + " " + argv[1];
Alex Lorenze82d89c2014-08-22 22:56:03 +000082 argv[1] = Invocation.c_str();
Justin Bognerd249a3b2014-10-30 20:57:49 +000083 return Func(argc - 1, argv + 1);
Alex Lorenze82d89c2014-08-22 22:56:03 +000084 }
85 }
86
Justin Bogner5a07bb82015-03-24 23:34:36 +000087 if (argc > 1) {
88 if (sys::Process::StandardErrHasColors())
89 errs().changeColor(raw_ostream::RED);
90 errs() << "Unrecognized command: " << argv[1] << ".\n\n";
91 if (sys::Process::StandardErrHasColors())
92 errs().resetColor();
93 }
94 helpMain(argc, argv);
95 return 1;
Devang Patel37140652011-09-28 18:50:00 +000096}