Use the new pattern: '%comp%' where possible.

Change also the description comment of the Token::Match by adding the new pattern and the forgotten '%op%'.
This commit is contained in:
Edoardo Prezioso 2012-12-01 01:31:35 +01:00
parent aeb4531a58
commit 5101f3c029
5 changed files with 21 additions and 24 deletions

View File

@ -436,7 +436,7 @@ void CheckBufferOverrun::parse_for_body(const Token *tok, const ArrayInfo &array
// does condition check counter variable? // does condition check counter variable?
bool usesCounter = false; bool usesCounter = false;
const Token *tok3 = tok2->previous(); const Token *tok3 = tok2->previous();
while (Token::Match(tok3, "%var%|%num%|)|>=|>|<=|<|==|!=")) { while (Token::Match(tok3, "%comp%|%num%|%var%|)")) {
if (tok3->str() == strindex) { if (tok3->str() == strindex) {
usesCounter = true; usesCounter = true;
break; break;

View File

@ -184,7 +184,7 @@ void CheckOther::clarifyCondition()
for (std::size_t i = 0; i < functions; ++i) { for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i]; const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "!|<|<=|==|!=|>|>=")) { if (Token::Match(tok, "%comp%|!")) {
if (tok->link()) // don't write false positives when templates are used if (tok->link()) // don't write false positives when templates are used
continue; continue;
@ -1226,25 +1226,21 @@ void CheckOther::checkIncorrectLogicOperator()
const Token *term1Tok = NULL, *term2Tok = NULL; const Token *term1Tok = NULL, *term2Tok = NULL;
const Token *op1Tok = NULL, *op2Tok = NULL, *op3Tok = NULL, *nextTok = NULL; const Token *op1Tok = NULL, *op2Tok = NULL, *op3Tok = NULL, *nextTok = NULL;
if (Token::Match(tok, "( %any% %op% %any% ) &&|%oror%") && if (Token::Match(tok, "( %any% %comp% %any% ) &&|%oror%")) {
tok->tokAt(2)->isComparisonOp()) {
term1Tok = tok->next(); term1Tok = tok->next();
op1Tok = tok->tokAt(2); op1Tok = tok->tokAt(2);
op2Tok = tok->tokAt(5); op2Tok = tok->tokAt(5);
} else if (Token::Match(tok, "%any% %op% %any% &&|%oror%") && } else if (Token::Match(tok, "%any% %comp% %any% &&|%oror%")) {
tok->tokAt(1)->isComparisonOp()) {
term1Tok = tok; term1Tok = tok;
op1Tok = tok->next(); op1Tok = tok->next();
op2Tok = tok->tokAt(3); op2Tok = tok->tokAt(3);
} }
if (op2Tok) { if (op2Tok) {
if (Token::Match(op2Tok->next(), "( %any% %op% %any% ) %any%") && if (Token::Match(op2Tok->next(), "( %any% %comp% %any% ) %any%")) {
op2Tok->tokAt(3)->isComparisonOp()) {
term2Tok = op2Tok->tokAt(2); term2Tok = op2Tok->tokAt(2);
op3Tok = op2Tok->tokAt(3); op3Tok = op2Tok->tokAt(3);
nextTok = op2Tok->tokAt(6); nextTok = op2Tok->tokAt(6);
} else if (Token::Match(op2Tok->next(), "%any% %op% %any% %any%") && } else if (Token::Match(op2Tok->next(), "%any% %comp% %any% %any%")) {
op2Tok->tokAt(2)->isComparisonOp()) {
term2Tok = op2Tok->next(); term2Tok = op2Tok->next();
op3Tok = op2Tok->tokAt(2); op3Tok = op2Tok->tokAt(2);
nextTok = op2Tok->tokAt(4); nextTok = op2Tok->tokAt(4);
@ -3061,7 +3057,7 @@ void CheckOther::checkDuplicateExpression()
complexDuplicateExpressionCheck(constFunctions, scope->classStart, "&&", "%oror%|%or%"); complexDuplicateExpressionCheck(constFunctions, scope->classStart, "&&", "%oror%|%or%");
for (const Token *tok = scope->classStart; tok && tok != scope->classStart->link(); tok = tok->next()) { for (const Token *tok = scope->classStart; tok && tok != scope->classStart->link(); tok = tok->next()) {
if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% ==|!=|<=|>=|<|>|- %var% )|&&|%oror%|;|,") && if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% %comp%|- %var% )|&&|%oror%|;|,") &&
tok->strAt(1) == tok->strAt(3)) { tok->strAt(1) == tok->strAt(3)) {
// float == float and float != float are valid NaN checks // float == float and float != float are valid NaN checks
if (Token::Match(tok->tokAt(2), "==|!=") && tok->next()->varId()) { if (Token::Match(tok->tokAt(2), "==|!=") && tok->next()->varId()) {
@ -3078,7 +3074,7 @@ void CheckOther::checkDuplicateExpression()
continue; continue;
duplicateExpressionError(tok->next(), tok->tokAt(3), tok->strAt(2)); duplicateExpressionError(tok->next(), tok->tokAt(3), tok->strAt(2));
} else if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% . %var% ==|!=|<=|>=|<|>|- %var% . %var% )|&&|%oror%|;|,") && } else if (Token::Match(tok, ",|=|return|(|&&|%oror% %var% . %var% %comp%|- %var% . %var% )|&&|%oror%|;|,") &&
tok->strAt(1) == tok->strAt(5) && tok->strAt(3) == tok->strAt(7)) { tok->strAt(1) == tok->strAt(5) && tok->strAt(3) == tok->strAt(7)) {
// If either variable token is an expanded macro then // If either variable token is an expanded macro then
@ -3224,7 +3220,7 @@ void CheckOther::checkModuloAlwaysTrueFalse()
for (std::size_t i = 0; i < functions; ++i) { for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i]; const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if ((Token::Match(tok, "% %num% %op% %num%") && tok->tokAt(2)->isComparisonOp()) && if ((Token::Match(tok, "% %num% %comp% %num%")) &&
(!tok->tokAt(4) || !tok->tokAt(4)->isArithmeticalOp())) { (!tok->tokAt(4) || !tok->tokAt(4)->isArithmeticalOp())) {
if (MathLib::isLessEqual(tok->strAt(1), tok->strAt(3))) if (MathLib::isLessEqual(tok->strAt(1), tok->strAt(3)))
moduloAlwaysTrueFalseError(tok, tok->strAt(1)); moduloAlwaysTrueFalseError(tok, tok->strAt(1));
@ -3376,24 +3372,24 @@ void CheckOther::checkComparisonOfBoolExpressionWithInt()
const Token* numTok = 0; const Token* numTok = 0;
const Token* opTok = 0; const Token* opTok = 0;
char op = 0; char op = 0;
if (Token::Match(tok, "&&|%oror% %any% ) %op% %any%") && tok->tokAt(3)->isComparisonOp()) { if (Token::Match(tok, "&&|%oror% %any% ) %comp% %any%")) {
numTok = tok->tokAt(4); numTok = tok->tokAt(4);
opTok = tok->tokAt(3); opTok = tok->tokAt(3);
if (Token::Match(opTok, "<|>")) if (Token::Match(opTok, "<|>"))
op = opTok->str()[0]; op = opTok->str()[0];
} else if (Token::Match(tok, "%any% %op% ( %any% &&|%oror%") && tok->next()->isComparisonOp()) { } else if (Token::Match(tok, "%any% %comp% ( %any% &&|%oror%")) {
numTok = tok; numTok = tok;
opTok = tok->next(); opTok = tok->next();
if (Token::Match(opTok, "<|>")) if (Token::Match(opTok, "<|>"))
op = opTok->str()[0]=='>'?'<':'>'; op = opTok->str()[0]=='>'?'<':'>';
} }
else if (Token::Match(tok, "! %var% %op% %any%") && tok->tokAt(2)->isComparisonOp()) { else if (Token::Match(tok, "! %var% %comp% %any%")) {
numTok = tok->tokAt(3); numTok = tok->tokAt(3);
opTok = tok->tokAt(2); opTok = tok->tokAt(2);
if (Token::Match(opTok, "<|>")) if (Token::Match(opTok, "<|>"))
op = opTok->str()[0]; op = opTok->str()[0];
} else if (Token::Match(tok, "%any% %op% ! %var%") && tok->next()->isComparisonOp()) { } else if (Token::Match(tok, "%any% %comp% ! %var%")) {
numTok = tok; numTok = tok;
opTok = tok->next(); opTok = tok->next();
if (Token::Match(opTok, "<|>")) if (Token::Match(opTok, "<|>"))

View File

@ -694,7 +694,7 @@ static bool isLowerThanAnd(const Token* lower)
} }
static bool isLowerThanShift(const Token* lower) static bool isLowerThanShift(const Token* lower)
{ {
return isLowerThanAnd(lower) || Token::Match(lower, "&|<|<=|>|>=|==|!="); return isLowerThanAnd(lower) || Token::Match(lower, "%comp%|&");
} }
static bool isLowerThanPlusMinus(const Token* lower) static bool isLowerThanPlusMinus(const Token* lower)
{ {
@ -808,8 +808,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
ret = true; ret = true;
} }
if (tok->type() == Token::eChar && if (Token::Match(tok->previous(), "(|&&|%oror% %char% %comp% %num% &&|%oror%|)")) {
Token::Match(tok->previous(), "(|&&|%oror% %any% ==|!=|<=|<|>=|> %num% &&|%oror%|)")) {
tok->str(MathLib::longToString(tok->str()[1] & 0xff)); tok->str(MathLib::longToString(tok->str()[1] & 0xff));
} }
@ -921,7 +920,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
ret = true; ret = true;
} }
if (Token::Match(tok, "%num% ==|!=|<=|>=|<|> %num%") && if (Token::Match(tok, "%num% %comp% %num%") &&
MathLib::isInt(tok->str()) && MathLib::isInt(tok->str()) &&
MathLib::isInt(tok->strAt(2))) { MathLib::isInt(tok->strAt(2))) {
if (Token::Match(tok->previous(), "(|&&|%oror%") && Token::Match(tok->tokAt(3), ")|&&|%oror%")) { if (Token::Match(tok->previous(), "(|&&|%oror%") && Token::Match(tok->tokAt(3), ")|&&|%oror%")) {

View File

@ -124,8 +124,10 @@ public:
* - "%num%" Any numeric token, e.g. "23" * - "%num%" Any numeric token, e.g. "23"
* - "%bool%" true or false * - "%bool%" true or false
* - "%char%" Any token enclosed in &apos;-character. * - "%char%" Any token enclosed in &apos;-character.
* - "%comp%" Any token such that isComparisonOp() returns true.
* - "%str%" Any token starting with &quot;-character (C-string). * - "%str%" Any token starting with &quot;-character (C-string).
* - "%varid%" Match with parameter varid * - "%varid%" Match with parameter varid
* - "%op%" Any token such that isOp() returns true.
* - "%or%" A bitwise-or operator '|' * - "%or%" A bitwise-or operator '|'
* - "%oror%" A logical-or operator '||' * - "%oror%" A logical-or operator '||'
* - "[abc]" Any of the characters 'a' or 'b' or 'c' * - "[abc]" Any of the characters 'a' or 'b' or 'c'

View File

@ -6260,9 +6260,9 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
} }
// Using the variable in condition.. // Using the variable in condition..
if (Token::Match(tok3->previous(), ("if ( " + structname + " %varid% ==|!=|<|<=|>|>=|)").c_str(), varid) || if (Token::Match(tok3->previous(), ("if ( " + structname + " %varid% %comp%|)").c_str(), varid) ||
Token::Match(tok3, ("( " + structname + " %varid% ==|!=|<|<=|>|>=").c_str(), varid) || Token::Match(tok3, ("( " + structname + " %varid% %comp%").c_str(), varid) ||
Token::Match(tok3, ("!|==|!=|<|<=|>|>= " + structname + " %varid% ==|!=|<|<=|>|>=|)|;").c_str(), varid) || Token::Match(tok3, ("%comp%|! " + structname + " %varid% %comp%|)|;").c_str(), varid) ||
Token::Match(tok3->previous(), "strlen|free ( %varid% )", varid)) { Token::Match(tok3->previous(), "strlen|free ( %varid% )", varid)) {
if (value[0] == '\"' && tok3->previous()->str() != "strlen") { if (value[0] == '\"' && tok3->previous()->str() != "strlen") {
// bail out if value is a string unless if it's just given // bail out if value is a string unless if it's just given