From e6fdcb9de20f67eb2c8bad1f47ba6f51e6db4d57 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sun, 19 Jun 2022 01:16:27 -0500 Subject: [PATCH] Refactor to use derefShared (#4220) --- lib/valueflow.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 0aac6a1a4..508c24387 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3249,6 +3249,11 @@ std::vector getLifetimeObjValues(const Token* tok, bool inconc static bool hasUniqueOwnership(const Token* tok) { + if (!tok) + return false; + const Variable* var = tok->variable(); + if (var && var->isArray() && !var->isArgument()) + return true; if (astIsPointer(tok)) return false; if (astIsUniqueSmartPointer(tok)) @@ -3263,11 +3268,10 @@ static bool derefShared(const Token* tok) { if (!tok) return false; - if (tok->str() == "." && tok->originalName() != "->") { + if (!tok->isUnaryOp("*") && tok->str() != "[" && tok->str() != ".") return false; - } else if (!tok->isUnaryOp("*") && tok->str() == "[") { + if (tok->str() == "." && tok->originalName() != "->") return false; - } const Token* ptrTok = tok->astOperand1(); return !hasUniqueOwnership(ptrTok); } @@ -3386,10 +3390,8 @@ static std::vector getLifetimeTokens(const Token* tok, } else if (Token::Match(tok, ".|::|[") || tok->isUnaryOp("*")) { const Token *vartok = tok; - if (tok->isUnaryOp("*")) - vartok = tok->astOperand1(); while (vartok) { - if (vartok->str() == "[" || vartok->originalName() == "->") + if (vartok->str() == "[" || vartok->originalName() == "->" || vartok->isUnaryOp("*")) vartok = vartok->astOperand1(); else if (vartok->str() == "." || vartok->str() == "::") vartok = vartok->astOperand2(); @@ -3399,10 +3401,7 @@ static std::vector getLifetimeTokens(const Token* tok, if (!vartok) return {{tok, std::move(errorPath)}}; - const Variable *tokvar = vartok->variable(); - const bool isContainer = astIsContainer(vartok) && !astIsPointer(vartok); - if (!astIsUniqueSmartPointer(vartok) && !isContainer && !(tokvar && tokvar->isArray() && !tokvar->isArgument()) && - (Token::Match(vartok->astParent(), "[|*") || vartok->astParent()->originalName() == "->")) { + if (derefShared(vartok->astParent())) { for (const ValueFlow::Value &v : vartok->values()) { if (!v.isLocalLifetimeValue()) continue;