reduced scope of some variables and avoided some copies (#4328)
This commit is contained in:
parent
32c0167eab
commit
98b9f2cbf1
|
@ -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;
|
||||||
|
|
|
@ -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",
|
||||||
|
|
|
@ -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 ¯o)
|
void Preprocessor::validateCfgError(const std::string &file, const unsigned int line, const std::string &cfg, const std::string ¯o)
|
||||||
{
|
{
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,6 +4868,9 @@ static void valueFlowAfterMove(TokenList* tokenlist, SymbolDatabase* symboldatab
|
||||||
continue;
|
continue;
|
||||||
const Token * const endOfVarScope = var->scope()->bodyEnd;
|
const Token * const endOfVarScope = var->scope()->bodyEnd;
|
||||||
|
|
||||||
|
const Token * openParentesisOfMove = findOpenParentesisOfMove(varTok);
|
||||||
|
const Token * endOfFunctionCall = findEndOfFunctionCallForParameter(openParentesisOfMove);
|
||||||
|
if (endOfFunctionCall) {
|
||||||
ValueFlow::Value value;
|
ValueFlow::Value value;
|
||||||
value.valueType = ValueFlow::Value::ValueType::MOVED;
|
value.valueType = ValueFlow::Value::ValueType::MOVED;
|
||||||
value.moveKind = moveKind;
|
value.moveKind = moveKind;
|
||||||
|
@ -4876,12 +4879,9 @@ static void valueFlowAfterMove(TokenList* tokenlist, SymbolDatabase* symboldatab
|
||||||
else // if (moveKind == ValueFlow::Value::ForwardedVariable)
|
else // if (moveKind == ValueFlow::Value::ForwardedVariable)
|
||||||
value.errorPath.emplace_back(tok, "Calling std::forward(" + varTok->str() + ")");
|
value.errorPath.emplace_back(tok, "Calling std::forward(" + varTok->str() + ")");
|
||||||
value.setKnown();
|
value.setKnown();
|
||||||
std::list<ValueFlow::Value> values;
|
|
||||||
values.push_back(value);
|
valueFlowForward(const_cast<Token*>(endOfFunctionCall), endOfVarScope, varTok, std::move(value), tokenlist);
|
||||||
const Token * openParentesisOfMove = findOpenParentesisOfMove(varTok);
|
}
|
||||||
const Token * endOfFunctionCall = findEndOfFunctionCallForParameter(openParentesisOfMove);
|
|
||||||
if (endOfFunctionCall)
|
|
||||||
valueFlowForward(const_cast<Token*>(endOfFunctionCall), endOfVarScope, varTok, values, 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,10 +8118,13 @@ static void valueFlowContainerSize(TokenList* tokenlist,
|
||||||
values = getContainerSizeFromConstructor(constructorArgs, var->valueType(), settings, known);
|
values = getContainerSizeFromConstructor(constructorArgs, var->valueType(), settings, known);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const ValueFlow::Value& value : values) {
|
|
||||||
if (constSize)
|
if (constSize) {
|
||||||
valueFlowForwardConst(var->nameToken()->next(), var->scope()->bodyEnd, var, values, settings);
|
valueFlowForwardConst(var->nameToken()->next(), var->scope()->bodyEnd, var, values, settings);
|
||||||
else
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const ValueFlow::Value& value : values) {
|
||||||
valueFlowForward(var->nameToken()->next(), var->nameToken(), value, tokenlist);
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue