blob: 4c3b574451c35058f9bb935f1c087b184695456d [file] [log] [blame]
Yuchen Wu298c7652013-12-05 22:02:29 +00001//===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
Devang Pateld02c42b2011-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 Pateld02c42b2011-09-28 18:50:00 +000010// llvm-cov is a command line tools to analyze and report coverage information.
11//
Devang Pateld02c42b2011-09-28 18:50:00 +000012//===----------------------------------------------------------------------===//
13
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000014#include "llvm/ADT/StringRef.h"
Justin Bognerd33e6772014-10-30 20:29:48 +000015#include "llvm/ADT/StringSwitch.h"
Justin Bognercbf53112015-06-03 02:48:09 +000016#include "llvm/Support/CommandLine.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000017#include "llvm/Support/InitLLVM.h"
Richard Smith0eeb3d42016-06-09 00:53:21 +000018#include "llvm/Support/ManagedStatic.h"
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000019#include "llvm/Support/Path.h"
Justin Bognerb9e97c72015-03-24 23:34:36 +000020#include "llvm/Support/Process.h"
Chandler Carruth1b279142015-01-14 11:23:27 +000021#include "llvm/Support/raw_ostream.h"
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000022#include <string>
23
24using namespace llvm;
25
Adrian Prantl26b584c2018-05-01 15:54:18 +000026/// The main entry point for the 'show' subcommand.
Justin Bogner76ebe3d2014-10-30 20:57:49 +000027int showMain(int argc, const char *argv[]);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000028
Adrian Prantl26b584c2018-05-01 15:54:18 +000029/// The main entry point for the 'report' subcommand.
Justin Bogner76ebe3d2014-10-30 20:57:49 +000030int reportMain(int argc, const char *argv[]);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000031
Adrian Prantl26b584c2018-05-01 15:54:18 +000032/// The main entry point for the 'export' subcommand.
Vedant Kumar53397d12016-07-26 22:50:58 +000033int exportMain(int argc, const char *argv[]);
34
Adrian Prantl26b584c2018-05-01 15:54:18 +000035/// The main entry point for the 'convert-for-testing' subcommand.
Justin Bogner76ebe3d2014-10-30 20:57:49 +000036int convertForTestingMain(int argc, const char *argv[]);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000037
Adrian Prantl26b584c2018-05-01 15:54:18 +000038/// The main entry point for the gcov compatible coverage tool.
Justin Bogner76ebe3d2014-10-30 20:57:49 +000039int gcovMain(int argc, const char *argv[]);
Devang Pateld02c42b2011-09-28 18:50:00 +000040
Adrian Prantl26b584c2018-05-01 15:54:18 +000041/// Top level help.
Benjamin Kramer0df4e222015-03-09 16:23:46 +000042static int helpMain(int argc, const char *argv[]) {
Vedant Kumar53397d12016-07-26 22:50:58 +000043 errs() << "Usage: llvm-cov {export|gcov|report|show} [OPTION]...\n\n"
Justin Bognerb9e97c72015-03-24 23:34:36 +000044 << "Shows code coverage information.\n\n"
45 << "Subcommands:\n"
Vedant Kumar53397d12016-07-26 22:50:58 +000046 << " export: Export instrprof file to structured format.\n"
Justin Bognerb9e97c72015-03-24 23:34:36 +000047 << " gcov: Work with the gcov format.\n"
Vedant Kumar53397d12016-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 Bognerd33e6772014-10-30 20:29:48 +000051 return 0;
52}
53
Adrian Prantl26b584c2018-05-01 15:54:18 +000054/// Top level version information.
Justin Bognercbf53112015-06-03 02:48:09 +000055static int versionMain(int argc, const char *argv[]) {
56 cl::PrintVersionMessage();
57 return 0;
58}
59
Alex Lorenz35822f72014-07-28 18:03:51 +000060int main(int argc, const char **argv) {
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000061 InitLLVM X(argc, argv);
Richard Smith0eeb3d42016-06-09 00:53:21 +000062
Alex Lorenz6c7a6a12014-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 Bogner76ebe3d2014-10-30 20:57:49 +000065 return gcovMain(argc, argv);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000066
67 // Check if we are invoking a specific tool command.
68 if (argc > 1) {
Justin Bogner76ebe3d2014-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 Kumar53397d12016-07-26 22:50:58 +000072 .Case("export", exportMain)
Justin Bogner76ebe3d2014-10-30 20:57:49 +000073 .Case("gcov", gcovMain)
74 .Case("report", reportMain)
75 .Case("show", showMain)
76 .Cases("-h", "-help", "--help", helpMain)
Justin Bognercbf53112015-06-03 02:48:09 +000077 .Cases("-version", "--version", versionMain)
Justin Bogner76ebe3d2014-10-30 20:57:49 +000078 .Default(nullptr);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000079
Justin Bogner76ebe3d2014-10-30 20:57:49 +000080 if (Func) {
Patrik Hagglund6c0e0532014-09-18 11:52:57 +000081 std::string Invocation = std::string(argv[0]) + " " + argv[1];
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000082 argv[1] = Invocation.c_str();
Justin Bogner76ebe3d2014-10-30 20:57:49 +000083 return Func(argc - 1, argv + 1);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000084 }
85 }
86
Justin Bognerb9e97c72015-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 Pateld02c42b2011-09-28 18:50:00 +000096}