aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/tools/extra/clang-tidy/utils/IncludeInserter.h
blob: 5308f76bd21511c0b8de204aa89bcb267d0951d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//===---------- IncludeInserter.h - clang-tidy ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H

#include "IncludeSorter.h"
#include "clang/Basic/Diagnostic.h"
#include "llvm/ADT/StringSet.h"
#include <memory>
#include <optional>

namespace clang {
class Preprocessor;
namespace tidy::utils {

/// Produces fixes to insert specified includes to source files, if not
/// yet present.
///
/// ``IncludeInserter`` can be used in clang-tidy checks in the following way:
/// \code
/// #include "../ClangTidyCheck.h"
/// #include "../utils/IncludeInserter.h"
///
/// namespace clang {
/// namespace tidy {
///
/// class MyCheck : public ClangTidyCheck {
///  public:
///   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
///                            Preprocessor *ModuleExpanderPP) override {
///     Inserter.registerPreprocessor(PP);
///   }
///
///   void registerMatchers(ast_matchers::MatchFinder* Finder) override { ... }
///
///   void check(
///       const ast_matchers::MatchFinder::MatchResult& Result) override {
///     ...
///     Inserter.createMainFileIncludeInsertion("path/to/Header.h");
///     ...
///   }
///
///  private:
///   utils::IncludeInserter Inserter{utils::IncludeSorter::IS_Google};
/// };
/// } // namespace tidy
/// } // namespace clang
/// \endcode
class IncludeInserter {
public:
  /// Initializes the IncludeInserter using the IncludeStyle \p Style.
  /// In most cases the \p Style will be retrieved from the ClangTidyOptions
  /// using \code
  ///   Options.getLocalOrGlobal("IncludeStyle", <DefaultStyle>)
  /// \endcode
  explicit IncludeInserter(IncludeSorter::IncludeStyle Style,
                           bool SelfContainedDiags);

  /// Registers this with the Preprocessor \p PP, must be called before this
  /// class is used.
  void registerPreprocessor(Preprocessor *PP);

  /// Creates a \p Header inclusion directive fixit in the File \p FileID.
  /// When \p Header is enclosed in angle brackets, uses angle brackets in the
  /// inclusion directive, otherwise uses quotes.
  /// Returns ``std::nullopt`` on error or if the inclusion directive already
  /// exists.
  std::optional<FixItHint> createIncludeInsertion(FileID FileID,
                                                  llvm::StringRef Header);

  /// Creates a \p Header inclusion directive fixit in the main file.
  /// When \p Header is enclosed in angle brackets, uses angle brackets in the
  /// inclusion directive, otherwise uses quotes.
  /// Returns ``std::nullopt`` on error or if the inclusion directive already
  /// exists.
  std::optional<FixItHint>
  createMainFileIncludeInsertion(llvm::StringRef Header);

  IncludeSorter::IncludeStyle getStyle() const { return Style; }

private:
  void addInclude(StringRef FileName, bool IsAngled,
                  SourceLocation HashLocation, SourceLocation EndLocation);

  IncludeSorter &getOrCreate(FileID FileID);

  llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
  llvm::DenseMap<FileID, llvm::StringSet<>> InsertedHeaders;
  const SourceManager *SourceMgr{nullptr};
  const IncludeSorter::IncludeStyle Style;
  const bool SelfContainedDiags;
  friend class IncludeInserterCallback;
};

} // namespace tidy::utils
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H