132c187ef9eb72f5291ac2656fb878bd96cd5151
[oota-llvm.git] / 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/raw_ostream.h"
17 #include "llvm/Support/Path.h"
18 #include <string>
19
20 using namespace llvm;
21
22 /// \brief The main entry point for the 'show' subcommand.
23 int show_main(int argc, const char **argv);
24
25 /// \brief The main entry point for the 'report' subcommand.
26 int report_main(int argc, const char **argv);
27
28 /// \brief The main entry point for the 'convert-for-testing' subcommand.
29 int convert_for_testing_main(int argc, const char **argv);
30
31 /// \brief The main entry point for the gcov compatible coverage tool.
32 int gcov_main(int argc, const char **argv);
33
34 /// \brief Top level help.
35 int help_main(int argc, const char **argv) {
36   errs() << "OVERVIEW: LLVM code coverage tool\n\n"
37          << "USAGE: llvm-cov {gcov|report|show}\n";
38   return 0;
39 }
40
41 int main(int argc, const char **argv) {
42   // If argv[0] is or ends with 'gcov', always be gcov compatible
43   if (sys::path::stem(argv[0]).endswith_lower("gcov"))
44     return gcov_main(argc, argv);
45
46   // Check if we are invoking a specific tool command.
47   if (argc > 1) {
48     typedef int (*MainFunction)(int, const char **);
49     MainFunction func =
50         StringSwitch<MainFunction>(argv[1])
51             .Case("convert-for-testing", convert_for_testing_main)
52             .Case("gcov", gcov_main)
53             .Case("report", report_main)
54             .Case("show", show_main)
55             .Cases("-h", "-help", "--help", help_main)
56             .Default(nullptr);
57
58     if (func) {
59       std::string Invocation = std::string(argv[0]) + " " + argv[1];
60       argv[1] = Invocation.c_str();
61       return func(argc - 1, argv + 1);
62     }
63   }
64
65   // Give a warning and fall back to gcov
66   errs().changeColor(raw_ostream::RED);
67   errs() << "warning:";
68   // Assume that argv[1] wasn't a command when it stats with a '-' or is a
69   // filename (i.e. contains a '.')
70   if (argc > 1 && !StringRef(argv[1]).startswith("-") &&
71       StringRef(argv[1]).find(".") == StringRef::npos)
72     errs() << " Unrecognized command '" << argv[1] << "'.";
73   errs() << " Using the gcov compatible mode "
74             "(this behaviour may be dropped in the future).";
75   errs().resetColor();
76   errs() << "\n";
77
78   return gcov_main(argc, argv);
79 }