clang-tools 10.0.0

IncludeInserter.h
Go to the documentation of this file.
1 //===---------- IncludeInserter.h - clang-tidy ----------------------------===//
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_INCLUDEINSERTER_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
11
12 #include "IncludeSorter.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Lex/PPCallbacks.h"
17 #include <memory>
18 #include <string>
19
20 namespace clang {
21 namespace tidy {
22 namespace utils {
23
24 /// Produces fixes to insert specified includes to source files, if not
25 /// yet present.
26 ///
27 /// ``IncludeInserter`` can be used in clang-tidy checks in the following way:
28 /// \code
29 /// #include "../utils/IncludeInserter.h"
30 /// #include "clang/Frontend/CompilerInstance.h"
31 ///
32 /// class MyCheck : public ClangTidyCheck {
33 /// public:
34 /// void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
35 /// Preprocessor *ModuleExpanderPP) override {
36 /// Inserter = std::make_unique<IncludeInserter>(
37 /// SM, getLangOpts(), utils::IncludeSorter::IS_Google);
38 /// PP->addPPCallbacks(Inserter->CreatePPCallbacks());
39 /// }
40 ///
41 /// void registerMatchers(ast_matchers::MatchFinder* Finder) override { ... }
42 ///
43 /// void check(
44 /// const ast_matchers::MatchFinder::MatchResult& Result) override {
45 /// ...
46 /// Inserter->CreateIncludeInsertion(
47 /// Result.SourceManager->getMainFileID(), "path/to/Header.h",
48 /// /*IsAngled=*/false);
49 /// ...
50 /// }
51 ///
52 /// private:
53 /// std::unique_ptr<clang::tidy::utils::IncludeInserter> Inserter;
54 /// };
55 /// \endcode
56 class IncludeInserter {
57 public:
58 IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts,
59 IncludeSorter::IncludeStyle Style);
60 ~IncludeInserter();
61
62 /// Create ``PPCallbacks`` for registration with the compiler's preprocessor.
63 std::unique_ptr<PPCallbacks> CreatePPCallbacks();
64
65 /// Creates a \p Header inclusion directive fixit. Returns ``llvm::None`` on
66 /// error or if inclusion directive already exists.
67 llvm::Optional<FixItHint>
68 CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled);
69
70 private:
71 void AddInclude(StringRef FileName, bool IsAngled,
72 SourceLocation HashLocation, SourceLocation EndLocation);
73
74 llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
75 llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders;
76 const SourceManager &SourceMgr;
77 const LangOptions &LangOpts;
78 const IncludeSorter::IncludeStyle Style;
79 friend class IncludeInserterCallback;
80 };
81
82 } // namespace utils
83 } // namespace tidy
84 } // namespace clang
85 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
clang::tidy::utils::IncludeInserter::CreatePPCallbacks
std::unique_ptr< PPCallbacks > CreatePPCallbacks()
Create PPCallbacks for registration with the compiler&#39;s preprocessor.
Definition: IncludeInserter.cpp:44
clang::tidy::utils::IncludeSorter::IncludeStyle
IncludeStyle
Supported include styles.
Definition: IncludeSorter.h:26
clang::tidy::utils::IncludeInserter::~IncludeInserter
~IncludeInserter()
Definition: IncludeInserter.cpp:42
IsAngled
bool IsAngled
true if this was an include with angle brackets
Definition: IncludeOrderCheck.cpp:40
FileName
PathRef FileName
Definition: CodeComplete.cpp:1018
clang::tidy::utils::IncludeInserterCallback
Definition: IncludeInserter.cpp:16
IncludeSorter.h
clang::tidy::utils::IncludeInserter::IncludeInserter
IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts, IncludeSorter::IncludeStyle Style)
Definition: IncludeInserter.cpp:37
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::IncludeInserter
Produces fixes to insert specified includes to source files, if not yet present.
Definition: IncludeInserter.h:56
clang::tidy::utils::IncludeInserter::CreateIncludeInsertion
llvm::Optional< FixItHint > CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled)
Creates a Header inclusion directive fixit.
Definition: IncludeInserter.cpp:49