blob: bf66f583a5282eea59091e482cc0ff076ceb5688 [file] [log] [blame]
Yuchen Wuc3e64242013-12-05 22:02:29 +00001//===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +00002//
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 Patel37140652011-09-28 18:50:00 +000010// llvm-cov is a command line tools to analyze and report coverage information.
11//
Devang Patel37140652011-09-28 18:50:00 +000012//===----------------------------------------------------------------------===//
13
Alex Lorenze82d89c2014-08-22 22:56:03 +000014#include "llvm/ADT/StringRef.h"
Justin Bogner533e2612014-10-30 20:29:48 +000015#include "llvm/ADT/StringSwitch.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000016#include "llvm/Support/Path.h"
Justin Bogner5a07bb82015-03-24 23:34:36 +000017#include "llvm/Support/Process.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/Support/raw_ostream.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000019#include <string>
20
21using namespace llvm;
22
23/// \brief The main entry point for the 'show' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000024int showMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000025
26/// \brief The main entry point for the 'report' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000027int reportMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000028
29/// \brief The main entry point for the 'convert-for-testing' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000030int convertForTestingMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000031
32/// \brief The main entry point for the gcov compatible coverage tool.
Justin Bognerd249a3b2014-10-30 20:57:49 +000033int gcovMain(int argc, const char *argv[]);
Devang Patel37140652011-09-28 18:50:00 +000034
Justin Bogner533e2612014-10-30 20:29:48 +000035/// \brief Top level help.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000036static int helpMain(int argc, const char *argv[]) {
Justin Bogner5a07bb82015-03-24 23:34:36 +000037 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 Bogner533e2612014-10-30 20:29:48 +000043 return 0;
44}
45
Alex Lorenz2b5d03a2014-07-28 18:03:51 +000046int main(int argc, const char **argv) {
Alex Lorenze82d89c2014-08-22 22:56:03 +000047 // If argv[0] is or ends with 'gcov', always be gcov compatible
48 if (sys::path::stem(argv[0]).endswith_lower("gcov"))
Justin Bognerd249a3b2014-10-30 20:57:49 +000049 return gcovMain(argc, argv);
Alex Lorenze82d89c2014-08-22 22:56:03 +000050
51 // Check if we are invoking a specific tool command.
52 if (argc > 1) {
Justin Bognerd249a3b2014-10-30 20:57:49 +000053 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 Lorenze82d89c2014-08-22 22:56:03 +000061
Justin Bognerd249a3b2014-10-30 20:57:49 +000062 if (Func) {
Patrik Hagglund07ccb102014-09-18 11:52:57 +000063 std::string Invocation = std::string(argv[0]) + " " + argv[1];
Alex Lorenze82d89c2014-08-22 22:56:03 +000064 argv[1] = Invocation.c_str();
Justin Bognerd249a3b2014-10-30 20:57:49 +000065 return Func(argc - 1, argv + 1);
Alex Lorenze82d89c2014-08-22 22:56:03 +000066 }
67 }
68
Justin Bogner5a07bb82015-03-24 23:34:36 +000069 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 Patel37140652011-09-28 18:50:00 +000078}