clang-tools  16.0.0git
ClangTidy.h
Go to the documentation of this file.
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 
13 #include "ClangTidyOptions.h"
14 #include "llvm/ADT/StringSet.h"
15 #include <memory>
16 #include <vector>
17 
18 namespace llvm {
19 class raw_ostream;
20 } // namespace llvm
21 
22 namespace clang {
23 
24 class ASTConsumer;
25 class CompilerInstance;
26 namespace tooling {
27 class CompilationDatabase;
28 } // namespace tooling
29 
30 namespace tidy {
31 
32 class ClangTidyCheckFactories;
33 
35 public:
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 
50 private:
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.
58 std::vector<std::string> getCheckNames(const ClangTidyOptions &Options,
60 
62  llvm::StringSet<> Names;
63  llvm::StringSet<> Options;
64 };
65 
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.
76 getCheckOptions(const ClangTidyOptions &Options,
78 
79 /// Run a set of clang-tidy checks on a set of files.
80 ///
81 /// \param EnableCheckProfile If provided, it enables check profile collection
82 /// in MatchFinder, and will contain the result of the profile.
83 /// \param StoreCheckProfile If provided, and EnableCheckProfile is true,
84 /// the profile will not be output to stderr, but will instead be stored
85 /// as a JSON file in the specified directory.
86 std::vector<ClangTidyError>
88  const tooling::CompilationDatabase &Compilations,
89  ArrayRef<std::string> InputFiles,
90  llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
91  bool ApplyAnyFix, bool EnableCheckProfile = false,
92  llvm::StringRef StoreCheckProfile = StringRef());
93 
94 /// Controls what kind of fixes clang-tidy is allowed to apply.
96  /// Don't try to apply any fix.
98  /// Only apply fixes added to warnings.
100  /// Apply fixes found in notes.
102 };
103 
104 // FIXME: This interface will need to be significantly extended to be useful.
105 // FIXME: Implement confidence levels for displaying/fixing errors.
106 //
107 /// Displays the found \p Errors to the users. If \p Fix is \ref FB_Fix or \ref
108 /// FB_FixNotes, \p Errors containing fixes are automatically applied and
109 /// reformatted. If no clang-format configuration file is found, the given \P
110 /// FormatStyle is used.
111 void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
112  ClangTidyContext &Context, FixBehaviour Fix,
113  unsigned &WarningsAsErrorsCount,
114  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
115 
116 /// Serializes replacements into YAML and writes them to the specified
117 /// output stream.
118 void exportReplacements(StringRef MainFilePath,
119  const std::vector<ClangTidyError> &Errors,
120  raw_ostream &OS);
121 
122 } // end namespace tidy
123 } // end namespace clang
124 
125 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
clang::tidy::FB_Fix
@ FB_Fix
Only apply fixes added to warnings.
Definition: ClangTidy.h:99
ClangTidyDiagnosticConsumer.h
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:31
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::getAllChecksAndOptions
NamesAndOptions getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers)
Definition: ClangTidy.cpp:627
StoreCheckProfile
static cl::opt< std::string > StoreCheckProfile("store-check-profile", cl::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))
clang::tidy::ClangTidyASTConsumerFactory::getCheckOptions
ClangTidyOptions::OptionMap getCheckOptions()
Get the union of options from all checks.
Definition: ClangTidy.cpp:475
clang::tidy::getCheckOptions
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers)
Returns the effective check-specific options.
Definition: ClangTidy.cpp:496
clang::tidy::handleErrors
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.
Definition: ClangTidy.cpp:585
clang::tidy::ClangTidyASTConsumerFactory::getCheckNames
std::vector< std::string > getCheckNames()
Get the list of enabled checks.
Definition: ClangTidy.cpp:458
clang::tidy::NamesAndOptions
Definition: ClangTidy.h:61
Fix
static cl::opt< bool > Fix("fix", cl::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))
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:50
clang::tidy::NamesAndOptions::Options
llvm::StringSet Options
Definition: ClangTidy.h:63
clang::tidy::FixBehaviour
FixBehaviour
Controls what kind of fixes clang-tidy is allowed to apply.
Definition: ClangTidy.h:95
clang::tidy::runClangTidy
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)
Definition: ClangTidy.cpp:507
AllowEnablingAnalyzerAlphaCheckers
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.
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:67
clang::tidy::ClangTidyASTConsumerFactory
Definition: ClangTidy.h:34
ASTConsumer
EnableCheckProfile
static cl::opt< bool > EnableCheckProfile("enable-check-profile", cl::desc(R"( Enable per-check timing profiles, and print a report to stderr. )"), cl::init(false), cl::cat(ClangTidyCategory))
clang::tidy::NamesAndOptions::Names
llvm::StringSet Names
Definition: ClangTidy.h:62
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
clang::tidy::getCheckNames
std::vector< std::string > getCheckNames(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers)
Fills the list of check names that are enabled when the provided filters are applied.
Definition: ClangTidy.cpp:485
clang::tidy::FB_FixNotes
@ FB_FixNotes
Apply fixes found in notes.
Definition: ClangTidy.h:101
clang::tidy::exportReplacements
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
Definition: ClangTidy.cpp:612
clang::tidy::ClangTidyASTConsumerFactory::createASTConsumer
std::unique_ptr< clang::ASTConsumer > createASTConsumer(clang::CompilerInstance &Compiler, StringRef File)
Returns an ASTConsumer that runs the specified clang-tidy checks.
Definition: ClangTidy.cpp:388
ClangTidyOptions.h
clang::tidy::ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory
ClangTidyASTConsumerFactory(ClangTidyContext &Context, IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > OverlayFS=nullptr)
Definition: ClangTidy.cpp:328
clang::tidy::FB_NoFix
@ FB_NoFix
Don't try to apply any fix.
Definition: ClangTidy.h:97