Skip to content

Commit 3638fd9

Browse files
committed
Fix #13647: Misra C 21.6: Location should point at function call instead of #include
1 parent 7967d5f commit 3638fd9

3 files changed

Lines changed: 24 additions & 20 deletions

File tree

addons/misra.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3964,12 +3964,16 @@ 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 = data.standards.c
3971+
if ((standard == 'c89' and token.previous.str in C90_STDLIB_IDENTIFIERS["stdio.h"]) or
3972+
(standard == 'c99' and (token.previous.str in C99_STDLIB_IDENTIFIERS["stdio.h"] or
3973+
token.previous.str in C99_STDLIB_IDENTIFIERS["wchar.h"])) or
3974+
(token.previous.str in C11_STDLIB_IDENTIFIERS["stdio.h"] or
3975+
token.previous.str in C11_STDLIB_IDENTIFIERS["wchar.h"])):
3976+
self.reportError(token, 21, 6)
39733977

39743978
def misra_21_7(self, data):
39753979
for token in data.tokenlist:

addons/test/misra/misra-test.c

Lines changed: 10 additions & 10 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>
@@ -70,7 +70,7 @@ static _Atomic int misra_1_4_var; // 1.4
7070
static _Noreturn void misra_1_4_func(void) // 1.4
7171
{
7272
if (0 != _Generic(misra_1_4_var)) {} // 1.4 17.3
73-
printf_s("hello"); // 1.4
73+
printf_s("hello"); // 1.4 21.6
7474
}
7575

7676
#define MISRA_2_2 (1*60)
@@ -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)