MISRA Discussion Forums
Rule 8.8 Does this apply to after pre-processing - 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:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.8 Declarations and defnitions (https://forum.misra.org.uk/forumdisplay.php?fid=163)
+---- Thread: Rule 8.8 Does this apply to after pre-processing (/showthread.php?tid=1146)



Rule 8.8 Does this apply to after pre-processing - misra-c - 13-02-2015

Is the following code compliant with rule 8.9 “An object should not be defined at block scope if its identifier only appears in a single function”?
Code:
#define MACHINE1 0
int32_t x;   /* compliant or not? */

void fn1 ( void )
{
   x = 3;
}

int32_t fn2 ( void )
{
#if ( MACHINE1 )
   return x;
#else
   return 0;
#endif
}



Re: Rule 8.8 Does this apply to after pre-processing - misra-c - 13-02-2015

The MISRA guidelines define "code" in the Glossary as
Quote:Code consists of everything within a translation unit that is not excluded by conditional compilation.
Therefore, the guidelines apply to code after preprocessing directives have been executed, unless otherwise stated in a guideline.

In this example, “x” is excluded by the conditional compilation in “fn2” and therefore only appears in “fn1”. Hence the code is not compliant with rule 8.9.