Rule 2.4 and forward declarations

Moderators: misra-c, david ward

Post Reply
michael.metivier
Posts: 10
Joined: Thu Mar 21, 2013 7:30 pm
Company: Gentex, Corp.

Rule 2.4 and forward declarations

Post by michael.metivier » Tue Apr 21, 2015 11:08 pm

Would the use of tags for forward declarations be considered a failure of this rule, if used such as

Code: Select all

struct test;
typedef struct test test_T;

typedef bool (*Callback_T)(test_T const * p_test);

struct test
{
    uint32_t var;
    Callback_T callback;
};
For consistency in type usage, I would prefer to utilize the typedef whenever referring to this structure, including in the callback definition, but this leads our analysis tools to flag that "test" is only used in the typedef.

misra-c
Posts: 548
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 2.4 and forward declarations

Post by misra-c » Mon May 11, 2015 8:54 am

The use of tag in a typedef is not a violation of rule 2.4, unless the only occurrence of the tag appears in the typedef declaration.

Code: Select all

struct A { ... };                 // compliant 
typedef struct A Atype;  
Atype ax;                                  
     // no subsequent use of identifier "A"  

typedef struct B { ... } Btype;  // non-compliant
Btype bx;                              
    // no subsequent use of identifier "B"

typedef struct { ... } Ctype;   // compliant
Ctype cx;
---
Posted by and on behalf of
the MISRA C Working Group

michael.metivier
Posts: 10
Joined: Thu Mar 21, 2013 7:30 pm
Company: Gentex, Corp.

Re: Rule 2.4 and forward declarations

Post by michael.metivier » Mon May 11, 2015 2:11 pm

unless the only occurrence of the tag appears in the typedef declaration.
That is the primary concern. There are no other uses of "test"; all other references to the structure are through the typedef, test_T.

The circular dependency between Callback_T and the structure test complicates these definitions. The example given allows all references to the structure test to be done through the typedef test_T, but is flagged as a violation because test is not used beyond the snippet given.

Post Reply

Return to “8.2 Unused code”