Refactorization: Replace several push_back-sequences by initializer lists

This commit is contained in:
PKEuS 2018-04-09 09:54:39 +02:00
parent bbfcccf078
commit b15cc3f236
7 changed files with 33 additions and 58 deletions

View File

@ -718,8 +718,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<const st
// memset, memcmp, memcpy, strncpy, fgets.. // memset, memcmp, memcpy, strncpy, fgets..
if (declarationId == 0 && Token::Match(tok, "%name% ( !!)")) { if (declarationId == 0 && Token::Match(tok, "%name% ( !!)")) {
std::list<const Token *> callstack; std::list<const Token *> callstack(1, tok);
callstack.push_back(tok);
const Token* tok2 = tok->tokAt(2); const Token* tok2 = tok->tokAt(2);
if (Token::Match(tok2, (varnames + " ,").c_str())) if (Token::Match(tok2, (varnames + " ,").c_str()))
checkFunctionParameter(*tok, 1, arrayInfo, callstack); checkFunctionParameter(*tok, 1, arrayInfo, callstack);
@ -1194,9 +1193,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
if (index < (isAddressOf(tok) ? elements + 1U : elements)) if (index < (isAddressOf(tok) ? elements + 1U : elements))
continue; continue;
std::list<const Token *> callstack; std::list<const Token *> callstack = { it->tokvalue, tok };
callstack.push_back(it->tokvalue);
callstack.push_back(tok);
std::vector<MathLib::bigint> indexes2(indexes.size()); std::vector<MathLib::bigint> indexes2(indexes.size());
for (unsigned int i = 0; i < indexes.size(); ++i) for (unsigned int i = 0; i < indexes.size(); ++i)
@ -1370,9 +1367,7 @@ void CheckBufferOverrun::checkStructVariable()
if (scope->nestedIn->isClassOrStruct()) if (scope->nestedIn->isClassOrStruct())
continue; continue;
std::vector<const std::string*> varname; std::vector<const std::string*> varname = { nullptr, &arrayInfo.varname() };
varname.push_back(NULL);
varname.push_back(&arrayInfo.varname());
// search the function and it's parameters // search the function and it's parameters
for (const Token *tok3 = func_scope->classDef; tok3 && tok3 != func_scope->classEnd; tok3 = tok3->next()) { for (const Token *tok3 = func_scope->classDef; tok3 && tok3 != func_scope->classEnd; tok3 = tok3->next()) {
@ -2106,8 +2101,7 @@ bool CheckBufferOverrun::analyseWholeProgram(const std::list<Check::FileInfo*> &
fileLoc.setfile(it->second.fileName); fileLoc.setfile(it->second.fileName);
fileLoc.line = it->second.linenr; fileLoc.line = it->second.linenr;
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<ErrorLogger::ErrorMessage::FileLocation> locationList(1, fileLoc);
locationList.push_back(fileLoc);
std::ostringstream ostr; std::ostringstream ostr;
ostr << "Array " << it->first << '[' << sz->second << "] accessed at index " << it->second.index << " which is out of bounds"; ostr << "Array " << it->first << '[' << sz->second << "] accessed at index " << it->second.index << " which is out of bounds";

View File

@ -1159,9 +1159,7 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco
void CheckClass::mallocOnClassWarning(const Token* tok, const std::string &memfunc, const Token* classTok) void CheckClass::mallocOnClassWarning(const Token* tok, const std::string &memfunc, const Token* classTok)
{ {
std::list<const Token *> toks; std::list<const Token *> toks = { tok, classTok };
toks.push_back(tok);
toks.push_back(classTok);
reportError(toks, Severity::warning, "mallocOnClassWarning", reportError(toks, Severity::warning, "mallocOnClassWarning",
"$symbol:" + memfunc +"\n" "$symbol:" + memfunc +"\n"
"Memory for class instance allocated with $symbol(), but class provides constructors.\n" "Memory for class instance allocated with $symbol(), but class provides constructors.\n"
@ -1171,9 +1169,7 @@ void CheckClass::mallocOnClassWarning(const Token* tok, const std::string &memfu
void CheckClass::mallocOnClassError(const Token* tok, const std::string &memfunc, const Token* classTok, const std::string &classname) void CheckClass::mallocOnClassError(const Token* tok, const std::string &memfunc, const Token* classTok, const std::string &classname)
{ {
std::list<const Token *> toks; std::list<const Token *> toks = { tok, classTok };
toks.push_back(tok);
toks.push_back(classTok);
reportError(toks, Severity::error, "mallocOnClassError", reportError(toks, Severity::error, "mallocOnClassError",
"$symbol:" + memfunc +"\n" "$symbol:" + memfunc +"\n"
"$symbol:" + classname +"\n" "$symbol:" + classname +"\n"
@ -2146,9 +2142,7 @@ void CheckClass::initializerListOrder()
void CheckClass::initializerListError(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &varname) void CheckClass::initializerListError(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &varname)
{ {
std::list<const Token *> toks; std::list<const Token *> toks = { tok1, tok2 };
toks.push_back(tok1);
toks.push_back(tok2);
reportError(toks, Severity::style, "initializerList", reportError(toks, Severity::style, "initializerList",
"$symbol:" + classname + "::" + varname +"\n" "$symbol:" + classname + "::" + varname +"\n"
"Member variable '$symbol' is in the wrong place in the initializer list.\n" "Member variable '$symbol' is in the wrong place in the initializer list.\n"
@ -2210,8 +2204,7 @@ void CheckClass::checkVirtualFunctionCallInConstructor()
const std::list<const Token *> & virtualFunctionCalls = getVirtualFunctionCalls(*scope->function, virtualFunctionCallsMap); const std::list<const Token *> & virtualFunctionCalls = getVirtualFunctionCalls(*scope->function, virtualFunctionCallsMap);
for (std::list<const Token *>::const_iterator it = virtualFunctionCalls.begin(); it != virtualFunctionCalls.end(); ++it) { for (std::list<const Token *>::const_iterator it = virtualFunctionCalls.begin(); it != virtualFunctionCalls.end(); ++it) {
const Token * callToken = *it; const Token * callToken = *it;
std::list<const Token *> callstack; std::list<const Token *> callstack(1, callToken);
callstack.push_back(callToken);
getFirstVirtualFunctionCallStack(virtualFunctionCallsMap, callToken, callstack); getFirstVirtualFunctionCallStack(virtualFunctionCallsMap, callToken, callstack);
if (callstack.empty()) if (callstack.empty())
continue; continue;
@ -2398,9 +2391,7 @@ void CheckClass::duplInheritedMembersError(const Token *tok1, const Token* tok2,
const std::string &derivedname, const std::string &basename, const std::string &derivedname, const std::string &basename,
const std::string &variablename, bool derivedIsStruct, bool baseIsStruct) const std::string &variablename, bool derivedIsStruct, bool baseIsStruct)
{ {
std::list<const Token *> toks; std::list<const Token *> toks = { tok1, tok2 };
toks.push_back(tok1);
toks.push_back(tok2);
const std::string symbols = "$symbol:" + derivedname + "\n$symbol:" + variablename + "\n$symbol:" + basename; const std::string symbols = "$symbol:" + derivedname + "\n$symbol:" + variablename + "\n$symbol:" + basename;

View File

@ -206,10 +206,7 @@ bool CheckCondition::assignIfParseScope(const Token * const assignTok,
void CheckCondition::assignIfError(const Token *tok1, const Token *tok2, const std::string &condition, bool result) void CheckCondition::assignIfError(const Token *tok1, const Token *tok2, const std::string &condition, bool result)
{ {
std::list<const Token *> locations; std::list<const Token *> locations = { tok1, tok2 };
locations.push_back(tok1);
locations.push_back(tok2);
reportError(locations, reportError(locations,
Severity::style, Severity::style,
"assignIfError", "assignIfError",
@ -219,9 +216,7 @@ void CheckCondition::assignIfError(const Token *tok1, const Token *tok2, const s
void CheckCondition::mismatchingBitAndError(const Token *tok1, const MathLib::bigint num1, const Token *tok2, const MathLib::bigint num2) void CheckCondition::mismatchingBitAndError(const Token *tok1, const MathLib::bigint num1, const Token *tok2, const MathLib::bigint num2)
{ {
std::list<const Token *> locations; std::list<const Token *> locations = { tok1, tok2 };
locations.push_back(tok1);
locations.push_back(tok2);
std::ostringstream msg; std::ostringstream msg;
msg << "Mismatching bitmasks. Result is always 0 (" msg << "Mismatching bitmasks. Result is always 0 ("
@ -687,9 +682,10 @@ void CheckCondition::oppositeInnerConditionError(const Token *tok1, const Token*
{ {
const std::string s1(tok1 ? tok1->expressionString() : "x"); const std::string s1(tok1 ? tok1->expressionString() : "x");
const std::string s2(tok2 ? tok2->expressionString() : "!x"); const std::string s2(tok2 ? tok2->expressionString() : "!x");
ErrorPath errorPath; ErrorPath errorPath = {
errorPath.push_back(ErrorPathItem(tok1, "outer condition: " + s1)); ErrorPathItem(tok1, "outer condition: " + s1),
errorPath.push_back(ErrorPathItem(tok2, "opposite inner condition: " + s2)); ErrorPathItem(tok2, "opposite inner condition: " + s2)
};
const std::string msg("Opposite inner 'if' condition leads to a dead code block.\n" const std::string msg("Opposite inner 'if' condition leads to a dead code block.\n"
"Opposite inner 'if' condition leads to a dead code block (outer condition is '" + s1 + "' and inner condition is '" + s2 + "')."); "Opposite inner 'if' condition leads to a dead code block (outer condition is '" + s1 + "' and inner condition is '" + s2 + "').");
reportError(errorPath, Severity::warning, "oppositeInnerCondition", msg, CWE398, false); reportError(errorPath, Severity::warning, "oppositeInnerCondition", msg, CWE398, false);
@ -699,9 +695,10 @@ void CheckCondition::identicalInnerConditionError(const Token *tok1, const Token
{ {
const std::string s1(tok1 ? tok1->expressionString() : "x"); const std::string s1(tok1 ? tok1->expressionString() : "x");
const std::string s2(tok2 ? tok2->expressionString() : "x"); const std::string s2(tok2 ? tok2->expressionString() : "x");
ErrorPath errorPath; ErrorPath errorPath = {
errorPath.push_back(ErrorPathItem(tok1, "outer condition: " + s1)); ErrorPathItem(tok1, "outer condition: " + s1),
errorPath.push_back(ErrorPathItem(tok2, "identical inner condition: " + s2)); ErrorPathItem(tok2, "identical inner condition: " + s2)
};
const std::string msg("Identical inner 'if' condition is always true.\n" const std::string msg("Identical inner 'if' condition is always true.\n"
"Identical inner 'if' condition is always true (outer condition is '" + s1 + "' and inner condition is '" + s2 + "')."); "Identical inner 'if' condition is always true (outer condition is '" + s1 + "' and inner condition is '" + s2 + "').");
reportError(errorPath, Severity::warning, "identicalInnerCondition", msg, CWE398, false); reportError(errorPath, Severity::warning, "identicalInnerCondition", msg, CWE398, false);
@ -710,9 +707,10 @@ void CheckCondition::identicalInnerConditionError(const Token *tok1, const Token
void CheckCondition::identicalConditionAfterEarlyExitError(const Token *cond1, const Token* cond2) void CheckCondition::identicalConditionAfterEarlyExitError(const Token *cond1, const Token* cond2)
{ {
const std::string cond(cond1 ? cond1->expressionString() : "x"); const std::string cond(cond1 ? cond1->expressionString() : "x");
ErrorPath errorPath; ErrorPath errorPath = {
errorPath.push_back(ErrorPathItem(cond1, "first condition")); ErrorPathItem(cond1, "first condition"),
errorPath.push_back(ErrorPathItem(cond2, "second condition")); ErrorPathItem(cond2, "second condition")
};
reportError(errorPath, Severity::warning, "identicalConditionAfterEarlyExit", "Identical condition '" + cond + "', second condition is always false", CWE398, false); reportError(errorPath, Severity::warning, "identicalConditionAfterEarlyExit", "Identical condition '" + cond + "', second condition is always false", CWE398, false);
} }

View File

@ -2860,9 +2860,7 @@ void CheckOther::checkFuncArgNamesDifferent()
void CheckOther::funcArgNamesDifferent(const std::string & functionName, size_t index, void CheckOther::funcArgNamesDifferent(const std::string & functionName, size_t index,
const Token* declaration, const Token* definition) const Token* declaration, const Token* definition)
{ {
std::list<const Token *> tokens; std::list<const Token *> tokens = { declaration,definition };
tokens.push_back(declaration);
tokens.push_back(definition);
reportError(tokens, Severity::style, "funcArgNamesDifferent", reportError(tokens, Severity::style, "funcArgNamesDifferent",
"$symbol:" + functionName + "\n" "$symbol:" + functionName + "\n"
"Function '$symbol' argument " + MathLib::toString(index + 1) + " names different: declaration '" + "Function '$symbol' argument " + MathLib::toString(index + 1) + " names different: declaration '" +
@ -2875,9 +2873,10 @@ void CheckOther::funcArgOrderDifferent(const std::string & functionName,
const std::vector<const Token *> & declarations, const std::vector<const Token *> & declarations,
const std::vector<const Token *> & definitions) const std::vector<const Token *> & definitions)
{ {
std::list<const Token *> tokens; std::list<const Token *> tokens = {
tokens.push_back(declarations.size() ? declarations[0] ? declarations[0] : declaration : nullptr); declarations.size() ? declarations[0] ? declarations[0] : declaration : nullptr,
tokens.push_back(definitions.size() ? definitions[0] ? definitions[0] : definition : nullptr); definitions.size() ? definitions[0] ? definitions[0] : definition : nullptr
};
std::string msg = "$symbol:" + functionName + "\nFunction '$symbol' argument order different: declaration '"; std::string msg = "$symbol:" + functionName + "\nFunction '$symbol' argument order different: declaration '";
for (std::size_t i = 0; i < declarations.size(); ++i) { for (std::size_t i = 0; i < declarations.size(); ++i) {
if (i != 0) if (i != 0)

View File

@ -67,9 +67,7 @@ void CheckStl::iteratorsError(const Token *tok, const std::string &container1, c
void CheckStl::dereferenceErasedError(const Token *erased, const Token* deref, const std::string &itername, bool inconclusive) void CheckStl::dereferenceErasedError(const Token *erased, const Token* deref, const std::string &itername, bool inconclusive)
{ {
if (erased) { if (erased) {
std::list<const Token*> callstack; std::list<const Token*> callstack = { deref, erased };
callstack.push_back(deref);
callstack.push_back(erased);
reportError(callstack, Severity::error, "eraseDereference", reportError(callstack, Severity::error, "eraseDereference",
"$symbol:" + itername + "\n" "$symbol:" + itername + "\n"
"Iterator '$symbol' used after element has been erased.\n" "Iterator '$symbol' used after element has been erased.\n"
@ -1028,9 +1026,7 @@ void CheckStl::missingComparison()
void CheckStl::missingComparisonError(const Token *incrementToken1, const Token *incrementToken2) void CheckStl::missingComparisonError(const Token *incrementToken1, const Token *incrementToken2)
{ {
std::list<const Token*> callstack; std::list<const Token*> callstack = { incrementToken1,incrementToken2 };
callstack.push_back(incrementToken1);
callstack.push_back(incrementToken2);
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "Missing bounds check for extra iterator increment in loop.\n" errmsg << "Missing bounds check for extra iterator increment in loop.\n"

View File

@ -166,8 +166,7 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin
if (err) { if (err) {
const ErrorLogger::ErrorMessage::FileLocation loc1(it->location.file(), it->location.line); const ErrorLogger::ErrorMessage::FileLocation loc1(it->location.file(), it->location.line);
std::list<ErrorLogger::ErrorMessage::FileLocation> callstack; std::list<ErrorLogger::ErrorMessage::FileLocation> callstack(1, loc1);
callstack.push_back(loc1);
ErrorLogger::ErrorMessage errmsg(callstack, ErrorLogger::ErrorMessage errmsg(callstack,
"", "",
@ -476,8 +475,7 @@ void CppCheck::internalError(const std::string &filename, const std::string &msg
if (_settings.isEnabled(Settings::INFORMATION)) { if (_settings.isEnabled(Settings::INFORMATION)) {
const ErrorLogger::ErrorMessage::FileLocation loc1(filename, 0); const ErrorLogger::ErrorMessage::FileLocation loc1(filename, 0);
std::list<ErrorLogger::ErrorMessage::FileLocation> callstack; std::list<ErrorLogger::ErrorMessage::FileLocation> callstack(1, loc1);
callstack.push_back(loc1);
ErrorLogger::ErrorMessage errmsg(callstack, ErrorLogger::ErrorMessage errmsg(callstack,
emptyString, emptyString,

View File

@ -92,8 +92,7 @@ static void execute(const Token *expr,
static void bailoutInternal(TokenList *tokenlist, ErrorLogger *errorLogger, const Token *tok, const std::string &what, const std::string &file, int line, const std::string &function) static void bailoutInternal(TokenList *tokenlist, ErrorLogger *errorLogger, const Token *tok, const std::string &what, const std::string &file, int line, const std::string &function)
{ {
std::list<ErrorLogger::ErrorMessage::FileLocation> callstack; std::list<ErrorLogger::ErrorMessage::FileLocation> callstack(1, ErrorLogger::ErrorMessage::FileLocation(tok, tokenlist));
callstack.push_back(ErrorLogger::ErrorMessage::FileLocation(tok, tokenlist));
ErrorLogger::ErrorMessage errmsg(callstack, tokenlist->getSourceFilePath(), Severity::debug, ErrorLogger::ErrorMessage errmsg(callstack, tokenlist->getSourceFilePath(), Severity::debug,
Path::stripDirectoryPart(file) + ":" + MathLib::toString(line) + ":" + function + " bailout: " + what, "valueFlowBailout", false); Path::stripDirectoryPart(file) + ":" + MathLib::toString(line) + ":" + function + " bailout: " + what, "valueFlowBailout", false);
errorLogger->reportErr(errmsg); errorLogger->reportErr(errmsg);