Fix and impove MISRA 6.1 and 6.2 rules (#2863)

This commit is contained in:
Georgy Komarov 2020-10-28 22:54:43 +03:00 committed by GitHub
parent 417bc5c732
commit b87aaaac52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 20 deletions

View File

@ -1023,7 +1023,7 @@ class MisraSettings(object):
class MisraChecker:
def __init__(self, settings, stdversion="c90"):
def __init__(self, settings, stdversion="c89"):
"""
:param settings: misra.py script settings.
"""
@ -1293,21 +1293,24 @@ class MisraChecker:
def misra_6_1(self, data):
# Bitfield type must be bool or explicity signed/unsigned int
for token in data.tokenlist:
if token.valueType == None:
for token in data.tokenlist:
if not token.valueType:
continue
if token.valueType.bits == 0:
continue
if token.valueType.type == 'bool':
if not token.variable:
continue
if not token.scope:
continue
if token.scope.type not in 'Struct':
continue
if token.valueType.type != 'int':
self.reportError(token, 6, 1)
if token.variable == None or token.variable.typeStartToken == None or token.variable.typeEndToken == None:
continue
if data.standards.c == 'c89':
if token.valueType.type != 'int':
self.reportError(token, 6, 1)
elif data.standards.c == 'c99':
if token.valueType.type == 'bool':
continue
isExplicitlySignedOrUnsigned = False
typeToken = token.variable.typeStartToken
@ -1318,7 +1321,7 @@ class MisraChecker:
if typeToken.Id == token.variable.typeEndToken.Id:
break
typeToken = typeToken.next
if not isExplicitlySignedOrUnsigned:
@ -1327,10 +1330,13 @@ class MisraChecker:
def misra_6_2(self, data):
# Bitfields of size 1 can not be signed
for token in data.tokenlist:
if token.valueType == None:
for token in data.tokenlist:
if not token.valueType:
continue
if not token.scope:
continue
if token.scope.type not in 'Struct':
continue
if token.valueType.bits == 1 and token.valueType.sign == 'signed':
self.reportError(token, 6, 2)

View File

@ -206,18 +206,21 @@ void misra_5_5_func1()
typedef unsigned int UINT_TYPEDEF;
struct struct_with_bitfields
{
unsigned int a:2; // Compliant
unsigned int a:2; // Compliant
signed int b:2; // Compliant
UINT_TYPEDEF c:2; // Compliant
int d:2; // 6.1 - plain int not compliant
bool e:2; // Compliant
int d:2; // 6.1 - plain int not compliant
signed long f:2; // 6.1 - signed long not compliant
unsigned int g:1; // Compliant
signed int h:1; // 6.2 - signed int with size 1 is not compliant
};
void misra6_1_fn() {
// "Use" occurrence should not generate warnings
struct_with_bitfields s;
s.h = 61;
}
void misra_7_1() {
int x = 066; // 7.1
}