Skip to content

Commit b938ee6

Browse files
committed
Fixed #10486 (FP: misra-c2012-8.4)
1 parent 6234e9d commit b938ee6

2 files changed

Lines changed: 71 additions & 54 deletions

File tree

addons/misra.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,10 +1959,24 @@ def misra_8_4(self, cfg):
19591959
if func.tokenDef.str == 'main':
19601960
continue
19611961
self.reportError(func.tokenDef, 8, 4)
1962+
1963+
extern_vars = []
1964+
var_defs = []
1965+
19621966
for var in cfg.variables:
1963-
# extern variable declaration in source file
1964-
if var.isExtern and var.nameToken and not is_header(var.nameToken.file):
1965-
self.reportError(var.nameToken, 8, 4)
1967+
if not var.isGlobal:
1968+
continue
1969+
if var.isStatic:
1970+
continue
1971+
if var.nameToken is None:
1972+
continue
1973+
if var.isExtern:
1974+
extern_vars.append(var.nameToken)
1975+
else:
1976+
var_defs.append(var.nameToken)
1977+
for vartok in var_defs:
1978+
if vartok not in extern_vars:
1979+
self.reportError(vartok, 8, 4)
19661980

19671981
def misra_8_5(self, dumpfile, cfg):
19681982
self._save_ctu_summary_identifiers(dumpfile, cfg)

addons/test/misra/misra-test.c

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ static void misra_3_2(int enable)
115115
(void)printf("x=%i, y=%i\n", x, y);
116116
}
117117

118-
extern int misra_5_1_extern_var_hides_var_x; // 8.4
119-
extern int misra_5_1_extern_var_hides_var_y; //5.1 8.4
120-
int misra_5_1_var_hides_var________a;
121-
int misra_5_1_var_hides_var________b; int misra_5_1_var_hides_var________b1; int misra_5_1_var_hides_var________b2; //5.1
122-
int misra_5_1_var_hides_var________c; //5.1
123-
int misra_5_1_var_hides_var________d; //5.1
124-
int misra_5_1_var_hides_var________e; //5.1
125-
126-
extern const uint8_t misra_5_2_var1; // 8.4
127-
const uint8_t misra_5_2_var1 = 3; // no warning
118+
extern int misra_5_1_extern_var_hides_var_x;
119+
extern int misra_5_1_extern_var_hides_var_y; //5.1
120+
int misra_5_1_var_hides_var________a; // 8.4
121+
int misra_5_1_var_hides_var________b; int misra_5_1_var_hides_var________b1; int misra_5_1_var_hides_var________b2; //5.1 8.4
122+
int misra_5_1_var_hides_var________c; //5.1 8.4
123+
int misra_5_1_var_hides_var________d; //5.1 8.4
124+
int misra_5_1_var_hides_var________e; //5.1 8.4
125+
126+
extern const uint8_t misra_5_2_var1;
127+
const uint8_t misra_5_2_var1 = 3; // 8.4
128128
static int misra_5_2_var_hides_var______31x;
129129
static int misra_5_2_var_hides_var______31y;//5.2
130130
static int misra_5_2_function_hides_var_31x;
@@ -160,30 +160,30 @@ struct misra_5_2_field_hides_field__63y { //5.2
160160
int misra_5_2_field_hides_field1_31x;
161161
int misra_5_2_field_hides_field1_31y;//5.2
162162
};
163-
const char *s41_1 = "\x41g"; // 4.1
164-
const char *s41_2 = "\x41\x42";
165-
const char *s41_3 = "\x41" "\x42";
166-
const char *s41_4 = "\x41" "g";
167-
const char *s41_5 = "\x41\xA";
168-
const char *s41_6 = "\xA\x41";
169-
const char *s41_7 = "\xAA\xg\x41"; // 4.1
170-
const char *s41_8 = "\xAA\x\x41"; // 4.1
171-
const char *s41_9 = "unknown\gsequence";
172-
const char *s41_10 = "simple\nsequence";
173-
const char *s41_11 = "string";
174-
int c41_3 = '\141t'; // 4.1
175-
int c41_4 = '\141\t';
176-
int c41_5 = '\0'; // 10.3
177-
int c41_6 = '\0\t';
178-
int c41_7 = '\12\t';
179-
int c41_8 = '\0t'; // 4.1
180-
int c41_9 = '\12';
181-
int c41_10 = '\12\n';
182-
int c41_11 = '\12n'; // 4.1
183-
int c41_12 = '\12323'; // 4.1
184-
int c41_13 = '\123\3';
163+
const char *s41_1 = "\x41g"; // 4.1 8.4
164+
const char *s41_2 = "\x41\x42"; // 8.4
165+
const char *s41_3 = "\x41" "\x42"; // 8.4
166+
const char *s41_4 = "\x41" "g"; // 8.4
167+
const char *s41_5 = "\x41\xA"; // 8.4
168+
const char *s41_6 = "\xA\x41"; // 8.4
169+
const char *s41_7 = "\xAA\xg\x41"; // 4.1 8.4
170+
const char *s41_8 = "\xAA\x\x41"; // 4.1 8.4
171+
const char *s41_9 = "unknown\gsequence"; // 8.4
172+
const char *s41_10 = "simple\nsequence"; // 8.4
173+
const char *s41_11 = "string"; // 8.4
174+
int c41_3 = '\141t'; // 4.1 8.4
175+
int c41_4 = '\141\t'; // 8.4
176+
int c41_5 = '\0'; // 10.3 8.4
177+
int c41_6 = '\0\t'; // 8.4
178+
int c41_7 = '\12\t'; // 8.4
179+
int c41_8 = '\0t'; // 4.1 8.4
180+
int c41_9 = '\12'; // 8.4
181+
int c41_10 = '\12\n'; // 8.4
182+
int c41_11 = '\12n'; // 4.1 8.4
183+
int c41_12 = '\12323'; // 4.1 8.4
184+
int c41_13 = '\123\3'; // 8.4
185185
// TODO int c41_14 = '\777\777';
186-
int c41_15 = 'a'; // 10.3
186+
int c41_15 = 'a'; // 10.3 8.4
187187

188188
static void misra_4_1(void)
189189
{
@@ -192,9 +192,9 @@ static void misra_4_1(void)
192192
(void)printf("\x41" "g");
193193
}
194194

195-
const char *s42_1 = "String containing trigraphs ??-??-??"; // 4.2
196-
const char *s42_2 = "String containing trigraph???=preceded by question mark"; // 4.2
197-
const char *s42_3 = "No trigraph?(?'?)";
195+
const char *s42_1 = "String containing trigraphs ??-??-??"; // 4.2 8.4
196+
const char *s42_2 = "String containing trigraph???=preceded by question mark"; // 4.2 8.4
197+
const char *s42_3 = "No trigraph?(?'?)"; // 8.4
198198

199199
static void misra_4_2(void)
200200
{
@@ -219,7 +219,7 @@ static void misra_4_2(void)
219219
#define misra_5_5_tag_hides_macro____31x 1
220220
#define misra_5_5_hides_macro________31x 1
221221

222-
int misra_5_5_var_hides_macro____31y; //5.5
222+
int misra_5_5_var_hides_macro____31y; //5.5 8.4
223223
static void misra_5_5_functionhides_macro31y(int misra_5_5_param_hides_macro__31y){(void)misra_5_5_param_hides_macro__31y;} //5.5
224224
struct misra_5_5_tag_hides_macro____31y { //5.5
225225
int x;
@@ -336,7 +336,7 @@ static void misra_7_4(void)
336336
misra_7_4_call(1, "text_call"); // 7.4 11.8
337337
}
338338

339-
const misra_8_1_a; // 8.1
339+
const misra_8_1_a; // 8.1 8.4
340340

341341
static int misra_8_2_a (int n, ...);
342342
extern int misra_8_2_b (int n);
@@ -362,9 +362,9 @@ static void misra_8_2_m(uint8_t * const x)
362362
{
363363
(void)x;
364364
}
365-
int16_t ( *misra_8_2_p_a ) (); // 8.2
366-
int16_t ( *misra_8_2_p_b ) (void);
367-
int16_t ( *misra_8_2_p_c ) (int);
365+
int16_t ( *misra_8_2_p_a ) (); // 8.2 8.4
366+
int16_t ( *misra_8_2_p_b ) (void); // 8.4
367+
int16_t ( *misra_8_2_p_c ) (int); // 8.4
368368
static int misra_8_2_n(int a)
369369
{ return a + 42; }
370370
static int misra_8_2_o(
@@ -381,17 +381,20 @@ static int misra_8_2_q
381381

382382
void misra_8_4_foo(void) {} // 8.4
383383
static void misra_8_4_bar(void) {} // Declared in header
384-
extern int16_t misra_8_4_count; // 8.4
384+
extern int16_t misra_8_4_count; // no-warning
385+
int16_t misra_8_4_count = 0; // 8.4
386+
extern uint8_t misra_8_4_buf1[13]; // no-warning
387+
uint8_t misra_8_4_buf2[24]; // 8.4
385388

386389
static int32_t misra_8_8 = 123;
387-
extern int32_t misra_8_8; // 8.8 8.4
390+
extern int32_t misra_8_8; // 8.8
388391

389392
static int32_t misra_8_9_i; // 8.9
390393
static int32_t misra_8_9_foo(void) { return misra_8_9_i++; }
391394

392395
inline int32_t misra_8_10_value(void) { return 123; } // 8.10 8.4
393396

394-
extern int a811[]; // 8.11 8.4
397+
extern int a811[]; // 8.11
395398

396399
enum misra_8_12_a { misra_a1 = 1, misra_a2 = 2, misra_a3, misra_a4 = 3 }; //8.12
397400
enum misra_8_12_b { misra_b1, misra_b2, misra_b3 = 3, misra_b4 = 3 }; // no-warning
@@ -730,8 +733,8 @@ static void misra_10_8(u8 x, s32 a, s32 b) {
730733
y = (u16) (a + b) //10.8
731734
}
732735

733-
int (*misra_11_1_p)(void);
734-
void *misra_11_1_bad1 = (void*)misra_11_1_p; // 11.1
736+
int (*misra_11_1_p)(void); // 8.4
737+
void *misra_11_1_bad1 = (void*)misra_11_1_p; // 11.1 8.4
735738

736739
struct misra_11_2_s;
737740
struct misra_11_2_t;
@@ -907,7 +910,7 @@ static void misra_12_4(uint8_t t) {
907910
}
908911

909912
struct misra_13_1_t { int a; int b; };
910-
uint8_t misra_13_1_x = 0;
913+
uint8_t misra_13_1_x = 0; // 8.4
911914
static void misra_13_1_bar(uint8_t a[2]);
912915
static void misra_13_1(int *p) {
913916
volatile int v;
@@ -1229,7 +1232,7 @@ static void misra_14_2_fn2(void)
12291232
struct {
12301233
unsigned int x:1;
12311234
unsigned int y:1;
1232-
} r14_4_struct;
1235+
} r14_4_struct; // 8.4
12331236
static void misra_14_4(bool b) {
12341237
if (x+4){} // 14.4
12351238
else {}
@@ -1653,12 +1656,12 @@ struct {
16531656
} nested_2;
16541657
uint8_t data_3[]; // 18.7
16551658
} nested_3;
1656-
} r18_7_struct;
1659+
} r18_7_struct; // 8.4
16571660
struct {
16581661
uint16_t len;
16591662
uint8_t data_1[ 19 ];
16601663
uint8_t data_2[ ]; // 18.7
1661-
} r18_7_struct;
1664+
} r18_7_struct; // 8.4
16621665

16631666
typedef enum {
16641667
R18_8_ENUM_CONSTANT_0,
@@ -1700,7 +1703,7 @@ union misra_19_2 { }; // 19.2
17001703
#define M_20_7_17(STRING1, STRING2, STRING3) (STRING1 + STRING2 " " STRING3) // 20.7
17011704

17021705
// Compliant: M is a structure member name, not an expression
1703-
struct { int a; } struct_20_7_s;
1706+
struct { int a; } struct_20_7_s; // 8.4
17041707
#define M_20_7_6(M) struct_20_7.M
17051708
#define M_20_7_7(M) (struct_20_7).M
17061709

0 commit comments

Comments
 (0)