Refactorization: Replace several push_back-sequences by initializer lists

This commit is contained in:
PKEuS 2018-04-09 09:41:24 +02:00
parent 6f9c115640
commit bbfcccf078
16 changed files with 48 additions and 122 deletions

View File

@ -3111,8 +3111,7 @@ private:
} }
void countSprintfLength() const { void countSprintfLength() const {
std::list<const Token*> unknownParameter; std::list<const Token*> unknownParameter(1, nullptr);
unknownParameter.push_back(0);
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("Hello", unknownParameter)); ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("Hello", unknownParameter));
ASSERT_EQUALS(2, CheckBufferOverrun::countSprintfLength("s", unknownParameter)); ASSERT_EQUALS(2, CheckBufferOverrun::countSprintfLength("s", unknownParameter));
@ -3134,8 +3133,7 @@ private:
ASSERT_EQUALS(4, CheckBufferOverrun::countSprintfLength("%%%%%d", unknownParameter)); ASSERT_EQUALS(4, CheckBufferOverrun::countSprintfLength("%%%%%d", unknownParameter));
Token strTok(0); Token strTok(0);
std::list<const Token*> stringAsParameter; std::list<const Token*> stringAsParameter(1, &strTok);
stringAsParameter.push_back(&strTok);
strTok.str("\"\""); strTok.str("\"\"");
ASSERT_EQUALS(4, CheckBufferOverrun::countSprintfLength("str%s", stringAsParameter)); ASSERT_EQUALS(4, CheckBufferOverrun::countSprintfLength("str%s", stringAsParameter));
strTok.str("\"12345\""); strTok.str("\"12345\"");
@ -3149,10 +3147,9 @@ private:
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%5.6s", stringAsParameter)); ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%5.6s", stringAsParameter));
ASSERT_EQUALS(7, CheckBufferOverrun::countSprintfLength("%6.6s", stringAsParameter)); ASSERT_EQUALS(7, CheckBufferOverrun::countSprintfLength("%6.6s", stringAsParameter));
std::list<const Token*> intAsParameter;
Token numTok(0); Token numTok(0);
numTok.str("12345"); numTok.str("12345");
intAsParameter.push_back(&numTok); std::list<const Token*> intAsParameter(1, &numTok);
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%02ld", intAsParameter)); ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%02ld", intAsParameter));
ASSERT_EQUALS(9, CheckBufferOverrun::countSprintfLength("%08ld", intAsParameter)); ASSERT_EQUALS(9, CheckBufferOverrun::countSprintfLength("%08ld", intAsParameter));
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%.2d", intAsParameter)); ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%.2d", intAsParameter));
@ -3165,26 +3162,21 @@ private:
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%1.5x", intAsParameter)); ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%1.5x", intAsParameter));
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%5.1x", intAsParameter)); ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%5.1x", intAsParameter));
std::list<const Token*> floatAsParameter;
Token floatTok(0); Token floatTok(0);
floatTok.str("1.12345f"); floatTok.str("1.12345f");
floatAsParameter.push_back(&floatTok); std::list<const Token*> floatAsParameter(1, &floatTok);
TODO_ASSERT_EQUALS(5, 3, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter)); TODO_ASSERT_EQUALS(5, 3, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter));
ASSERT_EQUALS(9, CheckBufferOverrun::countSprintfLength("%8.2f", floatAsParameter)); ASSERT_EQUALS(9, CheckBufferOverrun::countSprintfLength("%8.2f", floatAsParameter));
TODO_ASSERT_EQUALS(5, 3, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter)); TODO_ASSERT_EQUALS(5, 3, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter));
std::list<const Token*> floatAsParameter2;
Token floatTok2(0); Token floatTok2(0);
floatTok2.str("100.12345f"); floatTok2.str("100.12345f");
floatAsParameter2.push_back(&floatTok2); std::list<const Token*> floatAsParameter2(1, &floatTok2);
TODO_ASSERT_EQUALS(7, 3, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter2)); TODO_ASSERT_EQUALS(7, 3, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter2));
TODO_ASSERT_EQUALS(7, 3, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter)); TODO_ASSERT_EQUALS(7, 3, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter));
TODO_ASSERT_EQUALS(7, 5, CheckBufferOverrun::countSprintfLength("%4.2f", floatAsParameter)); TODO_ASSERT_EQUALS(7, 5, CheckBufferOverrun::countSprintfLength("%4.2f", floatAsParameter));
std::list<const Token*> multipleParams; std::list<const Token*> multipleParams = { &strTok, nullptr, &numTok };
multipleParams.push_back(&strTok);
multipleParams.push_back(0);
multipleParams.push_back(&numTok);
ASSERT_EQUALS(15, CheckBufferOverrun::countSprintfLength("str%s%d%d", multipleParams)); ASSERT_EQUALS(15, CheckBufferOverrun::countSprintfLength("str%s%d%d", multipleParams));
ASSERT_EQUALS(26, CheckBufferOverrun::countSprintfLength("str%-6s%08ld%08ld", multipleParams)); ASSERT_EQUALS(26, CheckBufferOverrun::countSprintfLength("str%-6s%08ld%08ld", multipleParams));
} }

View File

@ -114,8 +114,7 @@ private:
settings0.inconclusive = inconclusive; settings0.inconclusive = inconclusive;
// Raw tokens.. // Raw tokens..
std::vector<std::string> files; std::vector<std::string> files(1, filename);
files.push_back(filename);
std::istringstream istr(code); std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]); const simplecpp::TokenList tokens1(istr, files, files[0]);

View File

@ -120,9 +120,7 @@ private:
} }
void ErrorMessageConstructLocations() const { void ErrorMessageConstructLocations() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> locs; std::list<ErrorLogger::ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
locs.push_back(fooCpp5);
locs.push_back(barCpp8);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.", "errorId", false); ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.", "errorId", false);
ASSERT_EQUALS(2, (int)msg._callStack.size()); ASSERT_EQUALS(2, (int)msg._callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Programming error.", msg.shortMessage());
@ -142,9 +140,7 @@ private:
} }
void ErrorMessageVerboseLocations() const { void ErrorMessageVerboseLocations() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> locs; std::list<ErrorLogger::ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
locs.push_back(fooCpp5);
locs.push_back(barCpp8);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS(2, (int)msg._callStack.size()); ASSERT_EQUALS(2, (int)msg._callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Programming error.", msg.shortMessage());
@ -175,9 +171,7 @@ private:
void CustomFormatLocations() const { void CustomFormatLocations() const {
// Check that first location from location stack is used in template // Check that first location from location stack is used in template
std::list<ErrorLogger::ErrorMessage::FileLocation> locs; std::list<ErrorLogger::ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
locs.push_back(fooCpp5);
locs.push_back(barCpp8);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS(2, (int)msg._callStack.size()); ASSERT_EQUALS(2, (int)msg._callStack.size());
ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Programming error.", msg.shortMessage());
@ -202,9 +196,7 @@ private:
} }
void ToXmlV2Locations() const { void ToXmlV2Locations() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> locs; std::list<ErrorLogger::ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
locs.push_back(fooCpp5);
locs.push_back(barCpp8);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false); ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
std::string header("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"2\">\n"); std::string header("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"2\">\n");
header += " <cppcheck version=\""; header += " <cppcheck version=\"";

View File

@ -64,8 +64,7 @@ private:
void setIncludePaths1() const { void setIncludePaths1() const {
ImportProject::FileSettings fs; ImportProject::FileSettings fs;
std::list<std::string> in; std::list<std::string> in(1, "../include");
in.push_back("../include");
std::map<std::string, std::string, cppcheck::stricmp> variables; std::map<std::string, std::string, cppcheck::stricmp> variables;
fs.setIncludePaths("abc/def/", in, variables); fs.setIncludePaths("abc/def/", in, variables);
ASSERT_EQUALS(1U, fs.includePaths.size()); ASSERT_EQUALS(1U, fs.includePaths.size());
@ -74,8 +73,7 @@ private:
void setIncludePaths2() const { void setIncludePaths2() const {
ImportProject::FileSettings fs; ImportProject::FileSettings fs;
std::list<std::string> in; std::list<std::string> in(1, "$(SolutionDir)other");
in.push_back("$(SolutionDir)other");
std::map<std::string, std::string, cppcheck::stricmp> variables; std::map<std::string, std::string, cppcheck::stricmp> variables;
variables["SolutionDir"] = "c:/abc/"; variables["SolutionDir"] = "c:/abc/";
fs.setIncludePaths("/home/fred", in, variables); fs.setIncludePaths("/home/fred", in, variables);
@ -85,8 +83,7 @@ private:
void setIncludePaths3() const { // macro names are case insensitive void setIncludePaths3() const { // macro names are case insensitive
ImportProject::FileSettings fs; ImportProject::FileSettings fs;
std::list<std::string> in; std::list<std::string> in(1, "$(SOLUTIONDIR)other");
in.push_back("$(SOLUTIONDIR)other");
std::map<std::string, std::string, cppcheck::stricmp> variables; std::map<std::string, std::string, cppcheck::stricmp> variables;
variables["SolutionDir"] = "c:/abc/"; variables["SolutionDir"] = "c:/abc/";
fs.setIncludePaths("/home/fred", in, variables); fs.setIncludePaths("/home/fred", in, variables);

View File

@ -39,8 +39,7 @@ private:
// Raw tokens.. // Raw tokens..
std::vector<std::string> files; std::vector<std::string> files(1, "test.cpp");
files.push_back("test.cpp");
std::istringstream istr(code); std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]); const simplecpp::TokenList tokens1(istr, files, files[0]);

View File

@ -404,8 +404,7 @@ private:
// getcode.. // getcode..
CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings2, nullptr); CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings2, nullptr);
std::list<const Token *> callstack; std::list<const Token *> callstack(1, nullptr);
callstack.push_back(0);
CheckMemoryLeak::AllocType allocType, deallocType; CheckMemoryLeak::AllocType allocType, deallocType;
allocType = deallocType = CheckMemoryLeak::No; allocType = deallocType = CheckMemoryLeak::No;
Token *tokens = checkMemoryLeak.getcode(start, callstack, varId, allocType, deallocType, classfunc, 1); Token *tokens = checkMemoryLeak.getcode(start, callstack, varId, allocType, deallocType, classfunc, 1);
@ -6041,8 +6040,7 @@ private:
errout.str(""); errout.str("");
std::istringstream istr(code); std::istringstream istr(code);
std::vector<std::string> files; std::vector<std::string> files(1, "test.cpp");
files.push_back("test.cpp");
const simplecpp::TokenList tokens1(istr, files, files[0]); const simplecpp::TokenList tokens1(istr, files, files[0]);
// Preprocess... // Preprocess...

View File

@ -136,8 +136,7 @@ private:
settings.inconclusive = false; settings.inconclusive = false;
// Raw tokens.. // Raw tokens..
std::vector<std::string> files; std::vector<std::string> files(1, "test.cpp");
files.push_back("test.cpp");
std::istringstream istr(code); std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]); const simplecpp::TokenList tokens1(istr, files, files[0]);

View File

@ -254,8 +254,7 @@ private:
settings->experimental = false; settings->experimental = false;
// Raw tokens.. // Raw tokens..
std::vector<std::string> files; std::vector<std::string> files(1, filename);
files.push_back(filename);
std::istringstream istr(code); std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]); const simplecpp::TokenList tokens1(istr, files, files[0]);

View File

@ -91,11 +91,12 @@ private:
} }
void getRelative() const { void getRelative() const {
std::vector<std::string> basePaths; std::vector<std::string> basePaths = {
basePaths.push_back(""); // Don't crash with empty paths "", // Don't crash with empty paths
basePaths.push_back("C:/foo"); "C:/foo",
basePaths.push_back("C:/bar/"); "C:/bar/",
basePaths.push_back("C:/test.cpp"); "C:/test.cpp"
};
ASSERT_EQUALS("x.c", Path::getRelativePath("C:/foo/x.c", basePaths)); ASSERT_EQUALS("x.c", Path::getRelativePath("C:/foo/x.c", basePaths));
ASSERT_EQUALS("y.c", Path::getRelativePath("C:/bar/y.c", basePaths)); ASSERT_EQUALS("y.c", Path::getRelativePath("C:/bar/y.c", basePaths));

View File

@ -134,33 +134,25 @@ private:
} }
void twomasklongerpath1() const { void twomasklongerpath1() const {
std::vector<std::string> masks; std::vector<std::string> masks = { "src/", "module/" };
masks.push_back("src/");
masks.push_back("module/");
PathMatch match(masks); PathMatch match(masks);
ASSERT(!match.match("project/")); ASSERT(!match.match("project/"));
} }
void twomasklongerpath2() const { void twomasklongerpath2() const {
std::vector<std::string> masks; std::vector<std::string> masks = { "src/", "module/" };
masks.push_back("src/");
masks.push_back("module/");
PathMatch match(masks); PathMatch match(masks);
ASSERT(match.match("project/src/")); ASSERT(match.match("project/src/"));
} }
void twomasklongerpath3() const { void twomasklongerpath3() const {
std::vector<std::string> masks; std::vector<std::string> masks = { "src/", "module/" };
masks.push_back("src/");
masks.push_back("module/");
PathMatch match(masks); PathMatch match(masks);
ASSERT(match.match("project/module/")); ASSERT(match.match("project/module/"));
} }
void twomasklongerpath4() const { void twomasklongerpath4() const {
std::vector<std::string> masks; std::vector<std::string> masks = { "src/", "module/" };
masks.push_back("src/");
masks.push_back("module/");
PathMatch match(masks); PathMatch match(masks);
ASSERT(match.match("project/src/module/")); ASSERT(match.match("project/src/module/"));
} }

View File

@ -2210,13 +2210,12 @@ private:
void validateCfg() { void validateCfg() {
Preprocessor preprocessor(settings0, this); Preprocessor preprocessor(settings0, this);
std::list<simplecpp::MacroUsage> macroUsageList;
std::vector<std::string> files(1, "test.c"); std::vector<std::string> files(1, "test.c");
simplecpp::MacroUsage macroUsage(files); simplecpp::MacroUsage macroUsage(files);
macroUsage.useLocation.fileIndex = 0; macroUsage.useLocation.fileIndex = 0;
macroUsage.useLocation.line = 1; macroUsage.useLocation.line = 1;
macroUsage.macroName = "X"; macroUsage.macroName = "X";
macroUsageList.push_back(macroUsage); std::list<simplecpp::MacroUsage> macroUsageList(1, macroUsage);
ASSERT_EQUALS(true, preprocessor.validateCfg("", macroUsageList)); ASSERT_EQUALS(true, preprocessor.validateCfg("", macroUsageList));
ASSERT_EQUALS(false, preprocessor.validateCfg("X",macroUsageList)); ASSERT_EQUALS(false, preprocessor.validateCfg("X",macroUsageList));

View File

@ -105,14 +105,15 @@ private:
void runConsoleCodePageTranslationOnWindows() const { void runConsoleCodePageTranslationOnWindows() const {
REDIRECT; REDIRECT;
std::vector<std::string> msgs; std::vector<std::string> msgs = {
msgs.push_back("ASCII"); // first entry should be using only ASCII "ASCII", // first entry should be using only ASCII
msgs.push_back("kääk"); "kääk",
msgs.push_back("Português"); "Português"
// msgs.push_back("日本語"); // "日本語",
// msgs.push_back("한국어"); // "한국어",
// msgs.push_back("Русский"); // "Русский",
// msgs.push_back("中文"); // "中文",
};
Settings set1; Settings set1;
Settings setXML; Settings setXML;

View File

@ -70,8 +70,7 @@ private:
errout.str(""); errout.str("");
// Raw tokens.. // Raw tokens..
std::vector<std::string> files; std::vector<std::string> files(1, "test.cpp");
files.push_back("test.cpp");
std::istringstream istr(code); std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]); const simplecpp::TokenList tokens1(istr, files, files[0]);

View File

@ -43,7 +43,12 @@ private:
std::vector<std::string> assignmentOps; std::vector<std::string> assignmentOps;
void run() { void run() {
initOps(); arithmeticalOps = { "+", "-", "*", "/", "%", "<<", ">>" };
logicalOps = { "&&", "||", "!" };
comparisonOps = { "==", "!=", "<", "<=", ">", ">=" };
bitOps = { "&", "|", "^", "~" };
extendedOps = { ",", "[", "]", "(", ")", "?", ":" };
assignmentOps = { "=", "+=", "-=", "*=", "/=", "%=", "&=", "^=", "|=", "<<=", ">>=" };
TEST_CASE(nextprevious); TEST_CASE(nextprevious);
TEST_CASE(multiCompare); TEST_CASE(multiCompare);
@ -546,50 +551,6 @@ private:
dest.insert(dest.end(), src.begin(), src.end()); dest.insert(dest.end(), src.begin(), src.end());
} }
void initOps() {
arithmeticalOps.push_back("+");
arithmeticalOps.push_back("-");
arithmeticalOps.push_back("*");
arithmeticalOps.push_back("/");
arithmeticalOps.push_back("%");
arithmeticalOps.push_back("<<");
arithmeticalOps.push_back(">>");
logicalOps.push_back("&&");
logicalOps.push_back("||");
logicalOps.push_back("!");
comparisonOps.push_back("==");
comparisonOps.push_back("!=");
comparisonOps.push_back("<");
comparisonOps.push_back("<=");
comparisonOps.push_back(">");
comparisonOps.push_back(">=");
bitOps.push_back("&");
bitOps.push_back("|");
bitOps.push_back("^");
bitOps.push_back("~");
extendedOps.push_back(",");
extendedOps.push_back("[");
extendedOps.push_back("]");
extendedOps.push_back("(");
extendedOps.push_back(")");
extendedOps.push_back("?");
extendedOps.push_back(":");
assignmentOps.push_back("=");
assignmentOps.push_back("+=");
assignmentOps.push_back("-=");
assignmentOps.push_back("*=");
assignmentOps.push_back("/=");
assignmentOps.push_back("%=");
assignmentOps.push_back("&=");
assignmentOps.push_back("^=");
assignmentOps.push_back("|=");
assignmentOps.push_back("<<=");
assignmentOps.push_back(">>=");
}
void matchOp() { void matchOp() {
std::vector<std::string> test_ops; std::vector<std::string> test_ops;
append_vector(test_ops, arithmeticalOps); append_vector(test_ops, arithmeticalOps);

View File

@ -90,8 +90,7 @@ private:
settings.platform(platform); settings.platform(platform);
// Raw tokens.. // Raw tokens..
std::vector<std::string> files; std::vector<std::string> files(1, "test.cpp");
files.push_back("test.cpp");
std::istringstream istr(code); std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]); const simplecpp::TokenList tokens1(istr, files, files[0]);

View File

@ -229,8 +229,7 @@ private:
settings.debugwarnings = true; settings.debugwarnings = true;
errout.str(""); errout.str("");
std::vector<std::string> files; std::vector<std::string> files(1, "test.cpp");
files.push_back("test.cpp");
std::istringstream istr(code); std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]); const simplecpp::TokenList tokens1(istr, files, files[0]);