]> granicus.if.org Git - clang/commitdiff
[ASTImporter] Add test for C++ casts and fix broken const_cast importing.
authorRaphael Isemann <teemperor@gmail.com>
Mon, 20 Aug 2018 16:20:01 +0000 (16:20 +0000)
committerRaphael Isemann <teemperor@gmail.com>
Mon, 20 Aug 2018 16:20:01 +0000 (16:20 +0000)
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

lib/AST/ASTImporter.cpp
test/Import/cxx-casts/Inputs/F.cpp [new file with mode: 0644]
test/Import/cxx-casts/test.cpp [new file with mode: 0644]
tools/clang-import-test/clang-import-test.cpp

index c1136c9f428ce3c5d49bd9b8ba6912a026a10f1f..8a8b3213e25452e2475aa19c9fa78ab83ff160ea 100644 (file)
@@ -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<CXXConstCastExpr>(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 (file)
index 0000000..79326a7
--- /dev/null
@@ -0,0 +1,12 @@
+struct A {
+  virtual ~A() {}
+};
+struct B : public A {};
+
+void f() {
+  const A *b = new B();
+  const B *c1 = dynamic_cast<const B *>(b);
+  const B *c2 = static_cast<const B *>(b);
+  const B *c3 = reinterpret_cast<const B *>(b);
+  A *c4 = const_cast<A *>(b);
+}
diff --git a/test/Import/cxx-casts/test.cpp b/test/Import/cxx-casts/test.cpp
new file mode 100644 (file)
index 0000000..49215ce
--- /dev/null
@@ -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: <Dynamic>
+
+// CHECK: CXXStaticCastExpr
+// CHECK-SAME: static_cast
+// CHECK-SAME: <BaseToDerived (A)>
+
+// CHECK: CXXReinterpretCastExpr
+// CHECK-SAME: reinterpret_cast
+// CHECK-SAME: <BitCast>
+
+// CHECK: CXXConstCastExpr
+// CHECK-SAME: const_cast
+// CHECK-SAME: <NoOp>
+
+void expr() {
+  f();
+}
index 106f3d1d150d0ec27c8a9e5b6899328f2ba2a41e..ff14f62574ef82114f44a3a9fef2fda5445c42c7 100644 (file)
@@ -194,6 +194,8 @@ std::unique_ptr<CompilerInstance> 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();