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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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