clang-tools  12.0.0git
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
57 public:
58  IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts,
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  IncludeSorter &getOrCreate(FileID FileID);
75 
76  llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
77  llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders;
78  const SourceManager &SourceMgr;
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:26
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.
Class used by IncludeInserterCallback to record the names of the inclusions in a given source file be...
Definition: IncludeSorter.h:23