[cfe-dev] python bindings

Dmitri Gribenko gribozavr at gmail.com
Thu Feb 21 23:32:28 PST 2013

On Fri, Feb 22, 2013 at 6:52 AM, Sam Price <thesamprice at gmail.com> wrote:
> That is what I want.
> I think I need to use an ASTContext to get get the size of a type though.
> Waiting for code to compile. I ended up with something like this
> int clang_getSizeOf(CXType CT ) {
>   CXTranslationUnit TU = GetTU( CT );
>   ASTUnit * AU = clang::cxtu::getASTUnit( TU );
>   const ASTContext & AC = AU->getASTContext();
>   QualType T = GetQualType( CT );
>   return AC.getTypeSize(T);
> }

That looks correct as a first approximation.  But:
* change the return type to long long;
* you also need to check for incomplete types and return an error;
* LLVM and Clang style is to put star next to the identifier: "ASTUnit *AU";
* style: no spaces in function calls: "GetQualType(CT);"
* you might want to make this function work like sizeof() *or*
document the differences (for example, if computing the size requires
template instantiation, we should do it; or sizeof a reference type
equals the size of the type; what about sizeof void, sizeof a function
type etc...);
* please add tests.  You need to extend c-index-test for that.


(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/

More information about the cfe-dev mailing list