[cfe-dev] Evaluating bitfield width with libclang

Richard Smith richard at metafoo.co.uk
Wed Sep 5 17:13:53 PDT 2012


On Wed, Sep 5, 2012 at 4:08 PM, Sergiy Migdalskiy <migdalskiy at hotmail.com>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.
>
> 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();
>         }
>

You should use FD->getBitWidthValue(ASTContext) to get the width. Before
that call, you should also bail out with an error if
pBitWidth->isValueDependent().
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120905/3ce9bfc6/attachment.html>


More information about the cfe-dev mailing list