clang-tools 6.0.0

IncludeInserter.h
Go to the documentation of this file.
1 //===---------- IncludeInserter.h - clang-tidy ----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
12
13 #include "IncludeSorter.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/LangOptions.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Lex/PPCallbacks.h"
18 #include <memory>
19 #include <string>
20
21 namespace clang {
22 namespace tidy {
23 namespace utils {
24
25 /// \brief Produces fixes to insert specified includes to source files, if not
26 /// yet present.
27 ///
28 /// ``IncludeInserter`` can be used by ``ClangTidyCheck`` in the following
29 /// fashion:
30 /// \code
31 /// class MyCheck : public ClangTidyCheck {
32 /// public:
33 /// void registerPPCallbacks(CompilerInstance& Compiler) override {
34 /// Inserter.reset(new IncludeInserter(&Compiler.getSourceManager(),
35 /// &Compiler.getLangOpts()));
36 /// Compiler.getPreprocessor().addPPCallbacks(
37 /// Inserter->CreatePPCallback());
38 /// }
39 ///
40 /// void registerMatchers(ast_matchers::MatchFinder* Finder) override { ... }
41 ///
42 /// void check(
43 /// const ast_matchers::MatchFinder::MatchResult& Result) override {
44 /// ...
45 /// Inserter->CreateIncludeInsertion(
46 /// Result.SourceManager->getMainFileID(), "path/to/Header.h",
47 /// /*IsAngled=*/false);
48 /// ...
49 /// }
50 ///
51 /// private:
52 /// std::unique_ptr<IncludeInserter> Inserter;
53 /// };
54 /// \endcode
55 class IncludeInserter {
56 public:
57 IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts,
58 IncludeSorter::IncludeStyle Style);
59 ~IncludeInserter();
60
61 /// Create ``PPCallbacks`` for registration with the compiler's preprocessor.
62 std::unique_ptr<PPCallbacks> CreatePPCallbacks();
63
64 /// Creates a \p Header inclusion directive fixit. Returns ``llvm::None`` on
65 /// error or if inclusion directive already exists.
66 llvm::Optional<FixItHint>
67 CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled);
68
69 private:
70 void AddInclude(StringRef FileName, bool IsAngled,
71 SourceLocation HashLocation, SourceLocation EndLocation);
72
73 llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
74 llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders;
75 const SourceManager &SourceMgr;
76 const LangOptions &LangOpts;
77 const IncludeSorter::IncludeStyle Style;
78 friend class IncludeInserterCallback;
79 };
80
81 } // namespace utils
82 } // namespace tidy
83 } // namespace clang
84 #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:27
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:39
clang::tidy::utils::IncludeInserterCallback
Definition: IncludeInserter.cpp:17
IncludeSorter.h
clang::tidy::utils::IncludeInserter::IncludeInserter
IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts, IncludeSorter::IncludeStyle Style)
Definition: IncludeInserter.cpp:37
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::utils::IncludeInserter
Produces fixes to insert specified includes to source files, if not yet present.
Definition: IncludeInserter.h:55
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