Rule 8.4 - How are tentative definitions handled - 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.4 - How are tentative definitions handled (/showthread.php?tid=1012) |
Rule 8.4 - How are tentative definitions handled - misra-c - 19-12-2013 Which of the following are compliant with rule 8.4? Code: extern int32_t ext_val0 = 3; Re: Rule 8.4 - How are tentative deinfitions handled - misra-c - 19-12-2013 This will depend on the presence of other definitions as to whether the Tentative definition is treated as a declaration or a definition. Code: extern int32_t ext_val0 = 3; // Non-compliant - definition and no declaration Re: Rule 8.4 - How are tentative definitions handled - gdavis - 09-04-2018 The answer is clear, but I don't understand the reasoning. It would seem better to me if a tentative definition were to be treated as a definition for the sake of this rule. Otherwise, this rule permits something like the following: Code: /* header.h */ Code: /* file1.c */ Code: /* file2.c */ Many of the popular compilers come from a UNIX background where such tentative definitions are emitted as a "common" in the object file, a technique borrowed from Fortran. This extension is typically on by default even in strict language modes. When this happens, the multiple definitions of var that occur in both file1.c and file2.c won't be diagnosed by the user's linker. While this sort of code violates other MISRA rules like 8.5 and 8.6, I don't see anything to be gained by relying on a system rule to catch something that should be caught within a single translation unit. It is true that these popular compilers have a switch to turn off common variables (for example, GCC has -fno-common). But, there's no loss of expressiveness by requiring a declaration that is not a tentative definition. In anything, it also helps to avoid mistakes of the sort: Code: const uint8_t foo; My suggestion would be to add a sentence to the end of the amplification to the effect of: For the purposes of this rule, a tentative definition is considered a definition. This might go well with a rule to prohibit tentative definitions for external symbols. (There is a slight use-case for tentative definitions of static symbols, though). Thanks for your time, and keep up the good work. Best Regards, -Greg Re: Rule 8.4 - How are tentative definitions handled - misra-c - 24-04-2018 Thank you for your comments. We will consider it when we next review this rule. |