Skip to content

Commit ec82405

Browse files
committed
Handle bound in constructors better
1 parent 2b2c0a1 commit ec82405

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,7 +2745,6 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
27452745
std::unordered_map<const Token*, const Variable*> argToParam;
27462746
for (std::size_t i = 0; i < args.size(); i++)
27472747
argToParam[args[i]] = constructor->getArgumentVar(i);
2748-
const bool lifetimeBound = hasLifetimeBound(constructor);
27492748
if (const Token* initList = constructor->constructorMemberInitialization()) {
27502749
std::unordered_map<const Variable*, LifetimeCapture> paramCapture;
27512750
for (const Token* tok2 : astFlatten(initList->astOperand2(), ",")) {
@@ -2809,16 +2808,19 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
28092808
else
28102809
ls.byVal(tok, tokenlist, errorLogger, settings);
28112810
});
2812-
} else if (!lifetimeBound && hasBorrowingVariables(constructor->nestedIn->varlist, args)) {
2811+
} else if (hasBorrowingVariables(constructor->nestedIn->varlist, args)) {
28132812
LifetimeStore::forEach(tokenlist,
28142813
errorLogger,
28152814
settings,
28162815
args,
28172816
"Passed to constructor of '" + name + "'.",
28182817
ValueFlow::Value::LifetimeKind::SubObject,
28192818
[&](LifetimeStore& ls) {
2820-
ls.inconclusive = true;
28212819
const Variable* var = argToParam.at(ls.argtok);
2820+
// The lifetimes of a bound argument are stored below and are not inconclusive
2821+
if (var && var->isLifetimeBound())
2822+
return;
2823+
ls.inconclusive = true;
28222824
if (var && !var->isConst() && var->isReference())
28232825
ls.byRef(tok, tokenlist, errorLogger, settings);
28242826
else

‎test/testautovariables.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4101,6 +4101,25 @@ class TestAutoVariables : public TestFixture {
41014101
ASSERT_EQUALS(
41024102
"[test.cpp:8:20] -> [test.cpp:7:9] -> [test.cpp:8:16]: (error) Returning object that points to local variable 'b' that will be invalid when returning. [returnDanglingLifetime]\n",
41034103
errout_str());
4104+
4105+
// An argument for a parameter without the attribute is still inconclusive
4106+
check("struct Pair {\n"
4107+
" Pair(const int& a [[clang::lifetimebound]], const int* b);\n"
4108+
" const int* a;\n"
4109+
" const int* b;\n"
4110+
"};\n"
4111+
"Pair f1(const int& a) {\n"
4112+
" int b = 0;\n"
4113+
" return Pair(a, &b);\n"
4114+
"}\n"
4115+
"Pair f2(const int* b) {\n"
4116+
" int a = 0;\n"
4117+
" return Pair(a, b);\n"
4118+
"}\n");
4119+
ASSERT_EQUALS(
4120+
"[test.cpp:8:20] -> [test.cpp:8:20] -> [test.cpp:7:9] -> [test.cpp:8:16]: (error, inconclusive) Returning object that points to local variable 'b' that will be invalid when returning. [returnDanglingLifetime]\n"
4121+
"[test.cpp:12:17] -> [test.cpp:11:9] -> [test.cpp:12:16]: (error) Returning object that points to local variable 'a' that will be invalid when returning. [returnDanglingLifetime]\n",
4122+
errout_str());
41044123
}
41054124

41064125
void danglingLifetimeUserConstructor()

0 commit comments

Comments
 (0)