clang-tools 20.0.0git
IncludeCleanerCheck.h
Go to the documentation of this file.
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
25namespace 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
34public:
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
43private:
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
llvm::SmallString< 256U > Name
Base class for all clang-tidy checks.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Checks for unused and missing includes.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
llvm::StringMap< ClangTidyValue > OptionMap