[PATCH] D86369: [Sema][MSVC] warn at dynamic_cast when /GR- is given
Zequan Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 21 13:42:44 PDT 2020
zequanwu created this revision.
zequanwu added a reviewer: hans.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
zequanwu requested review of this revision.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D86369
Files:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaCast.cpp
clang/test/SemaCXX/ms_dynamic_cast.cpp
Index: clang/test/SemaCXX/ms_dynamic_cast.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/ms_dynamic_cast.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -fno-rtti-data -fsyntax-only -verify
+
+class B {
+public:
+ virtual ~B() = default;
+};
+
+class D1 : public B {
+public:
+ ~D1() = default;
+
+};
+
+void f() {
+ B *b = new D1();
+ auto d = dynamic_cast<D1 *>(b); // expected-warning{{should not use dynamic_cast with /GR-}}
+}
\ No newline at end of file
Index: clang/lib/Sema/SemaCast.cpp
===================================================================
--- clang/lib/Sema/SemaCast.cpp
+++ clang/lib/Sema/SemaCast.cpp
@@ -890,6 +890,11 @@
return;
}
+ // MSVC warns when dynamic_cast is used with /GR-.
+ if (!Self.getLangOpts().RTTIData) {
+ Self.Diag(OpRange.getBegin(), diag::warn_no_dynamic_cast_with_no_GR);
+ }
+
// Done. Everything else is run-time checks.
Kind = CK_Dynamic;
}
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7433,6 +7433,8 @@
"use of typeid requires -frtti">;
def err_no_dynamic_cast_with_fno_rtti : Error<
"use of dynamic_cast requires -frtti">;
+def warn_no_dynamic_cast_with_no_GR: Warning<
+ "should not use dynamic_cast with /GR-">;
def err_cannot_form_pointer_to_member_of_reference_type : Error<
"cannot form a pointer-to-member to member %0 of reference type %1">;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86369.287098.patch
Type: text/x-patch
Size: 1601 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200821/91b094c9/attachment.bin>
More information about the cfe-commits
mailing list