| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 40 |
AST_MATCHER_FUNCTION(ast_matchers::TypeMatcher, isReferenceToConst) { |
| 41 |
using namespace ast_matchers; |
| 42 |
return referenceType(pointee(qualType(isConstQualified()))); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
AST_MATCHER_FUNCTION(ast_matchers::TypeMatcher, isPointerToConst) { |
| 47 |
using namespace ast_matchers; |
| 48 |
return pointerType(pointee(qualType(isConstQualified()))); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 75 |
|
| 76 |
MatchUnqualified, |
| 77 |
|
| 78 |
|
| 79 |
MatchQualified, |
| 80 |
|
| 81 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 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 |
} |
| 124 |
|
| 125 |
#endif |