blob: a9e78d2155b498c53596296630c3018140e3efef [file] [log] [blame]
Stephan Bergmannf31b0dc2017-06-27 08:19:09 +00001//===- unittest/Tooling/CastExprTest.cpp ----------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Stephan Bergmannf31b0dc2017-06-27 08:19:09 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "TestVisitor.h"
10
11using namespace clang;
12
13namespace {
14
15struct CastExprVisitor : TestVisitor<CastExprVisitor> {
16 std::function<void(ExplicitCastExpr *)> OnExplicitCast;
17
18 bool VisitExplicitCastExpr(ExplicitCastExpr *Expr) {
19 if (OnExplicitCast)
20 OnExplicitCast(Expr);
21 return true;
22 }
23};
24
25TEST(CastExprTest, GetSubExprAsWrittenThroughMaterializedTemporary) {
26 CastExprVisitor Visitor;
27 Visitor.OnExplicitCast = [](ExplicitCastExpr *Expr) {
28 auto Sub = Expr->getSubExprAsWritten();
29 EXPECT_TRUE(isa<DeclRefExpr>(Sub))
30 << "Expected DeclRefExpr, but saw " << Sub->getStmtClassName();
31 };
32 Visitor.runOver("struct S1 {};\n"
33 "struct S2 { operator S1(); };\n"
34 "S1 f(S2 s) { return static_cast<S1>(s); }\n");
35}
36
37}