[PATCH] C++11: Reject string literal to non-const char * conversion
Ismail Pazarbasi
ismail.pazarbasi at gmail.com
Wed Nov 13 17:25:49 PST 2013
================
Comment at: lib/Sema/SemaOverload.cpp:1180-1182
@@ +1179,5 @@
+ const ImplicitConversionSequence &ICS) {
+ return (ICS.isStandard() && ICS.Standard.First == ICK_Array_To_Pointer &&
+ ICS.Standard.Second == ICK_Identity &&
+ ICS.Standard.Third == ICK_Qualification &&
+ ICS.Standard.DeprecatedStringLiteralToCharPtr &&
----------------
Richard Smith wrote:
> Do you really need to check these things? I would have thought checking `DeprecatedStirngLiteralToCharPtr` would be enough.
This function is removed in the new patch (coming soon). I have checked the full sequence, because the member `DeprecatedStringLiteralToCharPtr` was not initialized in other cases. I have fixed this in new patch. Instead of checking entire sequence, we check only `DeprecatedStringLiteralToCharPtr`. I thought it is better to initialize `DeprecatedStringLiteralToCharPtr` in all cases.
================
Comment at: lib/Sema/SemaOverload.cpp:3297-3305
@@ -3284,6 +3296,11 @@
// from any other user-defined conversion sequence.
if (ICS1.getKindRank() < ICS2.getKindRank())
- return ImplicitConversionSequence::Better;
+ return isIllFormedStringLiteralConversion(S, ICS1)
+ ? ImplicitConversionSequence::Worse
+ : ImplicitConversionSequence::Better;
+
if (ICS2.getKindRank() < ICS1.getKindRank())
- return ImplicitConversionSequence::Worse;
+ return isIllFormedStringLiteralConversion(S, ICS2)
+ ? ImplicitConversionSequence::Better
+ : ImplicitConversionSequence::Worse;
----------------
Richard Smith wrote:
> This looks suspicious: is it not possible for two conversions involving a string-literal-to-char* conversion to have different ranks? For instance, what if one is a user-defined conversion sequence that converts string literal -> char* -> class type, and the other just converts string literal -> char*?
>
> I think what you want is to start this function with something like:
>
> if (getLangOpts().CPlusPlus11 && !getLangOpts().WritableStrings &&
> ICS1.DeprecatedStringLiteralToCharPtr != ICS2.DeprecatedStringLiteralToCharPtr)
> return ICS1.DeprecatedStringLiteralToCharPtr ? ICS::Worse : ICS::Better;
>
> (That is, any ill-formed conversion is worse than any non-ill-formed conversion.)
Thank you for the heads up. I forgot user defined conversion sequence completely. I have used your suggestion almost exactly, with minor change. A new test added for user defined conversion sequence vs. ill-formed conversion ranking.
http://llvm-reviews.chandlerc.com/D1965
More information about the cfe-commits
mailing list