xref: /llvm-project/clang-tools-extra/clang-tidy/utils/Matchers.cpp (revision 01c11569fc67b2a00403f64695fff6d2b4e78fe5)
1 //===---------- Matchers.cpp - 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 #include "Matchers.h"
10 #include "ASTUtils.h"
11 
12 namespace clang::tidy::matchers {
13 
operator ()(const clang::ast_matchers::internal::BoundNodesMap & Nodes) const14 bool NotIdenticalStatementsPredicate::operator()(
15     const clang::ast_matchers::internal::BoundNodesMap &Nodes) const {
16   return !utils::areStatementsIdentical(Node.get<Stmt>(),
17                                         Nodes.getNodeAs<Stmt>(ID), *Context);
18 }
19 
MatchesAnyListedTypeNameMatcher(llvm::ArrayRef<StringRef> NameList)20 MatchesAnyListedTypeNameMatcher::MatchesAnyListedTypeNameMatcher(
21     llvm::ArrayRef<StringRef> NameList)
22     : NameMatchers(NameList.begin(), NameList.end()) {}
23 
24 MatchesAnyListedTypeNameMatcher::~MatchesAnyListedTypeNameMatcher() = default;
25 
matches(const QualType & Node,ast_matchers::internal::ASTMatchFinder * Finder,ast_matchers::internal::BoundNodesTreeBuilder * Builder) const26 bool MatchesAnyListedTypeNameMatcher::matches(
27     const QualType &Node, ast_matchers::internal::ASTMatchFinder *Finder,
28     ast_matchers::internal::BoundNodesTreeBuilder *Builder) const {
29 
30   if (NameMatchers.empty())
31     return false;
32 
33   PrintingPolicy PrintingPolicyWithSuppressedTag(
34       Finder->getASTContext().getLangOpts());
35   PrintingPolicyWithSuppressedTag.PrintCanonicalTypes = true;
36   PrintingPolicyWithSuppressedTag.SuppressElaboration = true;
37   PrintingPolicyWithSuppressedTag.SuppressScope = false;
38   PrintingPolicyWithSuppressedTag.SuppressTagKeyword = true;
39   PrintingPolicyWithSuppressedTag.SuppressUnwrittenScope = true;
40   std::string TypeName =
41       Node.getUnqualifiedType().getAsString(PrintingPolicyWithSuppressedTag);
42 
43   return llvm::any_of(NameMatchers, [&TypeName](const llvm::Regex &NM) {
44     return NM.isValid() && NM.match(TypeName);
45   });
46 }
47 
48 } // namespace clang::tidy::matchers
49