From 30ff1aad9ad7536d4e0dc31d20f4a5d660c140e6 Mon Sep 17 00:00:00 2001 From: apuly Date: Tue, 11 Apr 2023 16:13:10 +0200 Subject: [PATCH] misra: fix misra-3_1 false positive for URIs in block comments (#4939) * misra: fix misra-3_1 false positive for URIs in block comments * added unit test, improved new misra 3.1 based on false positives --------- Co-authored-by: Paul B --- addons/misra.py | 17 +++++++++++++---- addons/test/misra/misra-test.c | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index 2b4e064a5..69ac6df47 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -1543,10 +1543,19 @@ class MisraChecker: def misra_3_1(self, rawTokens): for token in rawTokens: starts_with_double_slash = token.str.startswith('//') - if token.str.startswith('/*') or starts_with_double_slash: - s = token.str.lstrip('/') - if ((not starts_with_double_slash) and '//' in s) or '/*' in s: - self.reportError(token, 3, 1) + starts_with_block_comment = token.str.startswith("/*") + s = token.str.lstrip('/') + if (starts_with_double_slash or starts_with_block_comment) and "/*" in s: + # Block comment inside of regular comment, violation + self.reportError(token, 3, 1) + elif starts_with_block_comment and "//" in s: + # "//" in block comment, check if it's a uri + while "//" in s: + possible_uri, s = s.split("//", 1) + if not re.search(r"\w+:$", possible_uri): + # Violation if no uri was found + self.reportError(token, 3, 1) + break def misra_3_2(self, rawTokens): for token in rawTokens: diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index a438b78d0..cbf3de69d 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -82,6 +82,7 @@ static void misra_2_2(int x) { /* // */ // 3.1 /* /* */ // 3.1 //// +/* https://cppcheck.net */ // http://example.com // no warning