Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 8.4: Prototype for function with internal linkage required?
#1
Does MISRA-C 2012 mandate prototypes for functions with internal linkage?

While MISRA-C 2012 rule 8.4 is very explicit about prototypes for functions with external linkage, we have not found a corresponding rule for internally linked functions.

Our static analysis tool takes the simplistic approach that prototypes are required in any case due to MISRA-C 2004 8.1 rule. I like to challenge its reasoning.

Thanks.
#2
MISRA C:2012 requires both function declarations and definitions with internal linkage to be in prototype form (rule 8.2).
However MISRA C:2012 does not require there to be a separate declaration in addition to the definition for functions with internal linkage. For example:
Code:
static void fn ( int32_t i32 );  /* Declaration required by MISRA-C:2004 rule 8.1,
                                    but not in MISRA C:2012.
                                    If the declaration is present, then it must
                                    be in prototype form.
                                  */

static void fn ( int32_t i32 )   /* Definition required to be in prototype form by
                                    MISRA-C:2004 rule 8.1 and MISRA C:2012 rule 8.2
                                  */
{
  ....
}
It should be noted that the MISRA C:2012 guidelines use the term "prototype" as defined in the C standard. The term refers to the declaration of a function that declares the types of its parameters (C99 6.2.1(2)) and applies equally to (non-defining) declarations and to definitions.

In summary:
  • The interface to a function must always be declared in prototype form. (MISRA C:2004 Rule 8.1, MISRA C:2012 Rule 8.2)
  • The interface to a function must never be defined "implicitly", i.e. by calling the function in the absence of a previous declaration or definition. (MISRA C:2004 Rule 8.1, MISRA C:2012 Rule 17.3)
  • For a function has external linkage, the interface to that function must be declared consistently across all translation units. (MISRA C:2004 Rules 8.1 & 8.8, MISRA C:2012 Rules 8.4 & 8.5)
  • For a function with internal linkage,
    • MISRA C:2004 Rule 8.1 requires a (non-defining) declaration in advance of the function definition or any call to the function.
    • MISRA C:2012 does not require a (non-defining) declaration in advance of the function definition.
    • MISRA C:2012 Rule 17.3 requires either a (non-defining) declaration or the function definition in advance of any call to the function.
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 3 Guest(s)