blob: 3b7cffc8413fc746be8619c229f5617e09166caa [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"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/Support/raw_ostream.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000018#include <string>
19
20using namespace llvm;
21
22/// \brief The main entry point for the 'show' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000023int showMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000024
25/// \brief The main entry point for the 'report' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000026int reportMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000027
28/// \brief The main entry point for the 'convert-for-testing' subcommand.
Justin Bognerd249a3b2014-10-30 20:57:49 +000029int convertForTestingMain(int argc, const char *argv[]);
Alex Lorenze82d89c2014-08-22 22:56:03 +000030
31/// \brief The main entry point for the gcov compatible coverage tool.
Justin Bognerd249a3b2014-10-30 20:57:49 +000032int gcovMain(int argc, const char *argv[]);
Devang Patel37140652011-09-28 18:50:00 +000033
Justin Bogner533e2612014-10-30 20:29:48 +000034/// \brief Top level help.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000035static int helpMain(int argc, const char *argv[]) {
Justin Bogner533e2612014-10-30 20:29:48 +000036 errs() << "OVERVIEW: LLVM code coverage tool\n\n"
37 << "USAGE: llvm-cov {gcov|report|show}\n";
38 return 0;
39}
40
Alex Lorenz2b5d03a2014-07-28 18:03:51 +000041int main(int argc, const char **argv) {
Alex Lorenze82d89c2014-08-22 22:56:03 +000042 // If argv[0] is or ends with 'gcov', always be gcov compatible
43 if (sys::path::stem(argv[0]).endswith_lower("gcov"))
Justin Bognerd249a3b2014-10-30 20:57:49 +000044 return gcovMain(argc, argv);
Alex Lorenze82d89c2014-08-22 22:56:03 +000045
46 // Check if we are invoking a specific tool command.
47 if (argc > 1) {
Justin Bognerd249a3b2014-10-30 20:57:49 +000048 typedef int (*MainFunction)(int, const char *[]);
49 MainFunction Func = StringSwitch<MainFunction>(argv[1])
50 .Case("convert-for-testing", convertForTestingMain)
51 .Case("gcov", gcovMain)
52 .Case("report", reportMain)
53 .Case("show", showMain)
54 .Cases("-h", "-help", "--help", helpMain)
55 .Default(nullptr);
Alex Lorenze82d89c2014-08-22 22:56:03 +000056
Justin Bognerd249a3b2014-10-30 20:57:49 +000057 if (Func) {
Patrik Hagglund07ccb102014-09-18 11:52:57 +000058 std::string Invocation = std::string(argv[0]) + " " + argv[1];
Alex Lorenze82d89c2014-08-22 22:56:03 +000059 argv[1] = Invocation.c_str();
Justin Bognerd249a3b2014-10-30 20:57:49 +000060 return Func(argc - 1, argv + 1);
Alex Lorenze82d89c2014-08-22 22:56:03 +000061 }
62 }
63
64 // Give a warning and fall back to gcov
65 errs().changeColor(raw_ostream::RED);
66 errs() << "warning:";
67 // Assume that argv[1] wasn't a command when it stats with a '-' or is a
68 // filename (i.e. contains a '.')
69 if (argc > 1 && !StringRef(argv[1]).startswith("-") &&
70 StringRef(argv[1]).find(".") == StringRef::npos)
71 errs() << " Unrecognized command '" << argv[1] << "'.";
72 errs() << " Using the gcov compatible mode "
73 "(this behaviour may be dropped in the future).";
74 errs().resetColor();
75 errs() << "\n";
76
Justin Bognerd249a3b2014-10-30 20:57:49 +000077 return gcovMain(argc, argv);
Devang Patel37140652011-09-28 18:50:00 +000078}