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
22using namespace llvm;
23
24/// The main entry point for the 'show' subcommand.
25int showMain(int argc, const char *argv[]);
26
27/// The main entry point for the 'report' subcommand.
28int reportMain(int argc, const char *argv[]);
29
30/// The main entry point for the 'export' subcommand.
31int exportMain(int argc, const char *argv[]);
32
33/// The main entry point for the 'convert-for-testing' subcommand.
34int convertForTestingMain(int argc, const char *argv[]);
35
36/// The main entry point for the gcov compatible coverage tool.
37int gcovMain(int argc, const char *argv[]);
38
39/// Top level help.
40static 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.
53static int versionMain(int argc, const char *argv[]) {
54 cl::PrintVersionMessage();
55 return 0;
56}
57
58int 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(path: argv[0]).ends_with_insensitive(Suffix: "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(S: "convert-for-testing", Value: convertForTestingMain)
70 .Case(S: "export", Value: exportMain)
71 .Case(S: "gcov", Value: gcovMain)
72 .Case(S: "report", Value: reportMain)
73 .Case(S: "show", Value: showMain)
74 .Cases(S0: "-h", S1: "-help", S2: "--help", Value: helpMain)
75 .Cases(S0: "-version", S1: "--version", Value: versionMain)
76 .Default(Value: 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(Color: 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

source code of llvm/tools/llvm-cov/llvm-cov.cpp