Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===--- UseRangesCheck.h - clang-tidy --------------------------*- C++ -*-===//
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_UTILS_USERANGESCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_USERANGESCHECK_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 #include "IncludeInserter.h"
0014 #include "clang/AST/Decl.h"
0015 #include "clang/AST/Expr.h"
0016 #include "clang/Basic/Diagnostic.h"
0017 #include "llvm/ADT/IntrusiveRefCntPtr.h"
0018 #include "llvm/ADT/StringMap.h"
0019 #include "llvm/ADT/StringRef.h"
0020 #include <optional>
0021 
0022 namespace clang::tidy::utils {
0023 
0024 /// Base class for handling converting std iterator algorithms to a range
0025 /// equivalent.
0026 class UseRangesCheck : public ClangTidyCheck {
0027 public:
0028   struct Indexes {
0029     enum Replace { First, Second };
0030     unsigned BeginArg;
0031     unsigned EndArg = BeginArg + 1;
0032     Replace ReplaceArg = First;
0033   };
0034 
0035   using Signature = SmallVector<Indexes, 2>;
0036 
0037   struct ReverseIteratorDescriptor {
0038     StringRef ReverseAdaptorName;
0039     std::optional<StringRef> ReverseHeader;
0040     ArrayRef<std::pair<StringRef, StringRef>> FreeReverseNames;
0041     bool IsPipeSyntax = false;
0042   };
0043 
0044   class Replacer : public llvm::RefCountedBase<Replacer> {
0045   public:
0046     /// Gets the name to replace a function with, return std::nullopt for a
0047     /// replacement where we just call a different overload.
0048     virtual std::optional<std::string>
0049     getReplaceName(const NamedDecl &OriginalName) const = 0;
0050 
0051     /// Gets the header needed to access the replaced function
0052     /// Return std::nullopt if no new header is needed.
0053     virtual std::optional<std::string>
0054     getHeaderInclusion(const NamedDecl &OriginalName) const;
0055 
0056     /// Gets an array of all the possible overloads for a function with indexes
0057     /// where begin and end arguments are.
0058     virtual ArrayRef<Signature> getReplacementSignatures() const = 0;
0059     virtual ~Replacer() = default;
0060   };
0061 
0062   using ReplacerMap = llvm::StringMap<llvm::IntrusiveRefCntPtr<Replacer>>;
0063 
0064   UseRangesCheck(StringRef Name, ClangTidyContext *Context);
0065   /// Gets a map of function to replace and methods to create the replacements
0066   virtual ReplacerMap getReplacerMap() const = 0;
0067   /// Create a diagnostic for the CallExpr
0068   /// Override this to support custom diagnostic messages
0069   virtual DiagnosticBuilder createDiag(const CallExpr &Call);
0070 
0071   virtual std::optional<ReverseIteratorDescriptor> getReverseDescriptor() const;
0072 
0073   /// Gets the fully qualified names of begin and end functions.
0074   /// The functions must take the container as their one and only argument
0075   /// `::std::begin` and `::std::end` are a common example
0076   virtual ArrayRef<std::pair<StringRef, StringRef>>
0077   getFreeBeginEndMethods() const;
0078 
0079   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
0080                            Preprocessor *ModuleExpanderPP) final;
0081   void registerMatchers(ast_matchers::MatchFinder *Finder) final;
0082   void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
0083   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;
0084   void storeOptions(ClangTidyOptions::OptionMap &Options) override;
0085   std::optional<TraversalKind> getCheckTraversalKind() const override;
0086 
0087 private:
0088   std::vector<llvm::IntrusiveRefCntPtr<Replacer>> Replacers;
0089   std::optional<ReverseIteratorDescriptor> ReverseDescriptor;
0090   IncludeInserter Inserter;
0091 };
0092 
0093 } // namespace clang::tidy::utils
0094 
0095 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_USERANGESCHECK_H