Skip to content

Commit ed3cb49

Browse files
committed
valueflow.cpp: removed need for LifetimeStore::Context / made TokenList const in more places [skip ci]
1 parent 498bd15 commit ed3cb49

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed

lib/valueflow.cpp

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,10 +3875,10 @@ bool ValueFlow::isLifetimeBorrowed(const Token *tok, const Settings &settings)
38753875
return true;
38763876
}
38773877

3878-
static void valueFlowLifetimeFunction(Token *tok, TokenList &tokenlist, ErrorLogger &errorLogger, const Settings &settings);
3878+
static void valueFlowLifetimeFunction(Token *tok, const TokenList &tokenlist, ErrorLogger &errorLogger, const Settings &settings);
38793879

38803880
static void valueFlowLifetimeConstructor(Token *tok,
3881-
TokenList &tokenlist,
3881+
const TokenList &tokenlist,
38823882
ErrorLogger &errorLogger,
38833883
const Settings &settings);
38843884

@@ -3957,7 +3957,7 @@ const Token* ValueFlow::getEndOfExprScope(const Token* tok, const Scope* default
39573957
return end;
39583958
}
39593959

3960-
static void valueFlowForwardLifetime(Token * tok, TokenList &tokenlist, ErrorLogger &errorLogger, const Settings &settings)
3960+
static void valueFlowForwardLifetime(Token * tok, const TokenList &tokenlist, ErrorLogger &errorLogger, const Settings &settings)
39613961
{
39623962
// Forward lifetimes to constructed variable
39633963
if (Token::Match(tok->previous(), "%var% {|(") && isVariableDecl(tok->previous())) {
@@ -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,24 @@ struct LifetimeStore {
40754068
{}
40764069

40774070
template<class F>
4078-
static void forEach(const std::vector<const Token*>& argtoks,
4071+
static void forEach(const 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::map<const Token*, 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(arg, ls.forwardTok);
40914085
}
40924086
for (const auto& p : forwardToks) {
4093-
const Context& c = p.second;
4094-
valueFlowForwardLifetime(c.tok, *c.tokenlist, *c.errorLogger, *c.settings);
4087+
const auto tok = p.second;
4088+
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
40954089
}
40964090
}
40974091

@@ -4120,7 +4114,7 @@ struct LifetimeStore {
41204114

41214115
template<class Predicate>
41224116
bool byRef(Token* tok,
4123-
TokenList& tokenlist,
4117+
const TokenList& tokenlist,
41244118
ErrorLogger& errorLogger,
41254119
const Settings& settings,
41264120
Predicate pred,
@@ -4161,7 +4155,7 @@ struct LifetimeStore {
41614155
}
41624156

41634157
bool byRef(Token* tok,
4164-
TokenList& tokenlist,
4158+
const TokenList& tokenlist,
41654159
ErrorLogger& errorLogger,
41664160
const Settings& settings,
41674161
SourceLocation loc = SourceLocation::current()) const
@@ -4179,7 +4173,7 @@ struct LifetimeStore {
41794173

41804174
template<class Predicate>
41814175
bool byVal(Token* tok,
4182-
TokenList& tokenlist,
4176+
const TokenList& tokenlist,
41834177
ErrorLogger& errorLogger,
41844178
const Settings& settings,
41854179
Predicate pred,
@@ -4256,7 +4250,7 @@ struct LifetimeStore {
42564250
}
42574251

42584252
bool byVal(Token* tok,
4259-
TokenList& tokenlist,
4253+
const TokenList& tokenlist,
42604254
ErrorLogger& errorLogger,
42614255
const Settings& settings,
42624256
SourceLocation loc = SourceLocation::current()) const
@@ -4274,7 +4268,7 @@ struct LifetimeStore {
42744268

42754269
template<class Predicate>
42764270
bool byDerefCopy(Token* tok,
4277-
TokenList& tokenlist,
4271+
const TokenList& tokenlist,
42784272
ErrorLogger& errorLogger,
42794273
const Settings& settings,
42804274
Predicate pred,
@@ -4310,7 +4304,7 @@ struct LifetimeStore {
43104304
}
43114305

43124306
bool byDerefCopy(Token* tok,
4313-
TokenList& tokenlist,
4307+
const TokenList& tokenlist,
43144308
ErrorLogger& errorLogger,
43154309
const Settings& settings,
43164310
SourceLocation loc = SourceLocation::current()) const
@@ -4327,14 +4321,9 @@ struct LifetimeStore {
43274321
}
43284322

43294323
private:
4330-
Context* mContext{};
4331-
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-
}
4324+
mutable Token* forwardTok{};
4325+
void forwardLifetime(Token* tok, const TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings) const {
4326+
forwardTok = tok;
43384327
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
43394328
}
43404329
};
@@ -4372,7 +4361,7 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
43724361
const Function* constructor,
43734362
const std::string& name,
43744363
const std::vector<const Token*>& args,
4375-
TokenList& tokenlist,
4364+
const TokenList& tokenlist,
43764365
ErrorLogger& errorLogger,
43774366
const Settings& settings)
43784367
{
@@ -4428,7 +4417,10 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
44284417
}
44294418
}
44304419
// TODO: Use SubExpressionAnalyzer for members
4431-
LifetimeStore::forEach(args,
4420+
LifetimeStore::forEach(tokenlist,
4421+
errorLogger,
4422+
settings,
4423+
args,
44324424
"Passed to constructor of '" + name + "'.",
44334425
ValueFlow::Value::LifetimeKind::SubObject,
44344426
[&](const LifetimeStore& ls) {
@@ -4442,7 +4434,10 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
44424434
ls.byVal(tok, tokenlist, errorLogger, settings);
44434435
});
44444436
} else if (hasBorrowingVariables(constructor->nestedIn->varlist, args)) {
4445-
LifetimeStore::forEach(args,
4437+
LifetimeStore::forEach(tokenlist,
4438+
errorLogger,
4439+
settings,
4440+
args,
44464441
"Passed to constructor of '" + name + "'.",
44474442
ValueFlow::Value::LifetimeKind::SubObject,
44484443
[&](LifetimeStore& ls) {
@@ -4456,7 +4451,7 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
44564451
}
44574452
}
44584453

4459-
static void valueFlowLifetimeFunction(Token *tok, TokenList &tokenlist, ErrorLogger &errorLogger, const Settings &settings)
4454+
static void valueFlowLifetimeFunction(Token *tok, const TokenList &tokenlist, ErrorLogger &errorLogger, const Settings &settings)
44604455
{
44614456
if (!Token::Match(tok, "%name% ("))
44624457
return;
@@ -4647,7 +4642,7 @@ static const Function* findConstructor(const Scope* scope, const Token* tok, con
46474642

46484643
static void valueFlowLifetimeClassConstructor(Token* tok,
46494644
const Type* t,
4650-
TokenList& tokenlist,
4645+
const TokenList& tokenlist,
46514646
ErrorLogger& errorLogger,
46524647
const Settings& settings)
46534648
{
@@ -4663,7 +4658,10 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
46634658
// If the type is unknown then assume it captures by value in the
46644659
// constructor, but make each lifetime inconclusive
46654660
std::vector<const Token*> args = getArguments(tok);
4666-
LifetimeStore::forEach(args,
4661+
LifetimeStore::forEach(tokenlist,
4662+
errorLogger,
4663+
settings,
4664+
args,
46674665
"Passed to initializer list.",
46684666
ValueFlow::Value::LifetimeKind::SubObject,
46694667
[&](LifetimeStore& ls) {
@@ -4681,6 +4679,9 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
46814679
if (scope->numConstructors == 0) {
46824680
auto it = scope->varlist.cbegin();
46834681
LifetimeStore::forEach(
4682+
tokenlist,
4683+
errorLogger,
4684+
settings,
46844685
args,
46854686
"Passed to constructor of '" + t->name() + "'.",
46864687
ValueFlow::Value::LifetimeKind::SubObject,
@@ -4706,7 +4707,7 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
47064707
}
47074708
}
47084709

4709-
static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings)
4710+
static void valueFlowLifetimeConstructor(Token* tok, const TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings)
47104711
{
47114712
if (!Token::Match(tok, "(|{"))
47124713
return;
@@ -4725,7 +4726,10 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, Error
47254726
for (const ValueType& vt : vts) {
47264727
if (vt.pointer > 0) {
47274728
std::vector<const Token*> args = getArguments(tok);
4728-
LifetimeStore::forEach(args,
4729+
LifetimeStore::forEach(tokenlist,
4730+
errorLogger,
4731+
settings,
4732+
args,
47294733
"Passed to initializer list.",
47304734
ValueFlow::Value::LifetimeKind::SubObject,
47314735
[&](const LifetimeStore& ls) {
@@ -4738,14 +4742,20 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, Error
47384742
.byRef(tok, tokenlist, errorLogger, settings);
47394743
} else if (args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1])) {
47404744
LifetimeStore::forEach(
4745+
tokenlist,
4746+
errorLogger,
4747+
settings,
47414748
args,
47424749
"Passed to initializer list.",
47434750
ValueFlow::Value::LifetimeKind::SubObject,
47444751
[&](const LifetimeStore& ls) {
47454752
ls.byDerefCopy(tok, tokenlist, errorLogger, settings);
47464753
});
47474754
} else if (vt.container->hasInitializerListConstructor) {
4748-
LifetimeStore::forEach(args,
4755+
LifetimeStore::forEach(tokenlist,
4756+
errorLogger,
4757+
settings,
4758+
args,
47494759
"Passed to initializer list.",
47504760
ValueFlow::Value::LifetimeKind::SubObject,
47514761
[&](const LifetimeStore& ls) {

0 commit comments

Comments
 (0)