Skip to content

Commit 38c8092

Browse files
committed
valueflow.cpp: removed need for LifetimeStore::Context [skip ci]
1 parent 498bd15 commit 38c8092

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

lib/valueflow.cpp

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,13 +4055,6 @@ struct LifetimeStore {
40554055
bool inconclusive{};
40564056
bool forward = true;
40574057

4058-
struct Context {
4059-
Token* tok{};
4060-
TokenList* tokenlist{};
4061-
ErrorLogger* errorLogger{};
4062-
const Settings* settings{};
4063-
};
4064-
40654058
LifetimeStore() = default;
40664059

40674060
LifetimeStore(const Token* argtok,
@@ -4075,23 +4068,23 @@ struct LifetimeStore {
40754068
{}
40764069

40774070
template<class F>
4078-
static void forEach(const std::vector<const Token*>& argtoks,
4071+
static void forEach(TokenList& tokenlist,
4072+
ErrorLogger& errorLogger,
4073+
const Settings& settings,
4074+
const std::vector<const Token*>& argtoks,
40794075
const std::string& message,
40804076
ValueFlow::Value::LifetimeKind type,
40814077
F f) {
4082-
std::map<const Token*, Context> forwardToks;
4078+
std::set<Token*> forwardToks;
40834079
for (const Token* arg : argtoks) {
40844080
LifetimeStore ls{arg, message, type};
4085-
Context c{};
4086-
ls.mContext = &c;
40874081
ls.forward = false;
40884082
f(ls);
4089-
if (c.tok)
4090-
forwardToks[c.tok] = c;
4083+
if (ls.forwardTok)
4084+
forwardToks.emplace(ls.forwardTok);
40914085
}
4092-
for (const auto& p : forwardToks) {
4093-
const Context& c = p.second;
4094-
valueFlowForwardLifetime(c.tok, *c.tokenlist, *c.errorLogger, *c.settings);
4086+
for (auto* tok : forwardToks) {
4087+
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
40954088
}
40964089
}
40974090

@@ -4327,14 +4320,9 @@ struct LifetimeStore {
43274320
}
43284321

43294322
private:
4330-
Context* mContext{};
4323+
mutable Token* forwardTok{};
43314324
void forwardLifetime(Token* tok, TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings) const {
4332-
if (mContext) {
4333-
mContext->tok = tok;
4334-
mContext->tokenlist = &tokenlist;
4335-
mContext->errorLogger = &errorLogger;
4336-
mContext->settings = &settings;
4337-
}
4325+
forwardTok = tok;
43384326
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
43394327
}
43404328
};
@@ -4428,7 +4416,10 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
44284416
}
44294417
}
44304418
// TODO: Use SubExpressionAnalyzer for members
4431-
LifetimeStore::forEach(args,
4419+
LifetimeStore::forEach(tokenlist,
4420+
errorLogger,
4421+
settings,
4422+
args,
44324423
"Passed to constructor of '" + name + "'.",
44334424
ValueFlow::Value::LifetimeKind::SubObject,
44344425
[&](const LifetimeStore& ls) {
@@ -4442,7 +4433,10 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
44424433
ls.byVal(tok, tokenlist, errorLogger, settings);
44434434
});
44444435
} else if (hasBorrowingVariables(constructor->nestedIn->varlist, args)) {
4445-
LifetimeStore::forEach(args,
4436+
LifetimeStore::forEach(tokenlist,
4437+
errorLogger,
4438+
settings,
4439+
args,
44464440
"Passed to constructor of '" + name + "'.",
44474441
ValueFlow::Value::LifetimeKind::SubObject,
44484442
[&](LifetimeStore& ls) {
@@ -4663,7 +4657,10 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
46634657
// If the type is unknown then assume it captures by value in the
46644658
// constructor, but make each lifetime inconclusive
46654659
std::vector<const Token*> args = getArguments(tok);
4666-
LifetimeStore::forEach(args,
4660+
LifetimeStore::forEach(tokenlist,
4661+
errorLogger,
4662+
settings,
4663+
args,
46674664
"Passed to initializer list.",
46684665
ValueFlow::Value::LifetimeKind::SubObject,
46694666
[&](LifetimeStore& ls) {
@@ -4681,6 +4678,9 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
46814678
if (scope->numConstructors == 0) {
46824679
auto it = scope->varlist.cbegin();
46834680
LifetimeStore::forEach(
4681+
tokenlist,
4682+
errorLogger,
4683+
settings,
46844684
args,
46854685
"Passed to constructor of '" + t->name() + "'.",
46864686
ValueFlow::Value::LifetimeKind::SubObject,
@@ -4725,7 +4725,10 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, Error
47254725
for (const ValueType& vt : vts) {
47264726
if (vt.pointer > 0) {
47274727
std::vector<const Token*> args = getArguments(tok);
4728-
LifetimeStore::forEach(args,
4728+
LifetimeStore::forEach(tokenlist,
4729+
errorLogger,
4730+
settings,
4731+
args,
47294732
"Passed to initializer list.",
47304733
ValueFlow::Value::LifetimeKind::SubObject,
47314734
[&](const LifetimeStore& ls) {
@@ -4738,14 +4741,20 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, Error
47384741
.byRef(tok, tokenlist, errorLogger, settings);
47394742
} else if (args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1])) {
47404743
LifetimeStore::forEach(
4744+
tokenlist,
4745+
errorLogger,
4746+
settings,
47414747
args,
47424748
"Passed to initializer list.",
47434749
ValueFlow::Value::LifetimeKind::SubObject,
47444750
[&](const LifetimeStore& ls) {
47454751
ls.byDerefCopy(tok, tokenlist, errorLogger, settings);
47464752
});
47474753
} else if (vt.container->hasInitializerListConstructor) {
4748-
LifetimeStore::forEach(args,
4754+
LifetimeStore::forEach(tokenlist,
4755+
errorLogger,
4756+
settings,
4757+
args,
47494758
"Passed to initializer list.",
47504759
ValueFlow::Value::LifetimeKind::SubObject,
47514760
[&](const LifetimeStore& ls) {

0 commit comments

Comments
 (0)