Skip to content

Commit

Permalink
valueflow.cpp: removed need for LifetimeStore::Context / made `Toke…
Browse files Browse the repository at this point in the history
…nList` const in more places [skip ci]
  • Loading branch information
firewave committed Mar 9, 2024
1 parent 498bd15 commit ed3cb49
Showing 1 changed file with 51 additions and 41 deletions.
92 changes: 51 additions & 41 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3875,10 +3875,10 @@ bool ValueFlow::isLifetimeBorrowed(const Token *tok, const Settings &settings)
return true;
}

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

static void valueFlowLifetimeConstructor(Token *tok,
TokenList &tokenlist,
const TokenList &tokenlist,
ErrorLogger &errorLogger,
const Settings &settings);

Expand Down Expand Up @@ -3957,7 +3957,7 @@ const Token* ValueFlow::getEndOfExprScope(const Token* tok, const Scope* default
return end;
}

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

template<class F>
static void forEach(const std::vector<const Token*>& argtoks,
static void forEach(const 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::map<const Token*, 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(arg, ls.forwardTok);
}
for (const auto& p : forwardToks) {
const Context& c = p.second;
valueFlowForwardLifetime(c.tok, *c.tokenlist, *c.errorLogger, *c.settings);
const auto tok = p.second;
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
}
}

Expand Down Expand Up @@ -4120,7 +4114,7 @@ struct LifetimeStore {

template<class Predicate>
bool byRef(Token* tok,
TokenList& tokenlist,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
Predicate pred,
Expand Down Expand Up @@ -4161,7 +4155,7 @@ struct LifetimeStore {
}

bool byRef(Token* tok,
TokenList& tokenlist,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current()) const
Expand All @@ -4179,7 +4173,7 @@ struct LifetimeStore {

template<class Predicate>
bool byVal(Token* tok,
TokenList& tokenlist,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
Predicate pred,
Expand Down Expand Up @@ -4256,7 +4250,7 @@ struct LifetimeStore {
}

bool byVal(Token* tok,
TokenList& tokenlist,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current()) const
Expand All @@ -4274,7 +4268,7 @@ struct LifetimeStore {

template<class Predicate>
bool byDerefCopy(Token* tok,
TokenList& tokenlist,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
Predicate pred,
Expand Down Expand Up @@ -4310,7 +4304,7 @@ struct LifetimeStore {
}

bool byDerefCopy(Token* tok,
TokenList& tokenlist,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current()) const
Expand All @@ -4327,14 +4321,9 @@ struct LifetimeStore {
}

private:
Context* mContext{};
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;
}
mutable Token* forwardTok{};
void forwardLifetime(Token* tok, const TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings) const {
forwardTok = tok;
valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings);
}
};
Expand Down Expand Up @@ -4372,7 +4361,7 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
const Function* constructor,
const std::string& name,
const std::vector<const Token*>& args,
TokenList& tokenlist,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings)
{
Expand Down Expand Up @@ -4428,7 +4417,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 +4434,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 All @@ -4456,7 +4451,7 @@ static void valueFlowLifetimeUserConstructor(Token* tok,
}
}

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

static void valueFlowLifetimeClassConstructor(Token* tok,
const Type* t,
TokenList& tokenlist,
const TokenList& tokenlist,
ErrorLogger& errorLogger,
const Settings& settings)
{
Expand All @@ -4663,7 +4658,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 +4679,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 All @@ -4706,7 +4707,7 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
}
}

static void valueFlowLifetimeConstructor(Token* tok, TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings)
static void valueFlowLifetimeConstructor(Token* tok, const TokenList& tokenlist, ErrorLogger& errorLogger, const Settings& settings)
{
if (!Token::Match(tok, "(|{"))
return;
Expand All @@ -4725,7 +4726,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 +4742,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 ed3cb49

Please sign in to comment.