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