From e7220b404224da6f4d811f37cf1d13a9be156d2a Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 6 Sep 2023 12:59:12 +0200 Subject: [PATCH 1/3] reduced scope of variable in `followAllReferences()` --- lib/astutils.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 62a616ee671..94061d7f99a 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1212,43 +1212,48 @@ SmallVector followAllReferences(const Token* tok, return x.token < y.token; } }; - SmallVector refs_result; if (!tok) - return refs_result; + return {}; if (depth < 0) { - refs_result.push_back({tok, std::move(errors)}); + SmallVector refs_result; + refs_result.emplace_back(ReferenceToken{tok, std::move(errors)}); return refs_result; } const Variable *var = tok->variable(); if (var && var->declarationId() == tok->varId()) { if (var->nameToken() == tok || isStructuredBindingVariable(var)) { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } if (var->isReference() || var->isRValueReference()) { const Token * const varDeclEndToken = var->declEndToken(); if (!varDeclEndToken) { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } if (var->isArgument()) { errors.emplace_back(varDeclEndToken, "Passed to reference."); + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } if (Token::simpleMatch(varDeclEndToken, "=")) { if (astHasToken(varDeclEndToken, tok)) - return refs_result; + return {}; errors.emplace_back(varDeclEndToken, "Assigned to reference."); const Token *vartok = varDeclEndToken->astOperand2(); if (vartok == tok || (!temporary && isTemporary(true, vartok, nullptr, true) && (var->isConst() || var->isRValueReference()))) { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } if (vartok) return followAllReferences(vartok, temporary, inconclusive, std::move(errors), depth - 1); } else { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } @@ -1263,11 +1268,13 @@ SmallVector followAllReferences(const Token* tok, result.insert(refs.cbegin(), refs.cend()); if (!inconclusive && result.size() != 1) { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } if (!result.empty()) { + SmallVector refs_result; refs_result.insert(refs_result.end(), result.cbegin(), result.cend()); return refs_result; } @@ -1275,6 +1282,7 @@ SmallVector followAllReferences(const Token* tok, } else if (tok->previous() && tok->previous()->function() && Token::Match(tok->previous(), "%name% (")) { const Function *f = tok->previous()->function(); if (!Function::returnsReference(f)) { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } @@ -1287,17 +1295,20 @@ SmallVector followAllReferences(const Token* tok, followAllReferences(returnTok, temporary, inconclusive, errors, depth - returns.size())) { const Variable* argvar = rt.token->variable(); if (!argvar) { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } if (argvar->isArgument() && (argvar->isReference() || argvar->isRValueReference())) { const int n = getArgumentPos(argvar, f); if (n < 0) { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } std::vector args = getArguments(tok->previous()); if (n >= args.size()) { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } @@ -1309,6 +1320,7 @@ SmallVector followAllReferences(const Token* tok, followAllReferences(argTok, temporary, inconclusive, std::move(er), depth - returns.size()); result.insert(refs.cbegin(), refs.cend()); if (!inconclusive && result.size() > 1) { + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } @@ -1316,10 +1328,12 @@ SmallVector followAllReferences(const Token* tok, } } if (!result.empty()) { + SmallVector refs_result; refs_result.insert(refs_result.end(), result.cbegin(), result.cend()); return refs_result; } } + SmallVector refs_result; refs_result.push_back({tok, std::move(errors)}); return refs_result; } From 07ba12805044e49f8e7f7f6b08e38dcba48401b2 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 9 Sep 2023 00:35:11 +0200 Subject: [PATCH 2/3] removed duplicated block in `followAllReferences()` --- lib/astutils.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 94061d7f99a..35d8f208ebd 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1252,10 +1252,6 @@ SmallVector followAllReferences(const Token* tok, } if (vartok) return followAllReferences(vartok, temporary, inconclusive, std::move(errors), depth - 1); - } else { - SmallVector refs_result; - refs_result.push_back({tok, std::move(errors)}); - return refs_result; } } } else if (Token::simpleMatch(tok, "?") && Token::simpleMatch(tok->astOperand2(), ":")) { From f8bac78b95edd3de585ab946426511376a4b3ca1 Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 26 Nov 2023 15:59:07 +0100 Subject: [PATCH 3/3] consistently use `push_back()` in `followAllReferences()` [skip ci] --- lib/astutils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 35d8f208ebd..ac541c220bc 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1216,7 +1216,7 @@ SmallVector followAllReferences(const Token* tok, return {}; if (depth < 0) { SmallVector refs_result; - refs_result.emplace_back(ReferenceToken{tok, std::move(errors)}); + refs_result.push_back({tok, std::move(errors)}); return refs_result; } const Variable *var = tok->variable();