Yuchen Wu | c3e6424 | 2013-12-05 22:02:29 +0000 | [diff] [blame] | 1 | //===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===// |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 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 | // |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 10 | // llvm-cov is a command line tools to analyze and report coverage information. |
| 11 | // |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Justin Bogner | 533e261 | 2014-10-30 20:29:48 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSwitch.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Path.h" |
Justin Bogner | 5a07bb8 | 2015-03-24 23:34:36 +0000 | [diff] [blame^] | 17 | #include "llvm/Support/Process.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 19 | #include <string> |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | /// \brief The main entry point for the 'show' subcommand. |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 24 | int showMain(int argc, const char *argv[]); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 25 | |
| 26 | /// \brief The main entry point for the 'report' subcommand. |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 27 | int reportMain(int argc, const char *argv[]); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 28 | |
| 29 | /// \brief The main entry point for the 'convert-for-testing' subcommand. |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 30 | int convertForTestingMain(int argc, const char *argv[]); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 31 | |
| 32 | /// \brief The main entry point for the gcov compatible coverage tool. |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 33 | int gcovMain(int argc, const char *argv[]); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 34 | |
Justin Bogner | 533e261 | 2014-10-30 20:29:48 +0000 | [diff] [blame] | 35 | /// \brief Top level help. |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 36 | static int helpMain(int argc, const char *argv[]) { |
Justin Bogner | 5a07bb8 | 2015-03-24 23:34:36 +0000 | [diff] [blame^] | 37 | errs() << "Usage: llvm-cov {gcov|report|show} [OPTION]...\n\n" |
| 38 | << "Shows code coverage information.\n\n" |
| 39 | << "Subcommands:\n" |
| 40 | << " gcov: Work with the gcov format.\n" |
| 41 | << " show: Annotate source files using instrprof style coverage.\n" |
| 42 | << " report: Summarize instrprof style coverage information.\n"; |
Justin Bogner | 533e261 | 2014-10-30 20:29:48 +0000 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
Alex Lorenz | 2b5d03a | 2014-07-28 18:03:51 +0000 | [diff] [blame] | 46 | int main(int argc, const char **argv) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 47 | // If argv[0] is or ends with 'gcov', always be gcov compatible |
| 48 | if (sys::path::stem(argv[0]).endswith_lower("gcov")) |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 49 | return gcovMain(argc, argv); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 50 | |
| 51 | // Check if we are invoking a specific tool command. |
| 52 | if (argc > 1) { |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 53 | typedef int (*MainFunction)(int, const char *[]); |
| 54 | MainFunction Func = StringSwitch<MainFunction>(argv[1]) |
| 55 | .Case("convert-for-testing", convertForTestingMain) |
| 56 | .Case("gcov", gcovMain) |
| 57 | .Case("report", reportMain) |
| 58 | .Case("show", showMain) |
| 59 | .Cases("-h", "-help", "--help", helpMain) |
| 60 | .Default(nullptr); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 61 | |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 62 | if (Func) { |
Patrik Hagglund | 07ccb10 | 2014-09-18 11:52:57 +0000 | [diff] [blame] | 63 | std::string Invocation = std::string(argv[0]) + " " + argv[1]; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 64 | argv[1] = Invocation.c_str(); |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 65 | return Func(argc - 1, argv + 1); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Justin Bogner | 5a07bb8 | 2015-03-24 23:34:36 +0000 | [diff] [blame^] | 69 | if (argc > 1) { |
| 70 | if (sys::Process::StandardErrHasColors()) |
| 71 | errs().changeColor(raw_ostream::RED); |
| 72 | errs() << "Unrecognized command: " << argv[1] << ".\n\n"; |
| 73 | if (sys::Process::StandardErrHasColors()) |
| 74 | errs().resetColor(); |
| 75 | } |
| 76 | helpMain(argc, argv); |
| 77 | return 1; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 78 | } |