brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · 7b2ed97 Raw
66 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "TestVisitor.h"10 11using namespace clang;12 13namespace {14 15/// \brief A visitor that optionally includes implicit code and matches16/// CXXConstructExpr.17///18/// The name recorded for the match is the name of the class whose constructor19/// is invoked by the CXXConstructExpr, not the name of the class whose20/// constructor the CXXConstructExpr is contained in.21class ConstructExprVisitor : public ExpectedLocationVisitor {22public:23  ConstructExprVisitor() { ShouldVisitImplicitCode = false; }24 25  bool VisitCXXConstructExpr(CXXConstructExpr *Expr) override {26    if (const CXXConstructorDecl* Ctor = Expr->getConstructor()) {27      if (const CXXRecordDecl* Class = Ctor->getParent()) {28        Match(Class->getName(), Expr->getLocation());29      }30    }31    return true;32  }33};34 35TEST(RecursiveASTVisitor, CanVisitImplicitMemberInitializations) {36  ConstructExprVisitor Visitor;37  Visitor.ShouldVisitImplicitCode = true;38  Visitor.ExpectMatch("WithCtor", 2, 8);39  // Simple has a constructor that implicitly initializes 'w'.  Test40  // that a visitor that visits implicit code visits that initialization.41  // Note: Clang lazily instantiates implicit declarations, so we need42  // to use them in order to force them to appear in the AST.43  EXPECT_TRUE(Visitor.runOver(44      "struct WithCtor { WithCtor(); }; \n"45      "struct Simple { WithCtor w; }; \n"46      "int main() { Simple s; }\n"));47}48 49// The same as CanVisitImplicitMemberInitializations, but checking that the50// visits are omitted when the visitor does not include implicit code.51TEST(RecursiveASTVisitor, CanSkipImplicitMemberInitializations) {52  ConstructExprVisitor Visitor;53  Visitor.ShouldVisitImplicitCode = false;54  Visitor.DisallowMatch("WithCtor", 2, 8);55  // Simple has a constructor that implicitly initializes 'w'.  Test56  // that a visitor that skips implicit code skips that initialization.57  // Note: Clang lazily instantiates implicit declarations, so we need58  // to use them in order to force them to appear in the AST.59  EXPECT_TRUE(Visitor.runOver(60      "struct WithCtor { WithCtor(); }; \n"61      "struct Simple { WithCtor w; }; \n"62      "int main() { Simple s; }\n"));63}64 65} // end anonymous namespace66