Skip to content

Commit 6686f68

Browse files
Checks: Added new MISRA04_19.16: Preprocessing directives shall be syntactically meaningful even when excluded by the preprocessor [autosync]
1 parent adbb0f8 commit 6686f68

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

  • CodeCheck/All Checks/Language Specific/C and C++/Preprocessor/CPP_PR036

CodeCheck/All Checks/Language Specific/C and C++/Preprocessor/CPP_PR036/check.upy

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ otherDirectives = {'elifdef', 'elifndef', 'import', 'warning'}
1515

1616

1717
def ids():
18-
return ('MISRA08_16-0-8', 'MISRA12_20.13', 'M16-0-8', 'CPP_PR036', 'MISRA23_19.0.1', 'MISRA23_20.13', 'MISRA25_20.13')
18+
return ('MISRA08_16-0-8', 'MISRA12_20.13', 'M16-0-8', 'CPP_PR036', 'MISRA23_19.0.1', 'MISRA23_20.13', 'MISRA25_20.13', 'MISRA04_19.16')
1919

2020

2121
def name(id):
@@ -34,6 +34,8 @@ Invalid Preprocessor Directives''',
3434
20.13 A line whose first token is # shall be a valid preprocessing directive''',
3535
'MISRA25_20.13': '''Published Standards/MISRA C 2025/\
3636
20.13 A line whose first token is # shall be a valid preprocessing directive''',
37+
'MISRA04_19.16': '''Published Standards/MISRA-C 2004/\
38+
19.16 Preprocessing directives shall be syntactically meaningful even when excluded by the preprocessor''',
3739
}[id]
3840

3941

@@ -84,6 +86,12 @@ def tags(id):
8486
'Category: Required',
8587
'Preprocessor',
8688
],
89+
'MISRA04_19.16': [
90+
'Language: C',
91+
'Standard: MISRA C 2004',
92+
'Category: Required',
93+
'Preprocessor',
94+
],
8795
}.get(id)
8896

8997

@@ -425,6 +433,45 @@ appearing in a comment is not a token:</p>
425433
#start is not a token in a comment
426434
*/
427435
</code></pre>
436+
''',
437+
'MISRA04_19.16': '''\
438+
<p><b>Rule</b></p>
439+
440+
<p>Preprocessing directives shall be syntactically meaningful even when excluded
441+
by the preprocessor.</p>
442+
443+
<p><b>Rationale</b></p>
444+
445+
<p>When a section of source code is excluded by preprocessor directives, the
446+
content of each excluded statement is ignored until a <i>#else</i>, <i>#elif</i>
447+
or <i>#endif</i> directive is encountered (depending on the context). If one of
448+
these excluded directives is badly formed, it may be ignored without warning by a
449+
compiler with unfortunate consequences.</p>
450+
451+
<p>The requirement of this rule is that all preprocessor directives shall be
452+
syntactically valid even when they occur within an excluded block of code.</p>
453+
454+
<p>In particular, ensure that <i>#else</i> and <i>#endif</i> directives are not
455+
followed by any characters other than white-space. Compilers are not always
456+
consistent in enforcing this ISO requirement.</p>
457+
458+
<p><b>Example</b></p>
459+
460+
<pre><code language="C">#define AAA 2
461+
...
462+
int foo(void)
463+
{
464+
int x = 0;
465+
...
466+
#ifndef AAA
467+
x = 1;
468+
#else1 /* Non-compliant */
469+
x = AAA;
470+
#endif
471+
...
472+
return x;
473+
}
474+
</code></pre>
428475
''',
429476
}[id]
430477

0 commit comments

Comments
 (0)