-1

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; 
}
kapilddit
  • 1,729
  • 4
  • 26
  • 51
  • 1
    For `pclint 9003` you need to show where it is used. It's telling you that it doesn't need to be a global variable. Read the [documentation](http://www.gimpel-online.com/MsgRef.html). It tells you clearly what each warning means and what the suggested fix is. – kaylum Jan 07 '21 at 10:44
  • @kaylum: I added how I am using it. – kapilddit Jan 07 '21 at 10:54
  • 2
    *looks something*. Is that really how your code looks like? The lint warning implies `EL_adv` is only used in one function. Are you claiming it is actually used directly in more than one function? Please provide real code that can reproduce the problem as a [minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). – kaylum Jan 07 '21 at 11:00
  • @kaylum: let me check. I have seen it is being used in one function only for few variables not two. – kapilddit Jan 07 '21 at 11:02
  • Please post the actual code then, otherwise it looks like another Lint bug - which is what we come to expect from Lint... Though if you say it is only used once, Lint it is apparently innocent for once and actually reported the correct error. – Lundin Jan 07 '21 at 11:09
  • @Lundin I verified, I am getting the value of variable using external interface in fun1, and using the received value (which I got in fun1) in two function: fun2 & fun3. fun2 willl be called after Init & fun3 is cyclic function. – kapilddit Jan 07 '21 at 11:20
  • 1
    Ok then it _is_ yet another Lint bug. It's a pretty hopeless tool, but it's far from the only broken MISRA checker on the market. – Lundin Jan 07 '21 at 11:21
  • @Lundin I think I have two option, 1. I can make the scope to local if possible or 2. Ignore this advisory warning. Is there any other way to fix ? – kapilddit Jan 07 '21 at 11:23
  • Given that the variable is used in two functions, declaring with `static` is correct, and not a MISRA violation. Suggest annotate as "False positive" and move on. – Andrew Jan 10 '21 at 11:18

1 Answers1

1

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • However, in the example cited, `EL_adv` is shared across three functions, so a more local declaration would be incorrect! – Andrew Jan 10 '21 at 11:19