[llvm-commits] [PATCH] Casting.h: Don't cast away const with static_cast<>

Michael Spencer bigcheesegs at gmail.com
Tue Mar 26 14:56:45 PDT 2013


On Fri, Mar 22, 2013 at 3:20 PM, Rafael EspĂ­ndola <
rafael.espindola at gmail.com> wrote:

> On 20 March 2013 15:21, Sean Silva <silvas at purdue.edu> wrote:
> > This patch was mostly a "peephole" change that I saw when I was in the
> area.
> > I'm not familiar enough with this code (or at least it's not in cache
> for me
> > ATM) to be able to meaningfully comment on your patch.
> >
> > Judging by the look of some of the fixups you did for this patch, you
> might
> > want to doublecheck if there is anything on the clang side that needs to
> be
> > fixed up.
>
> Thanks for the reminder. And thanks for finding this in the first place!
>
> I took another look on what simplify_type was trying to do. What I found
> was:
>
> As the description says, it extracts the underlying type from a "smart
> pointer" (also includes things like llvm::Use). There are 3 basic
> kinds of pointers to consider:
>
> * const correct: A 'const MyPtr<int> &' produces a 'const int*'. A
> 'MyPtr<int> &' produces a 'int *'.
> * always const: Even a 'MyPtr<int> &' produces a 'const int*'.
> * no const: Even a 'const MyPtr<int> &' produces a 'int*'.
>
> The existing code had a broken forwarding system. It looks like it was
> trying to implement const correct smart pointer by default, but since
> it was broken, every specialization had both const and non const
> versions.
>
> Another problem with the existing implementation is that it was
> dropping the 'const' in some areas, forcing some specializations to
> use const_cast (::clang::CFGTerminator).
>
> The attached patches
>
> * Remove the unused specializations. Since they are unused, it is hard
> to know which variant should be implemented.
> * Make sure we don't drop const.
> * Fix the default forwarding so that const correct pointer only need
> one specialization.
> * Simplifies the existing specializations.
>
> The LLVM/Clang specific logic is fairly simple. It is the template
> meta programming that makes the patches a bit complex. Michael, are
> you OK reviewing these patches?
>
> > -- Sean Silva
>
> Thanks,
> Rafael
>

The LLVM part LGTM with the following change:

put

+template<typename T, typename Enable = void>
+struct add_lvalue_reference_if_not_pointer {
+  typedef T &type;
+};
+
+template<typename T>
+struct add_lvalue_reference_if_not_pointer<T,
+                                     typename enable_if<is_pointer<T>
>::type> {
+  typedef T type;
+};
+
+template<typename T, typename Enable = void>
+struct add_const_past_pointer {
+  typedef const T type;
+};
+
+template<typename T>
+struct add_const_past_pointer<T, typename enable_if<is_pointer<T> >::type>
{
+  typedef const typename remove_pointer<T>::type *type;
+};
+

in type_traits.h and comment them.

- Michael Spencer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130326/9a18ac09/attachment.html>


More information about the llvm-commits mailing list