[cfe-dev] _Generic constant string as controlling expression

Prathamesh Kulkarni bilbotheelffriend at gmail.com
Fri Jan 31 22:49:28 PST 2014


On Tue, Dec 10, 2013 at 11:39 AM, Prathamesh Kulkarni
<bilbotheelffriend at gmail.com> wrote:
> On Mon, Dec 9, 2013 at 1:47 PM, Richard Smith <richard at metafoo.co.uk> wrote:
>> On Sun, Dec 8, 2013 at 2:16 PM, Prathamesh Kulkarni
>> <bilbotheelffriend at gmail.com> wrote:
>>>
>>> I am not able to understand why does the following code
>>> fail to compile:
>>>
>>> int main(void)
>>> {
>>>   int x = _Generic("hello", char *: 1);
>>> }
>>>
>>> Compiling it with clang-3.2 gives the following error:
>>> f.c:5:20: error: controlling expression type 'char [6]' not compatible
>>> with any generic association type
>>>   int a = _Generic("hello", char *: 1);
>>>
>>> n1570 $6.3.2.1 3rd point states:
>>> "Except when it is the operand of the sizeof operator, the _Alignof
>>> operator, or the
>>> unary & operator, or is a string literal used to initialize an array,
>>> an expression that has type ''array of type'' is converted to an
>>> expression with type ''pointer to type'' that points to the initial
>>> element of the array object and is not an lvalue. If the array object
>>> has register storage class, the behavior is undefined."
>>>
>>> Since this clause has no mention for _Generic, shouldn't the type of
>>> "hello"
>>> be converted from char[6] to char * when it's used as a controlling
>>> expression ?
>>> Or am I missing something ?
>>
>>
>> Yes, I think it should -- this looks like a bug. Please file a bug report!
>>
> I browsed through the bug database, it seems it was already
> reported: http://llvm.org/bugs/show_bug.cgi?id=16340
Is the following fix correct ? After applying it, the error didn't get reported.

Index: SemaExpr.cpp
===================================================================
--- SemaExpr.cpp  (revision 200001)
+++ SemaExpr.cpp  (working copy)
@@ -1300,6 +1300,12 @@ Sema::CreateGenericSelectionExpr(SourceL
     ControllingExpr = result.take();
   }

+  QualType ty = ControllingExpr->getType();
+  if (ty->isArrayType())
+    ControllingExpr = ImpCastExprToType(ControllingExpr,
Context.getArrayDecayedType(ControllingExpr->getType()),
+                                        CK_ArrayToPointerDecay).take();
+
+
   bool TypeErrorFound = false,
        IsResultDependent = ControllingExpr->isTypeDependent(),
        ContainsUnexpandedParameterPack

>>>
>>> Thanks and Regards,
>>> Prathamesh
>>>
>>> _______________________________________________
>>> cfe-dev mailing list
>>> cfe-dev at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>>



More information about the cfe-dev mailing list