-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang] Add hasAdjSubstatements matcher #169965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2283,6 +2283,37 @@ using HasOpNameMatcher = | |
|
|
||
| HasOpNameMatcher hasAnyOperatorNameFunc(ArrayRef<const StringRef *> NameRefs); | ||
|
|
||
| /// Matches nodes of type T (CompoundStmt or StmtExpr) that contain a sequence | ||
| /// of consecutive substatements matching the provided matchers in order. | ||
| /// | ||
| /// See \c hasAdjSubstatements() in ASTMatchers.h for details. | ||
| template <typename T, typename ArgT = std::vector<Matcher<Stmt>>> | ||
| class HasAdjSubstatementsMatcher : public MatcherInterface<T> { | ||
| static_assert(std::is_same<T, CompoundStmt>::value || | ||
| std::is_same<T, StmtExpr>::value, | ||
| "Matcher only supports `CompoundStmt` and `StmtExpr`"); | ||
|
Comment on lines
+2292
to
+2294
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we refer to |
||
| static_assert(std::is_same<ArgT, std::vector<Matcher<Stmt>>>::value, | ||
| "Matcher ArgT must be std::vector<Matcher<Stmt>>"); | ||
|
|
||
| public: | ||
| explicit HasAdjSubstatementsMatcher(std::vector<Matcher<Stmt>> Matchers) | ||
| : Matchers(std::move(Matchers)) {} | ||
|
|
||
| bool matches(const T &Node, ASTMatchFinder *Finder, | ||
| BoundNodesTreeBuilder *Builder) const override; | ||
|
|
||
| private: | ||
| std::vector<Matcher<Stmt>> Matchers; | ||
| }; | ||
|
|
||
| using HasAdjSubstatementsMatcherType = | ||
| PolymorphicMatcher<HasAdjSubstatementsMatcher, | ||
| void(TypeList<CompoundStmt, StmtExpr>), | ||
| std::vector<Matcher<Stmt>>>; | ||
|
|
||
| HasAdjSubstatementsMatcherType | ||
| hasAdjSubstatementsFunc(ArrayRef<const Matcher<Stmt> *> MatcherRefs); | ||
|
|
||
| using HasOverloadOpNameMatcher = | ||
| PolymorphicMatcher<HasOverloadedOperatorNameMatcher, | ||
| void(TypeList<CXXOperatorCallExpr, FunctionDecl>), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| #include "llvm/Support/Regex.h" | ||
| #include "llvm/Support/WithColor.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
| #include <algorithm> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
| #include <cassert> | ||
| #include <cstddef> | ||
| #include <optional> | ||
|
|
@@ -467,6 +468,58 @@ hasAnyOverloadedOperatorNameFunc(ArrayRef<const StringRef *> NameRefs) { | |
| return HasOverloadOpNameMatcher(vectorFromRefs(NameRefs)); | ||
| } | ||
|
|
||
| static std::vector<Matcher<Stmt>> | ||
| vectorFromMatcherRefs(ArrayRef<const Matcher<Stmt> *> MatcherRefs) { | ||
| std::vector<Matcher<Stmt>> Matchers; | ||
| Matchers.reserve(MatcherRefs.size()); | ||
| for (auto *Matcher : MatcherRefs) | ||
| Matchers.push_back(*Matcher); | ||
| return Matchers; | ||
| } | ||
|
|
||
| HasAdjSubstatementsMatcherType | ||
| hasAdjSubstatementsFunc(ArrayRef<const Matcher<Stmt> *> MatcherRefs) { | ||
| return HasAdjSubstatementsMatcherType(vectorFromMatcherRefs(MatcherRefs)); | ||
| } | ||
|
|
||
| template <typename T, typename ArgT> | ||
| bool HasAdjSubstatementsMatcher<T, ArgT>::matches( | ||
| const T &Node, ASTMatchFinder *Finder, | ||
| BoundNodesTreeBuilder *Builder) const { | ||
| const CompoundStmt *CS = CompoundStmtMatcher<T>::get(Node); | ||
| if (!CS) | ||
| return false; | ||
|
|
||
| // Use llvm::search with lambda predicate that matches statements against | ||
| // matchers and accumulates BoundNodesTreeBuilder state | ||
| BoundNodesTreeBuilder CurrentBuilder; | ||
| const auto Found = llvm::search( | ||
| CS->body(), Matchers, | ||
| [&](const Stmt *StmtPtr, const Matcher<Stmt> &Matcher) mutable { | ||
| BoundNodesTreeBuilder StepBuilder; | ||
| StepBuilder.addMatch(CurrentBuilder); | ||
| if (!Matcher.matches(*StmtPtr, Finder, &StepBuilder)) { | ||
| // reset the state | ||
| CurrentBuilder = {}; | ||
| return false; | ||
| } | ||
| // Invalidate the state | ||
| CurrentBuilder = StepBuilder; | ||
| return true; | ||
| }); | ||
|
|
||
| if (Found == CS->body_end()) | ||
| return false; | ||
|
|
||
| Builder->addMatch(CurrentBuilder); | ||
| return true; | ||
| } | ||
|
|
||
| template bool HasAdjSubstatementsMatcher<CompoundStmt>::matches( | ||
| const CompoundStmt &, ASTMatchFinder *, BoundNodesTreeBuilder *) const; | ||
| template bool HasAdjSubstatementsMatcher<StmtExpr>::matches( | ||
| const StmtExpr &, ASTMatchFinder *, BoundNodesTreeBuilder *) const; | ||
|
|
||
| HasNameMatcher::HasNameMatcher(std::vector<std::string> N) | ||
| : UseUnqualifiedMatch( | ||
| llvm::all_of(N, [](StringRef Name) { return !Name.contains("::"); })), | ||
|
|
@@ -1046,6 +1099,10 @@ const internal::VariadicFunction<internal::Matcher<NamedDecl>, StringRef, | |
| const internal::VariadicFunction<internal::HasOpNameMatcher, StringRef, | ||
| internal::hasAnyOperatorNameFunc> | ||
| hasAnyOperatorName = {}; | ||
| const internal::VariadicFunction<internal::HasAdjSubstatementsMatcherType, | ||
| internal::Matcher<Stmt>, | ||
| internal::hasAdjSubstatementsFunc> | ||
| hasAdjSubstatements = {}; | ||
| const internal::VariadicFunction<internal::HasOverloadOpNameMatcher, StringRef, | ||
| internal::hasAnyOverloadedOperatorNameFunc> | ||
| hasAnyOverloadedOperatorName = {}; | ||
|
|
||
zwuis marked this conversation as resolved.
Show resolved
Hide resolved
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add tests to "llvm/unittests/ADT/STLExtrasTest.cpp". It seems that we should add this function in a separate PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated changes?