misra.py: Add rule 4.1

This commit is contained in:
Daniel Marjamäki 2018-03-31 12:17:55 +02:00
parent ecfb4fd26c
commit ed8fda571b
2 changed files with 45 additions and 0 deletions

View File

@ -302,6 +302,11 @@ def findInclude(directives, header):
return directive
return None
def isHexDigit(c):
return (c >= '0' and c <= '9') or (c >= 'a' and c <= 'f') or (c >= 'A' and c >= 'F')
def isOctalDigit(c):
return (c >= '0' and c <= '7')
def misra_3_1(rawTokens):
for token in rawTokens:
@ -309,6 +314,40 @@ def misra_3_1(rawTokens):
if '//' in token.str[2:] or '/*' in token.str[2:]:
reportError(token, 3, 1)
def misra_4_1(rawTokens):
for token in rawTokens:
if token.str[0] != '"':
continue
pos = 1
while pos < len(token.str) - 2:
pos1 = pos
pos = pos + 1
if token.str[pos1] != '\\':
continue
if token.str[pos1+1] == '\\':
pos = pos1 + 2
continue
if token.str[pos1+1] == 'x':
if not isHexDigit(token.str[pos1+2]):
reportError(token, 4, 1)
continue
if not isHexDigit(token.str[pos1+3]):
reportError(token, 4, 1)
continue
elif isOctalDigit(token.str[pos1+1]):
if not isOctalDigit(token.str[pos1+2]):
reportError(token, 4, 1)
continue
if not isOctalDigit(token.str[pos1+2]):
reportError(token, 4, 1)
continue
else:
continue
c = token.str[pos1 + 4]
if c != '"' and c != '\\':
reportError(token, 4, 1)
def misra_5_1(data):
for token in data.tokenlist:
@ -1185,10 +1224,13 @@ for arg in sys.argv[1:]:
if cfgNumber == 1:
misra_3_1(data.rawTokens)
misra_4_1(data.rawTokens)
misra_5_1(cfg)
misra_5_3(cfg)
misra_5_4(cfg)
misra_5_5(cfg)
# 6.1 require updates in Cppcheck (type info for bitfields are lost)
# 6.2 require updates in Cppcheck (type info for bitfields are lost)
if cfgNumber == 1:
misra_7_1(data.rawTokens)
misra_7_3(data.rawTokens)

View File

@ -14,6 +14,9 @@ typedef unsigned long long u64;
//// 3.1
const char *s41_1 = "\x41g"; // 4.1
const char *s41_2 = "\x41\x42";
void misra_5_1() {
int a123456789012345678901234567890; // no-warning
int a1234567890123456789012345678901; // 5.1