reduced scope of some variables and avoided some copies (#4328)

This commit is contained in:
Oliver Stöneberg 2022-08-16 22:28:39 +02:00 committed by GitHub
parent 32c0167eab
commit 98b9f2cbf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 59 deletions

View File

@ -618,9 +618,7 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam
alloc = CheckMemoryLeak::Many; alloc = CheckMemoryLeak::Many;
if (alloc != CheckMemoryLeak::Many && memberDealloc != CheckMemoryLeak::No && memberDealloc != CheckMemoryLeak::Many && memberDealloc != alloc) { if (alloc != CheckMemoryLeak::Many && memberDealloc != CheckMemoryLeak::No && memberDealloc != CheckMemoryLeak::Many && memberDealloc != alloc) {
std::list<const Token *> callstack; mismatchAllocDealloc({tok}, classname + "::" + varname);
callstack.push_back(tok);
mismatchAllocDealloc(callstack, classname + "::" + varname);
} }
memberAlloc = alloc; memberAlloc = alloc;
@ -645,9 +643,7 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam
dealloc = CheckMemoryLeak::Many; dealloc = CheckMemoryLeak::Many;
if (dealloc != CheckMemoryLeak::Many && memberAlloc != CheckMemoryLeak::No && memberAlloc != Many && memberAlloc != dealloc) { if (dealloc != CheckMemoryLeak::Many && memberAlloc != CheckMemoryLeak::No && memberAlloc != Many && memberAlloc != dealloc) {
std::list<const Token *> callstack; mismatchAllocDealloc({tok}, classname + "::" + varname);
callstack.push_back(tok);
mismatchAllocDealloc(callstack, classname + "::" + varname);
} }
memberDealloc = dealloc; memberDealloc = dealloc;

View File

@ -400,14 +400,13 @@ static bool reportClangErrors(std::istream &is, std::function<void(const ErrorMe
const std::string colnr = line.substr(pos2+1, pos3-pos2-1); const std::string colnr = line.substr(pos2+1, pos3-pos2-1);
const std::string msg = line.substr(line.find(":", pos3+1) + 2); const std::string msg = line.substr(line.find(":", pos3+1) + 2);
std::list<ErrorMessage::FileLocation> locationList; const std::string locFile = Path::toNativeSeparators(filename);
ErrorMessage::FileLocation loc; ErrorMessage::FileLocation loc;
loc.setfile(Path::toNativeSeparators(filename)); loc.setfile(locFile);
loc.line = std::atoi(linenr.c_str()); loc.line = std::atoi(linenr.c_str());
loc.column = std::atoi(colnr.c_str()); loc.column = std::atoi(colnr.c_str());
locationList.push_back(loc); ErrorMessage errmsg({std::move(loc)},
ErrorMessage errmsg(locationList, locFile,
loc.getfile(),
Severity::error, Severity::error,
msg, msg,
"syntaxError", "syntaxError",
@ -933,12 +932,11 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
for (const std::string &s : configurationError) for (const std::string &s : configurationError)
msg += '\n' + s; msg += '\n' + s;
std::list<ErrorMessage::FileLocation> locationList; const std::string locFile = Path::toNativeSeparators(filename);
ErrorMessage::FileLocation loc; ErrorMessage::FileLocation loc;
loc.setfile(Path::toNativeSeparators(filename)); loc.setfile(locFile);
locationList.push_back(loc); ErrorMessage errmsg({std::move(loc)},
ErrorMessage errmsg(locationList, locFile,
loc.getfile(),
Severity::information, Severity::information,
msg, msg,
"noValidConfiguration", "noValidConfiguration",

View File

@ -909,10 +909,8 @@ bool Preprocessor::validateCfg(const std::string &cfg, const std::list<simplecpp
void Preprocessor::validateCfgError(const std::string &file, const unsigned int line, const std::string &cfg, const std::string &macro) void Preprocessor::validateCfgError(const std::string &file, const unsigned int line, const std::string &cfg, const std::string &macro)
{ {
const std::string id = "ConfigurationNotChecked"; const std::string id = "ConfigurationNotChecked";
std::list<ErrorMessage::FileLocation> locationList; ErrorMessage::FileLocation loc(file, line, 0);
const ErrorMessage::FileLocation loc(file, line, 0); const ErrorMessage errmsg({std::move(loc)}, mFile0, Severity::information, "Skipping configuration '" + cfg + "' since the value of '" + macro + "' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.", id, Certainty::normal);
locationList.push_back(loc);
const ErrorMessage errmsg(locationList, mFile0, Severity::information, "Skipping configuration '" + cfg + "' since the value of '" + macro + "' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.", id, Certainty::normal);
mErrorLogger->reportInfo(errmsg); mErrorLogger->reportInfo(errmsg);
} }

View File

@ -4832,20 +4832,20 @@ static void valueFlowAfterMove(TokenList* tokenlist, SymbolDatabase* symboldatab
Token * varTok; Token * varTok;
if (Token::Match(tok, "%var% . reset|clear (") && tok->next()->originalName().empty()) { if (Token::Match(tok, "%var% . reset|clear (") && tok->next()->originalName().empty()) {
varTok = tok; varTok = tok;
const Variable *var = tok->variable();
if (!var || (!var->isLocal() && !var->isArgument()))
continue;
ValueFlow::Value value; ValueFlow::Value value;
value.valueType = ValueFlow::Value::ValueType::MOVED; value.valueType = ValueFlow::Value::ValueType::MOVED;
value.moveKind = ValueFlow::Value::MoveKind::NonMovedVariable; value.moveKind = ValueFlow::Value::MoveKind::NonMovedVariable;
value.errorPath.emplace_back(tok, "Calling " + tok->next()->expressionString() + " makes " + tok->str() + " 'non-moved'"); value.errorPath.emplace_back(tok, "Calling " + tok->next()->expressionString() + " makes " + tok->str() + " 'non-moved'");
value.setKnown(); value.setKnown();
std::list<ValueFlow::Value> values;
values.push_back(value);
const Variable *var = varTok->variable(); setTokenValue(tok, value, settings);
if (!var || (!var->isLocal() && !var->isArgument()))
continue;
const Token * const endOfVarScope = var->scope()->bodyEnd; const Token * const endOfVarScope = var->scope()->bodyEnd;
setTokenValue(varTok, value, settings); valueFlowForward(tok->next(), endOfVarScope, tok, std::move(value), tokenlist);
valueFlowForward(varTok->next(), endOfVarScope, varTok, values, tokenlist);
continue; continue;
} }
ValueFlow::Value::MoveKind moveKind; ValueFlow::Value::MoveKind moveKind;
@ -4868,20 +4868,20 @@ static void valueFlowAfterMove(TokenList* tokenlist, SymbolDatabase* symboldatab
continue; continue;
const Token * const endOfVarScope = var->scope()->bodyEnd; const Token * const endOfVarScope = var->scope()->bodyEnd;
ValueFlow::Value value;
value.valueType = ValueFlow::Value::ValueType::MOVED;
value.moveKind = moveKind;
if (moveKind == ValueFlow::Value::MoveKind::MovedVariable)
value.errorPath.emplace_back(tok, "Calling std::move(" + varTok->str() + ")");
else // if (moveKind == ValueFlow::Value::ForwardedVariable)
value.errorPath.emplace_back(tok, "Calling std::forward(" + varTok->str() + ")");
value.setKnown();
std::list<ValueFlow::Value> values;
values.push_back(value);
const Token * openParentesisOfMove = findOpenParentesisOfMove(varTok); const Token * openParentesisOfMove = findOpenParentesisOfMove(varTok);
const Token * endOfFunctionCall = findEndOfFunctionCallForParameter(openParentesisOfMove); const Token * endOfFunctionCall = findEndOfFunctionCallForParameter(openParentesisOfMove);
if (endOfFunctionCall) if (endOfFunctionCall) {
valueFlowForward(const_cast<Token*>(endOfFunctionCall), endOfVarScope, varTok, values, tokenlist); ValueFlow::Value value;
value.valueType = ValueFlow::Value::ValueType::MOVED;
value.moveKind = moveKind;
if (moveKind == ValueFlow::Value::MoveKind::MovedVariable)
value.errorPath.emplace_back(tok, "Calling std::move(" + varTok->str() + ")");
else // if (moveKind == ValueFlow::Value::ForwardedVariable)
value.errorPath.emplace_back(tok, "Calling std::forward(" + varTok->str() + ")");
value.setKnown();
valueFlowForward(const_cast<Token*>(endOfFunctionCall), endOfVarScope, varTok, std::move(value), tokenlist);
}
} }
} }
} }
@ -6696,12 +6696,11 @@ static void valueFlowForLoopSimplifyAfter(Token* fortok, nonneg int varid, const
endToken = fortok->scope()->bodyEnd; endToken = fortok->scope()->bodyEnd;
Token* blockTok = fortok->linkAt(1)->linkAt(1); Token* blockTok = fortok->linkAt(1)->linkAt(1);
std::list<ValueFlow::Value> values;
values.emplace_back(num);
values.back().errorPath.emplace_back(fortok,"After for loop, " + var->name() + " has value " + values.back().infoString());
if (blockTok != endToken) { if (blockTok != endToken) {
valueFlowForward(blockTok->next(), endToken, vartok, values, tokenlist); ValueFlow::Value v{num};
v.errorPath.emplace_back(fortok,"After for loop, " + var->name() + " has value " + v.infoString());
valueFlowForward(blockTok->next(), endToken, vartok, v, tokenlist);
} }
} }
@ -7790,11 +7789,9 @@ static void valueFlowSmartPointer(TokenList *tokenlist, ErrorLogger * errorLogge
valueFlowForwardAssign(inTok, var, values, constValue, true, tokenlist, errorLogger, settings); valueFlowForwardAssign(inTok, var, values, constValue, true, tokenlist, errorLogger, settings);
} else if (Token::Match(tok, "%var% ;")) { } else if (Token::Match(tok, "%var% ;")) {
std::list<ValueFlow::Value> values;
ValueFlow::Value v(0); ValueFlow::Value v(0);
v.setKnown(); v.setKnown();
values.push_back(v); valueFlowForwardAssign(tok, var, {std::move(v)}, false, true, tokenlist, errorLogger, settings);
valueFlowForwardAssign(tok, var, values, false, true, tokenlist, errorLogger, settings);
} }
} }
} else if (astIsLHS(tok) && Token::Match(tok->astParent(), ". %name% (") && } else if (astIsLHS(tok) && Token::Match(tok->astParent(), ". %name% (") &&
@ -7803,11 +7800,9 @@ static void valueFlowSmartPointer(TokenList *tokenlist, ErrorLogger * errorLogge
Token* ftok = tok->astParent()->tokAt(2); Token* ftok = tok->astParent()->tokAt(2);
if (Token::simpleMatch(tok->astParent(), ". reset (")) { if (Token::simpleMatch(tok->astParent(), ". reset (")) {
if (Token::simpleMatch(ftok, "( )")) { if (Token::simpleMatch(ftok, "( )")) {
std::list<ValueFlow::Value> values;
ValueFlow::Value v(0); ValueFlow::Value v(0);
v.setKnown(); v.setKnown();
values.push_back(v); valueFlowForwardAssign(ftok, tok, vars, {std::move(v)}, false, tokenlist, errorLogger, settings);
valueFlowForwardAssign(ftok, tok, vars, values, false, tokenlist, errorLogger, settings);
} else { } else {
tok->removeValues(std::mem_fn(&ValueFlow::Value::isIntValue)); tok->removeValues(std::mem_fn(&ValueFlow::Value::isIntValue));
Token* inTok = ftok->astOperand2(); Token* inTok = ftok->astOperand2();
@ -7829,11 +7824,9 @@ static void valueFlowSmartPointer(TokenList *tokenlist, ErrorLogger * errorLogge
} }
if (hasParentReset) if (hasParentReset)
continue; continue;
std::list<ValueFlow::Value> values;
ValueFlow::Value v(0); ValueFlow::Value v(0);
v.setKnown(); v.setKnown();
values.push_back(v); valueFlowForwardAssign(ftok, tok, vars, {std::move(v)}, false, tokenlist, errorLogger, settings);
valueFlowForwardAssign(ftok, tok, vars, values, false, tokenlist, errorLogger, settings);
} else if (Token::simpleMatch(tok->astParent(), ". get ( )")) { } else if (Token::simpleMatch(tok->astParent(), ". get ( )")) {
ValueFlow::Value v = makeSymbolic(tok); ValueFlow::Value v = makeSymbolic(tok);
setTokenValue(tok->astParent()->tokAt(2), v, settings); setTokenValue(tok->astParent()->tokAt(2), v, settings);
@ -8125,11 +8118,14 @@ static void valueFlowContainerSize(TokenList* tokenlist,
values = getContainerSizeFromConstructor(constructorArgs, var->valueType(), settings, known); values = getContainerSizeFromConstructor(constructorArgs, var->valueType(), settings, known);
} }
} }
if (constSize) {
valueFlowForwardConst(var->nameToken()->next(), var->scope()->bodyEnd, var, values, settings);
continue;
}
for (const ValueFlow::Value& value : values) { for (const ValueFlow::Value& value : values) {
if (constSize) valueFlowForward(var->nameToken()->next(), var->nameToken(), value, tokenlist);
valueFlowForwardConst(var->nameToken()->next(), var->scope()->bodyEnd, var, values, settings);
else
valueFlowForward(var->nameToken()->next(), var->nameToken(), value, tokenlist);
} }
} }
@ -8348,8 +8344,7 @@ static void valueFlowDynamicBufferSize(TokenList* tokenlist, SymbolDatabase* sym
value.errorPath.emplace_back(tok->tokAt(2), "Assign " + tok->strAt(1) + ", buffer with size " + MathLib::toString(sizeValue)); value.errorPath.emplace_back(tok->tokAt(2), "Assign " + tok->strAt(1) + ", buffer with size " + MathLib::toString(sizeValue));
value.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE; value.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE;
value.setKnown(); value.setKnown();
const std::list<ValueFlow::Value> values{value}; valueFlowForward(const_cast<Token*>(rhs), functionScope->bodyEnd, tok->next(), std::move(value), tokenlist);
valueFlowForward(const_cast<Token*>(rhs), functionScope->bodyEnd, tok->next(), values, tokenlist);
} }
} }
} }