1 //===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // llvm-cov is a command line tools to analyze and report coverage information.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/InitLLVM.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/Process.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <string>
21
22 using namespace llvm;
23
24 /// The main entry point for the 'show' subcommand.
25 int showMain(int argc, const char *argv[]);
26
27 /// The main entry point for the 'report' subcommand.
28 int reportMain(int argc, const char *argv[]);
29
30 /// The main entry point for the 'export' subcommand.
31 int exportMain(int argc, const char *argv[]);
32
33 /// The main entry point for the 'convert-for-testing' subcommand.
34 int convertForTestingMain(int argc, const char *argv[]);
35
36 /// The main entry point for the gcov compatible coverage tool.
37 int gcovMain(int argc, const char *argv[]);
38
39 /// Top level help.
helpMain(int argc,const char * argv[])40 static int helpMain(int argc, const char *argv[]) {
41 errs() << "Usage: llvm-cov {export|gcov|report|show} [OPTION]...\n\n"
42 << "Shows code coverage information.\n\n"
43 << "Subcommands:\n"
44 << " export: Export instrprof file to structured format.\n"
45 << " gcov: Work with the gcov format.\n"
46 << " report: Summarize instrprof style coverage information.\n"
47 << " show: Annotate source files using instrprof style coverage.\n";
48
49 return 0;
50 }
51
52 /// Top level version information.
versionMain(int argc,const char * argv[])53 static int versionMain(int argc, const char *argv[]) {
54 cl::PrintVersionMessage();
55 return 0;
56 }
57
main(int argc,const char ** argv)58 int main(int argc, const char **argv) {
59 InitLLVM X(argc, argv);
60
61 // If argv[0] is or ends with 'gcov', always be gcov compatible
62 if (sys::path::stem(argv[0]).ends_with_insensitive("gcov"))
63 return gcovMain(argc, argv);
64
65 // Check if we are invoking a specific tool command.
66 if (argc > 1) {
67 typedef int (*MainFunction)(int, const char *[]);
68 MainFunction Func = StringSwitch<MainFunction>(argv[1])
69 .Case("convert-for-testing", convertForTestingMain)
70 .Case("export", exportMain)
71 .Case("gcov", gcovMain)
72 .Case("report", reportMain)
73 .Case("show", showMain)
74 .Cases("-h", "-help", "--help", helpMain)
75 .Cases("-version", "--version", versionMain)
76 .Default(nullptr);
77
78 if (Func) {
79 std::string Invocation = std::string(argv[0]) + " " + argv[1];
80 argv[1] = Invocation.c_str();
81 return Func(argc - 1, argv + 1);
82 }
83 }
84
85 if (argc > 1) {
86 if (sys::Process::StandardErrHasColors())
87 errs().changeColor(raw_ostream::RED);
88 errs() << "Unrecognized command: " << argv[1] << ".\n\n";
89 if (sys::Process::StandardErrHasColors())
90 errs().resetColor();
91 }
92 helpMain(argc, argv);
93 return 1;
94 }
95