Skip to content

Commit

Permalink
valueflow.cpp: removed need for LifetimeStore::Context [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Mar 9, 2024
1 parent 498bd15 commit 38c8092
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4055,13 +4055,6 @@ struct LifetimeStore {
bool inconclusive{};
bool forward = true;

struct Context {
Token* tok{};
TokenList* tokenlist{};
ErrorLogger* errorLogger{};
const Settings* settings{};
};

LifetimeStore() = default;

LifetimeStore(const Token* argtok,
Expand All @@ -4075,23 +4068,23 @@ struct LifetimeStore {
{}

template<class F>
static void forEach(const std::vector<const Token*>& argtoks,
static void forEach(TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
const std::vector<const Token*>& argtoks,
const std::string& message,
ValueFlow::Value::LifetimeKind type,
F f) {
std::map<const Token*, Context> forwardToks;
std::set<Token*> forwardToks;
for (const Token* arg : argtoks) {
LifetimeStore ls{arg, message, type};
Context c{};
ls.mContext = &c;
ls.forward = false;
f(ls);
if (c.tok)
forwardToks[c.tok] = c;
if (ls.forwardTok)
forwardToks.emplace(ls.forwardTok);
}
for (const auto& p : forwardToks) {
const Context& c = p.second;
valueFlowForwardLifetime(c.tok, *c.tokenlist, *c.errorLogger, *c.settings);
for (auto* tok : forwardToks) {
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
}
}

Expand Down Expand Up @@ -4327,14 +4320,9 @@ struct LifetimeStore {
}

private:
Context* mContext{};
mutable Token* forwardTok{};
void forwardLifetime(Token* tok, TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings) const {
if (mContext) {
mContext->tok = tok;
mContext->tokenlist = &tokenlist;
mContext->errorLogger = &errorLogger;
mContext->settings = &settings;
}
forwardTok = tok;
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
}
};
Expand Down Expand Up @@ -4428,7 +4416,10 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
}
}
// TODO: Use SubExpressionAnalyzer for members
LifetimeStore::forEach(args,
LifetimeStore::forEach(tokenlist,
errorLogger,
settings,
args,
"Passed to constructor of '" + name + "'.",
ValueFlow::Value::LifetimeKind::SubObject,
[&](const LifetimeStore& ls) {
Expand All @@ -4442,7 +4433,10 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
ls.byVal(tok, tokenlist, errorLogger, settings);
});
} else if (hasBorrowingVariables(constructor->nestedIn->varlist, args)) {
LifetimeStore::forEach(args,
LifetimeStore::forEach(tokenlist,
errorLogger,
settings,
args,
"Passed to constructor of '" + name + "'.",
ValueFlow::Value::LifetimeKind::SubObject,
[&](LifetimeStore& ls) {
Expand Down Expand Up @@ -4663,7 +4657,10 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
// If the type is unknown then assume it captures by value in the
// constructor, but make each lifetime inconclusive
std::vector<const Token*> args = getArguments(tok);
LifetimeStore::forEach(args,
LifetimeStore::forEach(tokenlist,
errorLogger,
settings,
args,
"Passed to initializer list.",
ValueFlow::Value::LifetimeKind::SubObject,
[&](LifetimeStore& ls) {
Expand All @@ -4681,6 +4678,9 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
if (scope->numConstructors == 0) {
auto it = scope->varlist.cbegin();
LifetimeStore::forEach(
tokenlist,
errorLogger,
settings,
args,
"Passed to constructor of '" + t->name() + "'.",
ValueFlow::Value::LifetimeKind::SubObject,
Expand Down Expand Up @@ -4725,7 +4725,10 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, Error
for (const ValueType& vt : vts) {
if (vt.pointer > 0) {
std::vector<const Token*> args = getArguments(tok);
LifetimeStore::forEach(args,
LifetimeStore::forEach(tokenlist,
errorLogger,
settings,
args,
"Passed to initializer list.",
ValueFlow::Value::LifetimeKind::SubObject,
[&](const LifetimeStore& ls) {
Expand All @@ -4738,14 +4741,20 @@ static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, Error
.byRef(tok, tokenlist, errorLogger, settings);
} else if (args.size() == 2 && astIsIterator(args[0]) && astIsIterator(args[1])) {
LifetimeStore::forEach(
tokenlist,
errorLogger,
settings,
args,
"Passed to initializer list.",
ValueFlow::Value::LifetimeKind::SubObject,
[&](const LifetimeStore& ls) {
ls.byDerefCopy(tok, tokenlist, errorLogger, settings);
});
} else if (vt.container->hasInitializerListConstructor) {
LifetimeStore::forEach(args,
LifetimeStore::forEach(tokenlist,
errorLogger,
settings,
args,
"Passed to initializer list.",
ValueFlow::Value::LifetimeKind::SubObject,
[&](const LifetimeStore& ls) {
Expand Down

0 comments on commit 38c8092

Please sign in to comment.