clang-tools  14.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 <memory>
15 #include <vector>
16 
17 namespace llvm {
18 class raw_ostream;
19 } // namespace llvm
20 
21 namespace clang {
22 
23 class ASTConsumer;
24 class CompilerInstance;
25 namespace tooling {
26 class CompilationDatabase;
27 } // namespace tooling
28 
29 namespace tidy {
30 
31 class ClangTidyCheckFactories;
32 
34 public:
36  ClangTidyContext &Context,
37  IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS = nullptr);
38 
39  /// Returns an ASTConsumer that runs the specified clang-tidy checks.
40  std::unique_ptr<clang::ASTConsumer>
41  CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File);
42 
43  /// Get the list of enabled checks.
44  std::vector<std::string> getCheckNames();
45 
46  /// Get the union of options from all checks.
48 
49 private:
50  ClangTidyContext &Context;
51  IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS;
52  std::unique_ptr<ClangTidyCheckFactories> CheckFactories;
53 };
54 
55 /// Fills the list of check names that are enabled when the provided
56 /// filters are applied.
57 std::vector<std::string> getCheckNames(const ClangTidyOptions &Options,
59 
60 /// Returns the effective check-specific options.
61 ///
62 /// The method configures ClangTidy with the specified \p Options and collects
63 /// effective options from all created checks. The returned set of options
64 /// includes default check-specific options for all keys not overridden by \p
65 /// Options.
67 getCheckOptions(const ClangTidyOptions &Options,
69 
70 /// Run a set of clang-tidy checks on a set of files.
71 ///
72 /// \param EnableCheckProfile If provided, it enables check profile collection
73 /// in MatchFinder, and will contain the result of the profile.
74 /// \param StoreCheckProfile If provided, and EnableCheckProfile is true,
75 /// the profile will not be output to stderr, but will instead be stored
76 /// as a JSON file in the specified directory.
77 std::vector<ClangTidyError>
79  const tooling::CompilationDatabase &Compilations,
80  ArrayRef<std::string> InputFiles,
81  llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
82  bool ApplyAnyFix, bool EnableCheckProfile = false,
83  llvm::StringRef StoreCheckProfile = StringRef());
84 
85 /// Controls what kind of fixes clang-tidy is allowed to apply.
87  /// Don't try to apply any fix.
89  /// Only apply fixes added to warnings.
91  /// Apply fixes found in notes.
93 };
94 
95 // FIXME: This interface will need to be significantly extended to be useful.
96 // FIXME: Implement confidence levels for displaying/fixing errors.
97 //
98 /// Displays the found \p Errors to the users. If \p Fix is \ref FB_Fix or \ref
99 /// FB_FixNotes, \p Errors containing fixes are automatically applied and
100 /// reformatted. If no clang-format configuration file is found, the given \P
101 /// FormatStyle is used.
102 void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
103  ClangTidyContext &Context, FixBehaviour Fix,
104  unsigned &WarningsAsErrorsCount,
105  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
106 
107 /// Serializes replacements into YAML and writes them to the specified
108 /// output stream.
109 void exportReplacements(StringRef MainFilePath,
110  const std::vector<ClangTidyError> &Errors,
111  raw_ostream &OS);
112 
113 } // end namespace tidy
114 } // end namespace clang
115 
116 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
clang::tidy::FB_Fix
@ FB_Fix
Only apply fixes added to warnings.
Definition: ClangTidy.h:90
ClangTidyDiagnosticConsumer.h
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
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:481
clang::tidy::getCheckOptions
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers)
Returns the effective check-specific options.
Definition: ClangTidy.cpp:502
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:591
clang::tidy::ClangTidyASTConsumerFactory::getCheckNames
std::vector< std::string > getCheckNames()
Get the list of enabled checks.
Definition: ClangTidy.cpp:464
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::FixBehaviour
FixBehaviour
Controls what kind of fixes clang-tidy is allowed to apply.
Definition: ClangTidy.h:86
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:513
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::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
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
clang::tidy::ClangTidyASTConsumerFactory
Definition: ClangTidy.h:33
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
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
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:491
clang::tidy::FB_FixNotes
@ FB_FixNotes
Apply fixes found in notes.
Definition: ClangTidy.h:92
clang::tidy::exportReplacements
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
Definition: ClangTidy.cpp:618
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:88