clang-tools 23.0.0git
utils/UseRangesCheck.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_UTILS_USERANGESCHECK_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_USERANGESCHECK_H
11
12#include "../ClangTidyCheck.h"
13#include "IncludeInserter.h"
14#include "clang/AST/Decl.h"
15#include "clang/AST/Expr.h"
16#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/IntrusiveRefCntPtr.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
20#include <optional>
21
22namespace clang::tidy::utils {
23
24/// Base class for handling converting std iterator algorithms to a range
25/// equivalent.
27public:
28 struct Indexes {
29 enum Replace { First, Second };
30 unsigned BeginArg;
31 unsigned EndArg = BeginArg + 1;
33 };
34
36
39 std::optional<StringRef> ReverseHeader;
40 ArrayRef<std::pair<StringRef, StringRef>> FreeReverseNames;
41 bool IsPipeSyntax = false;
42 };
43
44 class Replacer : public llvm::RefCountedBase<Replacer> {
45 public:
55
56 /// Gets the name to replace a function with, return std::nullopt for a
57 /// replacement where we just call a different overload.
58 virtual std::optional<std::string>
59 getReplaceName(const NamedDecl &OriginalName) const = 0;
60
61 /// Gets the header needed to access the replaced function
62 /// Return std::nullopt if no new header is needed.
63 virtual std::optional<std::string>
64 getHeaderInclusion(const NamedDecl &OriginalName) const;
65
66 /// Gets an array of all the possible overloads for a function with indexes
67 /// where begin and end arguments are.
68 virtual ArrayRef<Signature> getReplacementSignatures() const = 0;
69
70 /// Gets how to handle fix-its when the original algorithm result is used.
71 virtual ResultUsePolicy getResultUsePolicy(const NamedDecl &OriginalName,
72 bool IsStructuredBinding) const;
73 virtual ~Replacer() = default;
74 };
75
76 using ReplacerMap = llvm::StringMap<llvm::IntrusiveRefCntPtr<Replacer>>;
77
78 UseRangesCheck(StringRef Name, ClangTidyContext *Context);
79 /// Gets a map of function to replace and methods to create the replacements
80 virtual ReplacerMap getReplacerMap() const = 0;
81 /// Create a diagnostic for the CallExpr
82 /// Override this to support custom diagnostic messages
83 virtual DiagnosticBuilder createDiag(const CallExpr &Call);
84
85 virtual std::optional<ReverseIteratorDescriptor> getReverseDescriptor() const;
86
87 /// Gets the fully qualified names of begin and end functions.
88 /// The functions must take the container as their one and only argument
89 /// `::std::begin` and `::std::end` are a common example
90 virtual ArrayRef<std::pair<StringRef, StringRef>>
92
93 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
94 Preprocessor *ModuleExpanderPP) final;
95 void registerMatchers(ast_matchers::MatchFinder *Finder) final;
96 void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
97 bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;
98 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
99 std::optional<TraversalKind> getCheckTraversalKind() const override;
100
101private:
102 std::vector<llvm::IntrusiveRefCntPtr<Replacer>> Replacers;
103 std::optional<ReverseIteratorDescriptor> ReverseDescriptor;
104 IncludeInserter Inserter;
105};
106
107} // namespace clang::tidy::utils
108
109#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_USERANGESCHECK_H
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
virtual ArrayRef< Signature > getReplacementSignatures() const =0
Gets an array of all the possible overloads for a function with indexes where begin and end arguments...
virtual ResultUsePolicy getResultUsePolicy(const NamedDecl &OriginalName, bool IsStructuredBinding) const
Gets how to handle fix-its when the original algorithm result is used.
virtual std::optional< std::string > getHeaderInclusion(const NamedDecl &OriginalName) const
Gets the header needed to access the replaced function Return std::nullopt if no new header is needed...
virtual std::optional< std::string > getReplaceName(const NamedDecl &OriginalName) const =0
Gets the name to replace a function with, return std::nullopt for a replacement where we just call a ...
llvm::StringMap< llvm::IntrusiveRefCntPtr< Replacer > > ReplacerMap
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
virtual DiagnosticBuilder createDiag(const CallExpr &Call)
Create a diagnostic for the CallExpr Override this to support custom diagnostic messages.
void registerMatchers(ast_matchers::MatchFinder *Finder) final
std::optional< TraversalKind > getCheckTraversalKind() const override
void check(const ast_matchers::MatchFinder::MatchResult &Result) final
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) final
virtual ReplacerMap getReplacerMap() const =0
Gets a map of function to replace and methods to create the replacements.
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
UseRangesCheck(StringRef Name, ClangTidyContext *Context)
virtual std::optional< ReverseIteratorDescriptor > getReverseDescriptor() const
virtual ArrayRef< std::pair< StringRef, StringRef > > getFreeBeginEndMethods() const
Gets the fully qualified names of begin and end functions.
llvm::StringMap< ClangTidyValue > OptionMap
ArrayRef< std::pair< StringRef, StringRef > > FreeReverseNames