CastExprTest.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. #include "TestVisitor.h"
  9. using namespace clang;
  10. namespace {
  11. struct CastExprVisitor : TestVisitor<CastExprVisitor> {
  12. std::function<void(ExplicitCastExpr *)> OnExplicitCast;
  13. bool VisitExplicitCastExpr(ExplicitCastExpr *Expr) {
  14. if (OnExplicitCast)
  15. OnExplicitCast(Expr);
  16. return true;
  17. }
  18. };
  19. TEST(CastExprTest, GetSubExprAsWrittenThroughMaterializedTemporary) {
  20. CastExprVisitor Visitor;
  21. Visitor.OnExplicitCast = [](ExplicitCastExpr *Expr) {
  22. auto Sub = Expr->getSubExprAsWritten();
  23. EXPECT_TRUE(isa<DeclRefExpr>(Sub))
  24. << "Expected DeclRefExpr, but saw " << Sub->getStmtClassName();
  25. };
  26. Visitor.runOver("struct S1 {};\n"
  27. "struct S2 { operator S1(); };\n"
  28. "S1 f(S2 s) { return static_cast<S1>(s); }\n");
  29. }
  30. }