avoid redundant `Token::declEndToken()` calls (#4363)
This commit is contained in:
parent
2afd5f80e7
commit
d73a33d17e
|
@ -374,7 +374,8 @@ bool isVariableDecl(const Token* tok)
|
||||||
return false;
|
return false;
|
||||||
if (var->nameToken() == tok)
|
if (var->nameToken() == tok)
|
||||||
return true;
|
return true;
|
||||||
if (Token::Match(var->declEndToken(), "; %var%") && var->declEndToken()->next() == tok)
|
const Token * const varDeclEndToken = var->declEndToken();
|
||||||
|
if (Token::Match(varDeclEndToken, "; %var%") && varDeclEndToken->next() == tok)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -1068,16 +1069,17 @@ std::vector<ReferenceToken> followAllReferences(const Token* tok,
|
||||||
if (var->nameToken() == tok || isStructuredBindingVariable(var)) {
|
if (var->nameToken() == tok || isStructuredBindingVariable(var)) {
|
||||||
return {{tok, std::move(errors)}};
|
return {{tok, std::move(errors)}};
|
||||||
} else if (var->isReference() || var->isRValueReference()) {
|
} else if (var->isReference() || var->isRValueReference()) {
|
||||||
if (!var->declEndToken())
|
const Token * const varDeclEndToken = var->declEndToken();
|
||||||
|
if (!varDeclEndToken)
|
||||||
return {{tok, std::move(errors)}};
|
return {{tok, std::move(errors)}};
|
||||||
if (var->isArgument()) {
|
if (var->isArgument()) {
|
||||||
errors.emplace_back(var->declEndToken(), "Passed to reference.");
|
errors.emplace_back(varDeclEndToken, "Passed to reference.");
|
||||||
return {{tok, std::move(errors)}};
|
return {{tok, std::move(errors)}};
|
||||||
} else if (Token::simpleMatch(var->declEndToken(), "=")) {
|
} else if (Token::simpleMatch(varDeclEndToken, "=")) {
|
||||||
if (astHasToken(var->declEndToken(), tok))
|
if (astHasToken(varDeclEndToken, tok))
|
||||||
return std::vector<ReferenceToken>{};
|
return std::vector<ReferenceToken>{};
|
||||||
errors.emplace_back(var->declEndToken(), "Assigned to reference.");
|
errors.emplace_back(varDeclEndToken, "Assigned to reference.");
|
||||||
const Token *vartok = var->declEndToken()->astOperand2();
|
const Token *vartok = varDeclEndToken->astOperand2();
|
||||||
if (vartok == tok || (!temporary && isTemporary(true, vartok, nullptr, true) &&
|
if (vartok == tok || (!temporary && isTemporary(true, vartok, nullptr, true) &&
|
||||||
(var->isConst() || var->isRValueReference())))
|
(var->isConst() || var->isRValueReference())))
|
||||||
return {{tok, std::move(errors)}};
|
return {{tok, std::move(errors)}};
|
||||||
|
|
|
@ -471,11 +471,12 @@ static bool isEscapedReference(const Variable* var)
|
||||||
return false;
|
return false;
|
||||||
if (!var->isReference())
|
if (!var->isReference())
|
||||||
return false;
|
return false;
|
||||||
if (!var->declEndToken())
|
const Token * const varDeclEndToken = var->declEndToken();
|
||||||
|
if (!varDeclEndToken)
|
||||||
return false;
|
return false;
|
||||||
if (!Token::simpleMatch(var->declEndToken(), "="))
|
if (!Token::simpleMatch(varDeclEndToken, "="))
|
||||||
return false;
|
return false;
|
||||||
const Token* vartok = var->declEndToken()->astOperand2();
|
const Token* vartok = varDeclEndToken->astOperand2();
|
||||||
return !isTemporary(true, vartok, nullptr, false);
|
return !isTemporary(true, vartok, nullptr, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1297,7 +1297,8 @@ void CheckOther::checkPassByReference()
|
||||||
if (var->scope() && var->scope()->function->arg->link()->strAt(-1) == "...")
|
if (var->scope() && var->scope()->function->arg->link()->strAt(-1) == "...")
|
||||||
continue; // references could not be used as va_start parameters (#5824)
|
continue; // references could not be used as va_start parameters (#5824)
|
||||||
|
|
||||||
if ((var->declEndToken() && var->declEndToken()->isExternC()) ||
|
const Token * const varDeclEndToken = var->declEndToken();
|
||||||
|
if ((varDeclEndToken && varDeclEndToken->isExternC()) ||
|
||||||
(var->scope() && var->scope()->function && var->scope()->function->tokenDef && var->scope()->function->tokenDef->isExternC()))
|
(var->scope() && var->scope()->function && var->scope()->function->tokenDef && var->scope()->function->tokenDef->isExternC()))
|
||||||
continue; // references cannot be used in functions in extern "C" blocks
|
continue; // references cannot be used in functions in extern "C" blocks
|
||||||
|
|
||||||
|
|
|
@ -3340,14 +3340,15 @@ static std::vector<LifetimeToken> getLifetimeTokens(const Token* tok,
|
||||||
return {{tok, std::move(errorPath)}};
|
return {{tok, std::move(errorPath)}};
|
||||||
if (var && var->declarationId() == tok->varId()) {
|
if (var && var->declarationId() == tok->varId()) {
|
||||||
if (var->isReference() || var->isRValueReference()) {
|
if (var->isReference() || var->isRValueReference()) {
|
||||||
if (!var->declEndToken())
|
const Token * const varDeclEndToken = var->declEndToken();
|
||||||
|
if (!varDeclEndToken)
|
||||||
return {{tok, true, std::move(errorPath)}};
|
return {{tok, true, std::move(errorPath)}};
|
||||||
if (var->isArgument()) {
|
if (var->isArgument()) {
|
||||||
errorPath.emplace_back(var->declEndToken(), "Passed to reference.");
|
errorPath.emplace_back(varDeclEndToken, "Passed to reference.");
|
||||||
return {{tok, true, std::move(errorPath)}};
|
return {{tok, true, std::move(errorPath)}};
|
||||||
} else if (Token::simpleMatch(var->declEndToken(), "=")) {
|
} else if (Token::simpleMatch(varDeclEndToken, "=")) {
|
||||||
errorPath.emplace_back(var->declEndToken(), "Assigned to reference.");
|
errorPath.emplace_back(varDeclEndToken, "Assigned to reference.");
|
||||||
const Token *vartok = var->declEndToken()->astOperand2();
|
const Token *vartok = varDeclEndToken->astOperand2();
|
||||||
const bool temporary = isTemporary(true, vartok, nullptr, true);
|
const bool temporary = isTemporary(true, vartok, nullptr, true);
|
||||||
const bool nonlocal = var->isStatic() || var->isGlobal();
|
const bool nonlocal = var->isStatic() || var->isGlobal();
|
||||||
if (vartok == tok || (nonlocal && temporary) ||
|
if (vartok == tok || (nonlocal && temporary) ||
|
||||||
|
@ -4021,6 +4022,8 @@ struct LifetimeStore {
|
||||||
return;
|
return;
|
||||||
if (!argtok)
|
if (!argtok)
|
||||||
return;
|
return;
|
||||||
|
if (!tok)
|
||||||
|
return;
|
||||||
for (const ValueFlow::Value &v : argtok->values()) {
|
for (const ValueFlow::Value &v : argtok->values()) {
|
||||||
if (!v.isLifetimeValue())
|
if (!v.isLifetimeValue())
|
||||||
continue;
|
continue;
|
||||||
|
@ -4031,7 +4034,8 @@ struct LifetimeStore {
|
||||||
er.insert(er.end(), errorPath.begin(), errorPath.end());
|
er.insert(er.end(), errorPath.begin(), errorPath.end());
|
||||||
if (!var)
|
if (!var)
|
||||||
continue;
|
continue;
|
||||||
for (const Token *tok3 = tok; tok3 && tok3 != var->declEndToken(); tok3 = tok3->previous()) {
|
const Token * const varDeclEndToken = var->declEndToken();
|
||||||
|
for (const Token *tok3 = tok; tok3 && tok3 != varDeclEndToken; tok3 = tok3->previous()) {
|
||||||
if (tok3->varId() == var->declarationId()) {
|
if (tok3->varId() == var->declarationId()) {
|
||||||
LifetimeStore{tok3, message, type, inconclusive}.byVal(tok, tokenlist, errorLogger, settings, pred);
|
LifetimeStore{tok3, message, type, inconclusive}.byVal(tok, tokenlist, errorLogger, settings, pred);
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue