From: Alexander Kornienko Date: Mon, 2 Nov 2015 22:23:21 +0000 (+0000) Subject: Make hasLHS and hasRHS matchers available for ArraySubscriptExpr X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=23e7a25830cc9d829a5df655b83260c862498bd3;p=clang Make hasLHS and hasRHS matchers available for ArraySubscriptExpr Summary: The hasBase and hasIndex don't tell anything about the position of the base and the index in the code, so we need hasLHS and hasRHS in some cases. Reviewers: klimek Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D14212 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@251842 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index e7e9646b55..8147de426e 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -3127,9 +3127,11 @@ AST_POLYMORPHIC_MATCHER_P(hasOperatorName, /// \code /// a || b /// \endcode -AST_MATCHER_P(BinaryOperator, hasLHS, - internal::Matcher, InnerMatcher) { - Expr *LeftHandSide = Node.getLHS(); +AST_POLYMORPHIC_MATCHER_P(hasLHS, + AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, + ArraySubscriptExpr), + internal::Matcher, InnerMatcher) { + const Expr *LeftHandSide = Node.getLHS(); return (LeftHandSide != nullptr && InnerMatcher.matches(*LeftHandSide, Finder, Builder)); } @@ -3140,9 +3142,11 @@ AST_MATCHER_P(BinaryOperator, hasLHS, /// \code /// a || b /// \endcode -AST_MATCHER_P(BinaryOperator, hasRHS, - internal::Matcher, InnerMatcher) { - Expr *RightHandSide = Node.getRHS(); +AST_POLYMORPHIC_MATCHER_P(hasRHS, + AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, + ArraySubscriptExpr), + internal::Matcher, InnerMatcher) { + const Expr *RightHandSide = Node.getRHS(); return (RightHandSide != nullptr && InnerMatcher.matches(*RightHandSide, Finder, Builder)); } @@ -3246,7 +3250,7 @@ AST_MATCHER(RecordDecl, isClass) { /// \endcode AST_MATCHER_P(ConditionalOperator, hasTrueExpression, internal::Matcher, InnerMatcher) { - Expr *Expression = Node.getTrueExpr(); + const Expr *Expression = Node.getTrueExpr(); return (Expression != nullptr && InnerMatcher.matches(*Expression, Finder, Builder)); } @@ -3259,7 +3263,7 @@ AST_MATCHER_P(ConditionalOperator, hasTrueExpression, /// \endcode AST_MATCHER_P(ConditionalOperator, hasFalseExpression, internal::Matcher, InnerMatcher) { - Expr *Expression = Node.getFalseExpr(); + const Expr *Expression = Node.getFalseExpr(); return (Expression != nullptr && InnerMatcher.matches(*Expression, Finder, Builder)); } @@ -4270,7 +4274,7 @@ AST_MATCHER_P(NestedNameSpecifierLoc, specifiesTypeLoc, AST_MATCHER_P_OVERLOAD(NestedNameSpecifier, hasPrefix, internal::Matcher, InnerMatcher, 0) { - NestedNameSpecifier *NextNode = Node.getPrefix(); + const NestedNameSpecifier *NextNode = Node.getPrefix(); if (!NextNode) return false; return InnerMatcher.matches(*NextNode, Finder, Builder); diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index 26ef2a7124..476a0be290 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -2372,6 +2372,11 @@ TEST(MatchBinaryOperator, HasLHSAndHasRHS) { EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse)); EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse)); EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse)); + + StatementMatcher OperatorIntPointer = arraySubscriptExpr( + hasLHS(hasType(isInteger())), hasRHS(hasType(pointsTo(qualType())))); + EXPECT_TRUE(matches("void x() { 1[\"abc\"]; }", OperatorIntPointer)); + EXPECT_TRUE(notMatches("void x() { \"abc\"[1]; }", OperatorIntPointer)); } TEST(MatchBinaryOperator, HasEitherOperand) {