Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -2749,6 +2749,15 @@ +
Matches a function declared with a trailing return type. + +Example matches Y (matcher = functionDecl(hasTrailingReturn())) +int X() {} +auto Y() -> int {} +
Matches constexpr variable and function declarations. Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -5893,6 +5893,19 @@ return Node.isScoped(); } +/// \brief Matches a function declared with a trailing return type. +/// +/// Example matches Y (matcher = functionDecl(hasTrailingReturn())) +/// \code +/// int X() {} +/// auto Y() -> int {} +/// \endcode +AST_MATCHER(FunctionDecl, hasTrailingReturn) { + if (const auto *F = Node.getType()->getAs()) + return F->hasTrailingReturn(); + return false; +} + } // namespace ast_matchers } // namespace clang Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -296,6 +296,7 @@ REGISTER_MATCHER(hasTemplateArgument); REGISTER_MATCHER(hasThen); REGISTER_MATCHER(hasThreadStorageDuration); + REGISTER_MATCHER(hasTrailingReturn); REGISTER_MATCHER(hasTrueExpression); REGISTER_MATCHER(hasTypeLoc); REGISTER_MATCHER(hasUnaryOperand); Index: unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -2112,5 +2112,24 @@ EXPECT_TRUE(notMatches("enum X {};", enumDecl(isScoped()))); } +TEST(HasTrailingReturn, MatchesTrailingReturn) { + EXPECT_TRUE(matches("auto Y() -> int { return 0; }", + functionDecl(hasTrailingReturn()))); + EXPECT_TRUE(matches("auto X() -> int;", functionDecl(hasTrailingReturn()))); + EXPECT_TRUE(notMatches("int X() { return 0; }", + functionDecl(hasTrailingReturn()))); + EXPECT_TRUE(notMatches("int X();", functionDecl(hasTrailingReturn()))); + EXPECT_TRUE(notMatchesC("void X();", functionDecl(hasTrailingReturn()))); +} + +TEST(HasTrailingReturn, MatchesLambdaTrailingReturn) { + EXPECT_TRUE(matches( + "auto lambda2 = [](double x, double y) -> double {return x + y;};", + functionDecl(hasTrailingReturn()))); + EXPECT_TRUE(notMatches( + "auto lambda2 = [](double x, double y) {return x + y;};", + functionDecl(hasTrailingReturn()))); +} + } // namespace ast_matchers } // namespace clang