IncludeInserter.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
  10. #include "IncludeSorter.h"
  11. #include "clang/Basic/Diagnostic.h"
  12. #include "llvm/ADT/StringSet.h"
  13. #include <memory>
  14. #include <optional>
  15. namespace clang {
  16. class Preprocessor;
  17. namespace tidy::utils {
  18. /// Produces fixes to insert specified includes to source files, if not
  19. /// yet present.
  20. ///
  21. /// ``IncludeInserter`` can be used in clang-tidy checks in the following way:
  22. /// \code
  23. /// #include "../ClangTidyCheck.h"
  24. /// #include "../utils/IncludeInserter.h"
  25. ///
  26. /// namespace clang {
  27. /// namespace tidy {
  28. ///
  29. /// class MyCheck : public ClangTidyCheck {
  30. /// public:
  31. /// void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
  32. /// Preprocessor *ModuleExpanderPP) override {
  33. /// Inserter.registerPreprocessor(PP);
  34. /// }
  35. ///
  36. /// void registerMatchers(ast_matchers::MatchFinder* Finder) override { ... }
  37. ///
  38. /// void check(
  39. /// const ast_matchers::MatchFinder::MatchResult& Result) override {
  40. /// ...
  41. /// Inserter.createMainFileIncludeInsertion("path/to/Header.h");
  42. /// ...
  43. /// }
  44. ///
  45. /// private:
  46. /// utils::IncludeInserter Inserter{utils::IncludeSorter::IS_Google};
  47. /// };
  48. /// } // namespace tidy
  49. /// } // namespace clang
  50. /// \endcode
  51. class IncludeInserter {
  52. public:
  53. /// Initializes the IncludeInserter using the IncludeStyle \p Style.
  54. /// In most cases the \p Style will be retrieved from the ClangTidyOptions
  55. /// using \code
  56. /// Options.getLocalOrGlobal("IncludeStyle", <DefaultStyle>)
  57. /// \endcode
  58. explicit IncludeInserter(IncludeSorter::IncludeStyle Style,
  59. bool SelfContainedDiags);
  60. /// Registers this with the Preprocessor \p PP, must be called before this
  61. /// class is used.
  62. void registerPreprocessor(Preprocessor *PP);
  63. /// Creates a \p Header inclusion directive fixit in the File \p FileID.
  64. /// When \p Header is enclosed in angle brackets, uses angle brackets in the
  65. /// inclusion directive, otherwise uses quotes.
  66. /// Returns ``std::nullopt`` on error or if the inclusion directive already
  67. /// exists.
  68. std::optional<FixItHint> createIncludeInsertion(FileID FileID,
  69. llvm::StringRef Header);
  70. /// Creates a \p Header inclusion directive fixit in the main file.
  71. /// When \p Header is enclosed in angle brackets, uses angle brackets in the
  72. /// inclusion directive, otherwise uses quotes.
  73. /// Returns ``std::nullopt`` on error or if the inclusion directive already
  74. /// exists.
  75. std::optional<FixItHint>
  76. createMainFileIncludeInsertion(llvm::StringRef Header);
  77. IncludeSorter::IncludeStyle getStyle() const { return Style; }
  78. private:
  79. void addInclude(StringRef FileName, bool IsAngled,
  80. SourceLocation HashLocation, SourceLocation EndLocation);
  81. IncludeSorter &getOrCreate(FileID FileID);
  82. llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
  83. llvm::DenseMap<FileID, llvm::StringSet<>> InsertedHeaders;
  84. const SourceManager *SourceMgr{nullptr};
  85. const IncludeSorter::IncludeStyle Style;
  86. const bool SelfContainedDiags;
  87. friend class IncludeInserterCallback;
  88. };
  89. } // namespace tidy::utils
  90. } // namespace clang
  91. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H