Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / unittests / Tooling / RecursiveASTVisitorTests / ConstructExpr.cpp
blobb4f4f54dc7e2f92264893ed3a7113e62c60be9d8
1 //===- unittest/Tooling/RecursiveASTVisitorTests/ConstructExpr.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 //===----------------------------------------------------------------------===//
9 #include "TestVisitor.h"
11 using namespace clang;
13 namespace {
15 /// \brief A visitor that optionally includes implicit code and matches
16 /// CXXConstructExpr.
17 ///
18 /// The name recorded for the match is the name of the class whose constructor
19 /// is invoked by the CXXConstructExpr, not the name of the class whose
20 /// constructor the CXXConstructExpr is contained in.
21 class ConstructExprVisitor
22 : public ExpectedLocationVisitor<ConstructExprVisitor> {
23 public:
24 ConstructExprVisitor() : ShouldVisitImplicitCode(false) {}
26 bool shouldVisitImplicitCode() const { return ShouldVisitImplicitCode; }
28 void setShouldVisitImplicitCode(bool NewValue) {
29 ShouldVisitImplicitCode = NewValue;
32 bool VisitCXXConstructExpr(CXXConstructExpr* Expr) {
33 if (const CXXConstructorDecl* Ctor = Expr->getConstructor()) {
34 if (const CXXRecordDecl* Class = Ctor->getParent()) {
35 Match(Class->getName(), Expr->getLocation());
38 return true;
41 private:
42 bool ShouldVisitImplicitCode;
45 TEST(RecursiveASTVisitor, CanVisitImplicitMemberInitializations) {
46 ConstructExprVisitor Visitor;
47 Visitor.setShouldVisitImplicitCode(true);
48 Visitor.ExpectMatch("WithCtor", 2, 8);
49 // Simple has a constructor that implicitly initializes 'w'. Test
50 // that a visitor that visits implicit code visits that initialization.
51 // Note: Clang lazily instantiates implicit declarations, so we need
52 // to use them in order to force them to appear in the AST.
53 EXPECT_TRUE(Visitor.runOver(
54 "struct WithCtor { WithCtor(); }; \n"
55 "struct Simple { WithCtor w; }; \n"
56 "int main() { Simple s; }\n"));
59 // The same as CanVisitImplicitMemberInitializations, but checking that the
60 // visits are omitted when the visitor does not include implicit code.
61 TEST(RecursiveASTVisitor, CanSkipImplicitMemberInitializations) {
62 ConstructExprVisitor Visitor;
63 Visitor.setShouldVisitImplicitCode(false);
64 Visitor.DisallowMatch("WithCtor", 2, 8);
65 // Simple has a constructor that implicitly initializes 'w'. Test
66 // that a visitor that skips implicit code skips that initialization.
67 // Note: Clang lazily instantiates implicit declarations, so we need
68 // to use them in order to force them to appear in the AST.
69 EXPECT_TRUE(Visitor.runOver(
70 "struct WithCtor { WithCtor(); }; \n"
71 "struct Simple { WithCtor w; }; \n"
72 "int main() { Simple s; }\n"));
75 } // end anonymous namespace