AnalyzerHelpFlags.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===--- CheckerRegistration.cpp - Registration for the Analyzer Checkers -===//
  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. // Defines the registration function for the analyzer checkers.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/StaticAnalyzer/Frontend/AnalyzerHelpFlags.h"
  13. #include "clang/Basic/Diagnostic.h"
  14. #include "clang/Frontend/CompilerInstance.h"
  15. #include "clang/Frontend/FrontendDiagnostic.h"
  16. #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
  17. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  18. #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
  19. #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <memory>
  23. using namespace clang;
  24. using namespace ento;
  25. void ento::printCheckerHelp(raw_ostream &out, CompilerInstance &CI) {
  26. out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
  27. out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
  28. auto CheckerMgr = std::make_unique<CheckerManager>(
  29. *CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
  30. CI.getFrontendOpts().Plugins);
  31. CheckerMgr->getCheckerRegistryData().printCheckerWithDescList(
  32. *CI.getAnalyzerOpts(), out);
  33. }
  34. void ento::printEnabledCheckerList(raw_ostream &out, CompilerInstance &CI) {
  35. out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n";
  36. auto CheckerMgr = std::make_unique<CheckerManager>(
  37. *CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
  38. CI.getFrontendOpts().Plugins);
  39. CheckerMgr->getCheckerRegistryData().printEnabledCheckerList(out);
  40. }
  41. void ento::printCheckerConfigList(raw_ostream &out, CompilerInstance &CI) {
  42. auto CheckerMgr = std::make_unique<CheckerManager>(
  43. *CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
  44. CI.getFrontendOpts().Plugins);
  45. CheckerMgr->getCheckerRegistryData().printCheckerOptionList(
  46. *CI.getAnalyzerOpts(), out);
  47. }
  48. void ento::printAnalyzerConfigList(raw_ostream &out) {
  49. // FIXME: This message sounds scary, should be scary, but incorrectly states
  50. // that all configs are super dangerous. In reality, many of them should be
  51. // accessible to the user. We should create a user-facing subset of config
  52. // options under a different frontend flag.
  53. out << R"(
  54. OVERVIEW: Clang Static Analyzer -analyzer-config Option List
  55. The following list of configurations are meant for development purposes only, as
  56. some of the variables they define are set to result in the most optimal
  57. analysis. Setting them to other values may drastically change how the analyzer
  58. behaves, and may even result in instabilities, crashes!
  59. USAGE: -analyzer-config <OPTION1=VALUE,OPTION2=VALUE,...>
  60. -analyzer-config OPTION1=VALUE, -analyzer-config OPTION2=VALUE, ...
  61. OPTIONS:
  62. )";
  63. using OptionAndDescriptionTy = std::pair<StringRef, std::string>;
  64. OptionAndDescriptionTy PrintableOptions[] = {
  65. #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
  66. { \
  67. CMDFLAG, \
  68. llvm::Twine(llvm::Twine() + "(" + \
  69. (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
  70. ") " DESC \
  71. " (default: " #DEFAULT_VAL ")").str() \
  72. },
  73. #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
  74. SHALLOW_VAL, DEEP_VAL) \
  75. { \
  76. CMDFLAG, \
  77. llvm::Twine(llvm::Twine() + "(" + \
  78. (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
  79. ") " DESC \
  80. " (default: " #SHALLOW_VAL " in shallow mode, " #DEEP_VAL \
  81. " in deep mode)").str() \
  82. },
  83. #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
  84. #undef ANALYZER_OPTION
  85. #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
  86. };
  87. llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS,
  88. const OptionAndDescriptionTy &RHS) {
  89. return LHS.first < RHS.first;
  90. });
  91. for (const auto &Pair : PrintableOptions) {
  92. AnalyzerOptions::printFormattedEntry(out, Pair, /*InitialPad*/ 2,
  93. /*EntryWidth*/ 30,
  94. /*MinLineWidth*/ 70);
  95. out << "\n\n";
  96. }
  97. }