diff options
Diffstat (limited to 'clang/unittests')
-rw-r--r-- | clang/unittests/AST/ASTTraverserTest.cpp | 119 | ||||
-rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp | 28 |
2 files changed, 147 insertions, 0 deletions
diff --git a/clang/unittests/AST/ASTTraverserTest.cpp b/clang/unittests/AST/ASTTraverserTest.cpp index c995f55d3c8..4b982431297 100644 --- a/clang/unittests/AST/ASTTraverserTest.cpp +++ b/clang/unittests/AST/ASTTraverserTest.cpp @@ -479,4 +479,123 @@ FunctionDecl 'func12' )cpp"); } +TEST(Traverse, LambdaUnlessSpelledInSource) { + + auto AST = + buildASTFromCodeWithArgs(R"cpp( + +void captures() { + int a = 0; + int b = 0; + int d = 0; + int f = 0; + + [a, &b, c = d, &e = f](int g, int h = 42) {}; +} + +void templated() { + int a = 0; + [a]<typename T>(T t) {}; +} + +struct SomeStruct { + int a = 0; + void capture_this() { + [this]() {}; + } + void capture_this_copy() { + [self = *this]() {}; + } +}; +)cpp", + {"-Wno-unused-value", "-Wno-c++2a-extensions"}); + + auto getLambdaNode = [&AST](const std::string &name) { + auto BN = ast_matchers::match( + lambdaExpr(hasAncestor(functionDecl(hasName(name)))).bind("lambda"), + AST->getASTContext()); + EXPECT_EQ(BN.size(), 1u); + return BN[0].getNodeAs<LambdaExpr>("lambda"); + }; + + { + auto L = getLambdaNode("captures"); + + EXPECT_EQ(dumpASTString(ast_type_traits::TK_IgnoreUnlessSpelledInSource, L), + R"cpp( +LambdaExpr +|-DeclRefExpr 'a' +|-DeclRefExpr 'b' +|-VarDecl 'c' +| `-DeclRefExpr 'd' +|-VarDecl 'e' +| `-DeclRefExpr 'f' +|-ParmVarDecl 'g' +|-ParmVarDecl 'h' +| `-IntegerLiteral +`-CompoundStmt +)cpp"); + + EXPECT_EQ(dumpASTString(ast_type_traits::TK_AsIs, L), + R"cpp( +LambdaExpr +|-CXXRecordDecl '' +| |-CXXMethodDecl 'operator()' +| | |-ParmVarDecl 'g' +| | |-ParmVarDecl 'h' +| | | `-IntegerLiteral +| | `-CompoundStmt +| |-FieldDecl '' +| |-FieldDecl '' +| |-FieldDecl '' +| |-FieldDecl '' +| `-CXXDestructorDecl '~' +|-ImplicitCastExpr +| `-DeclRefExpr 'a' +|-DeclRefExpr 'b' +|-ImplicitCastExpr +| `-DeclRefExpr 'd' +|-DeclRefExpr 'f' +`-CompoundStmt +)cpp"); + } + + { + auto L = getLambdaNode("templated"); + + EXPECT_EQ(dumpASTString(ast_type_traits::TK_IgnoreUnlessSpelledInSource, L), + R"cpp( +LambdaExpr +|-DeclRefExpr 'a' +|-TemplateTypeParmDecl 'T' +|-ParmVarDecl 't' +`-CompoundStmt +)cpp"); + } + + { + auto L = getLambdaNode("capture_this"); + + EXPECT_EQ(dumpASTString(ast_type_traits::TK_IgnoreUnlessSpelledInSource, L), + R"cpp( +LambdaExpr +|-CXXThisExpr +`-CompoundStmt +)cpp"); + } + + { + auto L = getLambdaNode("capture_this_copy"); + + EXPECT_EQ(dumpASTString(ast_type_traits::TK_IgnoreUnlessSpelledInSource, L), + R"cpp( +LambdaExpr +|-VarDecl 'self' +| `-UnaryOperator +| `-CXXThisExpr +`-CompoundStmt +)cpp"); + } +} + } // namespace clang diff --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp index a21ed04b32d..b9075927d74 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -1751,6 +1751,17 @@ B func12() { return c; } +void func13() { + int a = 0; + int c = 0; + + [a, b = c](int d) { int e = d; }; +} + +void func14() { + [] <typename TemplateType> (TemplateType t, TemplateType u) { int e = t + u; }; +} + )cpp"; EXPECT_TRUE(matches( @@ -1821,6 +1832,23 @@ B func12() { returnStmt(forFunction(functionDecl(hasName("func12"))), hasReturnValue( declRefExpr(to(varDecl(hasName("c"))))))))); + + EXPECT_TRUE(matches( + Code, + traverse( + ast_type_traits::TK_IgnoreUnlessSpelledInSource, + lambdaExpr(forFunction(functionDecl(hasName("func13"))), + has(compoundStmt(hasDescendant(varDecl(hasName("e"))))), + has(declRefExpr(to(varDecl(hasName("a"))))), + has(varDecl(hasName("b"), hasInitializer(declRefExpr(to( + varDecl(hasName("c"))))))), + has(parmVarDecl(hasName("d"))))))); + + EXPECT_TRUE(matches( + Code, traverse(ast_type_traits::TK_IgnoreUnlessSpelledInSource, + lambdaExpr( + forFunction(functionDecl(hasName("func14"))), + has(templateTypeParmDecl(hasName("TemplateType"))))))); } TEST(IgnoringImpCasts, MatchesImpCasts) { |