diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -2737,6 +2737,22 @@ return false; } +/// Matches the statement an attribute is attached to. +/// +/// Example: +/// \code +/// attributedStmt(hasSubStmt(returnStmt())) +/// \endcode +/// would match return 1; here: +/// \code +/// else [[unlikely]] +/// return 1; +/// \endcode +AST_MATCHER_P(AttributedStmt, hasSubStmt, internal::Matcher, InnerMatcher) { + const Stmt *const Statement = Node.getSubStmt(); + return (Statement != nullptr && InnerMatcher.matches(*Statement, Finder, Builder)); +} + /// Matches \c QualTypes in the clang AST. extern const internal::VariadicAllOfMatcher qualType; diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp --- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp @@ -356,6 +356,7 @@ REGISTER_MATCHER(hasSpecializedTemplate); REGISTER_MATCHER(hasStaticStorageDuration); REGISTER_MATCHER(hasStructuredBlock); + REGISTER_MATCHER(hasSubStmt); REGISTER_MATCHER(hasSyntacticForm); REGISTER_MATCHER(hasTargetDecl); REGISTER_MATCHER(hasTemplateArgument);