[PATCH] D113943: Add `withTag` matcher.
jcking1034 updated this revision to Diff 390429. jcking1034 added a comment. Chang `withTag` to `withIntrospection` Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D113943/new/ https://reviews.llvm.org/D113943 Files: clang/include/clang/ASTMatchers/ASTMatchers.h clang/include/clang/ASTMatchers/ASTMatchersInternal.h Index: clang/include/clang/ASTMatchers/ASTMatchersInternal.h === --- clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -43,6 +43,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/ExprObjC.h" #include "clang/AST/NestedNameSpecifier.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/Stmt.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" @@ -1057,6 +1058,46 @@ std::vector Names; }; +template +class WithIntrospectionMatcher : public MatcherInterface { +public: + explicit WithIntrospectionMatcher(std::string _BeforeTag, +std::string _AfterTag, +internal::Matcher _InnerMatcher) + : BeforeTag(std::move(_BeforeTag)), AfterTag(std::move(_AfterTag)), +InnerMatcher(_InnerMatcher) {} + explicit WithIntrospectionMatcher(internal::Matcher _InnerMatcher) + : BeforeTag("â Attempting new match"), +AfterTag("âï¸ Concluding attempt"), InnerMatcher(_InnerMatcher) {} + bool matches(const T , ASTMatchFinder *Finder, + BoundNodesTreeBuilder *Builder) const override { +DynTypedNode DTN = DynTypedNode::create(Node); + +llvm::errs() << BeforeTag << "\n"; + +llvm::errs() << "Matcher Name: " << InnerMatcher.getName() << "\n"; + +llvm::errs() << "Node Kind: " << DTN.getNodeKind().asStringRef() << "\n" + << "Node Value:\n```\n"; +DTN.print(llvm::errs(), PrintingPolicy(LangOptions())); +llvm::errs() << "\n```\n" + << "Node AST:\n"; +DTN.dump(llvm::errs(), Finder->getASTContext()); + +bool result = InnerMatcher.matches(Node, Finder, Builder); +llvm::errs() << "Result: " << (result ? "Successful\n" : "Unsuccessful\n"); + +llvm::errs() << AfterTag << "\n\n"; + +return result; + } + +private: + std::string BeforeTag; + std::string AfterTag; + internal::Matcher InnerMatcher; +}; + /// Matches named declarations with a specific name. /// /// See \c hasName() and \c hasAnyName() in ASTMatchers.h for details. @@ -1734,6 +1775,38 @@ std::tuple Params; }; +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +class PolymorphicWithIntrospectionMatcher { +public: + PolymorphicWithIntrospectionMatcher( + internal::PolymorphicMatcher + _InnerMatcher) + : InnerMatcher(_InnerMatcher) {} + + using ReturnTypes = typename ExtractFunctionArgMeta::type; + + template operator Matcher() const LLVM_LVALUE_FUNCTION { +static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); +return internal::Matcher(new internal::WithIntrospectionMatcher( +internal::Matcher(InnerMatcher))); + } + +#if LLVM_HAS_RVALUE_REFERENCE_THIS + template operator Matcher() && { +static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); +return internal::Matcher(new internal::WithIntrospectionMatcher( +internal::Matcher(std::move(InnerMatcher; + } +#endif + +private: + internal::PolymorphicMatcher + InnerMatcher; +}; + /// Matches nodes of type T that have child nodes of type ChildT for /// which a specified child matcher matches. /// Index: clang/include/clang/ASTMatchers/ASTMatchers.h === --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -2993,6 +2993,46 @@ new internal::HasNameMatcher({std::string(Name)})); } +template +inline internal::Matcher +withIntrospection(std::string BeforeTag, std::string AfterTag, + const internal::Matcher ) { + return internal::Matcher(new internal::WithIntrospectionMatcher( + BeforeTag, AfterTag, InnerMatcher)); +} + +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +inline internal::PolymorphicWithIntrospectionMatcher +withIntrospection( +std::string BeforeTag, std::string AfterTag, +const internal::PolymorphicMatcher +) { + return internal::PolymorphicWithIntrospectionMatcher( + BeforeTag, AfterTag, InnerMatcher); +} + +template +inline internal::Matcher +withIntrospection(const internal::Matcher ) { + return internal::Matcher( + new internal::WithIntrospectionMatcher(InnerMatcher)); +} + +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +inline internal::PolymorphicWithIntrospectionMatcher
[PATCH] D113943: Add `withTag` matcher.
jcking1034 updated this revision to Diff 388045. jcking1034 added a comment. Change `getMatcherName` to `getName` Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D113943/new/ https://reviews.llvm.org/D113943 Files: clang/include/clang/ASTMatchers/ASTMatchers.h clang/include/clang/ASTMatchers/ASTMatchersInternal.h Index: clang/include/clang/ASTMatchers/ASTMatchersInternal.h === --- clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -43,6 +43,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/ExprObjC.h" #include "clang/AST/NestedNameSpecifier.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/Stmt.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" @@ -1042,6 +1043,44 @@ std::vector Names; }; +template class WithTagMatcher : public MatcherInterface { +public: + explicit WithTagMatcher(std::string _BeforeTag, std::string _AfterTag, + internal::Matcher _InnerMatcher) + : BeforeTag(std::move(_BeforeTag)), AfterTag(std::move(_AfterTag)), +InnerMatcher(_InnerMatcher) {} + explicit WithTagMatcher(internal::Matcher _InnerMatcher) + : BeforeTag("â Attempting new match"), +AfterTag("âï¸ Concluding attempt"), InnerMatcher(_InnerMatcher) {} + bool matches(const T , ASTMatchFinder *Finder, + BoundNodesTreeBuilder *Builder) const override { +DynTypedNode DTN = DynTypedNode::create(Node); + +llvm::errs() << BeforeTag << "\n"; + +llvm::errs() << "Matcher Name: " << InnerMatcher.getName() << "\n"; + +llvm::errs() << "Node Kind: " << DTN.getNodeKind().asStringRef() << "\n" + << "Node Value:\n```\n"; +DTN.print(llvm::errs(), PrintingPolicy(LangOptions())); +llvm::errs() << "\n```\n" + << "Node AST:\n"; +DTN.dump(llvm::errs(), Finder->getASTContext()); + +bool result = InnerMatcher.matches(Node, Finder, Builder); +llvm::errs() << "Result: " << (result ? "Successful\n" : "Unsuccessful\n"); + +llvm::errs() << AfterTag << "\n\n"; + +return result; + } + +private: + std::string BeforeTag; + std::string AfterTag; + internal::Matcher InnerMatcher; +}; + /// Matches named declarations with a specific name. /// /// See \c hasName() and \c hasAnyName() in ASTMatchers.h for details. @@ -1719,6 +1758,38 @@ std::tuple Params; }; +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +class PolymorphicWithTagMatcher { +public: + PolymorphicWithTagMatcher( + internal::PolymorphicMatcher + _InnerMatcher) + : InnerMatcher(_InnerMatcher) {} + + using ReturnTypes = typename ExtractFunctionArgMeta::type; + + template operator Matcher() const LLVM_LVALUE_FUNCTION { +static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); +return internal::Matcher( +new internal::WithTagMatcher(internal::Matcher(InnerMatcher))); + } + +#if LLVM_HAS_RVALUE_REFERENCE_THIS + template operator Matcher() && { +static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); +return internal::Matcher(new internal::WithTagMatcher( +internal::Matcher(std::move(InnerMatcher; + } +#endif + +private: + internal::PolymorphicMatcher + InnerMatcher; +}; + /// Matches nodes of type T that have child nodes of type ChildT for /// which a specified child matcher matches. /// Index: clang/include/clang/ASTMatchers/ASTMatchers.h === --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -2993,6 +2993,40 @@ new internal::HasNameMatcher({std::string(Name)})); } +template +inline internal::Matcher withTag(std::string BeforeTag, std::string AfterTag, +const internal::Matcher ) { + return internal::Matcher( + new internal::WithTagMatcher(BeforeTag, AfterTag, InnerMatcher)); +} + +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +inline internal::PolymorphicWithTagMatcher +withTag(std::string BeforeTag, std::string AfterTag, +const internal::PolymorphicMatcher ) { + return internal::PolymorphicWithTagMatcher(BeforeTag, AfterTag, +InnerMatcher); +} + +template +inline internal::Matcher withTag(const internal::Matcher ) { + return internal::Matcher(new internal::WithTagMatcher(InnerMatcher)); +} + +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +inline internal::PolymorphicWithTagMatcher +withTag(const internal::PolymorphicMatcher ) { + return internal::PolymorphicWithTagMatcher(InnerMatcher); +} + /// Matches NamedDecl nodes that
[PATCH] D113943: Add `withTag` matcher.
jcking1034 updated this revision to Diff 387769. jcking1034 added a comment. Improve wrapping of PolymorphicMatchers Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D113943/new/ https://reviews.llvm.org/D113943 Files: clang/include/clang/ASTMatchers/ASTMatchers.h clang/include/clang/ASTMatchers/ASTMatchersInternal.h Index: clang/include/clang/ASTMatchers/ASTMatchersInternal.h === --- clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -43,6 +43,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/ExprObjC.h" #include "clang/AST/NestedNameSpecifier.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/Stmt.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" @@ -1042,6 +1043,44 @@ std::vector Names; }; +template class WithTagMatcher : public MatcherInterface { +public: + explicit WithTagMatcher(std::string _BeforeTag, std::string _AfterTag, + internal::Matcher _InnerMatcher) + : BeforeTag(std::move(_BeforeTag)), AfterTag(std::move(_AfterTag)), +InnerMatcher(_InnerMatcher) {} + explicit WithTagMatcher(internal::Matcher _InnerMatcher) + : BeforeTag("â Attempting new match"), +AfterTag("âï¸ Concluding attempt"), InnerMatcher(_InnerMatcher) {} + bool matches(const T , ASTMatchFinder *Finder, + BoundNodesTreeBuilder *Builder) const override { +DynTypedNode DTN = DynTypedNode::create(Node); + +llvm::errs() << BeforeTag << "\n"; + +llvm::errs() << "Matcher Name: " << InnerMatcher.getMatcherName() << "\n"; + +llvm::errs() << "Node Kind: " << DTN.getNodeKind().asStringRef() << "\n" + << "Node Value:\n```\n"; +DTN.print(llvm::errs(), PrintingPolicy(LangOptions())); +llvm::errs() << "\n```\n" + << "Node AST:\n"; +DTN.dump(llvm::errs(), Finder->getASTContext()); + +bool result = InnerMatcher.matches(Node, Finder, Builder); +llvm::errs() << "Result: " << (result ? "Successful\n" : "Unsuccessful\n"); + +llvm::errs() << AfterTag << "\n\n"; + +return result; + } + +private: + std::string BeforeTag; + std::string AfterTag; + internal::Matcher InnerMatcher; +}; + /// Matches named declarations with a specific name. /// /// See \c hasName() and \c hasAnyName() in ASTMatchers.h for details. @@ -1718,6 +1757,38 @@ std::tuple Params; }; +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +class PolymorphicWithTagMatcher { +public: + PolymorphicWithTagMatcher( + internal::PolymorphicMatcher + _InnerMatcher) + : InnerMatcher(_InnerMatcher) {} + + using ReturnTypes = typename ExtractFunctionArgMeta::type; + + template operator Matcher() const LLVM_LVALUE_FUNCTION { +static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); +return internal::Matcher( +new internal::WithTagMatcher(internal::Matcher(InnerMatcher))); + } + +#if LLVM_HAS_RVALUE_REFERENCE_THIS + template operator Matcher() && { +static_assert(TypeListContainsSuperOf::value, + "right polymorphic conversion"); +return internal::Matcher(new internal::WithTagMatcher( +internal::Matcher(std::move(InnerMatcher; + } +#endif + +private: + internal::PolymorphicMatcher + InnerMatcher; +}; + /// Matches nodes of type T that have child nodes of type ChildT for /// which a specified child matcher matches. /// Index: clang/include/clang/ASTMatchers/ASTMatchers.h === --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -2993,6 +2993,40 @@ new internal::HasNameMatcher({std::string(Name)})); } +template +inline internal::Matcher withTag(std::string BeforeTag, std::string AfterTag, +const internal::Matcher ) { + return internal::Matcher( + new internal::WithTagMatcher(BeforeTag, AfterTag, InnerMatcher)); +} + +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +inline internal::PolymorphicWithTagMatcher +withTag(std::string BeforeTag, std::string AfterTag, +const internal::PolymorphicMatcher ) { + return internal::PolymorphicWithTagMatcher(BeforeTag, AfterTag, +InnerMatcher); +} + +template +inline internal::Matcher withTag(const internal::Matcher ) { + return internal::Matcher(new internal::WithTagMatcher(InnerMatcher)); +} + +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +inline internal::PolymorphicWithTagMatcher +withTag(const internal::PolymorphicMatcher ) { + return internal::PolymorphicWithTagMatcher(InnerMatcher); +} + /// Matches NamedDecl
[PATCH] D113943: Add withTag matcher.
jcking1034 created this revision. jcking1034 added reviewers: ymandel, tdl-g, aaron.ballman. jcking1034 requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits. Adds a `withTag` matcher which outputs contextual information for better debugging. This relies on changes made in https://reviews.llvm.org/D113917 to access the names of matchers. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D113943 Files: clang/include/clang/ASTMatchers/ASTMatchers.h clang/include/clang/ASTMatchers/ASTMatchersInternal.h Index: clang/include/clang/ASTMatchers/ASTMatchersInternal.h === --- clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -43,6 +43,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/ExprObjC.h" #include "clang/AST/NestedNameSpecifier.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/Stmt.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" @@ -1042,6 +1043,44 @@ std::vector Names; }; +template class WithTagMatcher : public MatcherInterface { +public: + explicit WithTagMatcher(std::string _BeforeTag, std::string _AfterTag, + internal::Matcher _InnerMatcher) + : BeforeTag(std::move(_BeforeTag)), AfterTag(std::move(_AfterTag)), +InnerMatcher(_InnerMatcher) {} + explicit WithTagMatcher(internal::Matcher _InnerMatcher) + : BeforeTag("⭐ Attempting new match"), +AfterTag("✔️ Concluding attempt"), InnerMatcher(_InnerMatcher) {} + bool matches(const T , ASTMatchFinder *Finder, + BoundNodesTreeBuilder *Builder) const override { +DynTypedNode DTN = DynTypedNode::create(Node); + +llvm::errs() << BeforeTag << "\n"; + +llvm::errs() << "Matcher Name: " << InnerMatcher.getMatcherName() << "\n"; + +llvm::errs() << "Node Kind: " << DTN.getNodeKind().asStringRef() << "\n" + << "Node Value:\n```\n"; +DTN.print(llvm::errs(), PrintingPolicy(LangOptions())); +llvm::errs() << "\n```\n" + << "Node AST:\n"; +DTN.dump(llvm::errs(), Finder->getASTContext()); + +bool result = InnerMatcher.matches(Node, Finder, Builder); +llvm::errs() << "Result: " << (result ? "Successful\n" : "Unsuccessful\n"); + +llvm::errs() << AfterTag << "\n\n"; + +return result; + } + +private: + std::string BeforeTag; + std::string AfterTag; + internal::Matcher InnerMatcher; +}; + /// Matches named declarations with a specific name. /// /// See \c hasName() and \c hasAnyName() in ASTMatchers.h for details. Index: clang/include/clang/ASTMatchers/ASTMatchers.h === --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -2993,6 +2993,37 @@ new internal::HasNameMatcher({std::string(Name)})); } +template +inline internal::Matcher withTag(std::string BeforeTag, std::string AfterTag, +const internal::Matcher ) { + return internal::Matcher( + new internal::WithTagMatcher(BeforeTag, AfterTag, InnerMatcher)); +} + +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +inline MatcherT +withTag(std::string BeforeTag, std::string AfterTag, +const internal::PolymorphicMatcher ) { + return internal::Matcher(new internal::WithTagMatcher( + BeforeTag, AfterTag, internal::Matcher(InnerMatcher))); +} + +template +inline internal::Matcher withTag(const internal::Matcher ) { + return internal::Matcher(new internal::WithTagMatcher(InnerMatcher)); +} + +template class MatcherT, + typename ReturnTypesF, typename... ParamTypes> +inline MatcherT +withTag(const internal::PolymorphicMatcher ) { + return internal::Matcher( + new internal::WithTagMatcher(internal::Matcher(InnerMatcher))); +} + /// Matches NamedDecl nodes that have any of the specified names. /// /// This matcher is only provided as a performance optimization of hasName. Index: clang/include/clang/ASTMatchers/ASTMatchersInternal.h === --- clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -43,6 +43,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/ExprObjC.h" #include "clang/AST/NestedNameSpecifier.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/Stmt.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" @@ -1042,6 +1043,44 @@ std::vector Names; }; +template class WithTagMatcher : public MatcherInterface { +public: + explicit WithTagMatcher(std::string _BeforeTag, std::string _AfterTag, + internal::Matcher _InnerMatcher) + : BeforeTag(std::move(_BeforeTag)), AfterTag(std::move(_AfterTag)), +