MISRA Discussion Forums
Rule 5.2, at what scope is the rule broken? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.5 Identifiers (https://forum.misra.org.uk/forumdisplay.php?fid=32)
+---- Thread: Rule 5.2, at what scope is the rule broken? (/showthread.php?tid=796)



Rule 5.2, at what scope is the rule broken? - joa07 - 06-12-2010

Hello!

Rule 5.2 says that an identifier in a inner scope shall not hide an identifier in an outer scope.
I have two cases where I'm not sure what the rule says.

1. May a function use the name of an identifier that is local for this translation unit, but may be extern global for another translation unit? The first translation unit does not know this identifier at compile-time, so this error should only be detectable at link-time.

2. If the first question was a Yes, does this following code break this rule (it may brake some other Misra rules)?
Code:
int func1(void) {
    int var = 23;
    return var;
}

extern double var;    /* The name is reused here, global scope. Probably breaks rule 8.8 */

double func2(void) {
    return var;
}



Re: Rule 5.2, at what scope is the rule broken? - misra-c - 17-12-2010

Rule 5.2 deals with identifiers declared with block scope that hide identifiers declared at an outer block, or file, scope. It does not apply to identifiers declared in other translation units. The situation described in your point (1) does not break Rule 5.2 but it does break Rule 5.5.

The code example in your point (2) also breaks Rule 5.5 because the identifier var is declared with static storage duration and is also declared at block scope. It may also break Rule 8.8 if there are other declarations of var with static storage duration in other translation units.

If your example had another function at the end, for example:
Code:
int func3 ( void)
{
int var = 25;
return var;
}
then the block-scope identifier var does hide the earlier file-scope declaration. This would break Rule 5.2.