From a338be46823da291e67d66c389ad017f65708a02 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 3 Jan 2023 16:02:48 -0500 Subject: [PATCH] Add source traces when using LifetimeStore (#4678) --- lib/valueflow.cpp | 84 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 14 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c5097b9b5..c9e6aae48 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3925,7 +3925,13 @@ struct LifetimeStore { } template - bool byRef(Token* tok, TokenList* tokenlist, ErrorLogger* errorLogger, const Settings* settings, Predicate pred) const { + bool byRef(Token* tok, + TokenList* tokenlist, + ErrorLogger* errorLogger, + const Settings* settings, + Predicate pred, + SourceLocation loc = SourceLocation::current()) const + { if (!argtok) return false; bool update = false; @@ -3950,6 +3956,8 @@ struct LifetimeStore { // Don't add the value a second time if (std::find(tok->values().cbegin(), tok->values().cend(), value) != tok->values().cend()) return false; + if (settings->debugnormal) + setSourceLocation(value, loc, tok); setTokenValue(tok, value, tokenlist->getSettings()); update = true; } @@ -3958,14 +3966,31 @@ struct LifetimeStore { return update; } - bool byRef(Token* tok, TokenList* tokenlist, ErrorLogger* errorLogger, const Settings* settings) const { - return byRef(tok, tokenlist, errorLogger, settings, [](const Token*) { + bool byRef(Token* tok, + TokenList* tokenlist, + ErrorLogger* errorLogger, + const Settings* settings, + SourceLocation loc = SourceLocation::current()) const + { + return byRef( + tok, + tokenlist, + errorLogger, + settings, + [](const Token*) { return true; - }); + }, + loc); } template - bool byVal(Token* tok, TokenList* tokenlist, ErrorLogger* errorLogger, const Settings* settings, Predicate pred) const { + bool byVal(Token* tok, + TokenList* tokenlist, + ErrorLogger* errorLogger, + const Settings* settings, + Predicate pred, + SourceLocation loc = SourceLocation::current()) const + { if (!argtok) return false; bool update = false; @@ -3991,7 +4016,8 @@ struct LifetimeStore { // Don't add the value a second time if (std::find(tok->values().cbegin(), tok->values().cend(), value) != tok->values().cend()) continue; - + if (settings->debugnormal) + setSourceLocation(value, loc, tok); setTokenValue(tok, value, tokenlist->getSettings()); update = true; } @@ -4024,6 +4050,8 @@ struct LifetimeStore { // Don't add the value a second time if (std::find(tok->values().cbegin(), tok->values().cend(), value) != tok->values().cend()) continue; + if (settings->debugnormal) + setSourceLocation(value, loc, tok); setTokenValue(tok, value, tokenlist->getSettings()); update = true; } @@ -4033,14 +4061,31 @@ struct LifetimeStore { return update; } - bool byVal(Token* tok, TokenList* tokenlist, ErrorLogger* errorLogger, const Settings* settings) const { - return byVal(tok, tokenlist, errorLogger, settings, [](const Token*) { + bool byVal(Token* tok, + TokenList* tokenlist, + ErrorLogger* errorLogger, + const Settings* settings, + SourceLocation loc = SourceLocation::current()) const + { + return byVal( + tok, + tokenlist, + errorLogger, + settings, + [](const Token*) { return true; - }); + }, + loc); } template - void byDerefCopy(Token *tok, TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings, Predicate pred) const { + void byDerefCopy(Token* tok, + TokenList* tokenlist, + ErrorLogger* errorLogger, + const Settings* settings, + Predicate pred, + SourceLocation loc = SourceLocation::current()) const + { if (!settings->certainty.isEnabled(Certainty::inconclusive) && inconclusive) return; if (!argtok) @@ -4060,17 +4105,28 @@ struct LifetimeStore { const Token * const varDeclEndToken = var->declEndToken(); for (const Token *tok3 = tok; tok3 && tok3 != varDeclEndToken; tok3 = tok3->previous()) { if (tok3->varId() == var->declarationId()) { - LifetimeStore{tok3, message, type, inconclusive}.byVal(tok, tokenlist, errorLogger, settings, pred); + LifetimeStore{tok3, message, type, inconclusive}.byVal(tok, tokenlist, errorLogger, settings, pred, loc); break; } } } } - void byDerefCopy(Token *tok, TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings) const { - byDerefCopy(tok, tokenlist, errorLogger, settings, [](const Token *) { + void byDerefCopy(Token* tok, + TokenList* tokenlist, + ErrorLogger* errorLogger, + const Settings* settings, + SourceLocation loc = SourceLocation::current()) const + { + byDerefCopy( + tok, + tokenlist, + errorLogger, + settings, + [](const Token*) { return true; - }); + }, + loc); } private: