blob: 172ec9f3cedffffdfa49bc1c7906651726fc3937 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Devang Patel37140652011-09-28 18:50:00 +00006//
7//===----------------------------------------------------------------------===//
8//
Devang Patel37140652011-09-28 18:50:00 +00009// llvm-cov is a command line tools to analyze and report coverage information.
10//
Devang Patel37140652011-09-28 18:50:00 +000011//===----------------------------------------------------------------------===//
12
Alex Lorenze82d89c2014-08-22 22:56:03 +000013#include "llvm/ADT/StringRef.h"
Justin Bogner533e2612014-10-30 20:29:48 +000014#include "llvm/ADT/StringSwitch.h"
Justin Bogner733013c2015-06-03 02:48:09 +000015#include "llvm/Support/CommandLine.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000016#include "llvm/Support/InitLLVM.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"
Justin Bogner5a07bb82015-03-24 23:34:36 +000019#include "llvm/Support/Process.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/Support/raw_ostream.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000021#include <string>
22
23using namespace llvm;
24
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000025/// The main entry point for the 'show' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000026int showMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000027
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000028/// The main entry point for the 'report' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000029int reportMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000030
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000031/// The main entry point for the 'export' subcommand.
Vedant Kumar7101d732016-07-26 22:50:58 +000032int exportMain(int argc, const char *argv[]);
33
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000034/// The main entry point for the 'convert-for-testing' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000035int convertForTestingMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000036
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000037/// The main entry point for the gcov compatible coverage tool.
Justin Bognerd249a3b2014-10-30 20:57:49 +000038int gcovMain(int argc, const char *argv[]);
Devang Patel37140652011-09-28 18:50:00 +000039
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000040/// Top level help.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000041static int helpMain(int argc, const char *argv[]) {
Vedant Kumar7101d732016-07-26 22:50:58 +000042 errs() << "Usage: llvm-cov {export|gcov|report|show} [OPTION]...\n\n"
Justin Bogner5a07bb82015-03-24 23:34:36 +000043 << "Shows code coverage information.\n\n"
44 << "Subcommands:\n"
Vedant Kumar7101d732016-07-26 22:50:58 +000045 << " export: Export instrprof file to structured format.\n"
Justin Bogner5a07bb82015-03-24 23:34:36 +000046 << " gcov: Work with the gcov format.\n"
Vedant Kumar7101d732016-07-26 22:50:58 +000047 << " report: Summarize instrprof style coverage information.\n"
48 << " show: Annotate source files using instrprof style coverage.\n";
49
Justin Bogner533e2612014-10-30 20:29:48 +000050 return 0;
51}
52
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000053/// Top level version information.
Justin Bogner733013c2015-06-03 02:48:09 +000054static int versionMain(int argc, const char *argv[]) {
55 cl::PrintVersionMessage();
56 return 0;
57}
58
Alex Lorenz2b5d03a2014-07-28 18:03:51 +000059int main(int argc, const char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +000060 InitLLVM X(argc, argv);
Richard Smith2ad6d482016-06-09 00:53:21 +000061
Alex Lorenze82d89c2014-08-22 22:56:03 +000062 // If argv[0] is or ends with 'gcov', always be gcov compatible
63 if (sys::path::stem(argv[0]).endswith_lower("gcov"))
Justin Bognerd249a3b2014-10-30 20:57:49 +000064 return gcovMain(argc, argv);
Alex Lorenze82d89c2014-08-22 22:56:03 +000065
66 // Check if we are invoking a specific tool command.
67 if (argc > 1) {
Justin Bognerd249a3b2014-10-30 20:57:49 +000068 typedef int (*MainFunction)(int, const char *[]);
69 MainFunction Func = StringSwitch<MainFunction>(argv[1])
70 .Case("convert-for-testing", convertForTestingMain)
Vedant Kumar7101d732016-07-26 22:50:58 +000071 .Case("export", exportMain)
Justin Bognerd249a3b2014-10-30 20:57:49 +000072 .Case("gcov", gcovMain)
73 .Case("report", reportMain)
74 .Case("show", showMain)
75 .Cases("-h", "-help", "--help", helpMain)
Justin Bogner733013c2015-06-03 02:48:09 +000076 .Cases("-version", "--version", versionMain)
Justin Bognerd249a3b2014-10-30 20:57:49 +000077 .Default(nullptr);
Alex Lorenze82d89c2014-08-22 22:56:03 +000078
Justin Bognerd249a3b2014-10-30 20:57:49 +000079 if (Func) {
Patrik Hagglund07ccb102014-09-18 11:52:57 +000080 std::string Invocation = std::string(argv[0]) + " " + argv[1];
Alex Lorenze82d89c2014-08-22 22:56:03 +000081 argv[1] = Invocation.c_str();
Justin Bognerd249a3b2014-10-30 20:57:49 +000082 return Func(argc - 1, argv + 1);
Alex Lorenze82d89c2014-08-22 22:56:03 +000083 }
84 }
85
Rui Ueyama4d41c332019-08-02 07:22:34 +000086 if (argc > 1) {
87 if (sys::Process::StandardErrHasColors())
88 errs().changeColor(raw_ostream::RED);
89 errs() << "Unrecognized command: " << argv[1] << ".\n\n";
90 if (sys::Process::StandardErrHasColors())
91 errs().resetColor();
92 }
Justin Bogner5a07bb82015-03-24 23:34:36 +000093 helpMain(argc, argv);
94 return 1;
Devang Patel37140652011-09-28 18:50:00 +000095}