llvm-cov.cpp 3.3 KB

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