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
58 public:
59  IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts,
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;
81 };
82 
83 } // namespace utils
84 } // namespace tidy
85 } // namespace clang
86 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
std::unique_ptr< PPCallbacks > CreatePPCallbacks()
Create PPCallbacks for registration with the compiler&#39;s preprocessor.
IncludeStyle
Supported include styles.
Definition: IncludeSorter.h:27
bool IsAngled
true if this was an include with angle brackets
PathRef FileName
IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts, IncludeSorter::IncludeStyle Style)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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.