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