Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2662,11 +2662,13 @@ std::string lifetimeType(const Token *tok, const ValueFlow::Value *val)
return result;
}

const Variable *getLifetimeVariable(const Token *tok, ValueFlow::Value::ErrorPath &errorPath)
const Variable *getLifetimeVariable(const Token *tok, ValueFlow::Value::ErrorPath &errorPath, int depth)
{
if (!tok)
return nullptr;
const Variable *var = tok->variable();
if (depth < 0)
return var;
if (var && var->declarationId() == tok->varId()) {
if (var->isReference() || var->isRValueReference()) {
if (!var->declEndToken())
Expand All @@ -2680,7 +2682,7 @@ const Variable *getLifetimeVariable(const Token *tok, ValueFlow::Value::ErrorPat
if (vartok == tok)
return nullptr;
if (vartok)
return getLifetimeVariable(vartok, errorPath);
return getLifetimeVariable(vartok, errorPath, depth-1);
} else {
return nullptr;
}
Expand All @@ -2694,7 +2696,9 @@ const Variable *getLifetimeVariable(const Token *tok, ValueFlow::Value::ErrorPat
const Token *returnTok = findSimpleReturn(f);
if (!returnTok)
return nullptr;
const Variable *argvar = getLifetimeVariable(returnTok, errorPath);
if (returnTok == tok)
return var;
const Variable *argvar = getLifetimeVariable(returnTok, errorPath, depth-1);
if (!argvar)
return nullptr;
if (argvar->isArgument() && (argvar->isReference() || argvar->isRValueReference())) {
Expand All @@ -2704,7 +2708,7 @@ const Variable *getLifetimeVariable(const Token *tok, ValueFlow::Value::ErrorPat
const Token *argTok = getArguments(tok->previous()).at(n);
errorPath.emplace_back(returnTok, "Return reference.");
errorPath.emplace_back(tok->previous(), "Called function passing '" + argTok->str() + "'.");
return getLifetimeVariable(argTok, errorPath);
return getLifetimeVariable(argTok, errorPath, depth-1);
}
}
return var;
Expand Down
2 changes: 1 addition & 1 deletion lib/valueflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ namespace ValueFlow {
std::string eitherTheConditionIsRedundant(const Token *condition);
}

const Variable *getLifetimeVariable(const Token *tok, ValueFlow::Value::ErrorPath &errorPath);
const Variable *getLifetimeVariable(const Token *tok, ValueFlow::Value::ErrorPath &errorPath, int depth=20);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit off-topic: is there a specific reason why those functions are declared within this header but outside the namespace?


std::string lifetimeType(const Token *tok, const ValueFlow::Value *val);

Expand Down
10 changes: 10 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class TestAutoVariables : public TestFixture {
TEST_CASE(returnReferenceCalculation);
TEST_CASE(returnReferenceLambda);
TEST_CASE(returnReferenceInnerScope);
TEST_CASE(returnReferenceRecursive);

TEST_CASE(danglingReference);

Expand Down Expand Up @@ -1246,6 +1247,15 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void returnReferenceRecursive() {
check("int& f() { return f(); }");
ASSERT_EQUALS("", errout.str());

check("int& g(int& i) { return i; }\n"
"int& f() { return g(f()); }\n");
ASSERT_EQUALS("", errout.str());
}

void danglingReference() {
check("int f( int k )\n"
"{\n"
Expand Down