MISRA Rule 6.1/6.2 Bit field check (#2861)
This commit is contained in:
parent
4eb829933e
commit
79b9dd5345
|
@ -1290,6 +1290,51 @@ class MisraChecker:
|
||||||
if scope.className and scope.className[:num_sign_chars] in macroNames:
|
if scope.className and scope.className[:num_sign_chars] in macroNames:
|
||||||
self.reportError(scope.bodyStart, 5, 5)
|
self.reportError(scope.bodyStart, 5, 5)
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if token.valueType.bits == 0:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if token.valueType.type == 'bool':
|
||||||
|
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
|
||||||
|
|
||||||
|
isExplicitlySignedOrUnsigned = False
|
||||||
|
typeToken = token.variable.typeStartToken
|
||||||
|
while typeToken:
|
||||||
|
if typeToken.isUnsigned or typeToken.isSigned:
|
||||||
|
isExplicitlySignedOrUnsigned = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if typeToken.Id == token.variable.typeEndToken.Id:
|
||||||
|
break
|
||||||
|
|
||||||
|
typeToken = typeToken.next
|
||||||
|
|
||||||
|
if not isExplicitlySignedOrUnsigned:
|
||||||
|
self.reportError(token, 6, 1)
|
||||||
|
|
||||||
|
|
||||||
|
def misra_6_2(self, data):
|
||||||
|
# Bitfields of size 1 can not be signed
|
||||||
|
for token in data.tokenlist:
|
||||||
|
if token.valueType == None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if token.valueType.bits == 1 and token.valueType.sign == 'signed':
|
||||||
|
self.reportError(token, 6, 2)
|
||||||
|
|
||||||
|
|
||||||
def misra_7_1(self, rawTokens):
|
def misra_7_1(self, rawTokens):
|
||||||
compiled = re.compile(r'^0[0-7]+$')
|
compiled = re.compile(r'^0[0-7]+$')
|
||||||
for tok in rawTokens:
|
for tok in rawTokens:
|
||||||
|
@ -2902,8 +2947,8 @@ class MisraChecker:
|
||||||
self.executeCheck(502, self.misra_5_2, cfg)
|
self.executeCheck(502, self.misra_5_2, cfg)
|
||||||
self.executeCheck(504, self.misra_5_4, cfg)
|
self.executeCheck(504, self.misra_5_4, cfg)
|
||||||
self.executeCheck(505, self.misra_5_5, cfg)
|
self.executeCheck(505, self.misra_5_5, cfg)
|
||||||
# 6.1 require updates in Cppcheck (type info for bitfields are lost)
|
self.executeCheck(601, self.misra_6_1, cfg)
|
||||||
# 6.2 require updates in Cppcheck (type info for bitfields are lost)
|
self.executeCheck(602, self.misra_6_2, cfg)
|
||||||
if cfgNumber == 0:
|
if cfgNumber == 0:
|
||||||
self.executeCheck(701, self.misra_7_1, data.rawTokens)
|
self.executeCheck(701, self.misra_7_1, data.rawTokens)
|
||||||
self.executeCheck(703, self.misra_7_3, data.rawTokens)
|
self.executeCheck(703, self.misra_7_3, data.rawTokens)
|
||||||
|
|
|
@ -203,6 +203,20 @@ void misra_5_5_func1()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef unsigned int UINT_TYPEDEF;
|
||||||
|
struct struct_with_bitfields
|
||||||
|
{
|
||||||
|
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
|
||||||
|
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 misra_7_1() {
|
void misra_7_1() {
|
||||||
int x = 066; // 7.1
|
int x = 066; // 7.1
|
||||||
|
|
Loading…
Reference in New Issue