llvm-cov.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===- llvm-cov.cpp - LLVM coverage tool ----------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // llvm-cov is a command line tools to analyze and report coverage information.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/ADT/StringSwitch.h"
  14. #include "llvm/Support/CommandLine.h"
  15. #include "llvm/Support/InitLLVM.h"
  16. #include "llvm/Support/Path.h"
  17. #include "llvm/Support/Process.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include <string>
  20. using namespace llvm;
  21. /// The main entry point for the 'show' subcommand.
  22. int showMain(int argc, const char *argv[]);
  23. /// The main entry point for the 'report' subcommand.
  24. int reportMain(int argc, const char *argv[]);
  25. /// The main entry point for the 'export' subcommand.
  26. int exportMain(int argc, const char *argv[]);
  27. /// The main entry point for the 'convert-for-testing' subcommand.
  28. int convertForTestingMain(int argc, const char *argv[]);
  29. /// The main entry point for the gcov compatible coverage tool.
  30. int gcovMain(int argc, const char *argv[]);
  31. /// Top level help.
  32. static int helpMain(int argc, const char *argv[]) {
  33. errs() << "Usage: llvm-cov {export|gcov|report|show} [OPTION]...\n\n"
  34. << "Shows code coverage information.\n\n"
  35. << "Subcommands:\n"
  36. << " export: Export instrprof file to structured format.\n"
  37. << " gcov: Work with the gcov format.\n"
  38. << " report: Summarize instrprof style coverage information.\n"
  39. << " show: Annotate source files using instrprof style coverage.\n";
  40. return 0;
  41. }
  42. /// Top level version information.
  43. static int versionMain(int argc, const char *argv[]) {
  44. cl::PrintVersionMessage();
  45. return 0;
  46. }
  47. int main(int argc, const char **argv) {
  48. InitLLVM X(argc, argv);
  49. // If argv[0] is or ends with 'gcov', always be gcov compatible
  50. if (sys::path::stem(argv[0]).endswith_insensitive("gcov"))
  51. return gcovMain(argc, argv);
  52. // Check if we are invoking a specific tool command.
  53. if (argc > 1) {
  54. typedef int (*MainFunction)(int, const char *[]);
  55. MainFunction Func = StringSwitch<MainFunction>(argv[1])
  56. .Case("convert-for-testing", convertForTestingMain)
  57. .Case("export", exportMain)
  58. .Case("gcov", gcovMain)
  59. .Case("report", reportMain)
  60. .Case("show", showMain)
  61. .Cases("-h", "-help", "--help", helpMain)
  62. .Cases("-version", "--version", versionMain)
  63. .Default(nullptr);
  64. if (Func) {
  65. std::string Invocation = std::string(argv[0]) + " " + argv[1];
  66. argv[1] = Invocation.c_str();
  67. return Func(argc - 1, argv + 1);
  68. }
  69. }
  70. if (argc > 1) {
  71. if (sys::Process::StandardErrHasColors())
  72. errs().changeColor(raw_ostream::RED);
  73. errs() << "Unrecognized command: " << argv[1] << ".\n\n";
  74. if (sys::Process::StandardErrHasColors())
  75. errs().resetColor();
  76. }
  77. helpMain(argc, argv);
  78. return 1;
  79. }