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
11using namespace clang;
12
13namespace {
14
15// Check to ensure that InitListExpr is visited twice, once each for the
16// syntactic and semantic form.
17class InitListExprPreOrderVisitor
18 : public ExpectedLocationVisitor<InitListExprPreOrderVisitor> {
19public:
20 InitListExprPreOrderVisitor(bool VisitImplicitCode)
21 : VisitImplicitCode(VisitImplicitCode) {}
22
23 bool shouldVisitImplicitCode() const { return VisitImplicitCode; }
24
25 bool VisitInitListExpr(InitListExpr *ILE) {
26 Match(Name: ILE->isSemanticForm() ? "semantic" : "syntactic", Location: ILE->getBeginLoc());
27 return true;
28 }
29
30private:
31 bool VisitImplicitCode;
32};
33
34TEST(RecursiveASTVisitor, InitListExprIsPreOrderVisitedTwice) {
35 InitListExprPreOrderVisitor Visitor(/*VisitImplicitCode=*/true);
36 Visitor.ExpectMatch(Match: "syntactic", Line: 2, Column: 21);
37 Visitor.ExpectMatch(Match: "semantic", Line: 2, Column: 21);
38 EXPECT_TRUE(Visitor.runOver("struct S { int x; };\n"
39 "static struct S s = {.x = 0};\n",
40 InitListExprPreOrderVisitor::Lang_C));
41}
42
43TEST(RecursiveASTVisitor, InitListExprVisitedOnceWhenNoImplicit) {
44 InitListExprPreOrderVisitor Visitor(/*VisitImplicitCode=*/false);
45 Visitor.ExpectMatch(Match: "syntactic", Line: 2, Column: 21);
46 Visitor.DisallowMatch(Match: "semantic", Line: 2, Column: 21);
47 EXPECT_TRUE(Visitor.runOver("struct S { int x; };\n"
48 "static struct S s = {.x = 0};\n",
49 InitListExprPreOrderVisitor::Lang_C));
50}
51
52} // end anonymous namespace
53

source code of clang/unittests/Tooling/RecursiveASTVisitorTests/InitListExprPreOrder.cpp