Skip to content

Commit e2002db

Browse files
committed
Replaced make_container by C++11 initializer lists
1 parent ae3e649 commit e2002db

16 files changed

Lines changed: 286 additions & 305 deletions

cli/cppcheckexecutor.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -319,21 +319,21 @@ static bool IsAddressOnStack(const void* ptr)
319319
* For now we only want to detect abnormal behaviour for a few selected signals:
320320
*/
321321

322-
#define DECLARE_SIGNAL(x) << std::make_pair(x, #x)
322+
#define DECLARE_SIGNAL(x) std::make_pair(x, #x)
323323
typedef std::map<int, std::string> Signalmap_t;
324-
static const Signalmap_t listofsignals = make_container< Signalmap_t > ()
325-
DECLARE_SIGNAL(SIGABRT)
326-
DECLARE_SIGNAL(SIGBUS)
327-
DECLARE_SIGNAL(SIGFPE)
328-
DECLARE_SIGNAL(SIGILL)
329-
DECLARE_SIGNAL(SIGINT)
330-
DECLARE_SIGNAL(SIGQUIT)
331-
DECLARE_SIGNAL(SIGSEGV)
332-
DECLARE_SIGNAL(SIGSYS)
333-
// don't care: SIGTERM
334-
DECLARE_SIGNAL(SIGUSR1)
335-
//DECLARE_SIGNAL(SIGUSR2) no usage currently
336-
;
324+
static const Signalmap_t listofsignals = {
325+
DECLARE_SIGNAL(SIGABRT),
326+
DECLARE_SIGNAL(SIGBUS),
327+
DECLARE_SIGNAL(SIGFPE),
328+
DECLARE_SIGNAL(SIGILL),
329+
DECLARE_SIGNAL(SIGINT),
330+
DECLARE_SIGNAL(SIGQUIT),
331+
DECLARE_SIGNAL(SIGSEGV),
332+
DECLARE_SIGNAL(SIGSYS),
333+
// don't care: SIGTERM
334+
DECLARE_SIGNAL(SIGUSR1),
335+
//DECLARE_SIGNAL(SIGUSR2) no usage currently
336+
};
337337
#undef DECLARE_SIGNAL
338338
/*
339339
* Entry pointer for signal handlers

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const
18981898

18991899
namespace {
19001900
// The container contains the STL types whose operator[] is not a const.
1901-
const std::set<std::string> stl_containers_not_const = make_container< std::set<std::string> >() << "map" << "unordered_map";
1901+
const std::set<std::string> stl_containers_not_const = { "map", "unordered_map" };
19021902
}
19031903

19041904
bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& memberAccessed) const

lib/checkexceptionsafety.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class CPPCHECKLIB CheckExceptionSafety : public Check {
129129
/** Missing exception specification */
130130
void unhandledExceptionSpecificationError(const Token * const tok1, const Token * const tok2, const std::string & funcname) {
131131
const std::string str1(tok1 ? tok1->str() : "foo");
132-
const std::list<const Token*> locationList = make_container< std::list<const Token*> > () << tok1 << tok2;
132+
const std::list<const Token*> locationList = { tok1, tok2 };
133133
reportError(locationList, Severity::style, "unhandledExceptionSpecification",
134134
"Unhandled exception specification when calling function " + str1 + "().\n"
135135
"Unhandled exception specification when calling function " + str1 + "(). "

lib/checkinternal.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,23 @@ void CheckInternal::checkTokenSimpleMatchPatterns()
191191
}
192192

193193
namespace {
194-
const std::set<std::string> knownPatterns = make_container< std::set<std::string> > ()
195-
<< "%any%"
196-
<< "%assign%"
197-
<< "%bool%"
198-
<< "%char%"
199-
<< "%comp%"
200-
<< "%num%"
201-
<< "%op%"
202-
<< "%cop%"
203-
<< "%or%"
204-
<< "%oror%"
205-
<< "%str%"
206-
<< "%type%"
207-
<< "%name%"
208-
<< "%var%"
209-
<< "%varid%";
194+
const std::set<std::string> knownPatterns = {
195+
"%any%"
196+
, "%assign%"
197+
, "%bool%"
198+
, "%char%"
199+
, "%comp%"
200+
, "%num%"
201+
, "%op%"
202+
, "%cop%"
203+
, "%or%"
204+
, "%oror%"
205+
, "%str%"
206+
, "%type%"
207+
, "%name%"
208+
, "%var%"
209+
, "%varid%"
210+
};
210211
}
211212

212213
void CheckInternal::checkMissingPercentCharacter()

lib/checkio.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ struct Filepointer {
114114
};
115115

116116
namespace {
117-
const std::set<std::string> whitelist = make_container< std::set<std::string> > ()
118-
<< "clearerr" << "feof" << "ferror" << "fgetpos" << "ftell" << "setbuf" << "setvbuf" << "ungetc" << "ungetwc";
117+
const std::set<std::string> whitelist = { "clearerr", "feof", "ferror", "fgetpos", "ftell", "setbuf", "setvbuf", "ungetc", "ungetwc" };
119118
}
120119

121120
void CheckIO::checkFileUsage()
@@ -1544,8 +1543,8 @@ CheckIO::ArgumentInfo::~ArgumentInfo()
15441543
}
15451544

15461545
namespace {
1547-
const std::set<std::string> stl_vector = make_container< std::set<std::string> >() << "array" << "vector";
1548-
const std::set<std::string> stl_string = make_container< std::set<std::string> >() << "string" << "u16string" << "u32string" << "wstring";
1546+
const std::set<std::string> stl_vector = { "array", "vector" };
1547+
const std::set<std::string> stl_string = { "string", "u16string", "u32string", "wstring" };
15491548
}
15501549

15511550
bool CheckIO::ArgumentInfo::isStdVectorOrString()
@@ -1605,13 +1604,13 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString()
16051604
}
16061605

16071606
namespace {
1608-
const std::set<std::string> stl_container = make_container< std::set<std::string> >() <<
1609-
"array" << "bitset" << "deque" << "forward_list" <<
1610-
"hash_map" << "hash_multimap" << "hash_set" <<
1611-
"list" << "map" << "multimap" << "multiset" <<
1612-
"priority_queue" << "queue" << "set" << "stack" <<
1613-
"unordered_map" << "unordered_multimap" << "unordered_multiset" << "unordered_set" << "vector"
1614-
;
1607+
const std::set<std::string> stl_container = {
1608+
"array", "bitset", "deque", "forward_list",
1609+
"hash_map", "hash_multimap", "hash_set",
1610+
"list", "map", "multimap", "multiset",
1611+
"priority_queue", "queue", "set", "stack",
1612+
"unordered_map", "unordered_multimap", "unordered_multiset", "unordered_set", "vector"
1613+
};
16151614
}
16161615

16171616
bool CheckIO::ArgumentInfo::isStdContainer(const Token *tok)

lib/checkmemoryleak.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,22 @@ static unsigned int countParameters(const Token *tok)
7575
* This list contains function names with const parameters e.g.: atof(const char *)
7676
* TODO: This list should be replaced by <leak-ignore/> in .cfg files.
7777
*/
78-
static const std::set<std::string> call_func_white_list = make_container < std::set<std::string> > ()
79-
<< "_open" << "_wopen" << "access" << "adjtime" << "asctime_r" << "asprintf" << "chdir" << "chmod" << "chown"
80-
<< "creat" << "ctime_r" << "execl" << "execle" << "execlp" << "execv" << "execve" << "fchmod" << "fcntl"
81-
<< "fdatasync" << "fclose" << "flock" << "fmemopen" << "fnmatch" << "fopen" << "fopencookie" << "for" << "free"
82-
<< "freopen"<< "fseeko" << "fstat" << "fsync" << "ftello" << "ftruncate" << "getgrnam" << "gethostbyaddr" << "gethostbyname"
83-
<< "getnetbyname" << "getopt" << "getopt_long" << "getprotobyname" << "getpwnam" << "getservbyname" << "getservbyport"
84-
<< "glob" << "gmtime" << "gmtime_r" << "if" << "index" << "inet_addr" << "inet_aton" << "inet_network" << "initgroups"
85-
<< "ioctl" << "link" << "localtime_r" << "lockf" << "lseek" << "lstat" << "mkdir" << "mkfifo" << "mknod" << "mkstemp"
86-
<< "obstack_printf" << "obstack_vprintf" << "open" << "opendir" << "parse_printf_format" << "pathconf"
87-
<< "perror" << "popen" << "posix_fadvise" << "posix_fallocate" << "pread" << "psignal" << "pwrite" << "read" << "readahead"
88-
<< "readdir" << "readdir_r" << "readlink" << "readv" << "realloc" << "regcomp" << "return" << "rewinddir" << "rindex"
89-
<< "rmdir" << "scandir" << "seekdir" << "setbuffer" << "sethostname" << "setlinebuf" << "sizeof" << "strdup"
90-
<< "stat" << "stpcpy" << "strcasecmp" << "stricmp" << "strncasecmp" << "switch"
91-
<< "symlink" << "sync_file_range" << "telldir" << "tempnam" << "time" << "typeid" << "unlink"
92-
<< "utime" << "utimes" << "vasprintf" << "while" << "wordexp" << "write" << "writev";
78+
static const std::set<std::string> call_func_white_list = {
79+
"_open", "_wopen", "access", "adjtime", "asctime_r", "asprintf", "chdir", "chmod", "chown"
80+
, "creat", "ctime_r", "execl", "execle", "execlp", "execv", "execve", "fchmod", "fcntl"
81+
, "fdatasync", "fclose", "flock", "fmemopen", "fnmatch", "fopen", "fopencookie", "for", "free"
82+
, "freopen", "fseeko", "fstat", "fsync", "ftello", "ftruncate", "getgrnam", "gethostbyaddr", "gethostbyname"
83+
, "getnetbyname", "getopt", "getopt_long", "getprotobyname", "getpwnam", "getservbyname", "getservbyport"
84+
, "glob", "gmtime", "gmtime_r", "if", "index", "inet_addr", "inet_aton", "inet_network", "initgroups"
85+
, "ioctl", "link", "localtime_r", "lockf", "lseek", "lstat", "mkdir", "mkfifo", "mknod", "mkstemp"
86+
, "obstack_printf", "obstack_vprintf", "open", "opendir", "parse_printf_format", "pathconf"
87+
, "perror", "popen", "posix_fadvise", "posix_fallocate", "pread", "psignal", "pwrite", "read", "readahead"
88+
, "readdir", "readdir_r", "readlink", "readv", "realloc", "regcomp", "return", "rewinddir", "rindex"
89+
, "rmdir", "scandir", "seekdir", "setbuffer", "sethostname", "setlinebuf", "sizeof", "strdup"
90+
, "stat", "stpcpy", "strcasecmp", "stricmp", "strncasecmp", "switch"
91+
, "symlink", "sync_file_range", "telldir", "tempnam", "time", "typeid", "unlink"
92+
, "utime", "utimes", "vasprintf", "while", "wordexp", "write", "writev"
93+
};
9394

9495
//---------------------------------------------------------------------------
9596

@@ -506,18 +507,19 @@ bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname, con
506507
}
507508

508509
namespace {
509-
const std::set<std::string> call_func_keywords = make_container < std::set<std::string> > ()
510-
<< "asprintf"
511-
<< "delete"
512-
<< "fclose"
513-
<< "for"
514-
<< "free"
515-
<< "if"
516-
<< "realloc"
517-
<< "return"
518-
<< "switch"
519-
<< "while"
520-
<< "sizeof";
510+
const std::set<std::string> call_func_keywords = {
511+
"asprintf"
512+
, "delete"
513+
, "fclose"
514+
, "for"
515+
, "free"
516+
, "if"
517+
, "realloc"
518+
, "return"
519+
, "switch"
520+
, "while"
521+
, "sizeof"
522+
};
521523
}
522524

523525
const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<const Token *> callstack, const unsigned int varid, AllocType &alloctype, AllocType &dealloctype, bool &allocpar, unsigned int sz)

lib/checknullpointer.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,11 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
144144
}
145145

146146
namespace {
147-
const std::set<std::string> stl_stream = make_container< std::set<std::string> >() <<
148-
"fstream" << "ifstream" << "iostream" << "istream" <<
149-
"istringstream" << "ofstream" << "ostream" << "ostringstream" <<
150-
"stringstream" << "wistringstream" << "wostringstream" << "wstringstream";
147+
const std::set<std::string> stl_stream = {
148+
"fstream", "ifstream", "iostream", "istream",
149+
"istringstream", "ofstream", "ostream", "ostringstream",
150+
"stringstream", "wistringstream", "wostringstream", "wstringstream"
151+
};
151152
}
152153

153154
/**
@@ -375,9 +376,10 @@ void CheckNullPointer::nullPointer()
375376
}
376377

377378
namespace {
378-
const std::set<std::string> stl_istream = make_container< std::set<std::string> >() <<
379-
"fstream" << "ifstream" << "iostream" << "istream" <<
380-
"istringstream" << "stringstream" << "wistringstream" << "wstringstream";
379+
const std::set<std::string> stl_istream = {
380+
"fstream", "ifstream", "iostream", "istream",
381+
"istringstream", "stringstream", "wistringstream", "wstringstream"
382+
};
381383
}
382384

383385
/** Dereferencing null constant (simplified token list) */

lib/checkother.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -709,21 +709,21 @@ void CheckOther::checkRedundantAssignment()
709709

710710
void CheckOther::redundantCopyError(const Token *tok1, const Token* tok2, const std::string& var)
711711
{
712-
const std::list<const Token *> callstack = make_container< std::list<const Token *> >() << tok1 << tok2;
712+
const std::list<const Token *> callstack = { tok1, tok2 };
713713
reportError(callstack, Severity::performance, "redundantCopy",
714714
"Buffer '" + var + "' is being written before its old content has been used.", CWE563, false);
715715
}
716716

717717
void CheckOther::redundantCopyInSwitchError(const Token *tok1, const Token* tok2, const std::string &var)
718718
{
719-
const std::list<const Token *> callstack = make_container< std::list<const Token *> >() << tok1 << tok2;
719+
const std::list<const Token *> callstack = { tok1, tok2 };
720720
reportError(callstack, Severity::warning, "redundantCopyInSwitch",
721721
"Buffer '" + var + "' is being written before its old content has been used. 'break;' missing?", CWE563, false);
722722
}
723723

724724
void CheckOther::redundantAssignmentError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive)
725725
{
726-
const std::list<const Token *> callstack = make_container< std::list<const Token *> >() << tok1 << tok2;
726+
const std::list<const Token *> callstack = { tok1, tok2 };
727727
if (inconclusive)
728728
reportError(callstack, Severity::style, "redundantAssignment",
729729
"Variable '" + var + "' is reassigned a value before the old one has been used if variable is no semaphore variable.\n"
@@ -735,7 +735,7 @@ void CheckOther::redundantAssignmentError(const Token *tok1, const Token* tok2,
735735

736736
void CheckOther::redundantAssignmentInSwitchError(const Token *tok1, const Token* tok2, const std::string &var)
737737
{
738-
const std::list<const Token *> callstack = make_container< std::list<const Token *> >() << tok1 << tok2;
738+
const std::list<const Token *> callstack = { tok1, tok2 };
739739
reportError(callstack, Severity::warning, "redundantAssignInSwitch",
740740
"Variable '" + var + "' is reassigned a value before the old one has been used. 'break;' missing?", CWE563, false);
741741
}
@@ -1788,7 +1788,7 @@ void CheckOther::checkDuplicateBranch()
17881788

17891789
void CheckOther::duplicateBranchError(const Token *tok1, const Token *tok2)
17901790
{
1791-
const std::list<const Token *> toks = make_container< std::list<const Token *> >() << tok2 << tok1;
1791+
const std::list<const Token *> toks = { tok2, tok1 };
17921792

17931793
reportError(toks, Severity::style, "duplicateBranch", "Found duplicate branches for 'if' and 'else'.\n"
17941794
"Finding the same code in an 'if' and related 'else' branch is suspicious and "
@@ -2008,7 +2008,7 @@ void CheckOther::checkDuplicateExpression()
20082008

20092009
void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2, const std::string &op)
20102010
{
2011-
const std::list<const Token *> toks = make_container< std::list<const Token *> >() << tok2 << tok1;
2011+
const std::list<const Token *> toks = { tok2, tok1 };
20122012

20132013
reportError(toks, Severity::style, "duplicateExpression", "Same expression on both sides of \'" + op + "\'.\n"
20142014
"Finding the same expression on both sides of an operator is suspicious and might "
@@ -2018,7 +2018,7 @@ void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2,
20182018

20192019
void CheckOther::duplicateAssignExpressionError(const Token *tok1, const Token *tok2)
20202020
{
2021-
const std::list<const Token *> toks = make_container< std::list<const Token *> >() << tok2 << tok1;
2021+
const std::list<const Token *> toks = { tok2, tok1 };
20222022

20232023
reportError(toks, Severity::style, "duplicateAssignExpression",
20242024
"Same expression used in consecutive assignments of '" + tok1->str() + "' and '" + tok2->str() + "'.\n"

lib/checkstl.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -301,20 +301,23 @@ void CheckStl::mismatchingContainersError(const Token *tok)
301301
reportError(tok, Severity::error, "mismatchingContainers", "Iterators of different containers are used together.", CWE664, false);
302302
}
303303

304-
static const std::set<std::string> algorithm2 = make_container< std::set<std::string> >() // func(begin1, end1
305-
<< "binary_search" << "copy" << "copy_if" << "equal_range"
306-
<< "generate" << "is_heap" << "is_heap_until" << "is_partitioned"
307-
<< "is_permutation" << "is_sorted" << "is_sorted_until" << "lower_bound" << "make_heap" << "max_element" << "minmax_element"
308-
<< "min_element" << "mismatch" << "move" << "move_backward" << "next_permutation" << "partition" << "partition_copy"
309-
<< "partition_point" << "pop_heap" << "prev_permutation" << "push_heap" << "random_shuffle" << "remove" << "remove_copy"
310-
<< "remove_copy_if" << "remove_if" << "replace" << "replace_copy" << "replace_copy_if" << "replace_if" << "reverse" << "reverse_copy"
311-
<< "shuffle" << "sort" << "sort_heap" << "stable_partition" << "stable_sort" << "swap_ranges" << "transform" << "unique"
312-
<< "unique_copy" << "upper_bound" << "string" << "wstring" << "u16string" << "u32string";
313-
static const std::set<std::string> algorithm22 = make_container< std::set<std::string> >() // func(begin1 << end1 << begin2 << end2
314-
<< "includes" << "lexicographical_compare" << "merge" << "partial_sort_copy"
315-
<< "set_difference" << "set_intersection" << "set_symmetric_difference" << "set_union";
316-
static const std::set<std::string> algorithm1x1 = make_container< std::set<std::string> >() // func(begin1 << x << end1
317-
<< "nth_element" << "partial_sort" << "rotate" << "rotate_copy";
304+
static const std::set<std::string> algorithm2 = { // func(begin1, end1
305+
"binary_search", "copy", "copy_if", "equal_range"
306+
, "generate", "is_heap", "is_heap_until", "is_partitioned"
307+
, "is_permutation", "is_sorted", "is_sorted_until", "lower_bound", "make_heap", "max_element", "minmax_element"
308+
, "min_element", "mismatch", "move", "move_backward", "next_permutation", "partition", "partition_copy"
309+
, "partition_point", "pop_heap", "prev_permutation", "push_heap", "random_shuffle", "remove", "remove_copy"
310+
, "remove_copy_if", "remove_if", "replace", "replace_copy", "replace_copy_if", "replace_if", "reverse", "reverse_copy"
311+
, "shuffle", "sort", "sort_heap", "stable_partition", "stable_sort", "swap_ranges", "transform", "unique"
312+
, "unique_copy", "upper_bound", "string", "wstring", "u16string", "u32string"
313+
};
314+
static const std::set<std::string> algorithm22 = { // func(begin1, end1, begin2, end2
315+
"includes", "lexicographical_compare", "merge", "partial_sort_copy"
316+
, "set_difference", "set_intersection", "set_symmetric_difference", "set_union"
317+
};
318+
static const std::set<std::string> algorithm1x1 = { // func(begin1, x, end1
319+
"nth_element", "partial_sort", "rotate", "rotate_copy"
320+
};
318321

319322
static const std::string iteratorBeginFuncPattern = "begin|cbegin|rbegin|crbegin";
320323
static const std::string iteratorEndFuncPattern = "end|cend|rend|crend";
@@ -1040,8 +1043,9 @@ static bool isLocal(const Token *tok)
10401043
}
10411044

10421045
namespace {
1043-
const std::set<std::string> stl_string_stream = make_container< std::set<std::string> >() <<
1044-
"istringstream" << "ostringstream" << "stringstream" << "wstringstream" ;
1046+
const std::set<std::string> stl_string_stream = {
1047+
"istringstream", "ostringstream", "stringstream", "wstringstream"
1048+
};
10451049
}
10461050

10471051
void CheckStl::string_c_str()
@@ -1375,11 +1379,12 @@ void CheckStl::autoPointerMallocError(const Token *tok, const std::string& alloc
13751379
}
13761380

13771381
namespace {
1378-
const std::set<std::string> stl_containers_with_empty_and_clear = make_container< std::set<std::string> >() <<
1379-
"deque" << "forward_list" << "list" <<
1380-
"map" << "multimap" << "multiset" << "set" << "string" <<
1381-
"unordered_map" << "unordered_multimap" << "unordered_multiset" <<
1382-
"unordered_set" << "vector" << "wstring";
1382+
const std::set<std::string> stl_containers_with_empty_and_clear = {
1383+
"deque", "forward_list", "list",
1384+
"map", "multimap", "multiset", "set", "string",
1385+
"unordered_map", "unordered_multimap", "unordered_multiset",
1386+
"unordered_set", "vector", "wstring"
1387+
};
13831388

13841389
}
13851390

lib/checkunusedfunctions.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,9 @@ static bool isOperatorFunction(const std::string & funcName)
271271
return true;
272272
}
273273

274-
const std::vector<std::string> additionalOperators = make_container< std::vector<std::string> >()
275-
<< "new"
276-
<< "new[]"
277-
<< "delete"
278-
<< "delete[]";
274+
const std::vector<std::string> additionalOperators = {
275+
"new", "new[]", "delete", "delete[]"
276+
};
279277

280278

281279
return std::find(additionalOperators.begin(), additionalOperators.end(), funcName.substr(operatorPrefix.length())) != additionalOperators.end();;

0 commit comments

Comments
 (0)