astutils.cpp: some small optimizations (#4378)

This commit is contained in:
Oliver Stöneberg 2022-08-21 17:39:21 +02:00 committed by GitHub
parent c820d2ba97
commit 9b4973d711
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 19 deletions

View File

@ -50,10 +50,10 @@ const Token* findExpression(const nonneg int exprid,
const Token* end, const Token* end,
const std::function<bool(const Token*)>& pred) const std::function<bool(const Token*)>& pred)
{ {
if (!precedes(start, end))
return nullptr;
if (exprid == 0) if (exprid == 0)
return nullptr; return nullptr;
if (!precedes(start, end))
return nullptr;
for (const Token* tok = start; tok != end; tok = tok->next()) { for (const Token* tok = start; tok != end; tok = tok->next()) {
if (tok->exprId() != exprid) if (tok->exprId() != exprid)
continue; continue;
@ -575,7 +575,7 @@ static std::vector<const Token*> getParentMembers(const Token* tok)
return {}; return {};
if (!Token::simpleMatch(tok->astParent(), ".")) if (!Token::simpleMatch(tok->astParent(), "."))
return {tok}; return {tok};
const Token* parent = tok; const Token* parent = tok->astParent();
while (Token::simpleMatch(parent->astParent(), ".")) while (Token::simpleMatch(parent->astParent(), "."))
parent = parent->astParent(); parent = parent->astParent();
std::vector<const Token*> result; std::vector<const Token*> result;
@ -885,7 +885,7 @@ bool isAliasOf(const Token* tok, const Token* expr, bool* inconclusive)
[&](const Token* aliasTok) { [&](const Token* aliasTok) {
return aliasTok->exprId() == childTok->exprId(); return aliasTok->exprId() == childTok->exprId();
})) { })) {
if (val.isInconclusive() && inconclusive) { // NOLINT if (val.isInconclusive() && inconclusive != nullptr) {
value = &val; value = &val;
} else { } else {
return true; return true;
@ -1168,8 +1168,10 @@ const Token* followReferences(const Token* tok, ErrorPath* errors)
static bool isSameLifetime(const Token * const tok1, const Token * const tok2) static bool isSameLifetime(const Token * const tok1, const Token * const tok2)
{ {
ValueFlow::Value v1 = getLifetimeObjValue(tok1); ValueFlow::Value v1 = getLifetimeObjValue(tok1);
if (!v1.isLifetimeValue())
return false;
ValueFlow::Value v2 = getLifetimeObjValue(tok2); ValueFlow::Value v2 = getLifetimeObjValue(tok2);
if (!v1.isLifetimeValue() || !v2.isLifetimeValue()) if (!v2.isLifetimeValue())
return false; return false;
return v1.tokvalue == v2.tokvalue; return v1.tokvalue == v2.tokvalue;
} }
@ -1227,10 +1229,12 @@ static inline bool isSameConstantValue(bool macro, const Token* tok1, const Toke
return tok->astOperand2(); return tok->astOperand2();
return tok; return tok;
}; };
tok1 = adjustForCast(tok1);
tok2 = adjustForCast(tok2);
if (!tok1->isNumber() || !tok2->isNumber()) tok1 = adjustForCast(tok1);
if (!tok1->isNumber())
return false;
tok2 = adjustForCast(tok2);
if (!tok2->isNumber())
return false; return false;
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg())) if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
@ -1403,20 +1407,20 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
value = &condTok->astOperand2()->values().front(); value = &condTok->astOperand2()->values().front();
varTok1 = condTok->astOperand1(); varTok1 = condTok->astOperand1();
} }
if (Token::simpleMatch(exprTok, "!")) const bool exprIsNot = Token::simpleMatch(exprTok, "!");
if (exprIsNot)
varTok2 = exprTok->astOperand1(); varTok2 = exprTok->astOperand1();
bool compare = false; bool compare = false;
if (value) { if (value) {
if (value->intvalue == 0 && Token::simpleMatch(exprTok, "!") && Token::simpleMatch(condTok, "==")) { if (value->intvalue == 0 && exprIsNot && Token::simpleMatch(condTok, "==")) {
compare = true; compare = true;
} else if (value->intvalue == 0 && !Token::simpleMatch(exprTok, "!") && Token::simpleMatch(condTok, "!=")) { } else if (value->intvalue == 0 && !exprIsNot && Token::simpleMatch(condTok, "!=")) {
compare = true; compare = true;
} else if (value->intvalue != 0 && Token::simpleMatch(exprTok, "!") && Token::simpleMatch(condTok, "!=")) { } else if (value->intvalue != 0 && exprIsNot && Token::simpleMatch(condTok, "!=")) {
compare = true; compare = true;
} else if (value->intvalue != 0 && !Token::simpleMatch(exprTok, "!") && Token::simpleMatch(condTok, "==")) { } else if (value->intvalue != 0 && !exprIsNot && Token::simpleMatch(condTok, "==")) {
compare = true; compare = true;
} }
} }
if (compare && astIsBoolLike(varTok1) && astIsBoolLike(varTok2)) if (compare && astIsBoolLike(varTok1) && astIsBoolLike(varTok2))
return isSameExpression(cpp, macro, varTok1, varTok2, library, pure, followVar, errors); return isSameExpression(cpp, macro, varTok1, varTok2, library, pure, followVar, errors);
@ -2133,20 +2137,21 @@ std::vector<const Variable*> getArgumentVars(const Token* tok, int argnr)
else else
return result; return result;
} }
if (Token::Match(tok->previous(), "%type% (|{") || Token::simpleMatch(tok, "{") || tok->variable()) { if (tok->variable() || Token::simpleMatch(tok, "{") || Token::Match(tok->previous(), "%type% (|{")) {
const bool constructor = Token::simpleMatch(tok, "{") || (tok->variable() && tok->variable()->nameToken() == tok);
const Type* type = Token::typeOf(tok); const Type* type = Token::typeOf(tok);
if (!type) if (!type)
return result; return result;
const Scope* typeScope = type->classScope; const Scope* typeScope = type->classScope;
if (!typeScope) if (!typeScope)
return result; return result;
const bool tokIsBrace = Token::simpleMatch(tok, "{");
// Aggregate constructor // Aggregate constructor
if (Token::simpleMatch(tok, "{") && typeScope->numConstructors == 0 && argnr < typeScope->varlist.size()) { if (tokIsBrace && typeScope->numConstructors == 0 && argnr < typeScope->varlist.size()) {
auto it = std::next(typeScope->varlist.begin(), argnr); auto it = std::next(typeScope->varlist.begin(), argnr);
return {&*it}; return {&*it};
} }
const int argCount = numberOfArguments(tok); const int argCount = numberOfArguments(tok);
const bool constructor = tokIsBrace || (tok->variable() && tok->variable()->nameToken() == tok);
for (const Function &function : typeScope->functionList) { for (const Function &function : typeScope->functionList) {
if (function.argCount() < argCount) if (function.argCount() < argCount)
continue; continue;
@ -3356,9 +3361,9 @@ static bool hasVolatileCastOrVar(const Token *expr)
bool ret = false; bool ret = false;
visitAstNodes(expr, visitAstNodes(expr,
[&ret](const Token *tok) { [&ret](const Token *tok) {
if (Token::simpleMatch(tok, "( volatile")) if (tok->variable() && tok->variable()->isVolatile())
ret = true; ret = true;
else if (tok->variable() && tok->variable()->isVolatile()) else if (Token::simpleMatch(tok, "( volatile"))
ret = true; ret = true;
return ret ? ChildrenToVisit::none : ChildrenToVisit::op1_and_op2; return ret ? ChildrenToVisit::none : ChildrenToVisit::op1_and_op2;
}); });