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