Menu ▾ ▴

#437 struct with bit fields reformats badly when variables set bit field sizes

closed-fixed
None
2023-05-15
2017-05-24
AmH
No

Best to illustrate with an example. This:

struct Bits
{
   unsigned int field1  : 8; // comment
   unsigned int field2  : FIELD2_BITS;
   unsigned int field3  : FIELD3_BITS; // comment
   unsigned int field4  : 9;
};

becomes this:

struct Bits
{
   unsigned int field1  : 8; // comment
unsigned int field2  :
   FIELD2_BITS;
unsigned int field3  :
   FIELD3_BITS; // comment
   unsigned int field4  : 9;
};

The desired behavior is that astyle do nothing to the above struct such that "after" matches "before". However, as you can see, lines with bit sizes set using variables (FIELD2_BITS, FIELD3_BITS) are reformatted (clobbered) while bit sizes set by literal values (8, 9) are correctly left untouched.

Discussion

  • Gregor Rebel

    Gregor Rebel - 2018-02-06

    I experience a similar problemusing this line:

    astyle --add-one-line-brackets --align-pointer=type --align-reference=middle --break-closing-brackets --convert-tabs --indent-cases --indent-classes --indent-col1-comments --indent-labels --indent-preproc-block --indent-preproc-define --indent-preproc-cond --indent=spaces=4 --indent-switches --keep-one-line-statements --lineend=linux --max-instatement-indent=100 --pad-oper --pad-paren-in --pad-header --style=java --suffix=none --unpad-paren --formatted 
    

    It works with this struct:

    struct foo_s {
        unsigned HSION        : 1; // abc
        unsigned PLL3ON       : 1; // def
        unsigned PLL3RDY      : 1; // ghi
        unsigned reserved3    : 2; // jkl
    };
    

    But when I add volatile to the individual fields, astyle screws it up:

    struct foo_s {
        volatile unsigned HSION        : 1; // abc
            volatile unsigned PLL3ON       : 1; // def
            volatile unsigned PLL3RDY      : 1; // ghi
            volatile unsigned reserved3    : 2; // jkl
        };
    
     
  • Gregor Rebel

    Gregor Rebel - 2018-02-07

    Just saw that the issue has been fixed in astyle v3.01.
    Thanks a lot! :-)

     
  • André Simon

    André Simon - 2023-05-10
    • status: open --> open-accepted
    • assigned_to: André Simon
     
  • André Simon

    André Simon - 2023-05-15
    • status: open-accepted --> closed-fixed
     
  • André Simon

    André Simon - 2023-05-15

    Fixed in 3.2.1

     

Log in to post a comment.