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;
|
||||
|
||||
if (alloc != CheckMemoryLeak::Many && memberDealloc != CheckMemoryLeak::No && memberDealloc != CheckMemoryLeak::Many && memberDealloc != alloc) {
|
||||
std::list<const Token *> callstack;
|
||||
callstack.push_back(tok);
|
||||
mismatchAllocDealloc(callstack, classname + "::" + varname);
|
||||
mismatchAllocDealloc({tok}, classname + "::" + varname);
|
||||
}
|
||||
|
||||
memberAlloc = alloc;
|
||||
|
@ -645,9 +643,7 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam
|
|||
dealloc = CheckMemoryLeak::Many;
|
||||
|
||||
if (dealloc != CheckMemoryLeak::Many && memberAlloc != CheckMemoryLeak::No && memberAlloc != Many && memberAlloc != dealloc) {
|
||||
std::list<const Token *> callstack;
|
||||
callstack.push_back(tok);
|
||||
mismatchAllocDealloc(callstack, classname + "::" + varname);
|
||||
mismatchAllocDealloc({tok}, classname + "::" + varname);
|
||||
}
|
||||
|
||||
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 msg = line.substr(line.find(":", pos3+1) + 2);
|
||||
|
||||
std::list<ErrorMessage::FileLocation> locationList;
|
||||
const std::string locFile = Path::toNativeSeparators(filename);
|
||||
ErrorMessage::FileLocation loc;
|
||||
loc.setfile(Path::toNativeSeparators(filename));
|
||||
loc.setfile(locFile);
|
||||
loc.line = std::atoi(linenr.c_str());
|
||||
loc.column = std::atoi(colnr.c_str());
|
||||
locationList.push_back(loc);
|
||||
ErrorMessage errmsg(locationList,
|
||||
loc.getfile(),
|
||||
ErrorMessage errmsg({std::move(loc)},
|
||||
locFile,
|
||||
Severity::error,
|
||||
msg,
|
||||
"syntaxError",
|
||||
|
@ -933,12 +932,11 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
|
|||
for (const std::string &s : configurationError)
|
||||
msg += '\n' + s;
|
||||
|
||||
std::list<ErrorMessage::FileLocation> locationList;
|
||||
const std::string locFile = Path::toNativeSeparators(filename);
|
||||
ErrorMessage::FileLocation loc;
|
||||
loc.setfile(Path::toNativeSeparators(filename));
|
||||
locationList.push_back(loc);
|
||||
ErrorMessage errmsg(locationList,
|
||||
loc.getfile(),
|
||||
loc.setfile(locFile);
|
||||
ErrorMessage errmsg({std::move(loc)},
|
||||
locFile,
|
||||
Severity::information,
|
||||
msg,
|
||||
"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)
|
||||
{
|
||||
const std::string id = "ConfigurationNotChecked";
|
||||
std::list<ErrorMessage::FileLocation> locationList;
|
||||
const ErrorMessage::FileLocation loc(file, line, 0);
|
||||
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);
|
||||
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);
|
||||
mErrorLogger->reportInfo(errmsg);
|
||||
}
|
||||
|
||||
|
|
|
@ -4832,20 +4832,20 @@ static void valueFlowAfterMove(TokenList* tokenlist, SymbolDatabase* symboldatab
|
|||
Token * varTok;
|
||||
if (Token::Match(tok, "%var% . reset|clear (") && tok->next()->originalName().empty()) {
|
||||
varTok = tok;
|
||||
|
||||
const Variable *var = tok->variable();
|
||||
if (!var || (!var->isLocal() && !var->isArgument()))
|
||||
continue;
|
||||
|
||||
ValueFlow::Value value;
|
||||
value.valueType = ValueFlow::Value::ValueType::MOVED;
|
||||
value.moveKind = ValueFlow::Value::MoveKind::NonMovedVariable;
|
||||
value.errorPath.emplace_back(tok, "Calling " + tok->next()->expressionString() + " makes " + tok->str() + " 'non-moved'");
|
||||
value.setKnown();
|
||||
std::list<ValueFlow::Value> values;
|
||||
values.push_back(value);
|
||||
|
||||
const Variable *var = varTok->variable();
|
||||
if (!var || (!var->isLocal() && !var->isArgument()))
|
||||
continue;
|
||||
setTokenValue(tok, value, settings);
|
||||
const Token * const endOfVarScope = var->scope()->bodyEnd;
|
||||
setTokenValue(varTok, value, settings);
|
||||
valueFlowForward(varTok->next(), endOfVarScope, varTok, values, tokenlist);
|
||||
valueFlowForward(tok->next(), endOfVarScope, tok, std::move(value), tokenlist);
|
||||
continue;
|
||||
}
|
||||
ValueFlow::Value::MoveKind moveKind;
|
||||
|
@ -4868,6 +4868,9 @@ static void valueFlowAfterMove(TokenList* tokenlist, SymbolDatabase* symboldatab
|
|||
continue;
|
||||
const Token * const endOfVarScope = var->scope()->bodyEnd;
|
||||
|
||||
const Token * openParentesisOfMove = findOpenParentesisOfMove(varTok);
|
||||
const Token * endOfFunctionCall = findEndOfFunctionCallForParameter(openParentesisOfMove);
|
||||
if (endOfFunctionCall) {
|
||||
ValueFlow::Value value;
|
||||
value.valueType = ValueFlow::Value::ValueType::MOVED;
|
||||
value.moveKind = moveKind;
|
||||
|
@ -4876,12 +4879,9 @@ static void valueFlowAfterMove(TokenList* tokenlist, SymbolDatabase* symboldatab
|
|||
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 * endOfFunctionCall = findEndOfFunctionCallForParameter(openParentesisOfMove);
|
||||
if (endOfFunctionCall)
|
||||
valueFlowForward(const_cast<Token*>(endOfFunctionCall), endOfVarScope, varTok, values, tokenlist);
|
||||
|
||||
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;
|
||||
|
||||
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) {
|
||||
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);
|
||||
|
||||
} else if (Token::Match(tok, "%var% ;")) {
|
||||
std::list<ValueFlow::Value> values;
|
||||
ValueFlow::Value v(0);
|
||||
v.setKnown();
|
||||
values.push_back(v);
|
||||
valueFlowForwardAssign(tok, var, values, false, true, tokenlist, errorLogger, settings);
|
||||
valueFlowForwardAssign(tok, var, {std::move(v)}, false, true, tokenlist, errorLogger, settings);
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
if (Token::simpleMatch(tok->astParent(), ". reset (")) {
|
||||
if (Token::simpleMatch(ftok, "( )")) {
|
||||
std::list<ValueFlow::Value> values;
|
||||
ValueFlow::Value v(0);
|
||||
v.setKnown();
|
||||
values.push_back(v);
|
||||
valueFlowForwardAssign(ftok, tok, vars, values, false, tokenlist, errorLogger, settings);
|
||||
valueFlowForwardAssign(ftok, tok, vars, {std::move(v)}, false, tokenlist, errorLogger, settings);
|
||||
} else {
|
||||
tok->removeValues(std::mem_fn(&ValueFlow::Value::isIntValue));
|
||||
Token* inTok = ftok->astOperand2();
|
||||
|
@ -7829,11 +7824,9 @@ static void valueFlowSmartPointer(TokenList *tokenlist, ErrorLogger * errorLogge
|
|||
}
|
||||
if (hasParentReset)
|
||||
continue;
|
||||
std::list<ValueFlow::Value> values;
|
||||
ValueFlow::Value v(0);
|
||||
v.setKnown();
|
||||
values.push_back(v);
|
||||
valueFlowForwardAssign(ftok, tok, vars, values, false, tokenlist, errorLogger, settings);
|
||||
valueFlowForwardAssign(ftok, tok, vars, {std::move(v)}, false, tokenlist, errorLogger, settings);
|
||||
} else if (Token::simpleMatch(tok->astParent(), ". get ( )")) {
|
||||
ValueFlow::Value v = makeSymbolic(tok);
|
||||
setTokenValue(tok->astParent()->tokAt(2), v, settings);
|
||||
|
@ -8125,10 +8118,13 @@ static void valueFlowContainerSize(TokenList* tokenlist,
|
|||
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);
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const ValueFlow::Value& value : values) {
|
||||
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.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE;
|
||||
value.setKnown();
|
||||
const std::list<ValueFlow::Value> values{value};
|
||||
valueFlowForward(const_cast<Token*>(rhs), functionScope->bodyEnd, tok->next(), values, tokenlist);
|
||||
valueFlowForward(const_cast<Token*>(rhs), functionScope->bodyEnd, tok->next(), std::move(value), tokenlist);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue