clang-tools  14.0.0git
clang-tidy/utils/Matchers.h
Go to the documentation of this file.
1 //===--- Matchers.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 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_MATCHERS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_MATCHERS_H
11 
12 #include "TypeTraits.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 
15 namespace clang {
16 namespace tidy {
17 namespace matchers {
18 
19 AST_MATCHER(BinaryOperator, isRelationalOperator) {
20  return Node.isRelationalOp();
21 }
22 
23 AST_MATCHER(BinaryOperator, isEqualityOperator) { return Node.isEqualityOp(); }
24 
26  llvm::Optional<bool> IsExpensive =
27  utils::type_traits::isExpensiveToCopy(Node, Finder->getASTContext());
28  return IsExpensive && *IsExpensive;
29 }
30 
33  Node, Finder->getASTContext());
34 }
35 
38 }
39 
40 // Returns QualType matcher for references to const.
41 AST_MATCHER_FUNCTION(ast_matchers::TypeMatcher, isReferenceToConst) {
42  using namespace ast_matchers;
43  return referenceType(pointee(qualType(isConstQualified())));
44 }
45 
46 // Returns QualType matcher for pointers to const.
47 AST_MATCHER_FUNCTION(ast_matchers::TypeMatcher, isPointerToConst) {
48  using namespace ast_matchers;
49  return pointerType(pointee(qualType(isConstQualified())));
50 }
51 
52 // A matcher implementation that matches a list of type name regular expressions
53 // against a NamedDecl. If a regular expression contains the substring "::"
54 // matching will occur against the qualified name, otherwise only the typename.
56  : public ast_matchers::internal::MatcherInterface<NamedDecl> {
57 public:
58  explicit MatchesAnyListedNameMatcher(llvm::ArrayRef<std::string> NameList) {
59  std::transform(
60  NameList.begin(), NameList.end(), std::back_inserter(NameMatchers),
61  [](const llvm::StringRef Name) { return NameMatcher(Name); });
62  }
63  bool matches(
64  const NamedDecl &Node, ast_matchers::internal::ASTMatchFinder *Finder,
65  ast_matchers::internal::BoundNodesTreeBuilder *Builder) const override {
66  return llvm::any_of(NameMatchers, [&Node](const NameMatcher &NM) {
67  return NM.match(Node);
68  });
69  }
70 
71 private:
72  class NameMatcher {
73  llvm::Regex Regex;
74  enum class MatchMode {
75  // Match against the unqualified name because the regular expression
76  // does not contain ":".
77  MatchUnqualified,
78  // Match against the qualified name because the regular expression
79  // contains ":" suggesting name and namespace should be matched.
80  MatchQualified,
81  // Match against the fully qualified name because the regular expression
82  // starts with ":".
83  MatchFullyQualified,
84  };
85  MatchMode Mode;
86 
87  public:
88  NameMatcher(const llvm::StringRef Regex)
89  : Regex(Regex), Mode(determineMatchMode(Regex)) {}
90 
91  bool match(const NamedDecl &ND) const {
92  switch (Mode) {
93  case MatchMode::MatchQualified:
94  return Regex.match(ND.getQualifiedNameAsString());
95  case MatchMode::MatchFullyQualified:
96  return Regex.match("::" + ND.getQualifiedNameAsString());
97  default:
98  return Regex.match(ND.getName());
99  }
100  }
101 
102  private:
103  MatchMode determineMatchMode(llvm::StringRef Regex) {
104  if (Regex.startswith(":") || Regex.startswith("^:")) {
105  return MatchMode::MatchFullyQualified;
106  }
107  return Regex.contains(":") ? MatchMode::MatchQualified
108  : MatchMode::MatchUnqualified;
109  }
110  };
111 
112  std::vector<NameMatcher> NameMatchers;
113 };
114 
115 // Returns a matcher that matches NamedDecl's against a list of provided regular
116 // expressions. If a regular expression contains starts ':' the NamedDecl's
117 // qualified name will be used for matching, otherwise its name will be used.
118 inline ::clang::ast_matchers::internal::Matcher<NamedDecl>
119 matchesAnyListedName(llvm::ArrayRef<std::string> NameList) {
120  return ::clang::ast_matchers::internal::makeMatcher(
121  new MatchesAnyListedNameMatcher(NameList));
122 }
123 
124 } // namespace matchers
125 } // namespace tidy
126 } // namespace clang
127 
128 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_MATCHERS_H
clang::tidy::matchers::MatchesAnyListedNameMatcher
Definition: clang-tidy/utils/Matchers.h:55
clang::tidy::utils::type_traits::isTriviallyDestructible
bool isTriviallyDestructible(QualType Type)
Returns true if Type is trivially destructible.
Definition: TypeTraits.cpp:139
clang::tidy::matchers::MatchesAnyListedNameMatcher::MatchesAnyListedNameMatcher
MatchesAnyListedNameMatcher(llvm::ArrayRef< std::string > NameList)
Definition: clang-tidy/utils/Matchers.h:58
clang::tidy::utils::type_traits::isExpensiveToCopy
llvm::Optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
Definition: TypeTraits.cpp:41
clang::tidy::utils::type_traits::isTriviallyDefaultConstructible
bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context)
Returns true if Type is trivially default constructible.
Definition: TypeTraits.cpp:92
clang::clangd::match
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
TypeTraits.h
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:36
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::matchers::AST_MATCHER_FUNCTION
AST_MATCHER_FUNCTION(ast_matchers::TypeMatcher, isReferenceToConst)
Definition: clang-tidy/utils/Matchers.h:41
clang::tidy::utils::type_traits::recordIsTriviallyDefaultConstructible
bool recordIsTriviallyDefaultConstructible(const RecordDecl &RecordDecl, const ASTContext &Context)
Returns true if RecordDecl is trivially default constructible.
Definition: TypeTraits.cpp:51
clang::tidy::matchers::AST_MATCHER
AST_MATCHER(BinaryOperator, isRelationalOperator)
Definition: clang-tidy/utils/Matchers.h:19
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::matchers::MatchesAnyListedNameMatcher::matches
bool matches(const NamedDecl &Node, ast_matchers::internal::ASTMatchFinder *Finder, ast_matchers::internal::BoundNodesTreeBuilder *Builder) const override
Definition: clang-tidy/utils/Matchers.h:63
clang::tidy::matchers::matchesAnyListedName
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< std::string > NameList)
Definition: clang-tidy/utils/Matchers.h:119