Dir 4.8 if struct is only visible through a function declaration

Moderators: misra-c, david ward

Post Reply
LMende
Posts: 1
Joined: Fri Jan 15, 2016 7:30 am
Company: Intenta GmbH

Dir 4.8 if struct is only visible through a function declaration

Post by LMende » Wed Aug 29, 2018 1:22 pm

Hi there,
I humbly ask for help with compliance to Dir 4.8.
(Borrowing from cxlin, thx)

Code: Select all

/* def.h */
struct SomeType
{
/* Object implementation */
}

/* other.h */
extern void usetype(someType* p);

/* no-use.c */
#include def.h
#include other.h
/* Do something. But no use of SomeType at all. */
Given a structure SomeType which implementation is visible to no-use.c.
A pointer to SomeType is only visible through the included other.h where the usetype function is declared.
Is the implementation of no-use.c compliant with Dir 4.8 or not?

dg1980
Posts: 109
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: Dir 4.8 if struct is only visible through a function declaration

Post by dg1980 » Thu Aug 30, 2018 8:48 am

First let me quote MISRA C 2012 chapter 6.1:
A directive is a guideline for which it is not possible to provide the full description necessary to perform a check for compliance
Also, it is classified advisory, so it would not require a formal deviation.

The answer to your question depends on whether other translation units in your project include def.h and use struct SomeType directly (creating a variable, accessing members etc.).
In that case, the opaque technique described in DIR 4.8 does not apply.

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

Re: Dir 4.8 if struct is only visible through a function declaration

Post by misra-c » Tue Oct 16, 2018 11:41 am

The following discussion assumes that the prototype for usetype is

Code: Select all

    extern void usetype(struct SomeType* p);
The translation unit for no-use.c never accesses the members of "struct SomeType" and therefore the example is not compliant with directive 4.8. To be compliant with this rule another header file should be created.

Code: Select all

/* opaque_def.h */
typedef struct SomeType *pSomeType;

/* other.h */
extern void usetype(pSomeType p);

/* no-use.c */
#include opaque_def.h
#include other.h
/* Do something. But no use of SomeType at all. */
opaque_def.h would be used in those translation units which did not dereference the pointer "struct SomeType *" whereas def.h would be used in those translation units which did dereference the pointer.
Whether a particular translation unit violates directive 4.8 does not depend on how "struct SomeType" is used in other translation units.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “7.4 Code design”