Rule 5.2, at what scope is the rule broken?

6.5 Identifiers

Moderators: misra-c, david ward

Post Reply
joa07
Posts: 2
Joined: Fri Jul 04, 2008 2:00 pm
Company: Saab Avitronics

Rule 5.2, at what scope is the rule broken?

Post by joa07 » Mon Dec 06, 2010 2:33 pm

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: Select all

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;
}

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

Re: Rule 5.2, at what scope is the rule broken?

Post by misra-c » Fri Dec 17, 2010 2:11 pm

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: Select all

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.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.5 Identifiers”