From: Raphael Isemann Date: Mon, 20 Aug 2018 16:20:01 +0000 (+0000) Subject: [ASTImporter] Add test for C++ casts and fix broken const_cast importing. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ae679b05d34a1503557bd00c1d99d327b6f413e9;p=clang [ASTImporter] Add test for C++ casts and fix broken const_cast importing. Summary: The ASTImporter does currently not handle const_casts. This patch adds the missing const_cast importer code and the test case that discovered this. Reviewers: a.sidorin, a_sidorin Reviewed By: a_sidorin Subscribers: a_sidorin, martong, cfe-commits Differential Revision: https://reviews.llvm.org/D50932 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@340182 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp index c1136c9f42..8a8b3213e2 100644 --- a/lib/AST/ASTImporter.cpp +++ b/lib/AST/ASTImporter.cpp @@ -6897,6 +6897,10 @@ Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { return CXXReinterpretCastExpr::Create( Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath, ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets); + } else if (isa(E)) { + return CXXConstCastExpr::Create(Importer.getToContext(), ToType, VK, ToOp, + ToWritten, ToOperatorLoc, ToRParenLoc, + ToAngleBrackets); } else { return nullptr; } diff --git a/test/Import/cxx-casts/Inputs/F.cpp b/test/Import/cxx-casts/Inputs/F.cpp new file mode 100644 index 0000000000..79326a7e4b --- /dev/null +++ b/test/Import/cxx-casts/Inputs/F.cpp @@ -0,0 +1,12 @@ +struct A { + virtual ~A() {} +}; +struct B : public A {}; + +void f() { + const A *b = new B(); + const B *c1 = dynamic_cast(b); + const B *c2 = static_cast(b); + const B *c3 = reinterpret_cast(b); + A *c4 = const_cast(b); +} diff --git a/test/Import/cxx-casts/test.cpp b/test/Import/cxx-casts/test.cpp new file mode 100644 index 0000000000..49215ce81c --- /dev/null +++ b/test/Import/cxx-casts/test.cpp @@ -0,0 +1,21 @@ +// RUN: clang-import-test -dump-ast -import %S/Inputs/F.cpp -expression %s | FileCheck %s + +// CHECK: CXXDynamicCastExpr +// CHECK-SAME: dynamic_cast +// CHECK-SAME: + +// CHECK: CXXStaticCastExpr +// CHECK-SAME: static_cast +// CHECK-SAME: + +// CHECK: CXXReinterpretCastExpr +// CHECK-SAME: reinterpret_cast +// CHECK-SAME: + +// CHECK: CXXConstCastExpr +// CHECK-SAME: const_cast +// CHECK-SAME: + +void expr() { + f(); +} diff --git a/tools/clang-import-test/clang-import-test.cpp b/tools/clang-import-test/clang-import-test.cpp index 106f3d1d15..ff14f62574 100644 --- a/tools/clang-import-test/clang-import-test.cpp +++ b/tools/clang-import-test/clang-import-test.cpp @@ -194,6 +194,8 @@ std::unique_ptr BuildCompilerInstance() { Inv->getLangOpts()->ThreadsafeStatics = false; Inv->getLangOpts()->AccessControl = false; Inv->getLangOpts()->DollarIdents = true; + // Needed for testing dynamic_cast. + Inv->getLangOpts()->RTTI = true; Inv->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo); Inv->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();