diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp --- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -603,91 +603,6 @@ llvm::make_unique>("x"))); } -TEST(Matcher, IgnoresElidableConstructors) { - EXPECT_TRUE( - matches("struct H {};" - "template H B(T A);" - "void f() {" - " H D1;" - " D1 = B(B(1));" - "}", - cxxOperatorCallExpr(hasArgument( - 1, callExpr(hasArgument( - 0, ignoringElidableConstructorCall(callExpr()))))), - LanguageMode::Cxx11OrLater)); - EXPECT_TRUE( - matches("struct H {};" - "template H B(T A);" - "void f() {" - " H D1;" - " D1 = B(1);" - "}", - cxxOperatorCallExpr(hasArgument( - 1, callExpr(hasArgument(0, ignoringElidableConstructorCall( - integerLiteral()))))), - LanguageMode::Cxx11OrLater)); - EXPECT_TRUE(matches( - "struct H {};" - "H G();" - "void f() {" - " H D = G();" - "}", - varDecl(hasInitializer(anyOf( - ignoringElidableConstructorCall(callExpr()), - exprWithCleanups(has(ignoringElidableConstructorCall(callExpr())))))), - LanguageMode::Cxx11OrLater)); -} - -TEST(Matcher, IgnoresElidableInReturn) { - auto matcher = expr(ignoringElidableConstructorCall(declRefExpr())); - EXPECT_TRUE(matches("struct H {};" - "H f() {" - " H g;" - " return g;" - "}", - matcher, LanguageMode::Cxx11OrLater)); - EXPECT_TRUE(notMatches("struct H {};" - "H f() {" - " return H();" - "}", - matcher, LanguageMode::Cxx11OrLater)); -} - -TEST(Matcher, IgnoreElidableConstructorDoesNotMatchConstructors) { - EXPECT_TRUE(matches("struct H {};" - "void f() {" - " H D;" - "}", - varDecl(hasInitializer( - ignoringElidableConstructorCall(cxxConstructExpr()))), - LanguageMode::Cxx11OrLater)); -} - -TEST(Matcher, IgnoresElidableDoesNotPreventMatches) { - EXPECT_TRUE(matches("void f() {" - " int D = 10;" - "}", - expr(ignoringElidableConstructorCall(integerLiteral())), - LanguageMode::Cxx11OrLater)); -} - -TEST(Matcher, IgnoresElidableInVarInit) { - auto matcher = - varDecl(hasInitializer(ignoringElidableConstructorCall(callExpr()))); - EXPECT_TRUE(matches("struct H {};" - "H G();" - "void f(H D = G()) {" - " return;" - "}", - matcher, LanguageMode::Cxx11OrLater)); - EXPECT_TRUE(matches("struct H {};" - "H G();" - "void f() {" - " H D = G();" - "}", - matcher, LanguageMode::Cxx11OrLater)); -} - TEST(Matcher, BindTheSameNameInAlternatives) { StatementMatcher matcher = anyOf( binaryOperator(hasOperatorName("+"), diff --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp --- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -1461,6 +1461,91 @@ unless(anything()))))); } +TEST(Matcher, IgnoresElidableConstructors) { + EXPECT_TRUE( + matches("struct H {};" + "template H B(T A);" + "void f() {" + " H D1;" + " D1 = B(B(1));" + "}", + cxxOperatorCallExpr(hasArgument( + 1, callExpr(hasArgument( + 0, ignoringElidableConstructorCall(callExpr()))))), + LanguageMode::Cxx11OrLater)); + EXPECT_TRUE( + matches("struct H {};" + "template H B(T A);" + "void f() {" + " H D1;" + " D1 = B(1);" + "}", + cxxOperatorCallExpr(hasArgument( + 1, callExpr(hasArgument(0, ignoringElidableConstructorCall( + integerLiteral()))))), + LanguageMode::Cxx11OrLater)); + EXPECT_TRUE(matches( + "struct H {};" + "H G();" + "void f() {" + " H D = G();" + "}", + varDecl(hasInitializer(anyOf( + ignoringElidableConstructorCall(callExpr()), + exprWithCleanups(has(ignoringElidableConstructorCall(callExpr())))))), + LanguageMode::Cxx11OrLater)); +} + +TEST(Matcher, IgnoresElidableInReturn) { + auto matcher = expr(ignoringElidableConstructorCall(declRefExpr())); + EXPECT_TRUE(matches("struct H {};" + "H f() {" + " H g;" + " return g;" + "}", + matcher, LanguageMode::Cxx11OrLater)); + EXPECT_TRUE(notMatches("struct H {};" + "H f() {" + " return H();" + "}", + matcher, LanguageMode::Cxx11OrLater)); +} + +TEST(Matcher, IgnoreElidableConstructorDoesNotMatchConstructors) { + EXPECT_TRUE(matches("struct H {};" + "void f() {" + " H D;" + "}", + varDecl(hasInitializer( + ignoringElidableConstructorCall(cxxConstructExpr()))), + LanguageMode::Cxx11OrLater)); +} + +TEST(Matcher, IgnoresElidableDoesNotPreventMatches) { + EXPECT_TRUE(matches("void f() {" + " int D = 10;" + "}", + expr(ignoringElidableConstructorCall(integerLiteral())), + LanguageMode::Cxx11OrLater)); +} + +TEST(Matcher, IgnoresElidableInVarInit) { + auto matcher = + varDecl(hasInitializer(ignoringElidableConstructorCall(callExpr()))); + EXPECT_TRUE(matches("struct H {};" + "H G();" + "void f(H D = G()) {" + " return;" + "}", + matcher, LanguageMode::Cxx11OrLater)); + EXPECT_TRUE(matches("struct H {};" + "H G();" + "void f() {" + " H D = G();" + "}", + matcher, LanguageMode::Cxx11OrLater)); +} + TEST(IgnoringImplicit, MatchesImplicit) { EXPECT_TRUE(matches("class C {}; C a = C();", varDecl(has(ignoringImplicit(cxxConstructExpr())))));