| 1 | //===- unittest/Tooling/RecursiveASTVisitorTests/InitListExprPreOrder.cpp -===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "TestVisitor.h" |
| 10 | |
| 11 | using namespace clang; |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | // Check to ensure that InitListExpr is visited twice, once each for the |
| 16 | // syntactic and semantic form. |
| 17 | class InitListExprPreOrderVisitor : public ExpectedLocationVisitor { |
| 18 | public: |
| 19 | InitListExprPreOrderVisitor(bool VisitImplicitCode) { |
| 20 | ShouldVisitImplicitCode = VisitImplicitCode; |
| 21 | } |
| 22 | |
| 23 | bool VisitInitListExpr(InitListExpr *ILE) override { |
| 24 | Match(ILE->isSemanticForm() ? "semantic" : "syntactic" , ILE->getBeginLoc()); |
| 25 | return true; |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | TEST(RecursiveASTVisitor, InitListExprIsPreOrderVisitedTwice) { |
| 30 | InitListExprPreOrderVisitor Visitor(/*VisitImplicitCode=*/true); |
| 31 | Visitor.ExpectMatch("syntactic" , 2, 21); |
| 32 | Visitor.ExpectMatch("semantic" , 2, 21); |
| 33 | EXPECT_TRUE(Visitor.runOver("struct S { int x; };\n" |
| 34 | "static struct S s = {.x = 0};\n" , |
| 35 | InitListExprPreOrderVisitor::Lang_C)); |
| 36 | } |
| 37 | |
| 38 | TEST(RecursiveASTVisitor, InitListExprVisitedOnceWhenNoImplicit) { |
| 39 | InitListExprPreOrderVisitor Visitor(/*VisitImplicitCode=*/false); |
| 40 | Visitor.ExpectMatch("syntactic" , 2, 21); |
| 41 | Visitor.DisallowMatch("semantic" , 2, 21); |
| 42 | EXPECT_TRUE(Visitor.runOver("struct S { int x; };\n" |
| 43 | "static struct S s = {.x = 0};\n" , |
| 44 | InitListExprPreOrderVisitor::Lang_C)); |
| 45 | } |
| 46 | |
| 47 | } // end anonymous namespace |
| 48 | |