Clang Project

clang_source_code/unittests/Tooling/CastExprTest.cpp
1//===- unittest/Tooling/CastExprTest.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
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}
38