[cfe-dev] extending clang C-API (CIndex)

Douglas Gregor dgregor at apple.com
Mon Jun 14 13:45:24 PDT 2010


On Jun 11, 2010, at 12:24 PM, Sebastien Binet wrote:

> 
> hi there,
> 
> I am about to work on extending the C-API provided by CIndex and friends
> so I can introspect the AST and (especially) all the xyzDecl (from
> python-ctypes.)
> 
> not everything can be readily exposed but, for what is easy to do, I was
> thinking of exposing the methods of the Decls like so:
> 
> /*
> * \brief Whether this class has a definition
> */
> CINDEX_LINKAGE unsigned clang_CXXRecordDecl_hasDefinition(CXCursor C);
> 
> unsigned clang_CXXRecordDecl_hasDefinition(CXCursor C) {
>  if (!clang_isDeclaration(C.kind))
>    return 0;
>  CXXRecordDecl *D =
>  dyn_cast<CXXRecordDecl>(cxcursor::getCursorDecl(C));
>  return (D && D->hasDefinition()) ? 1 : 0;
> }
> 
> 
> so the convention would be:
> 
> clang_[C++class-name]_[method-name]( cursor )
> 
> 
> what do you think ?


While designing the C API in libclang, we should be careful to only expose important functionality that is unlikely to change, and should strive to avoid introducing redundant functions. We want the C API to be clean and stable. In your example:

	- CXXRecordDecl and RecordDecl should share the same cursor kind and have the same operations in the C API: we'll use dynamic checking to determine when we're dealing with a C struct/union that never has certain attributes (e.g., base classes).

	- hasDefinition() is a shortcut for getting the definition of the current cursor, then checking whether the definition cursor is the same as the original cursor. This works for all cursor kinds, while clang_CXXRecordDecl_hasDefinition() would only work for C++ classes, structs, and unions.

I think it's great to make the C API more full-featured, but let's try to keep it minimal and full-featured.

	- Doug



More information about the cfe-dev mailing list