Fix and impove MISRA 6.1 and 6.2 rules (#2863)
This commit is contained in:
parent
417bc5c732
commit
b87aaaac52
|
@ -1023,7 +1023,7 @@ class MisraSettings(object):
|
||||||
|
|
||||||
class MisraChecker:
|
class MisraChecker:
|
||||||
|
|
||||||
def __init__(self, settings, stdversion="c90"):
|
def __init__(self, settings, stdversion="c89"):
|
||||||
"""
|
"""
|
||||||
:param settings: misra.py script settings.
|
:param settings: misra.py script settings.
|
||||||
"""
|
"""
|
||||||
|
@ -1293,21 +1293,24 @@ class MisraChecker:
|
||||||
|
|
||||||
def misra_6_1(self, data):
|
def misra_6_1(self, data):
|
||||||
# Bitfield type must be bool or explicity signed/unsigned int
|
# Bitfield type must be bool or explicity signed/unsigned int
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if token.valueType == None:
|
if not token.valueType:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if token.valueType.bits == 0:
|
if token.valueType.bits == 0:
|
||||||
continue
|
continue
|
||||||
|
if not token.variable:
|
||||||
if token.valueType.type == 'bool':
|
continue
|
||||||
|
if not token.scope:
|
||||||
|
continue
|
||||||
|
if token.scope.type not in 'Struct':
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if token.valueType.type != 'int':
|
if data.standards.c == 'c89':
|
||||||
self.reportError(token, 6, 1)
|
if token.valueType.type != 'int':
|
||||||
|
self.reportError(token, 6, 1)
|
||||||
if token.variable == None or token.variable.typeStartToken == None or token.variable.typeEndToken == None:
|
elif data.standards.c == 'c99':
|
||||||
continue
|
if token.valueType.type == 'bool':
|
||||||
|
continue
|
||||||
|
|
||||||
isExplicitlySignedOrUnsigned = False
|
isExplicitlySignedOrUnsigned = False
|
||||||
typeToken = token.variable.typeStartToken
|
typeToken = token.variable.typeStartToken
|
||||||
|
@ -1318,7 +1321,7 @@ class MisraChecker:
|
||||||
|
|
||||||
if typeToken.Id == token.variable.typeEndToken.Id:
|
if typeToken.Id == token.variable.typeEndToken.Id:
|
||||||
break
|
break
|
||||||
|
|
||||||
typeToken = typeToken.next
|
typeToken = typeToken.next
|
||||||
|
|
||||||
if not isExplicitlySignedOrUnsigned:
|
if not isExplicitlySignedOrUnsigned:
|
||||||
|
@ -1327,10 +1330,13 @@ class MisraChecker:
|
||||||
|
|
||||||
def misra_6_2(self, data):
|
def misra_6_2(self, data):
|
||||||
# Bitfields of size 1 can not be signed
|
# Bitfields of size 1 can not be signed
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if token.valueType == None:
|
if not token.valueType:
|
||||||
|
continue
|
||||||
|
if not token.scope:
|
||||||
|
continue
|
||||||
|
if token.scope.type not in 'Struct':
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if token.valueType.bits == 1 and token.valueType.sign == 'signed':
|
if token.valueType.bits == 1 and token.valueType.sign == 'signed':
|
||||||
self.reportError(token, 6, 2)
|
self.reportError(token, 6, 2)
|
||||||
|
|
||||||
|
|
|
@ -206,18 +206,21 @@ void misra_5_5_func1()
|
||||||
typedef unsigned int UINT_TYPEDEF;
|
typedef unsigned int UINT_TYPEDEF;
|
||||||
struct struct_with_bitfields
|
struct struct_with_bitfields
|
||||||
{
|
{
|
||||||
unsigned int a:2; // Compliant
|
unsigned int a:2; // Compliant
|
||||||
signed int b:2; // Compliant
|
signed int b:2; // Compliant
|
||||||
UINT_TYPEDEF c:2; // Compliant
|
UINT_TYPEDEF c:2; // Compliant
|
||||||
int d:2; // 6.1 - plain int not compliant
|
int d:2; // 6.1 - plain int not compliant
|
||||||
|
|
||||||
bool e:2; // Compliant
|
|
||||||
signed long f:2; // 6.1 - signed long not compliant
|
signed long f:2; // 6.1 - signed long not compliant
|
||||||
|
|
||||||
unsigned int g:1; // Compliant
|
unsigned int g:1; // Compliant
|
||||||
signed int h:1; // 6.2 - signed int with size 1 is not 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() {
|
void misra_7_1() {
|
||||||
int x = 066; // 7.1
|
int x = 066; // 7.1
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue