[cfe-dev] Can't use sizeof ( typeof ( <vla> ))

Paulo Matos via cfe-dev cfe-dev at lists.llvm.org
Mon Dec 5 04:12:01 PST 2016



On 01/12/16 19:39, Friedman, Eli wrote:
> On 12/1/2016 2:13 AM, Paulo Matos wrote:
>>
>> On 22/11/16 21:44, Friedman, Eli wrote:
>>> The way it should work is that there are three calls to
>>> TransformToPotentiallyEvaluated: the first time, we're calling on the
>>> typeof, and it's unevaluated.  The second time, we're calling it on the
>>> sizeof(), and it's potentially evaluated.  The third time is a recursive
>>> call: in the process of transforming the sizeof(), we make a new
>>> typeof() expression, and call TransformToPotentiallyEvaluated on that;
>>> at that point, it should be potentially-evaluated.  (Yes, this is
>>> terribly inefficient, but it's a rare situation anyway.)
>>>
>> Apologies, only just managed to get back on this. I assume you're
>> suggesting a solution, not how it's implemented.
> 
> This is what's already implemented for sizeof() on a expression.  We
> just don't do it properly for sizeof() on a type, I think.  There are
> two functions named Sema::CreateUnaryExprOrTypeTraitExpr; compare the
> version which takes a type to the version which takes an expression.
> 

I see what you mean but it doesn't seem to be that straightforward. I
implemented the following patch which fixes the issue I was seeing but
crashes loads of other examples.

diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index dbaa04e..ff04a64 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -3780,8 +3780,18 @@
Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo,
     return ExprError();

   // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
-  return new (Context) UnaryExprOrTypeTraitExpr(
+  Expr *E = new (Context) UnaryExprOrTypeTraitExpr(
       ExprKind, TInfo, Context.getSizeType(), OpLoc, R.getEnd());
+
+  if (isUnevaluatedContext() &&
+      ExprKind == UETT_SizeOf &&
+      TInfo->getType()->isVariablyModifiedType()) {
+    ExprResult PE = TransformToPotentiallyEvaluated(E);
+    if (PE.isInvalid()) return ExprError();
+    E = PE.get();
+  }
+
+  return E;
 }


I will need to take a closer look into this to understand what isgoing on.

-- 
Paulo Matos



More information about the cfe-dev mailing list