1 //===--- ClangTidy.h - clang-tidy -------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
12 #include "ClangTidyDiagnosticConsumer.h"
13 #include "ClangTidyOptions.h"
14 #include "llvm/ADT/StringSet.h"
25 class CompilerInstance
;
27 class CompilationDatabase
;
28 } // namespace tooling
32 class ClangTidyCheckFactories
;
34 class ClangTidyASTConsumerFactory
{
36 ClangTidyASTConsumerFactory(
37 ClangTidyContext
&Context
,
38 IntrusiveRefCntPtr
<llvm::vfs::OverlayFileSystem
> OverlayFS
= nullptr);
40 /// Returns an ASTConsumer that runs the specified clang-tidy checks.
41 std::unique_ptr
<clang::ASTConsumer
>
42 createASTConsumer(clang::CompilerInstance
&Compiler
, StringRef File
);
44 /// Get the list of enabled checks.
45 std::vector
<std::string
> getCheckNames();
47 /// Get the union of options from all checks.
48 ClangTidyOptions::OptionMap
getCheckOptions();
51 ClangTidyContext
&Context
;
52 IntrusiveRefCntPtr
<llvm::vfs::OverlayFileSystem
> OverlayFS
;
53 std::unique_ptr
<ClangTidyCheckFactories
> CheckFactories
;
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
,
59 bool AllowEnablingAnalyzerAlphaCheckers
);
61 struct NamesAndOptions
{
62 llvm::StringSet
<> Names
;
63 llvm::StringSet
<> Options
;
67 getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers
= true);
69 /// Returns the effective check-specific options.
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
75 ClangTidyOptions::OptionMap
76 getCheckOptions(const ClangTidyOptions
&Options
,
77 bool AllowEnablingAnalyzerAlphaCheckers
);
79 /// Run a set of clang-tidy checks on a set of files.
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
>
87 runClangTidy(clang::tidy::ClangTidyContext
&Context
,
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());
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.
104 // FIXME: This interface will need to be significantly extended to be useful.
105 // FIXME: Implement confidence levels for displaying/fixing errors.
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
);
116 /// Serializes replacements into YAML and writes them to the specified
118 void exportReplacements(StringRef MainFilePath
,
119 const std::vector
<ClangTidyError
> &Errors
,
122 } // end namespace tidy
123 } // end namespace clang
125 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H