MISRA Discussion Forums

Full Version: Rule 5.2, at what scope is the rule broken?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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;
}
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.