[clang] 6a079df - [ASTMatchers] Add forCallable(), a generalization of forFunction().
Artem Dergachev via cfe-commits
cfe-commits at lists.llvm.org
Thu May 13 11:25:11 PDT 2021
Author: Artem Dergachev
Date: 2021-05-13T11:25:00-07:00
New Revision: 6a079dfdc992706408f2bde84c48bf76e52c4311
URL: https://github.com/llvm/llvm-project/commit/6a079dfdc992706408f2bde84c48bf76e52c4311
DIFF: https://github.com/llvm/llvm-project/commit/6a079dfdc992706408f2bde84c48bf76e52c4311.diff
LOG: [ASTMatchers] Add forCallable(), a generalization of forFunction().
The new matcher additionally covers blocks and Objective-C methods.
This matcher actually makes sure that the statement truly belongs
to that declaration's body. forFunction() incorrectly reported that
a statement in a nested block belonged to the surrounding function.
forFunction() is now deprecated due to the above footgun, in favor of
forCallable(functionDecl()) when only functions need to be considered.
Differential Revision: https://reviews.llvm.org/D102213
Added:
Modified:
clang/docs/LibASTMatchersReference.html
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/lib/ASTMatchers/Dynamic/Registry.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index 9c5a1229a77f0..73309fb081f96 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -8813,9 +8813,41 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>></td><td class="name" onclick="toggle('forCallable0')"><a name="forCallable0Anchor">forCallable</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>> InnerMatcher</td></tr>
+<tr><td colspan="4" class="doc" id="forCallable0"><pre>Matches declaration of the function, method, or block the statement
+belongs to.
+
+Given:
+F& operator=(const F& o) {
+ std::copy_if(o.begin(), o.end(), begin(), [](V v) { return v > 0; });
+ return *this;
+}
+returnStmt(forCallable(functionDecl(hasName("operator="))))
+ matches 'return *this'
+ but does not match 'return v > 0'
+
+Given:
+-(void) foo {
+ int x = 1;
+ dispatch_sync(queue, ^{ int y = 2; });
+}
+declStmt(forCallable(objcMethodDecl()))
+ matches 'int x = 1'
+ but does not match 'int y = 2'.
+whereas declStmt(forCallable(blockDecl()))
+ matches 'int y = 2'
+ but does not match 'int x = 1'.
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>></td><td class="name" onclick="toggle('forFunction0')"><a name="forFunction0Anchor">forFunction</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>> InnerMatcher</td></tr>
<tr><td colspan="4" class="doc" id="forFunction0"><pre>Matches declaration of the function the statement belongs to.
+Deprecated. Use forCallable() to correctly handle the situation when
+the declaration is not a function (but a block or an Objective-C method).
+forFunction() not only fails to take non-functions into account but also
+may match the wrong declaration in their presence.
+
Given:
F& operator=(const F& o) {
std::copy_if(o.begin(), o.end(), begin(), [](V v) { return v > 0; });
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 32d094d5e753d..b3c43458fce7c 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -7545,6 +7545,11 @@ AST_MATCHER_P(DecompositionDecl, hasAnyBinding, internal::Matcher<BindingDecl>,
/// Matches declaration of the function the statement belongs to.
///
+/// Deprecated. Use forCallable() to correctly handle the situation when
+/// the declaration is not a function (but a block or an Objective-C method).
+/// forFunction() not only fails to take non-functions into account but also
+/// may match the wrong declaration in their presence.
+///
/// Given:
/// \code
/// F& operator=(const F& o) {
@@ -7580,6 +7585,65 @@ AST_MATCHER_P(Stmt, forFunction, internal::Matcher<FunctionDecl>,
return false;
}
+/// Matches declaration of the function, method, or block the statement
+/// belongs to.
+///
+/// Given:
+/// \code
+/// F& operator=(const F& o) {
+/// std::copy_if(o.begin(), o.end(), begin(), [](V v) { return v > 0; });
+/// return *this;
+/// }
+/// \endcode
+/// returnStmt(forCallable(functionDecl(hasName("operator="))))
+/// matches 'return *this'
+/// but does not match 'return v > 0'
+///
+/// Given:
+/// \code
+/// -(void) foo {
+/// int x = 1;
+/// dispatch_sync(queue, ^{ int y = 2; });
+/// }
+/// \endcode
+/// declStmt(forCallable(objcMethodDecl()))
+/// matches 'int x = 1'
+/// but does not match 'int y = 2'.
+/// whereas declStmt(forCallable(blockDecl()))
+/// matches 'int y = 2'
+/// but does not match 'int x = 1'.
+AST_MATCHER_P(Stmt, forCallable, internal::Matcher<Decl>, InnerMatcher) {
+ const auto &Parents = Finder->getASTContext().getParents(Node);
+
+ llvm::SmallVector<DynTypedNode, 8> Stack(Parents.begin(), Parents.end());
+ while (!Stack.empty()) {
+ const auto &CurNode = Stack.back();
+ Stack.pop_back();
+ if (const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) {
+ if (InnerMatcher.matches(*FuncDeclNode, Finder, Builder)) {
+ return true;
+ }
+ } else if (const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) {
+ if (InnerMatcher.matches(*LambdaExprNode->getCallOperator(), Finder,
+ Builder)) {
+ return true;
+ }
+ } else if (const auto *ObjCMethodDeclNode = CurNode.get<ObjCMethodDecl>()) {
+ if (InnerMatcher.matches(*ObjCMethodDeclNode, Finder, Builder)) {
+ return true;
+ }
+ } else if (const auto *BlockDeclNode = CurNode.get<BlockDecl>()) {
+ if (InnerMatcher.matches(*BlockDeclNode, Finder, Builder)) {
+ return true;
+ }
+ } else {
+ for (const auto &Parent : Finder->getASTContext().getParents(CurNode))
+ Stack.push_back(Parent);
+ }
+ }
+ return false;
+}
+
/// Matches a declaration that has external formal linkage.
///
/// Example matches only z (matcher = varDecl(hasExternalFormalLinkage()))
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 8c0f688b54417..31b4f601faf47 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -236,6 +236,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(fieldDecl);
REGISTER_MATCHER(fixedPointLiteral);
REGISTER_MATCHER(floatLiteral);
+ REGISTER_MATCHER(forCallable);
REGISTER_MATCHER(forDecomposition);
REGISTER_MATCHER(forEach);
REGISTER_MATCHER(forEachArgumentWithParam);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index bcee21f9220a2..ae496d39e00cf 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -5524,6 +5524,83 @@ TEST(StatementMatcher, ForFunction) {
EXPECT_TRUE(notMatches(CppString2, returnStmt(forFunction(hasName("F")))));
}
+TEST(StatementMatcher, ForCallable) {
+ // These tests are copied over from the forFunction() test above.
+ StringRef CppString1 = "struct PosVec {"
+ " PosVec& operator=(const PosVec&) {"
+ " auto x = [] { return 1; };"
+ " return *this;"
+ " }"
+ "};";
+ StringRef CppString2 = "void F() {"
+ " struct S {"
+ " void F2() {"
+ " return;"
+ " }"
+ " };"
+ "}";
+
+ EXPECT_TRUE(
+ matches(
+ CppString1,
+ returnStmt(forCallable(functionDecl(hasName("operator="))),
+ has(unaryOperator(hasOperatorName("*"))))));
+ EXPECT_TRUE(
+ notMatches(
+ CppString1,
+ returnStmt(forCallable(functionDecl(hasName("operator="))),
+ has(integerLiteral()))));
+ EXPECT_TRUE(
+ matches(
+ CppString1,
+ returnStmt(forCallable(functionDecl(hasName("operator()"))),
+ has(integerLiteral()))));
+ EXPECT_TRUE(matches(CppString2,
+ returnStmt(forCallable(functionDecl(hasName("F2"))))));
+ EXPECT_TRUE(notMatches(CppString2,
+ returnStmt(forCallable(functionDecl(hasName("F"))))));
+
+ // These tests are specific to forCallable().
+ StringRef ObjCString1 = "@interface I"
+ "-(void) foo;"
+ "@end"
+ "@implementation I"
+ "-(void) foo {"
+ " void (^block)() = ^{ 0x2b | ~0x2b; };"
+ "}"
+ "@end";
+
+ EXPECT_TRUE(
+ matchesObjC(
+ ObjCString1,
+ binaryOperator(forCallable(blockDecl()))));
+
+ EXPECT_TRUE(
+ notMatchesObjC(
+ ObjCString1,
+ binaryOperator(forCallable(objcMethodDecl()))));
+
+ StringRef ObjCString2 = "@interface I"
+ "-(void) foo;"
+ "@end"
+ "@implementation I"
+ "-(void) foo {"
+ " 0x2b | ~0x2b;"
+ " void (^block)() = ^{};"
+ "}"
+ "@end";
+
+ EXPECT_TRUE(
+ matchesObjC(
+ ObjCString2,
+ binaryOperator(forCallable(objcMethodDecl()))));
+
+ EXPECT_TRUE(
+ notMatchesObjC(
+ ObjCString2,
+ binaryOperator(forCallable(blockDecl()))));
+}
+
TEST(Matcher, ForEachOverriden) {
const auto ForEachOverriddenInClass = [](const char *ClassName) {
return cxxMethodDecl(ofClass(hasName(ClassName)), isVirtual(),
More information about the cfe-commits
mailing list