misra: fix void parameter check

This commit is contained in:
Daniel Marjamäki 2022-04-01 21:34:47 +02:00
parent 4bf8718dcb
commit ea63b8e2bb
5 changed files with 29 additions and 22 deletions

View File

@ -202,6 +202,7 @@ class Token:
isUnsigned Is this token a unsigned type
isSigned Is this token a signed type
isExpandedMacro Is this token a expanded macro token
isRemovedVoidParameter Has void parameter been removed?
isSplittedVarDeclComma Is this a comma changed to semicolon in a splitted variable declaration ('int a,b;' => 'int a; int b;')
isSplittedVarDeclEq Is this a '=' changed to semicolon in a splitted variable declaration ('int a=5;' => 'int a; a=5;')
isImplicitInt Is this token an implicit "int"?
@ -254,6 +255,7 @@ class Token:
isUnsigned = False
isSigned = False
isExpandedMacro = False
isRemovedVoidParameter = False
isSplittedVarDeclComma = False
isSplittedVarDeclEq = False
isImplicitInt = False
@ -318,6 +320,8 @@ class Token:
self.isLogicalOp = True
if element.get('isExpandedMacro'):
self.isExpandedMacro = True
if element.get('isRemovedVoidParameter'):
self.isRemovedVoidParameter = True
if element.get('isSplittedVarDeclComma'):
self.isSplittedVarDeclComma = True
if element.get('isSplittedVarDeclEq'):

View File

@ -1863,19 +1863,12 @@ class MisraChecker:
return following
# Zero arguments should be in form ( void )
# TODO: Use rawTokens or add flag when void is removed
def checkZeroArguments(func, startCall, endCall):
if (len(func.argument) == 0):
voidArg = startCall.next
while voidArg is not endCall:
if voidArg.str == 'void':
break
voidArg = voidArg.next
if not voidArg.str == 'void':
if func.tokenDef.next:
self.reportError(func.tokenDef.next, 8, 2)
else:
self.reportError(func.tokenDef, 8, 2)
if not startCall.isRemovedVoidParameter and len(func.argument) == 0:
if func.tokenDef.next:
self.reportError(func.tokenDef.next, 8, 2)
else:
self.reportError(func.tokenDef, 8, 2)
def checkDeclarationArgumentsViolations(func, startCall, endCall):
# Collect the tokens for the arguments in function definition
@ -1936,7 +1929,7 @@ class MisraChecker:
endCall = startCall.link
if endCall is None or endCall.str != ')':
continue
# checkZeroArguments(func, startCall, endCall)
checkZeroArguments(func, startCall, endCall)
checkDefinitionArgumentsViolations(func, startCall, endCall)
# Check arguments in function declaration
@ -1948,7 +1941,7 @@ class MisraChecker:
endCall = startCall.link
if endCall is None or endCall.str != ')':
continue
# checkZeroArguments(func, startCall, endCall)
checkZeroArguments(func, startCall, endCall)
if tokenImpl:
checkDeclarationArgumentsViolations(func, startCall, endCall)
else:

View File

@ -337,7 +337,7 @@ const misra_8_1_a; // 8.1 8.4
static int misra_8_2_a (int n, ...);
extern int misra_8_2_b (int n);
extern int misra_8_2_c (int); // 8.2
static int misra_8_2_d (); // TODO: 8.2
static int misra_8_2_d (); // 8.2
static int misra_8_2_e (void);
static int misra_8_2_f (vec, n )
int *vec; // 8.2
@ -345,13 +345,13 @@ int n; // 8.2
{
return vec[ n - 1 ];
}
static int misra_8_2_g ( /* comment */ ); // TODO: 8.2
static int misra_8_2_h ( /* comment 1 */ /* comment 2 */ ); // TODO: 8.2
static int misra_8_2_g ( /* comment */ ); // 8.2
static int misra_8_2_h ( /* comment 1 */ /* comment 2 */ ); // 8.2
static int misra_8_2_i ( /* comment */ void);
static int misra_8_2_j ( /* comment */ void /* comment */);
static int misra_8_2_k ( //
void);
static int misra_8_2_l ( // TODO: 8.2
static int misra_8_2_l ( // 8.2
);
static void misra_8_2_m(uint8_t * const x);
static void misra_8_2_m(uint8_t * const x)
@ -373,7 +373,7 @@ static int misra_8_2_p(
const uint8_t *const a2
);
static int misra_8_2_q
(); // TODO: 8.2
(); // 8.2
void misra_8_4_foo(void) {} // 8.4
extern void misra_8_4_func(void);

View File

@ -644,6 +644,13 @@ public:
setFlag(fIsInline, b);
}
bool isRemovedVoidParameter() const {
return getFlag(fIsRemovedVoidParameter);
}
void setRemovedVoidParameter(bool b) {
setFlag(fIsRemovedVoidParameter, b);
}
bool isTemplate() const {
return getFlag(fIsTemplate);
}
@ -1263,6 +1270,7 @@ private:
fIsInline = (1ULL << 32), // Is this a inline type
fIsTemplate = (1ULL << 33),
fIsSimplifedScope = (1ULL << 34), // scope added when simplifying e.g. if (int i = ...; ...)
fIsRemovedVoidParameter = (1ULL << 35), // A void function parameter has been removed
};
Token::Type mTokType;

View File

@ -3111,10 +3111,10 @@ void Tokenizer::simplifyArrayAccessSyntax()
void Tokenizer::simplifyParameterVoid()
{
for (Token* tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "sizeof|decltype|typeof"))
continue;
if (Token::Match(tok, "%name% ( void )"))
if (Token::Match(tok, "%name% ( void )") && !Token::Match(tok, "sizeof|decltype|typeof")) {
tok->next()->deleteNext();
tok->next()->setRemovedVoidParameter(true);
}
}
}
@ -5544,6 +5544,8 @@ void Tokenizer::dump(std::ostream &out) const
}
if (tok->isExpandedMacro())
out << " isExpandedMacro=\"true\"";
if (tok->isRemovedVoidParameter())
out << " isRemovedVoidParameter=\"true\"";
if (tok->isSplittedVarDeclComma())
out << " isSplittedVarDeclComma=\"true\"";
if (tok->isSplittedVarDeclEq())