[cfe-dev] Evaluating bitfield width with libclang

Sergiy Migdalskiy migdalskiy at hotmail.com
Thu Sep 6 10:47:37 PDT 2012


Ok, does everyone agree on this variant?
Also, is the check "isValueDependent()" essentially for the cases when 
bitwidth is templatized? Or is there some other reason?

// Return bitfield bit width. Cursor must be a value-independent bit field 
declaration
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 && !pBitWidth->isValueDependent() )
        return FD->getBitWidthValue( getCursorASTUnit(C)->getASTContext() );
    }
  }
  return -1;
}

Sincerely,
Sergiy Migdalskiy

-----Original Message----- 
From: Jacob Carlborg
Sent: Thursday, September 06, 2012 1:03 AM
To: cfe-dev at cs.uiuc.edu
Subject: Re: [cfe-dev] Evaluating bitfield width with libclang

On 2012-09-06 01:08, Sergiy Migdalskiy wrote:
> Hello,
> 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.

This is just what I need.

> 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;
> }
> Sincerely,
> Sergiy Migdalskiy

Do we really need three different values to indicate a failure? Either
you can get the width of the bitfield or you can't.

-- 
/Jacob Carlborg

_______________________________________________
cfe-dev mailing list
cfe-dev at cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev 





More information about the cfe-dev mailing list