Search code examples
cmisrapc-lint

Could define variable 'EL_adv' at block scope [MISRA 2012 Rule 8.9, advisory] | pclint 9003 and pclint 9075


I have declared static global variable in my .c file as below. I am observing this issue for few of the static variable not all static variable. For some static variable it is not raising any warnings.

static uint8 EL_adv    = 0;

I got below MISRA warning:

"Could define variable 'EL_adv' at block scope [MISRA 2012 Rule 8.9, advisory] | pclint 9003"

If I remove static then I am getting error as below.

uint8 EL_adv    = 0;

"external symbol 'EL_adv' defined without a prior declaration [MISRA 2012 Rule 8.4, required] | pclint 9075"

I am using in code which looks something as below, I will get value of the variable in fun1 & will use the value in fun2 and fun3.

void EL_ReadAll(void)
{
    EL_adv  = getValue(); 
}

void get_my1_EL_Adv()
{
  my1EL_Adv = EL_adv; 
}

void get_my2_EL_Adv()
{
  my2EL_Adv = EL_adv; 
}

Solution

  • Generally, you get these kind of errors because MISRA-C prefers that file scope variables that are only used by one function should be declared inside that function. This isn't always practical in embedded systems though and the rule is only advisory. Either move the variable declaration inside the single function using it or ignore the advisory rule.

    Simply removing static will do you no good, since that turns the variable "global", which is a much worse MISRA violation.