Skip to content

Commit 02f5f90

Browse files
committed
extend hasOperatorName instead of adding hasOperator
1 parent 0b3632c commit 02f5f90

File tree

6 files changed

+68
-48
lines changed

6 files changed

+68
-48
lines changed

clang/docs/LibASTMatchersReference.html

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,11 +2976,18 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
29762976

29772977

29782978
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1BinaryOperator.html">BinaryOperator</a>&gt;</td><td class="name" onclick="toggle('hasOperatorName0')"><a name="hasOperatorName0Anchor">hasOperatorName</a></td><td>std::string Name</td></tr>
2979-
<tr><td colspan="4" class="doc" id="hasOperatorName0"><pre>Matches the operator Name of operator expressions (binary or
2980-
unary).
2979+
<tr><td colspan="4" class="doc" id="hasOperatorName0"><pre>Matches the operator Name of operator expressions and fold expressions
2980+
(binary or unary).
29812981

29822982
Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
29832983
!(a || b)
2984+
2985+
Example matches `(0 + ... + args)`
2986+
(matcher = cxxFoldExpr(hasOperatorName("+")))
2987+
template &lt;typename... Args&gt;
2988+
auto sum(Args... args) {
2989+
return (0 + ... + args);
2990+
}
29842991
</pre></td></tr>
29852992

29862993

@@ -3441,20 +3448,19 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
34413448
</pre></td></tr>
34423449

34433450

3444-
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXFoldExpr.html">CXXFoldExpr</a>&gt;</td><td class="name" onclick="toggle('hasOperator0')"><a name="hasOperator0Anchor">hasOperator</a></td><td>BinaryOperatorKind Op</td></tr>
3445-
<tr><td colspan="4" class="doc" id="hasOperator0"><pre>Matches the operator kind of the fold expression.
3451+
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXFoldExpr.html">CXXFoldExpr</a>&gt;</td><td class="name" onclick="toggle('hasOperatorName3')"><a name="hasOperatorName3Anchor">hasOperatorName</a></td><td>std::string Name</td></tr>
3452+
<tr><td colspan="4" class="doc" id="hasOperatorName3"><pre>Matches the operator Name of operator expressions and fold expressions
3453+
(binary or unary).
3454+
3455+
Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
3456+
!(a || b)
34463457

34473458
Example matches `(0 + ... + args)`
3448-
(matcher = cxxFoldExpr(hasOperator(BO_Add)))
3459+
(matcher = cxxFoldExpr(hasOperatorName("+")))
34493460
template &lt;typename... Args&gt;
34503461
auto sum(Args... args) {
34513462
return (0 + ... + args);
34523463
}
3453-
3454-
template &lt;typename... Args&gt;
3455-
auto multiply(Args... args) {
3456-
return (args * ... * 1);
3457-
}
34583464
</pre></td></tr>
34593465

34603466

@@ -3696,11 +3702,18 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
36963702

36973703

36983704
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXOperatorCallExpr.html">CXXOperatorCallExpr</a>&gt;</td><td class="name" onclick="toggle('hasOperatorName1')"><a name="hasOperatorName1Anchor">hasOperatorName</a></td><td>std::string Name</td></tr>
3699-
<tr><td colspan="4" class="doc" id="hasOperatorName1"><pre>Matches the operator Name of operator expressions (binary or
3700-
unary).
3705+
<tr><td colspan="4" class="doc" id="hasOperatorName1"><pre>Matches the operator Name of operator expressions and fold expressions
3706+
(binary or unary).
37013707

37023708
Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
37033709
!(a || b)
3710+
3711+
Example matches `(0 + ... + args)`
3712+
(matcher = cxxFoldExpr(hasOperatorName("+")))
3713+
template &lt;typename... Args&gt;
3714+
auto sum(Args... args) {
3715+
return (0 + ... + args);
3716+
}
37043717
</pre></td></tr>
37053718

37063719

@@ -3854,11 +3867,18 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
38543867

38553868

38563869
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXRewrittenBinaryOperator.html">CXXRewrittenBinaryOperator</a>&gt;</td><td class="name" onclick="toggle('hasOperatorName2')"><a name="hasOperatorName2Anchor">hasOperatorName</a></td><td>std::string Name</td></tr>
3857-
<tr><td colspan="4" class="doc" id="hasOperatorName2"><pre>Matches the operator Name of operator expressions (binary or
3858-
unary).
3870+
<tr><td colspan="4" class="doc" id="hasOperatorName2"><pre>Matches the operator Name of operator expressions and fold expressions
3871+
(binary or unary).
38593872

38603873
Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
38613874
!(a || b)
3875+
3876+
Example matches `(0 + ... + args)`
3877+
(matcher = cxxFoldExpr(hasOperatorName("+")))
3878+
template &lt;typename... Args&gt;
3879+
auto sum(Args... args) {
3880+
return (0 + ... + args);
3881+
}
38623882
</pre></td></tr>
38633883

38643884

@@ -5808,12 +5828,19 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
58085828
</pre></td></tr>
58095829

58105830

5811-
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1UnaryOperator.html">UnaryOperator</a>&gt;</td><td class="name" onclick="toggle('hasOperatorName3')"><a name="hasOperatorName3Anchor">hasOperatorName</a></td><td>std::string Name</td></tr>
5812-
<tr><td colspan="4" class="doc" id="hasOperatorName3"><pre>Matches the operator Name of operator expressions (binary or
5813-
unary).
5831+
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1UnaryOperator.html">UnaryOperator</a>&gt;</td><td class="name" onclick="toggle('hasOperatorName4')"><a name="hasOperatorName4Anchor">hasOperatorName</a></td><td>std::string Name</td></tr>
5832+
<tr><td colspan="4" class="doc" id="hasOperatorName4"><pre>Matches the operator Name of operator expressions and fold expressions
5833+
(binary or unary).
58145834

58155835
Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
58165836
!(a || b)
5837+
5838+
Example matches `(0 + ... + args)`
5839+
(matcher = cxxFoldExpr(hasOperatorName("+")))
5840+
template &lt;typename... Args&gt;
5841+
auto sum(Args... args) {
5842+
return (0 + ... + args);
5843+
}
58175844
</pre></td></tr>
58185845

58195846

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4678,25 +4678,6 @@ AST_MATCHER(CXXFoldExpr, isUnaryFold) { return Node.getInit() == nullptr; }
46784678
/// \endcode
46794679
AST_MATCHER(CXXFoldExpr, isBinaryFold) { return Node.getInit() != nullptr; }
46804680

4681-
/// Matches the operator kind of the fold expression.
4682-
///
4683-
/// Example matches `(0 + ... + args)`
4684-
/// (matcher = cxxFoldExpr(hasOperator(BO_Add)))
4685-
/// \code
4686-
/// template <typename... Args>
4687-
/// auto sum(Args... args) {
4688-
/// return (0 + ... + args);
4689-
/// }
4690-
///
4691-
/// template <typename... Args>
4692-
/// auto multiply(Args... args) {
4693-
/// return (args * ... * 1);
4694-
/// }
4695-
/// \endcode
4696-
AST_MATCHER_P(CXXFoldExpr, hasOperator, BinaryOperatorKind, Op) {
4697-
return Node.getOperator() == Op;
4698-
}
4699-
47004681
/// Matches the n'th item of an initializer list expression.
47014682
///
47024683
/// Example matches y.
@@ -5874,17 +5855,27 @@ AST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals,
58745855
.matchesNode(Node);
58755856
}
58765857

5877-
/// Matches the operator Name of operator expressions (binary or
5878-
/// unary).
5858+
/// Matches the operator Name of operator expressions and fold expressions
5859+
/// (binary or unary).
58795860
///
58805861
/// Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
58815862
/// \code
58825863
/// !(a || b)
58835864
/// \endcode
5865+
///
5866+
/// Example matches `(0 + ... + args)`
5867+
/// (matcher = cxxFoldExpr(hasOperatorName("+")))
5868+
/// \code
5869+
/// template <typename... Args>
5870+
/// auto sum(Args... args) {
5871+
/// return (0 + ... + args);
5872+
/// }
5873+
/// \endcode
58845874
AST_POLYMORPHIC_MATCHER_P(
58855875
hasOperatorName,
58865876
AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, CXXOperatorCallExpr,
5887-
CXXRewrittenBinaryOperator, UnaryOperator),
5877+
CXXRewrittenBinaryOperator, CXXFoldExpr,
5878+
UnaryOperator),
58885879
std::string, Name) {
58895880
if (std::optional<StringRef> OpName = internal::getOpName(Node))
58905881
return *OpName == Name;

clang/include/clang/ASTMatchers/ASTMatchersInternal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,9 @@ inline std::optional<StringRef> getOpName(const CXXOperatorCallExpr &Node) {
21952195
}
21962196
return BinaryOperator::getOpcodeStr(*optBinaryOpcode);
21972197
}
2198+
inline StringRef getOpName(const CXXFoldExpr &Node) {
2199+
return BinaryOperator::getOpcodeStr(Node.getOperator());
2200+
}
21982201

21992202
/// Matches overloaded operators with a specific name.
22002203
///

clang/lib/ASTMatchers/Dynamic/Registry.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ RegistryMaps::RegistryMaps() {
342342
REGISTER_MATCHER(hasNullSelector);
343343
REGISTER_MATCHER(hasObjectExpression);
344344
REGISTER_MATCHER(hasOperands);
345-
REGISTER_MATCHER(hasOperator);
346345
REGISTER_MATCHER(hasOperatorName);
347346
REGISTER_MATCHER(hasOverloadedOperatorName);
348347
REGISTER_MATCHER(hasParameter);

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,14 +804,14 @@ TEST_P(ImportExpr, ImportSizeOfPackExpr) {
804804
}
805805

806806
TEST_P(ImportExpr, ImportCXXFoldExpr) {
807-
auto Match1 = cxxFoldExpr(hasOperator(BO_Add), isLeftFold(),
807+
auto Match1 = cxxFoldExpr(hasOperatorName("+"), isLeftFold(),
808808
unless(hasFoldInit(expr())));
809809
auto Match2 =
810-
cxxFoldExpr(hasOperator(BO_Sub), isLeftFold(), hasFoldInit(expr()));
811-
auto Match3 = cxxFoldExpr(hasOperator(BO_Mul), isRightFold(),
810+
cxxFoldExpr(hasOperatorName("-"), isLeftFold(), hasFoldInit(expr()));
811+
auto Match3 = cxxFoldExpr(hasOperatorName("*"), isRightFold(),
812812
unless(hasFoldInit(expr())));
813813
auto Match4 =
814-
cxxFoldExpr(hasOperator(BO_Div), isRightFold(), hasFoldInit(expr()));
814+
cxxFoldExpr(hasOperatorName("/"), isRightFold(), hasFoldInit(expr()));
815815

816816
MatchVerifier<Decl> Verifier;
817817
testImport("template <typename... Ts>"

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,19 +4186,19 @@ TEST_P(ASTMatchersTest, hasOperator) {
41864186

41874187
EXPECT_TRUE(matches("template <typename... Args> auto sum(Args... args) { "
41884188
"return (0 + ... + args); }",
4189-
cxxFoldExpr(hasOperator(BO_Add))));
4189+
cxxFoldExpr(hasOperatorName("+"))));
41904190
EXPECT_TRUE(matches("template <typename... Args> auto sum(Args... args) { "
41914191
"return (... + args); };",
4192-
cxxFoldExpr(hasOperator(BO_Add))));
4192+
cxxFoldExpr(hasOperatorName("+"))));
41934193

41944194
EXPECT_FALSE(
41954195
matches("template <typename... Args> auto multiply(Args... args) { "
41964196
"return (0 * ... * args); }",
4197-
cxxFoldExpr(hasOperator(BO_Add))));
4197+
cxxFoldExpr(hasOperatorName("+"))));
41984198
EXPECT_FALSE(
41994199
matches("template <typename... Args> auto multiply(Args... args) { "
42004200
"return (... * args); };",
4201-
cxxFoldExpr(hasOperator(BO_Add))));
4201+
cxxFoldExpr(hasOperatorName("+"))));
42024202
}
42034203

42044204
TEST_P(ASTMatchersTest, IsMain) {

0 commit comments

Comments
 (0)