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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 9 | // llvm-cov is a command line tools to analyze and report coverage information. |
| 10 | // |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringRef.h" |
Justin Bogner | 533e261 | 2014-10-30 20:29:48 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringSwitch.h" |
Justin Bogner | 733013c | 2015-06-03 02:48:09 +0000 | [diff] [blame] | 15 | #include "llvm/Support/CommandLine.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 16 | #include "llvm/Support/InitLLVM.h" |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ManagedStatic.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Justin Bogner | 5a07bb8 | 2015-03-24 23:34:36 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Process.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 21 | #include <string> |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 25 | /// The main entry point for the 'show' subcommand. |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 26 | int showMain(int argc, const char *argv[]); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 27 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 28 | /// The main entry point for the 'report' subcommand. |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 29 | int reportMain(int argc, const char *argv[]); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 30 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 31 | /// The main entry point for the 'export' subcommand. |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 32 | int exportMain(int argc, const char *argv[]); |
| 33 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 34 | /// The main entry point for the 'convert-for-testing' subcommand. |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 35 | int convertForTestingMain(int argc, const char *argv[]); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 36 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 37 | /// The main entry point for the gcov compatible coverage tool. |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 38 | int gcovMain(int argc, const char *argv[]); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 39 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 40 | /// Top level help. |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 41 | static int helpMain(int argc, const char *argv[]) { |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 42 | errs() << "Usage: llvm-cov {export|gcov|report|show} [OPTION]...\n\n" |
Justin Bogner | 5a07bb8 | 2015-03-24 23:34:36 +0000 | [diff] [blame] | 43 | << "Shows code coverage information.\n\n" |
| 44 | << "Subcommands:\n" |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 45 | << " export: Export instrprof file to structured format.\n" |
Justin Bogner | 5a07bb8 | 2015-03-24 23:34:36 +0000 | [diff] [blame] | 46 | << " gcov: Work with the gcov format.\n" |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 47 | << " report: Summarize instrprof style coverage information.\n" |
| 48 | << " show: Annotate source files using instrprof style coverage.\n"; |
| 49 | |
Justin Bogner | 533e261 | 2014-10-30 20:29:48 +0000 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 53 | /// Top level version information. |
Justin Bogner | 733013c | 2015-06-03 02:48:09 +0000 | [diff] [blame] | 54 | static int versionMain(int argc, const char *argv[]) { |
| 55 | cl::PrintVersionMessage(); |
| 56 | return 0; |
| 57 | } |
| 58 | |
Alex Lorenz | 2b5d03a | 2014-07-28 18:03:51 +0000 | [diff] [blame] | 59 | int main(int argc, const char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 60 | InitLLVM X(argc, argv); |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 61 | |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 62 | // If argv[0] is or ends with 'gcov', always be gcov compatible |
| 63 | if (sys::path::stem(argv[0]).endswith_lower("gcov")) |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 64 | return gcovMain(argc, argv); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 65 | |
| 66 | // Check if we are invoking a specific tool command. |
| 67 | if (argc > 1) { |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 68 | typedef int (*MainFunction)(int, const char *[]); |
| 69 | MainFunction Func = StringSwitch<MainFunction>(argv[1]) |
| 70 | .Case("convert-for-testing", convertForTestingMain) |
Vedant Kumar | 7101d73 | 2016-07-26 22:50:58 +0000 | [diff] [blame] | 71 | .Case("export", exportMain) |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 72 | .Case("gcov", gcovMain) |
| 73 | .Case("report", reportMain) |
| 74 | .Case("show", showMain) |
| 75 | .Cases("-h", "-help", "--help", helpMain) |
Justin Bogner | 733013c | 2015-06-03 02:48:09 +0000 | [diff] [blame] | 76 | .Cases("-version", "--version", versionMain) |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 77 | .Default(nullptr); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 78 | |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 79 | if (Func) { |
Patrik Hagglund | 07ccb10 | 2014-09-18 11:52:57 +0000 | [diff] [blame] | 80 | std::string Invocation = std::string(argv[0]) + " " + argv[1]; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 81 | argv[1] = Invocation.c_str(); |
Justin Bogner | d249a3b | 2014-10-30 20:57:49 +0000 | [diff] [blame] | 82 | return Func(argc - 1, argv + 1); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Rui Ueyama | 4d41c33 | 2019-08-02 07:22:34 +0000 | [diff] [blame] | 86 | if (argc > 1) { |
| 87 | if (sys::Process::StandardErrHasColors()) |
| 88 | errs().changeColor(raw_ostream::RED); |
| 89 | errs() << "Unrecognized command: " << argv[1] << ".\n\n"; |
| 90 | if (sys::Process::StandardErrHasColors()) |
| 91 | errs().resetColor(); |
| 92 | } |
Justin Bogner | 5a07bb8 | 2015-03-24 23:34:36 +0000 | [diff] [blame] | 93 | helpMain(argc, argv); |
| 94 | return 1; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 95 | } |