[PATCH] [libclang] getTypeSize patch

Dmitri Gribenko gribozavr at gmail.com
Thu Mar 14 06:15:45 PDT 2013


On Sun, Mar 10, 2013 at 8:58 AM, Sam Price <thesamprice at gmail.com> wrote:
> I probably coded this up wrong...

This is a good first step.  But there are unhandled cases:

* sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
extension (lib/AST/ExprConstant.cpp:1372)

* C++ [expr.sizeof]p2: "When applied to a reference or a reference
type, the result is the size of the referenced type."
(lib/AST/ExprConstant.cpp:5228).

> Can I just use the Plan Old Datatype as the one and only check???

While we can add clang_getSizeAndAlignOfPODType(), a general version
would be useful.

> enum CXSizeOfTypeError clang_getSizeAndAlignOfType(CXType CT, long long
> *size, long long *alignment)
>
> {
>
>   if(clang_isPODType(CT) == 0)
>
>   {
>
> *size = *alignment = -1;
>
> return CXSizeOfTypeError_DependentType;
>
>   }

Why do you check for POD?  sizeof()/alignof() can be applied to
non-POD types, too.

>
>   QualType T = GetQualType(CT);
>
>   const Type *TP = T.getTypePtrOrNull();
>
>   if(!TP)

T.isNull()

>
>   {
>
>     *size = *alignment = -1;
>
>     return CXSizeOfTypeError_VoidPointer;
>
>   }
>
>   if(TP && TP->isDependentType()) {

No need to check that T is not null.

There's no need in TP at all, you can do T->isDependentType().

>
>     *size = *alignment = -1;
>
>     return CXSizeOfTypeError_DependentType;
>
>   }
>
>
>   CXTranslationUnit TU = GetTU(CT);
>
>   ASTUnit *AU = clang::cxtu::getASTUnit(TU);
>
>   const ASTContext &AC = AU->getASTContext();
>
>
>   *size = AC.getTypeSize(T);
>
>   *alignment = AC.getTypeAlign(T);
>
>   return CXSizeOfTypeError_None;
>
> }

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/



More information about the cfe-commits mailing list