[cfe-dev] Expr::isIntegerConstantExpr() gives bogus results with sizeof()

Nuno Lopes nunoplopes at sapo.pt
Sun Nov 25 09:39:03 PST 2007


Hi,

Expr::isIntegerConstantExpr() seems to return bogus results with the  
sizeof() operator.
For example, for input "21-2*2+4", it returns 21 as expected
but with sizeof:

int buf_src[4];
sizeof(buf_src) => 128 (4 * 4 * 8 bits)
sizeof(buf_src)-sizeof(buf_src)/sizeof(*buf_src) => 124 (???)

So it seems that sizeof() is being computed in bits, but then the  
operations aren't consistent. (in fact it should be computed in bytes,  
as that's the value of that operator in C/C++)

Proposed patch:

Index: Expr.cpp
===================================================================
--- Expr.cpp    (revision 44314)
+++ Expr.cpp    (working copy)
@@ -589,7 +589,7 @@
        // Get information about the size or align.
        if (Exp->getOpcode() == UnaryOperator::SizeOf)
          Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
-                                 Exp->getOperatorLoc());
+                                 Exp->getOperatorLoc()) / 8;
        else
          Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
                                    Exp->getOperatorLoc());



Regards,
Nuno




More information about the cfe-dev mailing list