ClangTidy.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //===--- ClangTidy.h - clang-tidy -------------------------------*- C++ -*-===//
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
  10. #include "ClangTidyDiagnosticConsumer.h"
  11. #include "ClangTidyOptions.h"
  12. #include "llvm/ADT/StringSet.h"
  13. #include <memory>
  14. #include <vector>
  15. namespace llvm {
  16. class raw_ostream;
  17. } // namespace llvm
  18. namespace clang {
  19. class ASTConsumer;
  20. class CompilerInstance;
  21. namespace tooling {
  22. class CompilationDatabase;
  23. } // namespace tooling
  24. namespace tidy {
  25. class ClangTidyCheckFactories;
  26. class ClangTidyASTConsumerFactory {
  27. public:
  28. ClangTidyASTConsumerFactory(
  29. ClangTidyContext &Context,
  30. IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS = nullptr);
  31. /// Returns an ASTConsumer that runs the specified clang-tidy checks.
  32. std::unique_ptr<clang::ASTConsumer>
  33. createASTConsumer(clang::CompilerInstance &Compiler, StringRef File);
  34. /// Get the list of enabled checks.
  35. std::vector<std::string> getCheckNames();
  36. /// Get the union of options from all checks.
  37. ClangTidyOptions::OptionMap getCheckOptions();
  38. private:
  39. ClangTidyContext &Context;
  40. IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS;
  41. std::unique_ptr<ClangTidyCheckFactories> CheckFactories;
  42. };
  43. /// Fills the list of check names that are enabled when the provided
  44. /// filters are applied.
  45. std::vector<std::string> getCheckNames(const ClangTidyOptions &Options,
  46. bool AllowEnablingAnalyzerAlphaCheckers);
  47. struct NamesAndOptions {
  48. llvm::StringSet<> Names;
  49. llvm::StringSet<> Options;
  50. };
  51. NamesAndOptions
  52. getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers = true);
  53. /// Returns the effective check-specific options.
  54. ///
  55. /// The method configures ClangTidy with the specified \p Options and collects
  56. /// effective options from all created checks. The returned set of options
  57. /// includes default check-specific options for all keys not overridden by \p
  58. /// Options.
  59. ClangTidyOptions::OptionMap
  60. getCheckOptions(const ClangTidyOptions &Options,
  61. bool AllowEnablingAnalyzerAlphaCheckers);
  62. /// Run a set of clang-tidy checks on a set of files.
  63. ///
  64. /// \param EnableCheckProfile If provided, it enables check profile collection
  65. /// in MatchFinder, and will contain the result of the profile.
  66. /// \param StoreCheckProfile If provided, and EnableCheckProfile is true,
  67. /// the profile will not be output to stderr, but will instead be stored
  68. /// as a JSON file in the specified directory.
  69. std::vector<ClangTidyError>
  70. runClangTidy(clang::tidy::ClangTidyContext &Context,
  71. const tooling::CompilationDatabase &Compilations,
  72. ArrayRef<std::string> InputFiles,
  73. llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
  74. bool ApplyAnyFix, bool EnableCheckProfile = false,
  75. llvm::StringRef StoreCheckProfile = StringRef());
  76. /// Controls what kind of fixes clang-tidy is allowed to apply.
  77. enum FixBehaviour {
  78. /// Don't try to apply any fix.
  79. FB_NoFix,
  80. /// Only apply fixes added to warnings.
  81. FB_Fix,
  82. /// Apply fixes found in notes.
  83. FB_FixNotes
  84. };
  85. // FIXME: This interface will need to be significantly extended to be useful.
  86. // FIXME: Implement confidence levels for displaying/fixing errors.
  87. //
  88. /// Displays the found \p Errors to the users. If \p Fix is \ref FB_Fix or \ref
  89. /// FB_FixNotes, \p Errors containing fixes are automatically applied and
  90. /// reformatted. If no clang-format configuration file is found, the given \P
  91. /// FormatStyle is used.
  92. void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
  93. ClangTidyContext &Context, FixBehaviour Fix,
  94. unsigned &WarningsAsErrorsCount,
  95. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
  96. /// Serializes replacements into YAML and writes them to the specified
  97. /// output stream.
  98. void exportReplacements(StringRef MainFilePath,
  99. const std::vector<ClangTidyError> &Errors,
  100. raw_ostream &OS);
  101. } // end namespace tidy
  102. } // end namespace clang
  103. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H