Yuchen Wu | 298c765 | 2013-12-05 22:02:29 +0000 | [diff] [blame] | 1 | //===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===// |
Devang Patel | d02c42b | 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 | d02c42b | 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 | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Justin Bogner | d33e677 | 2014-10-30 20:29:48 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSwitch.h" |
Justin Bogner | cbf5311 | 2015-06-03 02:48:09 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 17 | #include "llvm/Support/InitLLVM.h" |
Richard Smith | 0eeb3d4 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ManagedStatic.h" |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Justin Bogner | b9e97c7 | 2015-03-24 23:34:36 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Process.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 26 | /// The main entry point for the 'show' subcommand. |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 27 | int showMain(int argc, const char *argv[]); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 28 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 29 | /// The main entry point for the 'report' subcommand. |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 30 | int reportMain(int argc, const char *argv[]); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 31 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 32 | /// The main entry point for the 'export' subcommand. |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 33 | int exportMain(int argc, const char *argv[]); |
| 34 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 35 | /// The main entry point for the 'convert-for-testing' subcommand. |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 36 | int convertForTestingMain(int argc, const char *argv[]); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 37 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 38 | /// The main entry point for the gcov compatible coverage tool. |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 39 | int gcovMain(int argc, const char *argv[]); |
Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 40 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 41 | /// Top level help. |
Benjamin Kramer | 0df4e22 | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 42 | static int helpMain(int argc, const char *argv[]) { |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 43 | errs() << "Usage: llvm-cov {export|gcov|report|show} [OPTION]...\n\n" |
Justin Bogner | b9e97c7 | 2015-03-24 23:34:36 +0000 | [diff] [blame] | 44 | << "Shows code coverage information.\n\n" |
| 45 | << "Subcommands:\n" |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 46 | << " export: Export instrprof file to structured format.\n" |
Justin Bogner | b9e97c7 | 2015-03-24 23:34:36 +0000 | [diff] [blame] | 47 | << " gcov: Work with the gcov format.\n" |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 48 | << " report: Summarize instrprof style coverage information.\n" |
| 49 | << " show: Annotate source files using instrprof style coverage.\n"; |
| 50 | |
Justin Bogner | d33e677 | 2014-10-30 20:29:48 +0000 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 54 | /// Top level version information. |
Justin Bogner | cbf5311 | 2015-06-03 02:48:09 +0000 | [diff] [blame] | 55 | static int versionMain(int argc, const char *argv[]) { |
| 56 | cl::PrintVersionMessage(); |
| 57 | return 0; |
| 58 | } |
| 59 | |
Alex Lorenz | 35822f7 | 2014-07-28 18:03:51 +0000 | [diff] [blame] | 60 | int main(int argc, const char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 61 | InitLLVM X(argc, argv); |
Richard Smith | 0eeb3d4 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 62 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 63 | // If argv[0] is or ends with 'gcov', always be gcov compatible |
| 64 | if (sys::path::stem(argv[0]).endswith_lower("gcov")) |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 65 | return gcovMain(argc, argv); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 66 | |
| 67 | // Check if we are invoking a specific tool command. |
| 68 | if (argc > 1) { |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 69 | typedef int (*MainFunction)(int, const char *[]); |
| 70 | MainFunction Func = StringSwitch<MainFunction>(argv[1]) |
| 71 | .Case("convert-for-testing", convertForTestingMain) |
Vedant Kumar | 53397d1 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 72 | .Case("export", exportMain) |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 73 | .Case("gcov", gcovMain) |
| 74 | .Case("report", reportMain) |
| 75 | .Case("show", showMain) |
| 76 | .Cases("-h", "-help", "--help", helpMain) |
Justin Bogner | cbf5311 | 2015-06-03 02:48:09 +0000 | [diff] [blame] | 77 | .Cases("-version", "--version", versionMain) |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 78 | .Default(nullptr); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 79 | |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 80 | if (Func) { |
Patrik Hagglund | 6c0e053 | 2014-09-18 11:52:57 +0000 | [diff] [blame] | 81 | std::string Invocation = std::string(argv[0]) + " " + argv[1]; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 82 | argv[1] = Invocation.c_str(); |
Justin Bogner | 76ebe3d | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 83 | return Func(argc - 1, argv + 1); |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Justin Bogner | b9e97c7 | 2015-03-24 23:34:36 +0000 | [diff] [blame] | 87 | if (argc > 1) { |
| 88 | if (sys::Process::StandardErrHasColors()) |
| 89 | errs().changeColor(raw_ostream::RED); |
| 90 | errs() << "Unrecognized command: " << argv[1] << ".\n\n"; |
| 91 | if (sys::Process::StandardErrHasColors()) |
| 92 | errs().resetColor(); |
| 93 | } |
| 94 | helpMain(argc, argv); |
| 95 | return 1; |
Devang Patel | d02c42b | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 96 | } |