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 <unconfigured@null.spigotmc.org>
This commit is contained in:
apuly 2023-04-11 16:13:10 +02:00 committed by GitHub
parent 12118d8d67
commit 30ff1aad9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -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:
starts_with_block_comment = token.str.startswith("/*")
s = token.str.lstrip('/')
if ((not starts_with_double_slash) and '//' in s) or '/*' in s:
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:

View File

@ -82,6 +82,7 @@ static void misra_2_2(int x) {
/* // */ // 3.1
/* /* */ // 3.1
////
/* https://cppcheck.net */
// http://example.com // no warning