1 //===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // llvm-cov is a command line tools to analyze and report coverage information.
12 //===----------------------------------------------------------------------===//
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"
27 /// \brief The main entry point for the 'show' subcommand.
28 int showMain(int argc, const char *argv[]);
30 /// \brief The main entry point for the 'report' subcommand.
31 int reportMain(int argc, const char *argv[]);
33 /// \brief The main entry point for the 'export' subcommand.
34 int exportMain(int argc, const char *argv[]);
36 /// \brief The main entry point for the 'convert-for-testing' subcommand.
37 int convertForTestingMain(int argc, const char *argv[]);
39 /// \brief The main entry point for the gcov compatible coverage tool.
40 int gcovMain(int argc, const char *argv[]);
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"
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";
55 /// \brief Top level version information.
56 static int versionMain(int argc, const char *argv[]) {
57 cl::PrintVersionMessage();
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.
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);
71 // Check if we are invoking a specific tool command.
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)
85 std::string Invocation = std::string(argv[0]) + " " + argv[1];
86 argv[1] = Invocation.c_str();
87 return Func(argc - 1, argv + 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())