Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:22

0001 //===---------- IncludeInserter.h - clang-tidy ----------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H
0011 
0012 #include "IncludeSorter.h"
0013 #include "clang/Basic/Diagnostic.h"
0014 #include "llvm/ADT/StringSet.h"
0015 #include <memory>
0016 #include <optional>
0017 
0018 namespace clang {
0019 class Preprocessor;
0020 namespace tidy::utils {
0021 
0022 /// Produces fixes to insert specified includes to source files, if not
0023 /// yet present.
0024 ///
0025 /// ``IncludeInserter`` can be used in clang-tidy checks in the following way:
0026 /// \code
0027 /// #include "../ClangTidyCheck.h"
0028 /// #include "../utils/IncludeInserter.h"
0029 ///
0030 /// namespace clang {
0031 /// namespace tidy {
0032 ///
0033 /// class MyCheck : public ClangTidyCheck {
0034 ///  public:
0035 ///   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
0036 ///                            Preprocessor *ModuleExpanderPP) override {
0037 ///     Inserter.registerPreprocessor(PP);
0038 ///   }
0039 ///
0040 ///   void registerMatchers(ast_matchers::MatchFinder* Finder) override { ... }
0041 ///
0042 ///   void check(
0043 ///       const ast_matchers::MatchFinder::MatchResult& Result) override {
0044 ///     ...
0045 ///     Inserter.createMainFileIncludeInsertion("path/to/Header.h");
0046 ///     ...
0047 ///   }
0048 ///
0049 ///  private:
0050 ///   utils::IncludeInserter Inserter{utils::IncludeSorter::IS_Google};
0051 /// };
0052 /// } // namespace tidy
0053 /// } // namespace clang
0054 /// \endcode
0055 class IncludeInserter {
0056 public:
0057   /// Initializes the IncludeInserter using the IncludeStyle \p Style.
0058   /// In most cases the \p Style will be retrieved from the ClangTidyOptions
0059   /// using \code
0060   ///   Options.getLocalOrGlobal("IncludeStyle", <DefaultStyle>)
0061   /// \endcode
0062   explicit IncludeInserter(IncludeSorter::IncludeStyle Style,
0063                            bool SelfContainedDiags);
0064 
0065   /// Registers this with the Preprocessor \p PP, must be called before this
0066   /// class is used.
0067   void registerPreprocessor(Preprocessor *PP);
0068 
0069   /// Creates a \p Header inclusion directive fixit in the File \p FileID.
0070   /// When \p Header is enclosed in angle brackets, uses angle brackets in the
0071   /// inclusion directive, otherwise uses quotes.
0072   /// Returns ``std::nullopt`` on error or if the inclusion directive already
0073   /// exists.
0074   std::optional<FixItHint> createIncludeInsertion(FileID FileID,
0075                                                   llvm::StringRef Header);
0076 
0077   /// Creates a \p Header inclusion directive fixit in the main file.
0078   /// When \p Header is enclosed in angle brackets, uses angle brackets in the
0079   /// inclusion directive, otherwise uses quotes.
0080   /// Returns ``std::nullopt`` on error or if the inclusion directive already
0081   /// exists.
0082   std::optional<FixItHint>
0083   createMainFileIncludeInsertion(llvm::StringRef Header);
0084 
0085   IncludeSorter::IncludeStyle getStyle() const { return Style; }
0086 
0087 private:
0088   void addInclude(StringRef FileName, bool IsAngled,
0089                   SourceLocation HashLocation, SourceLocation EndLocation);
0090 
0091   IncludeSorter &getOrCreate(FileID FileID);
0092 
0093   llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
0094   llvm::DenseMap<FileID, llvm::StringSet<>> InsertedHeaders;
0095   const SourceManager *SourceMgr{nullptr};
0096   const IncludeSorter::IncludeStyle Style;
0097   const bool SelfContainedDiags;
0098   friend class IncludeInserterCallback;
0099 };
0100 
0101 } // namespace tidy::utils
0102 } // namespace clang
0103 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_INCLUDEINSERTER_H