Skip to content

Commit f82eff6

Browse files
IOBYTEdanmar
authored andcommitted
speedup checkpostfixoperator.cpp by caching commonly looked up stuff in the symbol database. Ticket: #4266
1 parent e5ebb49 commit f82eff6

1 file changed

Lines changed: 31 additions & 32 deletions

File tree

lib/checkpostfixoperator.cpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,45 +37,44 @@ void CheckPostfixOperator::postfixOperator()
3737
if (!_settings->isEnabled("performance"))
3838
return;
3939

40-
const Token *tok = _tokenizer->tokens();
4140
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
4241

43-
// prevent crash if first token is ++ or --
44-
if (Token::Match(tok, "++|--"))
45-
tok = tok->next();
46-
47-
for (; tok; tok = tok->next()) {
48-
bool result = false;
49-
if (Token::Match(tok, "++|--")) {
50-
if (Token::Match(tok->tokAt(-2), ";|{|}") && Token::Match(tok->next(), ";|)|,")) {
51-
result = true;
52-
} else if (tok->strAt(-2) == ",") {
53-
int i(1);
54-
while (tok->strAt(i) != ")" && tok->tokAt(i) != 0) {
55-
if (tok->strAt(i) == ";") {
56-
result = true;
57-
break;
42+
const std::size_t functions = symbolDatabase->functionScopes.size();
43+
for (std::size_t i = 0; i < functions; ++i) {
44+
const Scope * scope = symbolDatabase->functionScopes[i];
45+
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
46+
if (tok->type() == Token::eIncDecOp) {
47+
bool result = false;
48+
if (Token::Match(tok->tokAt(-2), ";|{|}") && Token::Match(tok->next(), ";|)|,")) {
49+
result = true;
50+
} else if (tok->strAt(-2) == ",") {
51+
int ii(1);
52+
while (tok->strAt(ii) != ")" && tok->tokAt(ii) != 0) {
53+
if (tok->strAt(ii) == ";") {
54+
result = true;
55+
break;
56+
}
57+
++ii;
5858
}
59-
++i;
59+
} else if (tok->strAt(-2) == "<<" && tok->strAt(1) == "<<") {
60+
result = true;
6061
}
61-
} else if (tok->strAt(-2) == "<<" && tok->strAt(1) == "<<") {
62-
result = true;
63-
}
64-
}
6562

66-
if (result && tok->previous()->varId()) {
67-
const Variable *var = symbolDatabase->getVariableFromVarId(tok->previous()->varId());
68-
if (!var || var->isPointer() || var->isArray() || var->isReference())
69-
continue;
63+
if (result && tok->previous()->varId()) {
64+
const Variable *var = symbolDatabase->getVariableFromVarId(tok->previous()->varId());
65+
if (!var || var->isPointer() || var->isArray() || var->isReference())
66+
continue;
7067

71-
const Token *decltok = var->nameToken();
68+
const Token *decltok = var->nameToken();
7269

73-
if (Token::Match(decltok->previous(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) {
74-
// the variable is an iterator
75-
postfixOperatorError(tok);
76-
} else if (var->type()) {
77-
// the variable is an instance of class
78-
postfixOperatorError(tok);
70+
if (Token::Match(decltok->previous(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) {
71+
// the variable is an iterator
72+
postfixOperatorError(tok);
73+
} else if (var->type()) {
74+
// the variable is an instance of class
75+
postfixOperatorError(tok);
76+
}
77+
}
7978
}
8079
}
8180
}

0 commit comments

Comments
 (0)