https://github.com/vbvictor created https://github.com/llvm/llvm-project/pull/203779
None >From 5cf7a21d6c42897fc940da161275b0c2229e683c Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Sun, 14 Jun 2026 21:35:29 +0300 Subject: [PATCH] [clang][ASTMatchers] format AST matchers internal --- .../clang/ASTMatchers/ASTMatchFinder.h | 33 +++-- .../clang/ASTMatchers/ASTMatchersInternal.h | 115 +++++++----------- 2 files changed, 58 insertions(+), 90 deletions(-) diff --git a/clang/include/clang/ASTMatchers/ASTMatchFinder.h b/clang/include/clang/ASTMatchers/ASTMatchFinder.h index b0ccbf22a4269..6906852fd09c7 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchFinder.h +++ b/clang/include/clang/ASTMatchers/ASTMatchFinder.h @@ -82,8 +82,8 @@ class MatchFinder { /// Utilities for interpreting the matched AST structures. /// @{ - clang::ASTContext * const Context; - clang::SourceManager * const SourceManager; + clang::ASTContext *const Context; + clang::SourceManager *const SourceManager; /// @} }; @@ -159,18 +159,14 @@ class MatchFinder { /// /// Does not take ownership of 'Action'. /// @{ - void addMatcher(const DeclarationMatcher &NodeMatch, - MatchCallback *Action); - void addMatcher(const TypeMatcher &NodeMatch, - MatchCallback *Action); - void addMatcher(const StatementMatcher &NodeMatch, - MatchCallback *Action); + void addMatcher(const DeclarationMatcher &NodeMatch, MatchCallback *Action); + void addMatcher(const TypeMatcher &NodeMatch, MatchCallback *Action); + void addMatcher(const StatementMatcher &NodeMatch, MatchCallback *Action); void addMatcher(const NestedNameSpecifierMatcher &NodeMatch, MatchCallback *Action); void addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch, MatchCallback *Action); - void addMatcher(const TypeLocMatcher &NodeMatch, - MatchCallback *Action); + void addMatcher(const TypeLocMatcher &NodeMatch, MatchCallback *Action); void addMatcher(const CXXCtorInitializerMatcher &NodeMatch, MatchCallback *Action); void addMatcher(const TemplateArgumentLocMatcher &NodeMatch, @@ -257,8 +253,8 @@ class MatchFinder { /// \see selectFirst /// @{ template <typename MatcherT, typename NodeT> -SmallVector<BoundNodes, 1> -match(MatcherT Matcher, const NodeT &Node, ASTContext &Context); +SmallVector<BoundNodes, 1> match(MatcherT Matcher, const NodeT &Node, + ASTContext &Context); template <typename MatcherT> SmallVector<BoundNodes, 1> match(MatcherT Matcher, const DynTypedNode &Node, @@ -281,8 +277,8 @@ SmallVector<BoundNodes, 1> match(MatcherT Matcher, ASTContext &Context); /// Node, Context)); /// \endcode template <typename NodeT> -const NodeT * -selectFirst(StringRef BoundTo, const SmallVectorImpl<BoundNodes> &Results) { +const NodeT *selectFirst(StringRef BoundTo, + const SmallVectorImpl<BoundNodes> &Results) { for (const BoundNodes &N : Results) { if (const NodeT *Node = N.getNodeAs<NodeT>(BoundTo)) return Node; @@ -303,7 +299,7 @@ class CollectMatchesCallback : public MatchFinder::MatchCallback { SmallVector<BoundNodes, 1> Nodes; }; -} +} // namespace internal template <typename MatcherT> SmallVector<BoundNodes, 1> match(MatcherT Matcher, const DynTypedNode &Node, @@ -316,14 +312,13 @@ SmallVector<BoundNodes, 1> match(MatcherT Matcher, const DynTypedNode &Node, } template <typename MatcherT, typename NodeT> -SmallVector<BoundNodes, 1> -match(MatcherT Matcher, const NodeT &Node, ASTContext &Context) { +SmallVector<BoundNodes, 1> match(MatcherT Matcher, const NodeT &Node, + ASTContext &Context) { return match(Matcher, DynTypedNode::create(Node), Context); } template <typename MatcherT> -SmallVector<BoundNodes, 1> -match(MatcherT Matcher, ASTContext &Context) { +SmallVector<BoundNodes, 1> match(MatcherT Matcher, ASTContext &Context) { internal::CollectMatchesCallback Callback; MatchFinder Finder; Finder.addMatcher(Matcher, &Callback); diff --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h index cb12d10d628b6..fe18ee1c6d942 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -124,7 +124,7 @@ struct VariadicFunction { ResultT operator()() const { return Func({}); } template <typename... ArgsT> - ResultT operator()(const ArgT &Arg1, const ArgsT &... Args) const { + ResultT operator()(const ArgT &Arg1, const ArgsT &...Args) const { return Execute(Arg1, static_cast<const ArgT &>(Args)...); } @@ -137,7 +137,7 @@ struct VariadicFunction { private: // Trampoline function to allow for implicit conversions to take place // before we make the array. - template <typename... ArgsT> ResultT Execute(const ArgsT &... Args) const { + template <typename... ArgsT> ResultT Execute(const ArgsT &...Args) const { const ArgT *const ArgsArray[] = {&Args...}; return Func(ArrayRef<const ArgT *>(ArgsArray, sizeof...(ArgsT))); } @@ -224,8 +224,7 @@ class BoundNodesMap { /// /// Returns NULL if there was no node bound to \c ID or if there is a node but /// it cannot be converted to the specified type. - template <typename T> - const T *getNodeAs(StringRef ID) const { + template <typename T> const T *getNodeAs(StringRef ID) const { IDToNodeMap::const_iterator It = NodeMap.find(ID); if (It == NodeMap.end()) { return nullptr; @@ -253,9 +252,7 @@ class BoundNodesMap { /// - we need an assignment operator using IDToNodeMap = std::map<std::string, DynTypedNode, std::less<>>; - const IDToNodeMap &getMap() const { - return NodeMap; - } + const IDToNodeMap &getMap() const { return NodeMap; } /// Returns \c true if this \c BoundNodesMap can be compared, i.e. all /// stored nodes have memoization data. @@ -286,7 +283,7 @@ class BoundNodesTreeBuilder { /// Called multiple times during a single call to VisitMatches(...). /// /// 'BoundNodesView' contains the bound nodes for a single match. - virtual void visitMatch(const BoundNodes& BoundNodesView) = 0; + virtual void visitMatch(const BoundNodes &BoundNodesView) = 0; }; /// Add a binding from an id to a node. @@ -303,7 +300,7 @@ class BoundNodesTreeBuilder { /// Visits all matches that this BoundNodesTree represents. /// /// The ownership of 'ResultVisitor' remains at the caller. - void visitMatches(Visitor* ResultVisitor); + void visitMatches(Visitor *ResultVisitor); template <typename ExcludePredicate> bool removeBindings(const ExcludePredicate &Predicate) { @@ -361,15 +358,13 @@ class DynMatcherInterface /// writing a simple matcher that only inspects properties of the /// current node and doesn't care about its children or descendants, /// implement SingleNodeMatcherInterface instead. -template <typename T> -class MatcherInterface : public DynMatcherInterface { +template <typename T> class MatcherInterface : public DynMatcherInterface { public: /// Returns true if 'Node' can be matched. /// /// May bind 'Node' to an ID via 'Builder', or recurse into /// the AST via 'Finder'. - virtual bool matches(const T &Node, - ASTMatchFinder *Finder, + virtual bool matches(const T &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const = 0; bool dynMatches(const DynTypedNode &DynNode, ASTMatchFinder *Finder, @@ -390,8 +385,7 @@ class SingleNodeMatcherInterface : public MatcherInterface<T> { private: /// Implements MatcherInterface::Matches. - bool matches(const T &Node, - ASTMatchFinder * /* Finder */, + bool matches(const T &Node, ASTMatchFinder * /* Finder */, BoundNodesTreeBuilder * /* Builder */) const override { return matchesNode(Node); } @@ -564,8 +558,7 @@ class DynTypedMatcher { /// with respect to T. The relationship is built via a type conversion /// operator rather than a type hierarchy to be able to templatize the /// type hierarchy instead of spelling it out. -template <typename T> -class Matcher { +template <typename T> class Matcher { public: /// Takes ownership of the provided implementation pointer. explicit Matcher(MatcherInterface<T> *Implementation) @@ -606,8 +599,7 @@ class Matcher { } /// Forwards the call to the underlying MatcherInterface<T> pointer. - bool matches(const T &Node, - ASTMatchFinder *Finder, + bool matches(const T &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const { return Implementation.matches(DynTypedNode::create(Node), Finder, Builder); } @@ -670,7 +662,7 @@ class Matcher { } DynTypedMatcher Implementation; -}; // class Matcher +}; // class Matcher // Deduction guide for Matcher. template <typename T> Matcher(MatcherInterface<T> *) -> Matcher<T>; @@ -809,6 +801,7 @@ class ASTMatchFinder { const DynTypedMatcher &Matcher, BoundNodesTreeBuilder *Builder, AncestorMatchMode MatchMode) = 0; + private: friend struct ASTChildrenNotSpelledInSourceScope; virtual bool isMatchingChildrenNotSpelledInSource() const = 0; @@ -896,7 +889,7 @@ static constexpr bool has_getDecl = template <typename T, typename ArgT> class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface<T> { static_assert(std::is_same<T, CXXOperatorCallExpr>::value || - std::is_base_of<FunctionDecl, T>::value, + std::is_base_of<FunctionDecl, T>::value, "unsupported class for matcher"); static_assert(std::is_same<ArgT, std::vector<std::string>>::value, "argument type must be std::vector<std::string>"); @@ -910,7 +903,6 @@ class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface<T> { } private: - /// CXXOperatorCallExpr exist only for calls to overloaded operators /// so this function returns true if the call is to an operator of the given /// name. @@ -933,7 +925,7 @@ class HasOverloadedOperatorNameMatcher : public SingleNodeMatcherInterface<T> { /// /// See \c hasName() and \c hasAnyName() in ASTMatchers.h for details. class HasNameMatcher : public SingleNodeMatcherInterface<NamedDecl> { - public: +public: explicit HasNameMatcher(std::vector<std::string> Names); bool matchesNode(const NamedDecl &Node) const override; @@ -970,8 +962,8 @@ Matcher<NamedDecl> hasAnyNameFunc(ArrayRef<const StringRef *> NameRefs); /// Trampoline function to use VariadicFunction<> to construct a /// hasAnySelector matcher. -Matcher<ObjCMessageExpr> hasAnySelectorFunc( - ArrayRef<const StringRef *> NameRefs); +Matcher<ObjCMessageExpr> +hasAnySelectorFunc(ArrayRef<const StringRef *> NameRefs); /// Matches declarations for QualType and CallExpr. /// @@ -1091,14 +1083,12 @@ class HasDeclarationMatcher : public MatcherInterface<T> { /// Extracts the Decl of the constructor call and returns whether the /// inner matcher matches on it. - bool matchesSpecialized(const CXXConstructExpr &Node, - ASTMatchFinder *Finder, + bool matchesSpecialized(const CXXConstructExpr &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const { return matchesDecl(Node.getConstructor(), Finder, Builder); } - bool matchesSpecialized(const ObjCIvarRefExpr &Node, - ASTMatchFinder *Finder, + bool matchesSpecialized(const ObjCIvarRefExpr &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const { return matchesDecl(Node.getDecl(), Finder, Builder); } @@ -1110,24 +1100,21 @@ class HasDeclarationMatcher : public MatcherInterface<T> { /// Extracts the operator new of the new call and returns whether the /// inner matcher matches on it. - bool matchesSpecialized(const CXXNewExpr &Node, - ASTMatchFinder *Finder, + bool matchesSpecialized(const CXXNewExpr &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const { return matchesDecl(Node.getOperatorNew(), Finder, Builder); } /// Extracts the \c ValueDecl a \c MemberExpr refers to and returns /// whether the inner matcher matches on it. - bool matchesSpecialized(const MemberExpr &Node, - ASTMatchFinder *Finder, + bool matchesSpecialized(const MemberExpr &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const { return matchesDecl(Node.getMemberDecl(), Finder, Builder); } /// Extracts the \c LabelDecl a \c AddrLabelExpr refers to and returns /// whether the inner matcher matches on it. - bool matchesSpecialized(const AddrLabelExpr &Node, - ASTMatchFinder *Finder, + bool matchesSpecialized(const AddrLabelExpr &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const { return matchesDecl(Node.getLabel(), Finder, Builder); } @@ -1153,8 +1140,7 @@ class HasDeclarationMatcher : public MatcherInterface<T> { /// IsBaseType<T>::value is true if T is a "base" type in the AST /// node class hierarchies. -template <typename T> -struct IsBaseType { +template <typename T> struct IsBaseType { static const bool value = std::is_same<T, Decl>::value || std::is_same<T, Stmt>::value || std::is_same<T, QualType>::value || std::is_same<T, Type>::value || @@ -1165,8 +1151,7 @@ struct IsBaseType { std::is_same<T, TemplateArgumentLoc>::value || std::is_same<T, Attr>::value; }; -template <typename T> -const bool IsBaseType<T>::value; +template <typename T> const bool IsBaseType<T>::value; /// A "type list" that contains all types. /// @@ -1339,7 +1324,7 @@ class VariadicAllOfMatcher /// conversion operator. template <typename... Ps> class VariadicOperatorMatcher { public: - VariadicOperatorMatcher(DynTypedMatcher::VariadicOperator Op, Ps &&... Params) + VariadicOperatorMatcher(DynTypedMatcher::VariadicOperator Op, Ps &&...Params) : Op(Op), Params(std::forward<Ps>(Params)...) {} template <typename T> operator Matcher<T>() const & { @@ -1379,7 +1364,7 @@ struct VariadicOperatorMatcherFunc { DynTypedMatcher::VariadicOperator Op; template <typename... Ms> - VariadicOperatorMatcher<Ms...> operator()(Ms &&... Ps) const { + VariadicOperatorMatcher<Ms...> operator()(Ms &&...Ps) const { static_assert(MinCount <= sizeof...(Ms) && sizeof...(Ms) <= MaxCount, "invalid number of parameters for variadic matcher"); return VariadicOperatorMatcher<Ms...>(Op, std::forward<Ms>(Ps)...); @@ -1402,8 +1387,7 @@ template <typename CladeType, typename... MatcherTypes> struct MapAnyOfMatcherImpl { template <typename... InnerMatchers> - BindableMatcher<CladeType> - operator()(InnerMatchers &&... InnerMatcher) const { + BindableMatcher<CladeType> operator()(InnerMatchers &&...InnerMatcher) const { return VariadicAllOfMatcher<CladeType>()(std::apply( internal::VariadicOperatorMatcherFunc< 0, std::numeric_limits<unsigned>::max()>{ @@ -1553,7 +1537,7 @@ template <template <typename T, typename... Params> class MatcherT, typename ReturnTypesF, typename... ParamTypes> class PolymorphicMatcher { public: - PolymorphicMatcher(const ParamTypes &... Params) : Params(Params...) {} + PolymorphicMatcher(const ParamTypes &...Params) : Params(Params...) {} using ReturnTypes = typename ExtractFunctionArgMeta<ReturnTypesF>::type; @@ -1611,9 +1595,8 @@ class ForEachMatcher : public MatcherInterface<T> { bool matches(const T &Node, ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) const override { - return Finder->matchesChildOf( - Node, this->InnerMatcher, Builder, - ASTMatchFinder::BK_All); + return Finder->matchesChildOf(Node, this->InnerMatcher, Builder, + ASTMatchFinder::BK_All); } }; @@ -1861,10 +1844,8 @@ class TypeLocTraverseMatcher : public MatcherInterface<T> { /// \code Getter<OuterT>::value() \endcode returns a /// \code InnerTBase (OuterT::*)() \endcode, which is used to adapt a \c OuterT /// object into a \c InnerT -template <typename InnerTBase, - template <typename OuterT> class Getter, - template <typename OuterT> class MatcherImpl, - typename ReturnTypesF> +template <typename InnerTBase, template <typename OuterT> class Getter, + template <typename OuterT> class MatcherImpl, typename ReturnTypesF> class TypeTraversePolymorphicMatcher { private: using Self = TypeTraversePolymorphicMatcher<InnerTBase, Getter, MatcherImpl, @@ -1917,9 +1898,8 @@ template <typename Matcher, Matcher (*Func)()> class MemoizedMatcher { template <typename InnerTBase, template <typename OuterT> class Getter, template <typename OuterT> class MatcherImpl, typename ReturnTypesF> TypeTraversePolymorphicMatcher<InnerTBase, Getter, MatcherImpl, ReturnTypesF> -TypeTraversePolymorphicMatcher< - InnerTBase, Getter, MatcherImpl, - ReturnTypesF>::create(ArrayRef<const Matcher<InnerTBase> *> InnerMatchers) { +TypeTraversePolymorphicMatcher<InnerTBase, Getter, MatcherImpl, ReturnTypesF>:: + create(ArrayRef<const Matcher<InnerTBase> *> InnerMatchers) { return Self(InnerMatchers); } @@ -1942,7 +1922,7 @@ getTemplateSpecializationArgs(const TemplateSpecializationType &T) { inline ArrayRef<TemplateArgument> getTemplateSpecializationArgs(const FunctionDecl &FD) { - if (const auto* TemplateArgs = FD.getTemplateSpecializationArgs()) + if (const auto *TemplateArgs = FD.getTemplateSpecializationArgs()) return TemplateArgs->asArray(); return {}; } @@ -2206,37 +2186,30 @@ getSubExpr<CXXOperatorCallExpr>(const CXXOperatorCallExpr &Node) { return Node.getArg(0); } -template <typename Ty> -struct HasSizeMatcher { +template <typename Ty> struct HasSizeMatcher { static bool hasSize(const Ty &Node, unsigned int N) { return Node.getSize() == N; } }; template <> -inline bool HasSizeMatcher<StringLiteral>::hasSize( - const StringLiteral &Node, unsigned int N) { +inline bool HasSizeMatcher<StringLiteral>::hasSize(const StringLiteral &Node, + unsigned int N) { return Node.getLength() == N; } -template <typename Ty> -struct GetSourceExpressionMatcher { - static const Expr *get(const Ty &Node) { - return Node.getSubExpr(); - } +template <typename Ty> struct GetSourceExpressionMatcher { + static const Expr *get(const Ty &Node) { return Node.getSubExpr(); } }; template <> -inline const Expr *GetSourceExpressionMatcher<OpaqueValueExpr>::get( - const OpaqueValueExpr &Node) { +inline const Expr * +GetSourceExpressionMatcher<OpaqueValueExpr>::get(const OpaqueValueExpr &Node) { return Node.getSourceExpr(); } -template <typename Ty> -struct CompoundStmtMatcher { - static const CompoundStmt *get(const Ty &Node) { - return &Node; - } +template <typename Ty> struct CompoundStmtMatcher { + static const CompoundStmt *get(const Ty &Node) { return &Node; } }; template <> _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
