clang-tools  3.9.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
56 public:
57  IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts,
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;
79 };
80 
81 } // namespace utils
82 } // namespace tidy
83 } // namespace clang
84 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
std::unique_ptr< PPCallbacks > CreatePPCallbacks()
Create PPCallbacks for registration with the compiler's preprocessor.
IncludeStyle
Supported include styles.
Definition: IncludeSorter.h:27
bool IsAngled
true if this was an include with angle brackets
IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts, IncludeSorter::IncludeStyle Style)
Produces fixes to insert specified includes to source files, if not yet present.
llvm::Optional< FixItHint > CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled)
Creates a Header inclusion directive fixit.