16.9 Reference to function pointer or not

6.16 Functions

Moderators: misra-c, david ward

Post Reply
hele
Posts: 1
Joined: Thu May 31, 2012 12:16 pm
Company: University Amberg-Weiden

16.9 Reference to function pointer or not

Post by hele » Tue Jun 05, 2012 8:55 am

Code: Select all

typedef void (*fp)(STATE_MACHINE *sm, EVENT input);
struct STATE_MACHINE_
{
  fp fp_cur_state;
};

void stm_Startup   (STATE_MACHINE *sm,  EVENT input);

Code: Select all

STM_STATE_MACHINE *sm;
sm->fp_cur_state = &stm_Startup;
or

Code: Select all

sm->fp_cur_state = stm_Startup;
??

I have two tools to check code for Misra C.
Checking version without & to the function pointer with Tastking I get the error:
MISRA-C rule 16.9 violation: [R] function calls with no parameters should have empty parentheses
What in fact means the exact rule (I guess):
MISRA-C rule 16.9 violation: [R] A function identifier shall only be used with either a preceding &, or with a parenthesised parameter list, which may be empty.
checking with PC-Lint with & I get
sm->fp_cur_state = &stm_Startup;
stateMachine.c 72 Warning 546: Suspicious use of &
What's right? I guess functionality should be the same in both cases.

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

Re: 16.9 Reference to function pointer or not

Post by misra-c » Mon Jun 18, 2012 6:32 pm

The version without the & operator is certainly a violation of Rule 16.9. The rule requires that any use of an identifier that designates a function (stm_Startup in the example) have a & operator unless the identifier is being used to designate a function in a function call. So, the Tasking tool is correct to diagnose a violation of Rule 16.9.

The version with the & operator is legal C. As described in the C90 standard, Section 6.2.2.1, a function designator is converted to a pointer to function except when it appears as the operand of a sizeof operator or a & operator. Some tools therefore diagnose an & operator applied to an identifier that designates a function because the & operator is redundant in this case. This probably explains why PC-lint issues a diagnostic.

You would need to ask your support contact for PC-lint why it is not diagnosing a violation of Rule 16.9 for the version without the & operator. It might be that the tool needs to be configured differently.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.16 Functions”