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