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