r267534 - [MSVC] PR27337: allow static_cast from private base to derived for WTL

Nico Weber via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 27 08:26:59 PDT 2016


Is it needed to parse system headers? In general, we implement things like
this if they're needed for system headers but don't otherwise.

On Wed, Apr 27, 2016 at 6:19 AM, Dmitry Polukhin via cfe-commits <
cfe-commits at lists.llvm.org> wrote:

> I added example with 'B*' to 'A*' just to make sure that my patch doesn't
> break it accidentally in MSVC mode.
>
> As for 'A*' to 'B*" case, indeed it is not very common and it looks like
> someone just forgot to specify 'public'. I tried to find more real world
> examples in public source codes but without Google Code Search it is hard
> to do (GitHub doesn't support regexp search). Please let me know if you
> think it doesn't make sense to support this "feature" in Clang and I'll
> revert my patch.
>
> On Wed, Apr 27, 2016 at 5:12 AM, Richard Smith via cfe-commits <
> cfe-commits at lists.llvm.org> wrote:
>
>> On Tue, Apr 26, 2016 at 7:04 PM, David Majnemer via cfe-commits <
>> cfe-commits at lists.llvm.org> wrote:
>>
>>> Visual Studio 2015 (19.00.23720.0) reports:
>>>
>>> error C2243: 'static_cast': conversion from 'B *' to 'A *' exists, but
>>> is inaccessible
>>>
>>
>> Right, it's the other direction ('A *' to 'B *') that this patch is
>> permitting.
>>
>>
>>> On Tue, Apr 26, 2016 at 6:33 PM, Richard Smith via cfe-commits <
>>> cfe-commits at lists.llvm.org> wrote:
>>>
>>>> As noted in PR27337, this only occurs in one WTL sample, and we have no
>>>> evidence that it actually occurs in real code. Have you seen uses of this
>>>> in the wild? We generally don't want to add compatibility for MSVC bugs
>>>> unless there's some real-world motivation.
>>>>
>>>>
>>>> On Tue, Apr 26, 2016 at 2:21 AM, Dmitry Polukhin via cfe-commits <
>>>> cfe-commits at lists.llvm.org> wrote:
>>>>
>>>>> Author: dpolukhin
>>>>> Date: Tue Apr 26 04:21:17 2016
>>>>> New Revision: 267534
>>>>>
>>>>> URL: http://llvm.org/viewvc/llvm-project?rev=267534&view=rev
>>>>> Log:
>>>>> [MSVC] PR27337: allow static_cast from private base to derived for WTL
>>>>>
>>>>> MSVC doesn't report even warning for cast from private base class to
>>>>> derived.
>>>>>
>>>>> Differential Revision: http://reviews.llvm.org/D19477
>>>>>
>>>>> Added:
>>>>>     cfe/trunk/test/SemaCXX/ext_ms_downcast.cpp   (with props)
>>>>> Modified:
>>>>>     cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>>>>>     cfe/trunk/lib/Sema/SemaCast.cpp
>>>>>
>>>>> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=267534&r1=267533&r2=267534&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
>>>>> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Apr 26
>>>>> 04:21:17 2016
>>>>> @@ -5764,6 +5764,9 @@ def err_static_downcast_via_virtual : Er
>>>>>    "cannot cast %0 to %1 via virtual base %2">;
>>>>>  def err_downcast_from_inaccessible_base : Error<
>>>>>    "cannot cast %select{private|protected}2 base class %1 to %0">;
>>>>> +def ext_ms_downcast_from_inaccessible_base : ExtWarn<
>>>>> +  "casting from %select{private|protected}2 base class %1 to derived
>>>>> class %0 is a Microsoft extension">,
>>>>> +  InGroup<MicrosoftCast>;
>>>>>  def err_upcast_to_inaccessible_base : Error<
>>>>>    "cannot cast %0 to its %select{private|protected}2 base class %1">;
>>>>>  def err_bad_dynamic_cast_not_ref_or_ptr : Error<
>>>>>
>>>>> Modified: cfe/trunk/lib/Sema/SemaCast.cpp
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCast.cpp?rev=267534&r1=267533&r2=267534&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- cfe/trunk/lib/Sema/SemaCast.cpp (original)
>>>>> +++ cfe/trunk/lib/Sema/SemaCast.cpp Tue Apr 26 04:21:17 2016
>>>>> @@ -1344,10 +1344,11 @@ TryStaticDowncast(Sema &Self, CanQualTyp
>>>>>    }
>>>>>
>>>>>    if (!CStyle) {
>>>>> -    switch (Self.CheckBaseClassAccess(OpRange.getBegin(),
>>>>> -                                      SrcType, DestType,
>>>>> -                                      Paths.front(),
>>>>> -
>>>>> diag::err_downcast_from_inaccessible_base)) {
>>>>> +    unsigned Diag = Self.getLangOpts().MSVCCompat
>>>>> +                        ? diag::ext_ms_downcast_from_inaccessible_base
>>>>> +                        : diag::err_downcast_from_inaccessible_base;
>>>>> +    switch (Self.CheckBaseClassAccess(OpRange.getBegin(), SrcType,
>>>>> DestType,
>>>>> +                                      Paths.front(), Diag)) {
>>>>>      case Sema::AR_accessible:
>>>>>      case Sema::AR_delayed:     // be optimistic
>>>>>      case Sema::AR_dependent:   // be optimistic
>>>>>
>>>>> Added: cfe/trunk/test/SemaCXX/ext_ms_downcast.cpp
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/ext_ms_downcast.cpp?rev=267534&view=auto
>>>>>
>>>>> ==============================================================================
>>>>> --- cfe/trunk/test/SemaCXX/ext_ms_downcast.cpp (added)
>>>>> +++ cfe/trunk/test/SemaCXX/ext_ms_downcast.cpp Tue Apr 26 04:21:17 2016
>>>>> @@ -0,0 +1,40 @@
>>>>> +// RUN: %clang_cc1 -fsyntax-only -fms-compatibility -verify %s
>>>>> +// RUN: %clang_cc1 -fsyntax-only -DNO_MS_COMPATIBILITY -verify %s
>>>>> +
>>>>> +// Minimal reproducer.
>>>>> +class A {};
>>>>> +class B : A {}; // expected-note 2 {{implicitly declared private
>>>>> here}}
>>>>> +
>>>>> +B* foo(A* p) {
>>>>> +  return static_cast<B*>(p);
>>>>> +#ifdef NO_MS_COMPATIBILITY
>>>>> +  // expected-error at -2 {{cannot cast private base class 'A' to 'B'}}
>>>>> +#else
>>>>> +  // expected-warning at -4 {{casting from private base class 'A' to
>>>>> derived class 'B' is a Microsoft extension}}
>>>>> +#endif
>>>>> +}
>>>>> +
>>>>> +A* bar(B* p) {
>>>>> +  return static_cast<A*>(p); // expected-error {{cannot cast 'B' to
>>>>> its private base class 'A'}}
>>>>> +}
>>>>> +
>>>>> +// from atlframe.h
>>>>> +template <class T>
>>>>> +class CUpdateUI {
>>>>> +public:
>>>>> +  CUpdateUI() {
>>>>> +    T* pT = static_cast<T*>(this);
>>>>> +#ifdef NO_MS_COMPATIBILITY
>>>>> +    // expected-error at -2 {{cannot cast private base class}}
>>>>> +#else
>>>>> +    // expected-warning at -4 {{casting from private base class
>>>>> 'CUpdateUI<CMDIFrame>' to derived class 'CMDIFrame' is a Microsoft
>>>>> extension}}
>>>>> +#endif
>>>>> +  }
>>>>> +};
>>>>> +
>>>>> +// from sample WTL/MDIDocVw (mainfrm.h
>>>>> +class CMDIFrame : CUpdateUI<CMDIFrame> {};
>>>>> +// expected-note at -1 {{implicitly declared private here}}
>>>>> +// expected-note at -2 {{in instantiation of member function}}
>>>>> +
>>>>> +CMDIFrame wndMain;
>>>>>
>>>>> Propchange: cfe/trunk/test/SemaCXX/ext_ms_downcast.cpp
>>>>>
>>>>> ------------------------------------------------------------------------------
>>>>>     svn:eol-style = native
>>>>>
>>>>> Propchange: cfe/trunk/test/SemaCXX/ext_ms_downcast.cpp
>>>>>
>>>>> ------------------------------------------------------------------------------
>>>>>     svn:keywords = "Author Date Id Rev URL"
>>>>>
>>>>> Propchange: cfe/trunk/test/SemaCXX/ext_ms_downcast.cpp
>>>>>
>>>>> ------------------------------------------------------------------------------
>>>>>     svn:mime-type = text/plain
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> cfe-commits mailing list
>>>>> cfe-commits at lists.llvm.org
>>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> cfe-commits mailing list
>>>> cfe-commits at lists.llvm.org
>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>>>>
>>>>
>>>
>>> _______________________________________________
>>> cfe-commits mailing list
>>> cfe-commits at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>>>
>>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>>
>>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160427/283e385b/attachment-0001.html>


More information about the cfe-commits mailing list