1 //===--- IncludeCleanerCheck.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_MISC_INCLUDECLEANER_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_INCLUDECLEANER_H
11 
12 #include "../ClangTidyCheck.h"
13 #include "../ClangTidyDiagnosticConsumer.h"
14 #include "../ClangTidyOptions.h"
15 #include "clang-include-cleaner/Record.h"
16 #include "clang-include-cleaner/Types.h"
17 #include "clang/ASTMatchers/ASTMatchFinder.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Lex/HeaderSearch.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "llvm/Support/Regex.h"
23 #include <vector>
24 
25 namespace clang::tidy::misc {
26 
27 /// Checks for unused and missing includes. Generates findings only for
28 /// the main file of a translation unit.
29 /// Findings correspond to https://clangd.llvm.org/design/include-cleaner.
30 ///
31 /// For the user-facing documentation see:
32 /// http://clang.llvm.org/extra/clang-tidy/checks/misc/include-cleaner.html
33 class IncludeCleanerCheck : public ClangTidyCheck {
34 public:
35   IncludeCleanerCheck(StringRef Name, ClangTidyContext *Context);
36   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
37   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
38   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
39                            Preprocessor *ModuleExpanderPP) override;
40   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
41   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;
42 
43 private:
44   include_cleaner::RecordedPP RecordedPreprocessor;
45   include_cleaner::PragmaIncludes RecordedPI;
46   const Preprocessor *PP = nullptr;
47   std::vector<StringRef> IgnoreHeaders;
48   // Whether emit only one finding per usage of a symbol.
49   const bool DeduplicateFindings;
50   llvm::SmallVector<llvm::Regex> IgnoreHeadersRegex;
51   bool shouldIgnore(const include_cleaner::Header &H);
52 };
53 
54 } // namespace clang::tidy::misc
55 
56 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_INCLUDECLEANER_H
57