Menu

#342 Using __attribute__ while defining a struct inside namespace breaks formatting

open-accepted
bug (5)
2023-06-29
2015-03-13
LKostyra
No

This is a very specific case. Consider the following piece of test C++ code I wrote:

namespace test {

struct __attribute__((aligned(16))) TestClass
{
    int test;

    TestClass() : test(0) {};
    TestClass(int i) : test(i) {};

    void AddFour();
};

void TestClass::AddFour()
{
    test += 4;
}

} // namespace test

Running astyle on a file containing such code breaks structure formatting - everything below int test; has its indentations removed. Used options:

style=allman
max-code-length=100
attach-namespaces
indent=spaces=4
convert-tabs
indent-switches
pad-oper
pad-header
align-pointer=type
align-reference=type
break-after-logical
indent-preproc-define

As I explored the issue, there are multiple conditions that have to be met in order to reproduce the bug:
Defining a structure (when class is defined instead of a struct, everything works as it should)
Adding memory alignment attribute (GCC requires it to be put in between keyword struct and structure name, adding it there produces the bug. I don't know if this shows up with different attributes/options added between struct and its name)
Wrapping TestClass in a namespace
TestClass structure must have some method definitions inside it (like in this example, constructors are one-line definitions. Moving them outside struct definition fixes the issue, however it requires to produce additional code, which is sometimes just not needed)

Thanks for the great job with astyle! I'm looking forward to hearing from you.

Discussion

  • LKostyra

    LKostyra - 2015-03-13

    And sorry for the horrible reproduction list format! Here is the list in correct formatting:

    • Defining a structure (when class is defined instead of a struct, everything works as it should)
    • Adding memory alignment attribute (GCC requires it to be put in between keyword struct and structure name, adding it there produces the bug. I don't know if this shows up with different attributes/options added between struct and its name)
    • Wrapping TestClass in a namespace
    • TestClass structure must have some method definitions inside it (like in this example, constructors are one-line definitions. Moving them outside struct definition fixes the issue, however it requires to produce additional code, which is sometimes just not needed)
     
  • Tony Kirker

    Tony Kirker - 2019-07-31

    similar to the initially reported issue:

    typedef struct
    {
        uint8_t     member1;
        float       member2;
        uint8_t     member3;
        uint32_t    member4;
        uint16_t    member5;
        uint8_t     member6;
    } mystruct_t;
    
    typedef struct __attribute__ ((__packed__))
    {
        uint8_t     member1;
        float       member2;
        uint8_t     member3;
        uint32_t    member4;
        uint16_t    member5;
        uint8_t     member6;
    } mypackedstruct_t;
    

    results in:

    typedef struct
    {
        uint8_t     member1;
        float       member2;
        uint8_t     member3;
        uint32_t    member4;
        uint16_t    member5;
        uint8_t     member6;
    } mystruct_t;
    
    typedef struct __attribute__ ((__packed__))
    {
        uint8_t     member1;
        float       member2;
        uint8_t     member3;
        uint32_t    member4;
        uint16_t    member5;
        uint8_t     member6;
    }
    mypackedstruct_t;
    

    ...which in turn upsets the static code analyser

    Seems like the issue is caused by the parentheses causing the struct declaration to be interpretted as a function declaration

     
  • André Simon

    André Simon - 2023-06-29
    • status: open --> open-accepted
    • assigned_to: André Simon
     

Log in to post a comment.