jvikstrom updated this revision to Diff 204245. jvikstrom added a comment. - Using CamelCase and also renamed ignoringElidableMoveConstructorCall to ignoringElidableConstructorCall
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63149/new/ https://reviews.llvm.org/D63149 Files: clang/docs/LibASTMatchersReference.html clang/include/clang/ASTMatchers/ASTMatchers.h clang/lib/ASTMatchers/Dynamic/Registry.cpp clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -566,6 +566,81 @@ llvm::make_unique<VerifyIdIsBoundTo<CXXMemberCallExpr>>("x"))); } +TEST(Matcher, IgnoresElidableConstructors) { + auto matcher1 = cxxOperatorCallExpr(hasArgument( + 1, callExpr(hasArgument( + 0, ignoringElidableMoveConstructorCall(callExpr().bind("c")))))); + auto matcher2 = cxxOperatorCallExpr(hasArgument( + 1, callExpr(hasArgument(0, ignoringElidableMoveConstructorCall( + integerLiteral().bind("c")))))); + auto matcher3 = + varDecl(hasInitializer(ignoringElidableMoveConstructorCall(callExpr()))); + + auto code1 = "struct H {};" + "template<typename T> H B(T A);" + "void f(){" + "H D1;" + "D1=B(B(1));" + "}"; + auto code2 = "struct H {};" + "template<typename T> H B(T A);" + "void f(){" + "H D1;" + "D1=B(1);" + "}"; + auto code3 = "struct H {};" + "H G();" + "void f(){" + "H D = G();" + "}"; + + EXPECT_TRUE(matchesConditionally(code1, matcher1, true, "-std=c++14")); + EXPECT_TRUE(matchesConditionally(code1, matcher1, true, "-std=c++17")); + + EXPECT_TRUE(matchesConditionally(code2, matcher2, true, "-std=c++14")); + EXPECT_TRUE(matchesConditionally(code2, matcher2, true, "-std=c++17")); + + EXPECT_TRUE(matchesConditionally(code3, matcher3, true, "-std=c++14")); + EXPECT_TRUE(matchesConditionally(code3, matcher3, true, "-std=c++17")); +} + +TEST(Matcher, IgnoresElidableInReturn) { + auto matcher = expr(ignoringElidableMoveConstructorCall(declRefExpr())); + auto code1 = "struct H{};" + "H f(){" + "H g;" + "return g;" + "}"; + auto code2 = "struct H{};" + "H f(){" + "return H();" + "}"; + EXPECT_TRUE(matchesConditionally(code1, matcher, true, "-std=c++14")); + EXPECT_TRUE(matchesConditionally(code1, matcher, true, "-std=c++17")); + EXPECT_TRUE(matchesConditionally(code2, matcher, false, "-std=c++14")); + EXPECT_TRUE(matchesConditionally(code2, matcher, false, "-std=c++17")); +} + +TEST(Matcher, IgnoreElidableConstructorDoesNotMatchConstructors) { + auto matcher = varDecl( + hasInitializer(ignoringElidableMoveConstructorCall(cxxConstructExpr()))); + auto code = "struct H {};" + "void f(){" + "H D;" + "}"; + EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++14")); + EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++17")); +}; + +TEST(Matcher, IgnoresElidableDoesNotPreventMatches) { + auto matcher = expr(ignoringElidableMoveConstructorCall(integerLiteral())); + auto code = "void f(){" + "int D = 10;" + "}"; + EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++14")); + EXPECT_TRUE(matchesConditionally(code, matcher, true, "-std=c++17")); +} + TEST(Matcher, BindTheSameNameInAlternatives) { StatementMatcher matcher = anyOf( binaryOperator(hasOperatorName("+"), Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ clang/lib/ASTMatchers/Dynamic/Registry.cpp @@ -320,6 +320,7 @@ REGISTER_MATCHER(hasUnqualifiedDesugaredType); REGISTER_MATCHER(hasValueType); REGISTER_MATCHER(ifStmt); + REGISTER_MATCHER(ignoringElidableMoveConstructorCall); REGISTER_MATCHER(ignoringImpCasts); REGISTER_MATCHER(ignoringImplicit); REGISTER_MATCHER(ignoringParenCasts); Index: clang/include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -6452,6 +6452,56 @@ return false; } +/// Matches expressions that match InnerMatcher that are possibly wrapped in an +/// elidable constructor. +/// +/// In C++17 copy elidable constructors are no longer being +/// generated in the AST as it is not permitted by the standard. They are +/// however part of the AST in C++14 and earlier. Therefore, to write a matcher +/// that works in all language modes, the matcher has to skip elidable +/// constructor AST nodes if they appear in the AST. This matcher can be used to +/// skip those elidable constructors. +/// +/// Given +/// +/// \code +/// struct H {}; +/// H G(); +/// void f(){ +/// H D = G(); +/// } +/// \endcode +/// +/// ``varDecl(hasInitializer(ignoringElidableMoveConstructorCall(callExpr())))`` +/// matches ``H D = G()`` +AST_MATCHER_P(Expr, ignoringElidableConstructorCall, + ast_matchers::internal::Matcher<Expr>, InnerMatcher) { + + if (const auto *ExprCleanups = dyn_cast<ExprWithCleanups>(&Node)) { + bool Matches = false; + for (const auto *ChildExpr : ExprCleanups->children()) { + if (const auto *CtorExpr = dyn_cast<CXXConstructExpr>(ChildExpr)) { + Matches = Matches | ignoringElidableConstructorCall(InnerMatcher) + .matches(*CtorExpr, Finder, Builder); + } + } + return Matches; + } + + if (const auto *CtorExpr = dyn_cast<CXXConstructExpr>(&Node)) { + if (CtorExpr->isElidable()) { + if (const auto *MaterializeTemp = + dyn_cast<MaterializeTemporaryExpr>(CtorExpr->getArg(0))) { + const auto *FirstChild = + dyn_cast<Expr>(*MaterializeTemp->children().begin()); + return InnerMatcher.matches(*FirstChild, Finder, Builder); + } + return InnerMatcher.matches(*CtorExpr, Finder, Builder); + } + } + return InnerMatcher.matches(Node, Finder, Builder); +} + //----------------------------------------------------------------------------// // OpenMP handling. //----------------------------------------------------------------------------// Index: clang/docs/LibASTMatchersReference.html =================================================================== --- clang/docs/LibASTMatchersReference.html +++ clang/docs/LibASTMatchersReference.html @@ -5728,6 +5728,30 @@ </pre></td></tr> +<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>></td><td class="name" onclick="toggle('ignoringElidableMoveConstructorCall0')"><a name="ignoringElidableMoveConstructorCall0Anchor">ignoringElidableMoveConstructorCall</a></td><td>ast_matchers::Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr> +<tr><td colspan="4" class="doc" id="ignoringElidableMoveConstructorCall0"><pre>Matches expressions that match InnerMatcher that are possibly wrapped in an +elidable constructor. + +In C++17 copy elidable constructors are no longer being +generated in the AST as it is not permitted by the standard. They are +however part of the AST in C++14 and earlier. Therefore, to write a matcher +that works in all language modes, the matcher has to skip elidable +constructor AST nodes if they appear in the AST. This matcher can be used to +skip those elidable constructors. + +Given + +struct H {}; +H G(); +void f(){ +H D = G(); +} + +``varDecl(hasInitializer(ignoringElidableMoveConstructorCall(callExpr())))`` +matches ``H D = G()`` +</pre></td></tr> + + <tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>></td><td class="name" onclick="toggle('ignoringImpCasts0')"><a name="ignoringImpCasts0Anchor">ignoringImpCasts</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr> <tr><td colspan="4" class="doc" id="ignoringImpCasts0"><pre>Matches expressions that match InnerMatcher after any implicit casts are stripped off.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits