clang-tools 22.0.0git
ClangTidy.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
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
35public:
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.
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,
61
63 llvm::StringSet<> Checks;
64 llvm::StringSet<> Options;
65};
66
69
70/// Returns the effective check-specific options.
71///
72/// The method configures ClangTidy with the specified \p Options and collects
73/// effective options from all created checks. The returned set of options
74/// includes default check-specific options for all keys not overridden by \p
75/// Options.
80
81/// Filters CheckOptions in \p Options to only include options specified in
82/// the \p EnabledChecks which is a sorted vector.
84 const std::vector<std::string> &EnabledChecks);
85
86/// Run a set of clang-tidy checks on a set of files.
87///
88/// \param EnableCheckProfile If provided, it enables check profile collection
89/// in MatchFinder, and will contain the result of the profile.
90/// \param StoreCheckProfile If provided, and EnableCheckProfile is true,
91/// the profile will not be output to stderr, but will instead be stored
92/// as a JSON file in the specified directory.
93std::vector<ClangTidyError>
95 const tooling::CompilationDatabase &Compilations,
96 ArrayRef<std::string> InputFiles,
97 llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
98 bool ApplyAnyFix, bool EnableCheckProfile = false,
99 llvm::StringRef StoreCheckProfile = StringRef(),
100 bool Quiet = false);
101
102/// Controls what kind of fixes clang-tidy is allowed to apply.
104 /// Don't try to apply any fix.
106 /// Only apply fixes added to warnings.
108 /// Apply fixes found in notes.
110};
111
112// FIXME: This interface will need to be significantly extended to be useful.
113// FIXME: Implement confidence levels for displaying/fixing errors.
114//
115/// Displays the found \p Errors to the users. If \p Fix is \ref FB_Fix or \ref
116/// FB_FixNotes, \p Errors containing fixes are automatically applied and
117/// reformatted. If no clang-format configuration file is found, the given \P
118/// FormatStyle is used.
119void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
120 ClangTidyContext &Context, FixBehaviour Fix,
121 unsigned &WarningsAsErrorsCount,
122 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
123
124/// Serializes replacements into YAML and writes them to the specified
125/// output stream.
126void exportReplacements(StringRef MainFilePath,
127 const std::vector<ClangTidyError> &Errors,
128 raw_ostream &OS);
129
130namespace custom {
131extern void (*RegisterCustomChecks)(const ClangTidyOptions &O,
132 ClangTidyCheckFactories &Factories);
133} // namespace custom
134} // end namespace tidy
135} // end namespace clang
136
137#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
static cl::opt< bool > EnableCheckProfile("enable-check-profile", desc(R"( Enable per-check timing profiles, and print a report to stderr. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > Fix("fix", desc(R"( Apply suggested fixes. Without -fix-errors clang-tidy will bail out if any compilation errors were found. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > ExperimentalCustomChecks("experimental-custom-checks", desc(R"( Enable experimental clang-query based custom checks. see https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChecks.html. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > AllowEnablingAnalyzerAlphaCheckers("allow-enabling-analyzer-alpha-checkers", cl::init(false), cl::Hidden, cl::cat(ClangTidyCategory))
This option allows enabling the experimental alpha checkers from the static analyzer.
static cl::opt< bool > Quiet("quiet", desc(R"( Run clang-tidy in quiet mode. This suppresses printing statistics about ignored warnings and warnings treated as errors if the respective options are specified. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< std::string > StoreCheckProfile("store-check-profile", desc(R"( By default reports are printed in tabulated format to stderr. When this option is passed, these per-TU profiles are instead stored as JSON. )"), cl::value_desc("prefix"), cl::cat(ClangTidyCategory))
std::unique_ptr< clang::ASTConsumer > createASTConsumer(clang::CompilerInstance &Compiler, StringRef File)
Returns an ASTConsumer that runs the specified clang-tidy checks.
ClangTidyOptions::OptionMap getCheckOptions()
Get the union of options from all checks.
ClangTidyASTConsumerFactory(ClangTidyContext &Context, IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > OverlayFS=nullptr)
std::vector< std::string > getCheckNames()
Get the list of enabled checks.
A collection of ClangTidyCheckFactory instances.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void(* RegisterCustomChecks)(const ClangTidyOptions &O, ClangTidyCheckFactories &Factories)
ChecksAndOptions getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers, bool ExperimentalCustomChecks)
FixBehaviour
Controls what kind of fixes clang-tidy is allowed to apply.
Definition ClangTidy.h:103
@ FB_NoFix
Don't try to apply any fix.
Definition ClangTidy.h:105
@ FB_FixNotes
Apply fixes found in notes.
Definition ClangTidy.h:109
@ FB_Fix
Only apply fixes added to warnings.
Definition ClangTidy.h:107
std::vector< std::string > getCheckNames(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers, bool ExperimentalCustomChecks)
Fills the list of check names that are enabled when the provided filters are applied.
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers, bool ExperimentalCustomChecks)
Returns the effective check-specific options.
void handleErrors(llvm::ArrayRef< ClangTidyError > Errors, ClangTidyContext &Context, FixBehaviour Fix, unsigned &WarningsAsErrorsCount, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS)
Displays the found Errors to the users.
void filterCheckOptions(ClangTidyOptions &Options, const std::vector< std::string > &EnabledChecks)
Filters CheckOptions in Options to only include options specified in the EnabledChecks which is a sor...
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
std::vector< ClangTidyError > runClangTidy(clang::tidy::ClangTidyContext &Context, const CompilationDatabase &Compilations, ArrayRef< std::string > InputFiles, llvm::IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > BaseFS, bool ApplyAnyFix, bool EnableCheckProfile, llvm::StringRef StoreCheckProfile, bool Quiet)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
Definition Generators.h:66
Contains options for clang-tidy.
llvm::StringMap< ClangTidyValue > OptionMap