Skip to content

Commit 66880df

Browse files
authored
Fix #13647: Misra C 21.6: Location should point at function call instead of #include (danmar#7355)
1 parent 7cdf9f9 commit 66880df

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

addons/misra.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -478,20 +478,20 @@ def rawlink(rawtoken):
478478
'wctype.h': C99_STDLIB_IDENTIFIERS['wctype.h'],
479479
}
480480

481-
def isStdLibId(id_, standard='c99'):
482-
id_lists = []
481+
def getStdLib(standard):
483482
if standard == 'c89':
484-
id_lists = C90_STDLIB_IDENTIFIERS.values()
485-
elif standard == 'c99':
486-
id_lists = C99_STDLIB_IDENTIFIERS.values()
487-
else:
488-
id_lists = C11_STDLIB_IDENTIFIERS.values()
483+
return C90_STDLIB_IDENTIFIERS
484+
if standard == 'c99':
485+
return C99_STDLIB_IDENTIFIERS
486+
return C11_STDLIB_IDENTIFIERS
487+
488+
def isStdLibId(id_, standard='c99'):
489+
id_lists = getStdLib(standard).values()
489490
for l in id_lists:
490491
if id_ in l:
491492
return True
492493
return False
493494

494-
495495
# Reserved keywords defined in ISO/IEC9899:1990 -- ch 6.1.1
496496
C90_KEYWORDS = {
497497
'auto', 'break', 'case', 'char', 'const', 'continue', 'default', 'do',
@@ -3964,12 +3964,13 @@ def misra_21_5(self, data):
39643964
self.reportError(directive, 21, 5)
39653965

39663966
def misra_21_6(self, data):
3967-
dir_stdio = findInclude(data.directives, '<stdio.h>')
3968-
dir_wchar = findInclude(data.directives, '<wchar.h>')
3969-
if dir_stdio:
3970-
self.reportError(dir_stdio, 21, 6)
3971-
if dir_wchar:
3972-
self.reportError(dir_wchar, 21, 6)
3967+
for token in data.tokenlist:
3968+
if not isFunctionCall(token) or token.previous.function:
3969+
continue
3970+
standard_id = getStdLib(data.standards.c)
3971+
funcname = token.previous.str
3972+
if funcname in standard_id.get("stdio.h", []) or funcname in standard_id.get("wchar.h", []):
3973+
self.reportError(token, 21, 6)
39733974

39743975
def misra_21_7(self, data):
39753976
for token in data.tokenlist:

addons/test/misra/misra-test.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
#include <setjmp.h> // 21.4
3939
#include <signal.h> // 21.5
40-
#include <stdio.h> //21.6
41-
#include <wchar.h> //21.6
40+
#include <stdio.h>
41+
#include <wchar.h>
4242
#include <time.h> // 21.10
4343
#include <tgmath.h> // 21.11
4444
#include <fenv.h>
@@ -134,7 +134,7 @@ static void misra_3_2(int enable)
134134
++y; // This is hidden if trigraph replacement is active
135135
}
136136

137-
(void)printf("x=%i, y=%i\n", x, y);
137+
(void)printf("x=%i, y=%i\n", x, y); //21.6
138138
}
139139

140140
extern int misra_5_1_extern_var_hides_var_x;
@@ -209,9 +209,9 @@ int c41_15 = 'a'; // 10.3 8.4
209209

210210
static void misra_4_1(void)
211211
{
212-
(void)printf("\x41g"); // 4.1
213-
(void)printf("\x41\x42");
214-
(void)printf("\x41" "g");
212+
(void)printf("\x41g"); // 4.1 21.6
213+
(void)printf("\x41\x42"); //21.6
214+
(void)printf("\x41" "g"); //21.6
215215
}
216216

217217
const char *s42_1 = "String containing trigraphs ??-??-??"; // 4.2 8.4
@@ -220,8 +220,8 @@ const char *s42_3 = "No trigraph?(?'?)"; // 8.4
220220

221221
static void misra_4_2(void)
222222
{
223-
(void)printf("??=Trigraph\n"); // 4.2
224-
(void)printf("No?/Trigraph\n");
223+
(void)printf("??=Trigraph\n"); // 4.2 21.6
224+
(void)printf("No?/Trigraph\n"); //21.6
225225
}
226226

227227
#define misra_5_4_macro_hides_macro__31x 1
@@ -965,7 +965,7 @@ void misra_12_3(int a, int b, int c) {
965965
int a41 = MISRA_12_3_FN3_2(a34, a35), a42; // 12.3
966966
int a43, a44 = MISRA_12_3_FN3_2(a34, a35); // 12.3
967967

968-
MISRA_12_3_FN3_2_MSG(fprintf(stderr, "test\n")); // 12.3
968+
MISRA_12_3_FN3_2_MSG(fprintf(stderr, "test\n")); // 12.3 21.6
969969

970970
f((1,2),3); // TODO
971971

test/cli/helloworld_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_addon_local_path():
6868
ret, stdout, stderr = cppcheck(args, cwd=__proj_dir)
6969
assert ret == 0, stdout
7070
assert stderr == ('[main.c:5]: (error) Division by zero.\n'
71-
'[main.c:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n')
71+
'[main.c:4]: (style) misra violation (use --rule-texts=<file> to get proper output)\n')
7272

7373
def test_addon_local_path_not_enable():
7474
args = [
@@ -91,7 +91,7 @@ def test_addon_absolute_path():
9191
filename = os.path.join(__proj_dir, 'main.c')
9292
assert ret == 0, stdout
9393
assert stderr == ('[%s:5]: (error) Division by zero.\n'
94-
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
94+
'[%s:4]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
9595

9696
def test_addon_relative_path():
9797
args = [
@@ -106,7 +106,7 @@ def test_addon_relative_path():
106106
assert stdout == ('Checking %s ...\n'
107107
'Checking %s: SOME_CONFIG...\n' % (filename, filename))
108108
assert stderr == ('[%s:5]: (error) Division by zero.\n'
109-
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
109+
'[%s:4]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
110110

111111
def test_addon_with_gui_project():
112112
project_file = os.path.join('helloworld', 'test.cppcheck')
@@ -123,7 +123,7 @@ def test_addon_with_gui_project():
123123
assert ret == 0, stdout
124124
assert stdout == 'Checking %s ...\n' % filename
125125
assert stderr == ('[%s:5]: (error) Division by zero.\n'
126-
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
126+
'[%s:4]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
127127

128128
def test_basepath_relative_path():
129129
args = [

0 commit comments

Comments
 (0)