From 4eb362352d7086efc463b97ca5ea296cbdb8d97c Mon Sep 17 00:00:00 2001 From: Georgy Komarov Date: Tue, 9 Jul 2019 16:53:23 +0300 Subject: [PATCH] Fixup MISRA 5.1 (#1969) --- addons/misra.py | 15 ++++++++------- addons/test/misra-test.c | 5 ++++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index bec397672..29088053e 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -716,9 +716,8 @@ class MisraChecker: if c != '"' and c != '\\': self.reportError(token, 4, 1) - def misra_5_1(self, data): - varnames = [] + long_vars = {} for var in data.variables: if var.nameToken is None: continue @@ -726,11 +725,13 @@ class MisraChecker: continue if not hasExternalLinkage(var): continue - if var.nameToken.str[:31] in varnames: - self.reportError(var.nameToken, 5, 1) - else: - varnames.append(var.nameToken.str[:31]) - + long_vars.setdefault(var.nameToken.str[:31], []).append(var.nameToken) + for name_prefix in long_vars: + tokens = long_vars[name_prefix] + if len(tokens) < 2: + continue + for tok in sorted(tokens, key=lambda t: (t.linenr, t.col))[1:]: + self.reportError(tok, 5, 1) def misra_5_2(self, data): scopeVars = {} diff --git a/addons/test/misra-test.c b/addons/test/misra-test.c index bf12361e0..e885dedb9 100644 --- a/addons/test/misra-test.c +++ b/addons/test/misra-test.c @@ -25,7 +25,10 @@ typedef unsigned long long u64; extern int misra_5_1_extern_var_hides_var_x; extern int misra_5_1_extern_var_hides_var_y; //5.1 int misra_5_1_var_hides_var________a; -int misra_5_1_var_hides_var________b; //5.1 +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 +int misra_5_1_var_hides_var________c; //5.1 +int misra_5_1_var_hides_var________d; //5.1 +int misra_5_1_var_hides_var________e; //5.1 extern const uint8_t misra_5_2_var1; const uint8_t misra_5_2_var1 = 3; // no warning