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

source code of clang-tools-extra/clang-tidy/ClangTidy.h