Skip to content

Commit eef052c

Browse files
committed
valueflow.cpp: removed need for LifetimeStore::Context
1 parent e27d171 commit eef052c

1 file changed

Lines changed: 37 additions & 28 deletions

File tree

lib/valueflow.cpp

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,13 +4051,6 @@ struct LifetimeStore {
40514051
bool inconclusive{};
40524052
bool forward = true;
40534053

4054-
struct Context {
4055-
Token* tok{};
4056-
TokenList* tokenlist{};
4057-
ErrorLogger* errorLogger{};
4058-
const Settings* settings{};
4059-
};
4060-
40614054
LifetimeStore() = default;
40624055

40634056
LifetimeStore(const Token* argtok,
@@ -4071,23 +4064,23 @@ struct LifetimeStore {
40714064
{}
40724065

40734066
template<class F>
4074-
static void forEach(const std::vector<const Token*>& argtoks,
4067+
static void forEach(TokenList& tokenlist,
4068+
ErrorLogger& errorLogger,
4069+
const Settings& settings,
4070+
const std::vector<const Token*>& argtoks,
40754071
const std::string& message,
40764072
ValueFlow::Value::LifetimeKind type,
40774073
F f) {
4078-
std::map<const Token*, Context> forwardToks;
4074+
std::set<Token*> forwardToks;
40794075
for (const Token* arg : argtoks) {
40804076
LifetimeStore ls{arg, message, type};
4081-
Context c{};
4082-
ls.mContext = &c;
40834077
ls.forward = false;
40844078
f(ls);
4085-
if (c.tok)
4086-
forwardToks[c.tok] = c;
4079+
if (ls.forwardTok)
4080+
forwardToks.emplace(ls.forwardTok);
40874081
}
4088-
for (const auto& p : forwardToks) {
4089-
const Context& c = p.second;
4090-
valueFlowForwardLifetime(c.tok, *c.tokenlist, *c.errorLogger, *c.settings);
4082+
for (auto* tok : forwardToks) {
4083+
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
40914084
}
40924085
}
40934086

@@ -4323,14 +4316,9 @@ struct LifetimeStore {
43234316
}
43244317

43254318
private:
4326-
Context* mContext{};
4319+
mutable Token* forwardTok{};
43274320
void forwardLifetime(Token* tok, TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings) const {
4328-
if (mContext) {
4329-
mContext->tok = tok;
4330-
mContext->tokenlist = &tokenlist;
4331-
mContext->errorLogger = &errorLogger;
4332-
mContext->settings = &settings;
4333-
}
4321+
forwardTok = tok;
43344322
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
43354323
}
43364324
};
@@ -4424,7 +4412,10 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
44244412
}
44254413
}
44264414
// TODO: Use SubExpressionAnalyzer for members
4427-
LifetimeStore::forEach(args,
4415+
LifetimeStore::forEach(tokenlist,
4416+
errorLogger,
4417+
settings,
4418+
args,
44284419
"Passed to constructor of '" + name + "'.",
44294420
ValueFlow::Value::LifetimeKind::SubObject,
44304421
[&](const LifetimeStore& ls) {
@@ -4438,7 +4429,10 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
44384429
ls.byVal(tok, tokenlist, errorLogger, settings);
44394430
});
44404431
} else if (hasBorrowingVariables(constructor->nestedIn->varlist, args)) {
4441-
LifetimeStore::forEach(args,
4432+
LifetimeStore::forEach(tokenlist,
4433+
errorLogger,
4434+
settings,
4435+
args,
44424436
"Passed to constructor of '" + name + "'.",
44434437
ValueFlow::Value::LifetimeKind::SubObject,
44444438
[&](LifetimeStore& ls) {
@@ -4659,7 +4653,10 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
46594653
// If the type is unknown then assume it captures by value in the
46604654
// constructor, but make each lifetime inconclusive
46614655
std::vector<const Token*> args = getArguments(tok);
4662-
LifetimeStore::forEach(args,
4656+
LifetimeStore::forEach(tokenlist,
4657+
errorLogger,
4658+
settings,
4659+
args,
46634660
"Passed to initializer list.",
46644661
ValueFlow::Value::LifetimeKind::SubObject,
46654662
[&](LifetimeStore& ls) {
@@ -4677,6 +4674,9 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
46774674
if (scope->numConstructors == 0) {
46784675
auto it = scope->varlist.cbegin();
46794676
LifetimeStore::forEach(
4677+
tokenlist,
4678+
errorLogger,
4679+
settings,
46804680
args,
46814681
"Passed to constructor of '" + t->name() + "'.",
46824682
ValueFlow::Value::LifetimeKind::SubObject,
@@ -4721,7 +4721,10 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, Error
47214721
for (const ValueType& vt : vts) {
47224722
if (vt.pointer > 0) {
47234723
std::vector<const Token*> args = getArguments(tok);
4724-
LifetimeStore::forEach(args,
4724+
LifetimeStore::forEach(tokenlist,
4725+
errorLogger,
4726+
settings,
4727+
args,
47254728
"Passed to initializer list.",
47264729
ValueFlow::Value::LifetimeKind::SubObject,
47274730
[&](const LifetimeStore& ls) {
@@ -4734,14 +4737,20 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, Error
47344737
.byRef(tok, tokenlist, errorLogger, settings);
47354738
} else if (args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1])) {
47364739
LifetimeStore::forEach(
4740+
tokenlist,
4741+
errorLogger,
4742+
settings,
47374743
args,
47384744
"Passed to initializer list.",
47394745
ValueFlow::Value::LifetimeKind::SubObject,
47404746
[&](const LifetimeStore& ls) {
47414747
ls.byDerefCopy(tok, tokenlist, errorLogger, settings);
47424748
});
47434749
} else if (vt.container->hasInitializerListConstructor) {
4744-
LifetimeStore::forEach(args,
4750+
LifetimeStore::forEach(tokenlist,
4751+
errorLogger,
4752+
settings,
4753+
args,
47454754
"Passed to initializer list.",
47464755
ValueFlow::Value::LifetimeKind::SubObject,
47474756
[&](const LifetimeStore& ls) {

0 commit comments

Comments
 (0)