[cfe-dev] Helpers to compute sizeof/alignof/offsetof?
Ted Kremenek
kremenek at apple.com
Thu Jan 17 09:05:52 PST 2008
On Jan 17, 2008, at 8:47 AM, Chris Lattner wrote:
> On Jan 17, 2008, at 3:11 AM, Eli Friedman wrote:
>
>> Currently, there are multiple places that need the values for
>> sizeof/alignof/offsetof (at least two places in isIntegerConstantExpr
>> and the visitor methods in CGExprScalar.cpp). However, there is no
>> common method to compute the needed information. There really should
>> be common methods (probably in ASTContext?) to get the result of
>> evaluating these expressions, because the logic isn't trivial.
>>
>> Sound reasonable?
>>
>> The reason I came upon this is that sizeof(void) currently crashes,
>> and there are currently three places that have to be changed to get
>> it
>> right.
>
> Sounds good to me.
I also think this is a good idea. ASTContext seems like a reasonably
place since all the type information is there, and clients of the ASTs
are likely to have a reference to ASTContext.
>> Implementation notes:
>> The two versions in isIntegerConstantExpr for sizeof/alignof (which
>> appear to be the same) look mostly correct, although they don't
>> handle
>> sizeof(void) correctly... the version in CGExprScalar.cpp also
>> mishandles "sizeof(void ())" (and a theoretical arch where
>> CHAR_BIT !=
>> 8...). Also, ASTContext::getTypeInfo currently mishandles references
>> in order to get the sizeof case correct, so once an alternative
>> exists, that should be fixed.
>
> The only big gotcha will be sizeof(VLA), which doesn't return a
> constant. I don't think there is a good way to represent this without
> knowing the client. The code generator (f.e.) needs to expand this
> out to LLVM IR instructions.
Agreed, clients will need to perform their own reasoning about VLAs.
What the code generator and (say) the static analysis engine do are
context specific.
For the "generic" method that returns the sizeof, we may consider
having it return a variant type that wraps an integer primitive. The
variant could also have a flag indicating whether or the not the
sizeof returned is valid. This way we can have the generic method
return "unknown" for VLAs if clients just blindly call sizeof on VLA
types. For example:
class SizeofVariant {
unsigned value; // probably will use something other than
unsigned, but you get the idea
bool IsValid;
public:
SizeOfVariant(unsigned V) : value(V), IsValid(true) {}
explicit SizeOfVariant() : value(0), IsValid(false) {}
unsigned getValue() const { assert (IsValid); return value; }
bool isValid() const { return IsValid; };
operator bool() const { return isValid(); }
operator unsigned() const { return getValue(); }
};
More information about the cfe-dev
mailing list