Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions clang/include/clang/ASTMatchers/ASTMatchersInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2161,8 +2161,10 @@ inline const Expr *getSubExpr(const NodeType &Node) {
template <>
inline const Expr *
getSubExpr<CXXOperatorCallExpr>(const CXXOperatorCallExpr &Node) {
if (!internal::equivalentUnaryOperator(Node))
if (!internal::equivalentUnaryOperator(Node) &&
Node.getOperator() != OO_Arrow) {
return nullptr;
}
return Node.getArg(0);
}

Expand Down Expand Up @@ -2223,14 +2225,9 @@ inline StringRef getOpName(const CXXRewrittenBinaryOperator &Node) {
return Node.getOpcodeStr();
}
inline std::optional<StringRef> getOpName(const CXXOperatorCallExpr &Node) {
auto optBinaryOpcode = equivalentBinaryOperator(Node);
if (!optBinaryOpcode) {
auto optUnaryOpcode = equivalentUnaryOperator(Node);
if (!optUnaryOpcode)
return std::nullopt;
return UnaryOperator::getOpcodeStr(*optUnaryOpcode);
}
return BinaryOperator::getOpcodeStr(*optBinaryOpcode);
if (const char *Str = getOperatorSpelling(Node.getOperator()))
return Str;
return std::nullopt;
}
inline StringRef getOpName(const CXXFoldExpr &Node) {
return BinaryOperator::getOpcodeStr(Node.getOperator());
Expand Down Expand Up @@ -2271,14 +2268,9 @@ class HasAnyOperatorNameMatcher : public SingleNodeMatcherInterface<T> {
return Node.getOpcodeStr();
}
static std::optional<StringRef> getOpName(const CXXOperatorCallExpr &Node) {
auto optBinaryOpcode = equivalentBinaryOperator(Node);
if (!optBinaryOpcode) {
auto optUnaryOpcode = equivalentUnaryOperator(Node);
if (!optUnaryOpcode)
return std::nullopt;
return UnaryOperator::getOpcodeStr(*optUnaryOpcode);
}
return BinaryOperator::getOpcodeStr(*optBinaryOpcode);
if (const char *Str = getOperatorSpelling(Node.getOperator()))
return Str;
return std::nullopt;
}

std::vector<std::string> Names;
Expand Down
23 changes: 19 additions & 4 deletions clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2025,21 +2025,36 @@ void plusIntOperator()
hasOperatorName("+"), hasUnaryOperand(expr())))));

Code = R"cpp(
struct HasOpArrow
struct S {
void foo(int);
};
struct HasOpStar
{
int& operator*();
};
void foo()
struct HasOpArrow
{
HasOpArrow s1;
*s1;
S* operator->();
};
void hasOpStarMem(HasOpStar s)
{
*s;
}
void hasOpArrowMem(HasOpArrow a)
{
a->foo(42);
}
)cpp";

EXPECT_TRUE(
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource,
cxxOperatorCallExpr(hasOperatorName("*"),
hasUnaryOperand(expr())))));
EXPECT_TRUE(matches(
Code, traverse(TK_IgnoreUnlessSpelledInSource,
cxxOperatorCallExpr(hasOperatorName("->"),
hasUnaryOperand(declRefExpr(
to(varDecl(hasName("a")))))))));
}

TEST(Matcher, UnaryOperatorTypes) {
Expand Down