[cfe-dev] Evaluating bitfield width with libclang

Sergiy Migdalskiy migdalskiy at hotmail.com
Wed Sep 5 16:08:03 PDT 2012


I just implemented my first function in libclang – it gets a field cursor and returns the bitfield width, if that’s a bitfield. Otherwise it returns a negative value.

I’d like to submit it as a patch (never did that before, I’ll read up on how to do that later), but first can someone review this routine? It seems to work, but having never written anything inside clang (and having played with clang for only a couple of weeks now), I suspect there may just be half a dozen bugs there. In any case, I hope it’ll be helpful at least for some people reading this list.

And by the way, kudos to all Clang developers for the high quality product. The code (at least the bits I looked at when implementing this) is a delight to read.

// -1 : cursor is not a field declaration
// -2 : cursor is not a bitfield declaration
// -3 : could not evaluate the bitfield width
int clang_Cursor_getBitfieldWidth(CXCursor C) {
  if (clang_isDeclaration(C.kind)) {
    Decl *D = cxcursor::getCursorDecl(C);
    if( const FieldDecl *FD = dyn_cast_or_null< FieldDecl >(D) ){
      Expr *pBitWidth = FD->getBitWidth();
      if( pBitWidth ){
        llvm::APSInt width;
        if( pBitWidth->EvaluateAsInt(width, getCursorASTUnit(C)->getASTContext() ) ){
          return *width.getRawData();
        return -3;
      return -2;
  return -1;

Sergiy Migdalskiy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120905/488a163b/attachment.html>

More information about the cfe-dev mailing list