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 <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 
33 class ClangTidyASTConsumerFactory {
34 public:
35   ClangTidyASTConsumerFactory(
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.
47   ClangTidyOptions::OptionMap getCheckOptions();
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,
58                                        bool AllowEnablingAnalyzerAlphaCheckers);
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.
66 ClangTidyOptions::OptionMap
67 getCheckOptions(const ClangTidyOptions &Options,
68                 bool AllowEnablingAnalyzerAlphaCheckers);
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>
78 runClangTidy(clang::tidy::ClangTidyContext &Context,
79              const tooling::CompilationDatabase &Compilations,
80              ArrayRef<std::string> InputFiles,
81              llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
82              bool EnableCheckProfile = false,
83              llvm::StringRef StoreCheckProfile = StringRef());
84 
85 // FIXME: This interface will need to be significantly extended to be useful.
86 // FIXME: Implement confidence levels for displaying/fixing errors.
87 //
88 /// Displays the found \p Errors to the users. If \p Fix is true, \p
89 /// Errors containing fixes are automatically applied and reformatted. If no
90 /// clang-format configuration file is found, the given \P FormatStyle is used.
91 void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
92                   ClangTidyContext &Context, bool Fix,
93                   unsigned &WarningsAsErrorsCount,
94                   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
95 
96 /// Serializes replacements into YAML and writes them to the specified
97 /// output stream.
98 void exportReplacements(StringRef MainFilePath,
99                         const std::vector<ClangTidyError> &Errors,
100                         raw_ostream &OS);
101 
102 } // end namespace tidy
103 } // end namespace clang
104 
105 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
106