[PATCH] [libclang] getTypeSize patch

Sam Price thesamprice at gmail.com
Sat Mar 9 22:58:51 PST 2013


I probably coded this up wrong...
Can I just use the Plan Old Datatype as the one and only check???

enum CXSizeOfTypeError clang_getSizeAndAlignOfType(CXType CT, long long*size,
long long *alignment)

{

  if(clang_isPODType(CT) == 0)

  {

*size = *alignment = -1;

return CXSizeOfTypeError_DependentType;

  }

  QualType T = GetQualType(CT);

  const Type *TP = T.getTypePtrOrNull();

  if(!TP)

  {

    *size = *alignment = -1;

    return CXSizeOfTypeError_VoidPointer;

  }

  if(TP && TP->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;

}


On Sat, Feb 23, 2013 at 7:17 PM, Argyrios Kyrtzidis <akyrtzi at gmail.com>wrote:

>
> On Feb 22, 2013, at 4:38 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
>
> > On Sat, Feb 23, 2013 at 1:22 AM, Sam Price <thesamprice at gmail.com>
> wrote:
> >> Added a command to grab the the size, and the proper hooks to access it
> >> within python bindings.
> >
> > Hi Sam,
> >
> > /**
> > + * \brief Retrieve the size of the given type.
> > + */
> > +CINDEX_LINKAGE long long clang_getTypeSize(CXType T);
> >
> > Is the size in bytes or bits?
> >
> > Please also expand this comment to cover what we do for corner cases.
> > I think that it is a good idea for this API to return exactly the same
> > value that sizeof returns.  (But I don't think this is the case with
> > current implementation -- consider function types, references,
> > incomplete types, uninstantiated templates).  I understand that this
> > is might be much more than you need to accomplish your task, but the
> > feature should be implemented completely, or not done at all.
> >
> > Argyrios, what is your preference on the semantics of this function?
> > Should it try to return the same thing as sizeof?
>
> Yes, that makes sense and is easy to understand what the function does.
>
> Also how about enhancing it to also return alignment with something like
> this:
> CXSizeOfTypeError clang_getSizeAndAlignOfType(CXType T, unsigned long long
> *size, unsigned long long *align)
>
> The return value would be an enum indicating if the function was
> successful and if not what kind of error occurred.
>
> -Argyrios
>
> >
> > +    /*Print Size of type*/
> >
> > It should be /* Print size of type. */ -- note spaces, capitalization,
> > full stop.
> >
> > +    {
> > +      PrintTypeSize(T, "[typesize=%d]");
> > +    }
> >
> > (1) There should be a space in the string, " [typesize...
> > (2) The format should be %lld
> > (3) Just inline the PrintTypeSize.  There's only one call site, thus
> > no need for this trivial function.
> >
> > +long long clang_getTypeSize(CXType CT ) {
> >
> > Extra space after CT.
> >
> > +  CXTranslationUnit TU = GetTU(CT);
> > +  ASTUnit *AU = clang::cxtu::getASTUnit(TU);
> > +  const ASTContext &AC = AU->getASTContext();
> > +  QualType T = GetQualType(CT);
> > +  return AC.getTypeSize(T);
> > +}
> >
> > I did not run this, but it would probably crash on an incomplete type.
> > Even if it doesn't, we should have a separate error code for that.
> >
> > The actual tests are missing.  I think that current tests should have
> > been broken by this, too, since you have changed the output format of
> > c-index-test (this is the correct thing to do, but you need to update
> > existing tests for the new information you print).
> >
> > Python bindings also need tests -- see bindings/python/tests/ directory.
> >
> > 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>*/
>
>


-- 
Thank you,

Sam Price
(707) 742-3726
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130310/6a1f64f1/attachment.html>


More information about the cfe-commits mailing list