From e2002db78d0c25624d97979aebb7cb7620aa70dc Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sun, 8 Apr 2018 22:54:10 +0200 Subject: [PATCH] Replaced make_container by C++11 initializer lists --- cli/cppcheckexecutor.cpp | 28 ++--- lib/checkclass.cpp | 2 +- lib/checkexceptionsafety.h | 2 +- lib/checkinternal.cpp | 33 +++--- lib/checkio.cpp | 21 ++-- lib/checkmemoryleak.cpp | 56 ++++----- lib/checknullpointer.cpp | 16 +-- lib/checkother.cpp | 14 +-- lib/checkstl.cpp | 47 ++++---- lib/checkunusedfunctions.cpp | 8 +- lib/errorlogger.cpp | 3 +- lib/symboldatabase.cpp | 42 ++++--- lib/token.cpp | 49 ++++---- lib/tokenize.cpp | 221 ++++++++++++++++++----------------- lib/utils.h | 32 ----- test/testsymboldatabase.cpp | 15 +-- 16 files changed, 285 insertions(+), 304 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 2febae141..c92dc6cbb 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -319,21 +319,21 @@ static bool IsAddressOnStack(const void* ptr) * For now we only want to detect abnormal behaviour for a few selected signals: */ -#define DECLARE_SIGNAL(x) << std::make_pair(x, #x) +#define DECLARE_SIGNAL(x) std::make_pair(x, #x) typedef std::map Signalmap_t; -static const Signalmap_t listofsignals = make_container< Signalmap_t > () - DECLARE_SIGNAL(SIGABRT) - DECLARE_SIGNAL(SIGBUS) - DECLARE_SIGNAL(SIGFPE) - DECLARE_SIGNAL(SIGILL) - DECLARE_SIGNAL(SIGINT) - DECLARE_SIGNAL(SIGQUIT) - DECLARE_SIGNAL(SIGSEGV) - DECLARE_SIGNAL(SIGSYS) - // don't care: SIGTERM - DECLARE_SIGNAL(SIGUSR1) - //DECLARE_SIGNAL(SIGUSR2) no usage currently - ; +static const Signalmap_t listofsignals = { + DECLARE_SIGNAL(SIGABRT), + DECLARE_SIGNAL(SIGBUS), + DECLARE_SIGNAL(SIGFPE), + DECLARE_SIGNAL(SIGILL), + DECLARE_SIGNAL(SIGINT), + DECLARE_SIGNAL(SIGQUIT), + DECLARE_SIGNAL(SIGSEGV), + DECLARE_SIGNAL(SIGSYS), + // don't care: SIGTERM + DECLARE_SIGNAL(SIGUSR1), + //DECLARE_SIGNAL(SIGUSR2) no usage currently +}; #undef DECLARE_SIGNAL /* * Entry pointer for signal handlers diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 1b7d0b37b..02f1fafd3 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1898,7 +1898,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const namespace { // The container contains the STL types whose operator[] is not a const. - const std::set stl_containers_not_const = make_container< std::set >() << "map" << "unordered_map"; + const std::set stl_containers_not_const = { "map", "unordered_map" }; } bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& memberAccessed) const diff --git a/lib/checkexceptionsafety.h b/lib/checkexceptionsafety.h index 60bab7f7c..0d78b20d0 100644 --- a/lib/checkexceptionsafety.h +++ b/lib/checkexceptionsafety.h @@ -129,7 +129,7 @@ private: /** Missing exception specification */ void unhandledExceptionSpecificationError(const Token * const tok1, const Token * const tok2, const std::string & funcname) { const std::string str1(tok1 ? tok1->str() : "foo"); - const std::list locationList = make_container< std::list > () << tok1 << tok2; + const std::list locationList = { tok1, tok2 }; reportError(locationList, Severity::style, "unhandledExceptionSpecification", "Unhandled exception specification when calling function " + str1 + "().\n" "Unhandled exception specification when calling function " + str1 + "(). " diff --git a/lib/checkinternal.cpp b/lib/checkinternal.cpp index a84d091c7..bdce41a8e 100644 --- a/lib/checkinternal.cpp +++ b/lib/checkinternal.cpp @@ -191,22 +191,23 @@ void CheckInternal::checkTokenSimpleMatchPatterns() } namespace { - const std::set knownPatterns = make_container< std::set > () - << "%any%" - << "%assign%" - << "%bool%" - << "%char%" - << "%comp%" - << "%num%" - << "%op%" - << "%cop%" - << "%or%" - << "%oror%" - << "%str%" - << "%type%" - << "%name%" - << "%var%" - << "%varid%"; + const std::set knownPatterns = { + "%any%" + , "%assign%" + , "%bool%" + , "%char%" + , "%comp%" + , "%num%" + , "%op%" + , "%cop%" + , "%or%" + , "%oror%" + , "%str%" + , "%type%" + , "%name%" + , "%var%" + , "%varid%" + }; } void CheckInternal::checkMissingPercentCharacter() diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 3d1111c1a..fd2a907c3 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -114,8 +114,7 @@ struct Filepointer { }; namespace { - const std::set whitelist = make_container< std::set > () - << "clearerr" << "feof" << "ferror" << "fgetpos" << "ftell" << "setbuf" << "setvbuf" << "ungetc" << "ungetwc"; + const std::set whitelist = { "clearerr", "feof", "ferror", "fgetpos", "ftell", "setbuf", "setvbuf", "ungetc", "ungetwc" }; } void CheckIO::checkFileUsage() @@ -1544,8 +1543,8 @@ CheckIO::ArgumentInfo::~ArgumentInfo() } namespace { - const std::set stl_vector = make_container< std::set >() << "array" << "vector"; - const std::set stl_string = make_container< std::set >() << "string" << "u16string" << "u32string" << "wstring"; + const std::set stl_vector = { "array", "vector" }; + const std::set stl_string = { "string", "u16string", "u32string", "wstring" }; } bool CheckIO::ArgumentInfo::isStdVectorOrString() @@ -1605,13 +1604,13 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString() } namespace { - const std::set stl_container = make_container< std::set >() << - "array" << "bitset" << "deque" << "forward_list" << - "hash_map" << "hash_multimap" << "hash_set" << - "list" << "map" << "multimap" << "multiset" << - "priority_queue" << "queue" << "set" << "stack" << - "unordered_map" << "unordered_multimap" << "unordered_multiset" << "unordered_set" << "vector" - ; + const std::set stl_container = { + "array", "bitset", "deque", "forward_list", + "hash_map", "hash_multimap", "hash_set", + "list", "map", "multimap", "multiset", + "priority_queue", "queue", "set", "stack", + "unordered_map", "unordered_multimap", "unordered_multiset", "unordered_set", "vector" + }; } bool CheckIO::ArgumentInfo::isStdContainer(const Token *tok) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 7ffc433d9..8076b25d7 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -75,21 +75,22 @@ static unsigned int countParameters(const Token *tok) * This list contains function names with const parameters e.g.: atof(const char *) * TODO: This list should be replaced by in .cfg files. */ -static const std::set call_func_white_list = make_container < std::set > () - << "_open" << "_wopen" << "access" << "adjtime" << "asctime_r" << "asprintf" << "chdir" << "chmod" << "chown" - << "creat" << "ctime_r" << "execl" << "execle" << "execlp" << "execv" << "execve" << "fchmod" << "fcntl" - << "fdatasync" << "fclose" << "flock" << "fmemopen" << "fnmatch" << "fopen" << "fopencookie" << "for" << "free" - << "freopen"<< "fseeko" << "fstat" << "fsync" << "ftello" << "ftruncate" << "getgrnam" << "gethostbyaddr" << "gethostbyname" - << "getnetbyname" << "getopt" << "getopt_long" << "getprotobyname" << "getpwnam" << "getservbyname" << "getservbyport" - << "glob" << "gmtime" << "gmtime_r" << "if" << "index" << "inet_addr" << "inet_aton" << "inet_network" << "initgroups" - << "ioctl" << "link" << "localtime_r" << "lockf" << "lseek" << "lstat" << "mkdir" << "mkfifo" << "mknod" << "mkstemp" - << "obstack_printf" << "obstack_vprintf" << "open" << "opendir" << "parse_printf_format" << "pathconf" - << "perror" << "popen" << "posix_fadvise" << "posix_fallocate" << "pread" << "psignal" << "pwrite" << "read" << "readahead" - << "readdir" << "readdir_r" << "readlink" << "readv" << "realloc" << "regcomp" << "return" << "rewinddir" << "rindex" - << "rmdir" << "scandir" << "seekdir" << "setbuffer" << "sethostname" << "setlinebuf" << "sizeof" << "strdup" - << "stat" << "stpcpy" << "strcasecmp" << "stricmp" << "strncasecmp" << "switch" - << "symlink" << "sync_file_range" << "telldir" << "tempnam" << "time" << "typeid" << "unlink" - << "utime" << "utimes" << "vasprintf" << "while" << "wordexp" << "write" << "writev"; +static const std::set call_func_white_list = { + "_open", "_wopen", "access", "adjtime", "asctime_r", "asprintf", "chdir", "chmod", "chown" + , "creat", "ctime_r", "execl", "execle", "execlp", "execv", "execve", "fchmod", "fcntl" + , "fdatasync", "fclose", "flock", "fmemopen", "fnmatch", "fopen", "fopencookie", "for", "free" + , "freopen", "fseeko", "fstat", "fsync", "ftello", "ftruncate", "getgrnam", "gethostbyaddr", "gethostbyname" + , "getnetbyname", "getopt", "getopt_long", "getprotobyname", "getpwnam", "getservbyname", "getservbyport" + , "glob", "gmtime", "gmtime_r", "if", "index", "inet_addr", "inet_aton", "inet_network", "initgroups" + , "ioctl", "link", "localtime_r", "lockf", "lseek", "lstat", "mkdir", "mkfifo", "mknod", "mkstemp" + , "obstack_printf", "obstack_vprintf", "open", "opendir", "parse_printf_format", "pathconf" + , "perror", "popen", "posix_fadvise", "posix_fallocate", "pread", "psignal", "pwrite", "read", "readahead" + , "readdir", "readdir_r", "readlink", "readv", "realloc", "regcomp", "return", "rewinddir", "rindex" + , "rmdir", "scandir", "seekdir", "setbuffer", "sethostname", "setlinebuf", "sizeof", "strdup" + , "stat", "stpcpy", "strcasecmp", "stricmp", "strncasecmp", "switch" + , "symlink", "sync_file_range", "telldir", "tempnam", "time", "typeid", "unlink" + , "utime", "utimes", "vasprintf", "while", "wordexp", "write", "writev" +}; //--------------------------------------------------------------------------- @@ -506,18 +507,19 @@ bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname, con } namespace { - const std::set call_func_keywords = make_container < std::set > () - << "asprintf" - << "delete" - << "fclose" - << "for" - << "free" - << "if" - << "realloc" - << "return" - << "switch" - << "while" - << "sizeof"; + const std::set call_func_keywords = { + "asprintf" + , "delete" + , "fclose" + , "for" + , "free" + , "if" + , "realloc" + , "return" + , "switch" + , "while" + , "sizeof" + }; } const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list callstack, const unsigned int varid, AllocType &alloctype, AllocType &dealloctype, bool &allocpar, unsigned int sz) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 677da04d8..244f0ff65 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -144,10 +144,11 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list stl_stream = make_container< std::set >() << - "fstream" << "ifstream" << "iostream" << "istream" << - "istringstream" << "ofstream" << "ostream" << "ostringstream" << - "stringstream" << "wistringstream" << "wostringstream" << "wstringstream"; + const std::set stl_stream = { + "fstream", "ifstream", "iostream", "istream", + "istringstream", "ofstream", "ostream", "ostringstream", + "stringstream", "wistringstream", "wostringstream", "wstringstream" + }; } /** @@ -375,9 +376,10 @@ void CheckNullPointer::nullPointer() } namespace { - const std::set stl_istream = make_container< std::set >() << - "fstream" << "ifstream" << "iostream" << "istream" << - "istringstream" << "stringstream" << "wistringstream" << "wstringstream"; + const std::set stl_istream = { + "fstream", "ifstream", "iostream", "istream", + "istringstream", "stringstream", "wistringstream", "wstringstream" + }; } /** Dereferencing null constant (simplified token list) */ diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 2ba5f0586..3318bcd38 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -709,21 +709,21 @@ void CheckOther::checkRedundantAssignment() void CheckOther::redundantCopyError(const Token *tok1, const Token* tok2, const std::string& var) { - const std::list callstack = make_container< std::list >() << tok1 << tok2; + const std::list callstack = { tok1, tok2 }; reportError(callstack, Severity::performance, "redundantCopy", "Buffer '" + var + "' is being written before its old content has been used.", CWE563, false); } void CheckOther::redundantCopyInSwitchError(const Token *tok1, const Token* tok2, const std::string &var) { - const std::list callstack = make_container< std::list >() << tok1 << tok2; + const std::list callstack = { tok1, tok2 }; reportError(callstack, Severity::warning, "redundantCopyInSwitch", "Buffer '" + var + "' is being written before its old content has been used. 'break;' missing?", CWE563, false); } void CheckOther::redundantAssignmentError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive) { - const std::list callstack = make_container< std::list >() << tok1 << tok2; + const std::list callstack = { tok1, tok2 }; if (inconclusive) reportError(callstack, Severity::style, "redundantAssignment", "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, void CheckOther::redundantAssignmentInSwitchError(const Token *tok1, const Token* tok2, const std::string &var) { - const std::list callstack = make_container< std::list >() << tok1 << tok2; + const std::list callstack = { tok1, tok2 }; reportError(callstack, Severity::warning, "redundantAssignInSwitch", "Variable '" + var + "' is reassigned a value before the old one has been used. 'break;' missing?", CWE563, false); } @@ -1788,7 +1788,7 @@ void CheckOther::checkDuplicateBranch() void CheckOther::duplicateBranchError(const Token *tok1, const Token *tok2) { - const std::list toks = make_container< std::list >() << tok2 << tok1; + const std::list toks = { tok2, tok1 }; reportError(toks, Severity::style, "duplicateBranch", "Found duplicate branches for 'if' and 'else'.\n" "Finding the same code in an 'if' and related 'else' branch is suspicious and " @@ -2008,7 +2008,7 @@ void CheckOther::checkDuplicateExpression() void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2, const std::string &op) { - const std::list toks = make_container< std::list >() << tok2 << tok1; + const std::list toks = { tok2, tok1 }; reportError(toks, Severity::style, "duplicateExpression", "Same expression on both sides of \'" + op + "\'.\n" "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, void CheckOther::duplicateAssignExpressionError(const Token *tok1, const Token *tok2) { - const std::list toks = make_container< std::list >() << tok2 << tok1; + const std::list toks = { tok2, tok1 }; reportError(toks, Severity::style, "duplicateAssignExpression", "Same expression used in consecutive assignments of '" + tok1->str() + "' and '" + tok2->str() + "'.\n" diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index f803c4e28..2a776e380 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -301,20 +301,23 @@ void CheckStl::mismatchingContainersError(const Token *tok) reportError(tok, Severity::error, "mismatchingContainers", "Iterators of different containers are used together.", CWE664, false); } -static const std::set algorithm2 = make_container< std::set >() // func(begin1, end1 - << "binary_search" << "copy" << "copy_if" << "equal_range" - << "generate" << "is_heap" << "is_heap_until" << "is_partitioned" - << "is_permutation" << "is_sorted" << "is_sorted_until" << "lower_bound" << "make_heap" << "max_element" << "minmax_element" - << "min_element" << "mismatch" << "move" << "move_backward" << "next_permutation" << "partition" << "partition_copy" - << "partition_point" << "pop_heap" << "prev_permutation" << "push_heap" << "random_shuffle" << "remove" << "remove_copy" - << "remove_copy_if" << "remove_if" << "replace" << "replace_copy" << "replace_copy_if" << "replace_if" << "reverse" << "reverse_copy" - << "shuffle" << "sort" << "sort_heap" << "stable_partition" << "stable_sort" << "swap_ranges" << "transform" << "unique" - << "unique_copy" << "upper_bound" << "string" << "wstring" << "u16string" << "u32string"; -static const std::set algorithm22 = make_container< std::set >() // func(begin1 << end1 << begin2 << end2 - << "includes" << "lexicographical_compare" << "merge" << "partial_sort_copy" - << "set_difference" << "set_intersection" << "set_symmetric_difference" << "set_union"; -static const std::set algorithm1x1 = make_container< std::set >() // func(begin1 << x << end1 - << "nth_element" << "partial_sort" << "rotate" << "rotate_copy"; +static const std::set algorithm2 = { // func(begin1, end1 + "binary_search", "copy", "copy_if", "equal_range" + , "generate", "is_heap", "is_heap_until", "is_partitioned" + , "is_permutation", "is_sorted", "is_sorted_until", "lower_bound", "make_heap", "max_element", "minmax_element" + , "min_element", "mismatch", "move", "move_backward", "next_permutation", "partition", "partition_copy" + , "partition_point", "pop_heap", "prev_permutation", "push_heap", "random_shuffle", "remove", "remove_copy" + , "remove_copy_if", "remove_if", "replace", "replace_copy", "replace_copy_if", "replace_if", "reverse", "reverse_copy" + , "shuffle", "sort", "sort_heap", "stable_partition", "stable_sort", "swap_ranges", "transform", "unique" + , "unique_copy", "upper_bound", "string", "wstring", "u16string", "u32string" +}; +static const std::set algorithm22 = { // func(begin1, end1, begin2, end2 + "includes", "lexicographical_compare", "merge", "partial_sort_copy" + , "set_difference", "set_intersection", "set_symmetric_difference", "set_union" +}; +static const std::set algorithm1x1 = { // func(begin1, x, end1 + "nth_element", "partial_sort", "rotate", "rotate_copy" +}; static const std::string iteratorBeginFuncPattern = "begin|cbegin|rbegin|crbegin"; static const std::string iteratorEndFuncPattern = "end|cend|rend|crend"; @@ -1040,8 +1043,9 @@ static bool isLocal(const Token *tok) } namespace { - const std::set stl_string_stream = make_container< std::set >() << - "istringstream" << "ostringstream" << "stringstream" << "wstringstream" ; + const std::set stl_string_stream = { + "istringstream", "ostringstream", "stringstream", "wstringstream" + }; } void CheckStl::string_c_str() @@ -1375,11 +1379,12 @@ void CheckStl::autoPointerMallocError(const Token *tok, const std::string& alloc } namespace { - const std::set stl_containers_with_empty_and_clear = make_container< std::set >() << - "deque" << "forward_list" << "list" << - "map" << "multimap" << "multiset" << "set" << "string" << - "unordered_map" << "unordered_multimap" << "unordered_multiset" << - "unordered_set" << "vector" << "wstring"; + const std::set stl_containers_with_empty_and_clear = { + "deque", "forward_list", "list", + "map", "multimap", "multiset", "set", "string", + "unordered_map", "unordered_multimap", "unordered_multiset", + "unordered_set", "vector", "wstring" + }; } diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 28a4e7079..2f15abbd3 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -271,11 +271,9 @@ static bool isOperatorFunction(const std::string & funcName) return true; } - const std::vector additionalOperators = make_container< std::vector >() - << "new" - << "new[]" - << "delete" - << "delete[]"; + const std::vector additionalOperators = { + "new", "new[]", "delete", "delete[]" + }; return std::find(additionalOperators.begin(), additionalOperators.end(), funcName.substr(operatorPrefix.length())) != additionalOperators.end();; diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 3618a947e..ca7ec0fc6 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -493,8 +493,7 @@ void ErrorLogger::reportUnmatchedSuppressions(const std::list callStack = make_container< std::list > () - << ErrorLogger::ErrorMessage::FileLocation(i->file, i->line); + const std::list callStack = { ErrorLogger::ErrorMessage::FileLocation(i->file, i->line) }; reportErr(ErrorLogger::ErrorMessage(callStack, emptyString, Severity::information, "Unmatched suppression: " + i->id, "unmatchedSuppression", false)); } } diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 75920d53a..8deaac73f 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -4712,25 +4712,29 @@ Function * SymbolDatabase::findFunctionInScope(const Token *func, const Scope *n //--------------------------------------------------------------------------- namespace { - const std::set c_keywords = make_container< std::set >() << - "_Bool" << "auto" << "break" << "case" << "char" << "const" << "continue" << "default" << "do" << - "double" << "else" << "enum" << "extern" << "float" << "for" << "goto" << "if" << "inline" << - "int" << "long" << "register" << "restrict" << "return" << "short" << "signed" << "sizeof" << - "static" << "struct" << "switch" << "typedef" << "union" << "unsigned" << "void" << "volatile" << - "while"; - const std::set cpp_keywords = make_container< std::set >() << - c_keywords << - "alignas" << "alignof" << "and" << "and_eq" << "asm" << "auto" << "bitand" << "bitor" << "bool" << - "break" << "case" << "catch" << "char" << "class" << "compl" << - "concept" << "const" << "constexpr" << "const_cast" << "continue" << "decltype" << "default" << - "delete" << "do" << "double" << "dynamic_cast" << "else" << "enum" << "explicit" << "export" << - "extern" << "false" << "float" << "for" << "friend" << "goto" << "if" << "inline" << "int" << "long" << - "mutable" << "namespace" << "new" << "noexcept" << "not" << "not_eq" << "nullptr" << "operator" << - "or" << "or_eq" << "private" << "protected" << "public" << "register" << "reinterpret_cast" << - "requires" << "return" << "short" << "signed" << "sizeof" << "static" << "static_assert" << - "static_cast" << "struct" << "switch" << "template" << "this" << "thread_local" << "throw" << - "true" << "try" << "typedef" << "typeid" << "typename" << "union" << "unsigned" << "using" << - "virtual" << "void" << "volatile" << "wchar_t" << "while" << "xor" << "xor_eq"; + +#define C_KEYWORDS\ + "_Bool", "auto", "break", "case", "char", "const", "continue", "default", "do",\ + "double", "else", "enum", "extern", "float", "for", "goto", "if", "inline",\ + "int", "long", "register", "restrict", "return", "short", "signed", "sizeof",\ + "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",\ + "while" + + const std::set c_keywords = { C_KEYWORDS }; + const std::set cpp_keywords = { + C_KEYWORDS, + "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", + "break", "case", "catch", "char", "class", "compl", + "concept", "const", "constexpr", "const_cast", "continue", "decltype", "default", + "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", + "extern", "false", "float", "for", "friend", "goto", "if", "inline", "int", "long", + "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", + "or", "or_eq", "private", "protected", "public", "register", "reinterpret_cast", + "requires", "return", "short", "signed", "sizeof", "static", "static_assert", + "static_cast", "struct", "switch", "template", "this", "thread_local", "throw", + "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", + "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq" + }; } bool SymbolDatabase::isReservedName(const std::string& iName) const diff --git a/lib/token.cpp b/lib/token.cpp index 5aa76bb8e..dbb749488 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -65,18 +65,19 @@ Token::~Token() delete _values; } -static const std::set controlFlowKeywords = make_container< std::set > () << - "goto" << - "do" << - "if" << - "else" << - "for" << - "while" << - "switch" << - "case" << - "break" << - "continue" << - "return"; +static const std::set controlFlowKeywords = { + "goto", + "do", + "if", + "else", + "for", + "while", + "switch", + "case", + "break", + "continue", + "return" +}; void Token::update_property_info() { @@ -133,18 +134,18 @@ void Token::update_property_info() update_property_isStandardType(); } -static const std::set stdTypes = - make_container >() << "bool" - << "_Bool" - << "char" - << "double" - << "float" - << "int" - << "long" - << "short" - << "size_t" - << "void" - << "wchar_t"; +static const std::set stdTypes = { "bool" + , "_Bool" + , "char" + , "double" + , "float" + , "int" + , "long" + , "short" + , "size_t" + , "void" + , "wchar_t" + }; void Token::update_property_isStandardType() { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9970ca067..faa12f994 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2568,11 +2568,11 @@ void Tokenizer::setVarId() // Variable declarations can't start with "return" etc. -static const std::set notstart_c = make_container< std::set > () - << "goto" << "NOT" << "return" << "sizeof"<< "typedef"; -static const std::set notstart_cpp = make_container< std::set > () - << notstart_c - << "delete" << "friend" << "new" << "throw" << "using" << "virtual" << "explicit" << "const_cast" << "dynamic_cast" << "reinterpret_cast" << "static_cast" << "template"; +#define NOTSTART_C "goto", "NOT", "return", "sizeof", "typedef" +static const std::set notstart_c = { NOTSTART_C }; +static const std::set notstart_cpp = { NOTSTART_C, + "delete", "friend", "new", "throw", "using", "virtual", "explicit", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast", "template" + }; void Tokenizer::setVarIdPass1() { @@ -6164,16 +6164,17 @@ void Tokenizer::simplifyVariableMultipleAssign() } // Binary operators simplification map -static const std::map cAlternativeTokens = make_container< std::map >() - << std::make_pair("and", "&&") - << std::make_pair("and_eq", "&=") - << std::make_pair("bitand", "&") - << std::make_pair("bitor", "|") - << std::make_pair("not_eq", "!=") - << std::make_pair("or", "||") - << std::make_pair("or_eq", "|=") - << std::make_pair("xor", "^") - << std::make_pair("xor_eq", "^=") ; +static const std::map cAlternativeTokens = { + std::make_pair("and", "&&") + , std::make_pair("and_eq", "&=") + , std::make_pair("bitand", "&") + , std::make_pair("bitor", "|") + , std::make_pair("not_eq", "!=") + , std::make_pair("or", "||") + , std::make_pair("or_eq", "|=") + , std::make_pair("xor", "^") + , std::make_pair("xor_eq", "^=") +}; // Simplify the C alternative tokens: // and => && @@ -7486,14 +7487,15 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr) return false; } -static const std::set stdFunctionsPresentInC = make_container< std::set > () << - "strcat" << - "strcpy" << - "strncat" << - "strncpy" << - "free" << - "malloc" << - "strdup"; +static const std::set stdFunctionsPresentInC = { + "strcat", + "strcpy", + "strncat", + "strncpy", + "free", + "malloc", + "strdup" +}; void Tokenizer::simplifyStd() { @@ -8942,15 +8944,16 @@ void Tokenizer::simplifyCPPAttribute() } } -static const std::set keywords = make_container< std::set >() - << "volatile" - << "inline" - << "_inline" - << "__inline" - << "__forceinline" - << "register" - << "__restrict" - << "__restrict__" ; +static const std::set keywords = { + "volatile" + , "inline" + , "_inline" + , "__inline" + , "__forceinline" + , "register" + , "__restrict" + , "__restrict__" +}; // Remove "volatile", "inline", "register", "restrict", "override", "final", "static" and "constexpr" // "restrict" keyword // - New to 1999 ANSI/ISO C standard @@ -9335,49 +9338,52 @@ void Tokenizer::simplifyBitfields() // Types and objects in std namespace that are neither functions nor templates -static const std::set stdTypes = make_container >() << - "string"<< "wstring"<< "u16string"<< "u32string" << - "iostream"<< "ostream"<< "ofstream"<< "ostringstream" << - "istream"<< "ifstream"<< "istringstream"<< "fstream"<< "stringstream" << - "wstringstream"<< "wistringstream"<< "wostringstream"<< "wstringbuf" << - "stringbuf"<< "streambuf"<< "ios"<< "filebuf"<< "ios_base" << - "exception"<< "bad_exception"<< "bad_alloc" << - "logic_error"<< "domain_error"<< "invalid_argument_"<< "length_error" << - "out_of_range"<< "runtime_error"<< "range_error"<< "overflow_error"<< "underflow_error" << - "locale" << - "cout"<< "cerr"<< "clog"<< "cin" << - "wcerr"<< "wcin"<< "wclog"<< "wcout" << - "endl"<< "ends"<< "flush" << - "boolalpha"<< "noboolalpha"<< "showbase"<< "noshowbase" << - "showpoint"<< "noshowpoint"<< "showpos"<< "noshowpos" << - "skipws"<< "noskipws"<< "unitbuf"<< "nounitbuf"<< "uppercase"<< "nouppercase" << - "dec"<< "hex"<< "oct" << - "fixed"<< "scientific" << - "internal"<< "left"<< "right" << - "fpos"<< "streamoff"<< "streampos"<< "streamsize"; +static const std::set stdTypes = { + "string", "wstring", "u16string", "u32string", + "iostream", "ostream", "ofstream", "ostringstream", + "istream", "ifstream", "istringstream", "fstream", "stringstream", + "wstringstream", "wistringstream", "wostringstream", "wstringbuf", + "stringbuf", "streambuf", "ios", "filebuf", "ios_base", + "exception", "bad_exception", "bad_alloc", + "logic_error", "domain_error", "invalid_argument_", "length_error", + "out_of_range", "runtime_error", "range_error", "overflow_error", "underflow_error", + "locale", + "cout", "cerr", "clog", "cin", + "wcerr", "wcin", "wclog", "wcout", + "endl", "ends", "flush", + "boolalpha", "noboolalpha", "showbase", "noshowbase", + "showpoint", "noshowpoint", "showpos", "noshowpos", + "skipws", "noskipws", "unitbuf", "nounitbuf", "uppercase", "nouppercase", + "dec", "hex", "oct", + "fixed", "scientific", + "internal", "left", "right", + "fpos", "streamoff", "streampos", "streamsize" +}; -static const std::set stdTemplates = make_container >() << - "array"<< "basic_string"<< "bitset"<< "deque"<< "list"<< "map"<< "multimap" << - "priority_queue"<< "queue"<< "set"<< "multiset"<< "stack"<< "vector"<< "pair" << - "iterator"<< "iterator_traits" << - "unordered_map"<< "unordered_multimap"<< "unordered_set"<< "unordered_multiset" << - "tuple"<< "function"; -static const std::set stdFunctions = make_container >() << - "getline" << - "for_each"<< "find"<< "find_if"<< "find_end"<< "find_first_of" << - "adjacent_find"<< "count"<< "count_if"<< "mismatch"<< "equal"<< "search"<< "search_n" << - "copy"<< "copy_backward"<< "swap"<< "swap_ranges"<< "iter_swap"<< "transform"<< "replace" << - "replace_if"<< "replace_copy"<< "replace_copy_if"<< "fill"<< "fill_n"<< "generate"<< "generate_n"<< "remove" << - "remove_if"<< "remove_copy"<< "remove_copy_if" << - "unique"<< "unique_copy"<< "reverse"<< "reverse_copy" << - "rotate"<< "rotate_copy"<< "random_shuffle"<< "partition"<< "stable_partition" << - "sort"<< "stable_sort"<< "partial_sort"<< "partial_sort_copy"<< "nth_element" << - "lower_bound"<< "upper_bound"<< "equal_range"<< "binary_search"<< "merge"<< "inplace_merge"<< "includes" << - "set_union"<< "set_intersection"<< "set_difference" << - "set_symmetric_difference"<< "push_heap"<< "pop_heap"<< "make_heap"<< "sort_heap" << - "min"<< "max"<< "min_element"<< "max_element"<< "lexicographical_compare"<< "next_permutation"<< "prev_permutation" << - "advance"<< "back_inserter"<< "distance"<< "front_inserter"<< "inserter" << - "make_pair"<< "make_shared"<< "make_tuple"; +static const std::set stdTemplates = { + "array", "basic_string", "bitset", "deque", "list", "map", "multimap", + "priority_queue", "queue", "set", "multiset", "stack", "vector", "pair", + "iterator", "iterator_traits", + "unordered_map", "unordered_multimap", "unordered_set", "unordered_multiset", + "tuple", "function" +}; +static const std::set stdFunctions = { + "getline", + "for_each", "find", "find_if", "find_end", "find_first_of", + "adjacent_find", "count", "count_if", "mismatch", "equal", "search", "search_n", + "copy", "copy_backward", "swap", "swap_ranges", "iter_swap", "transform", "replace", + "replace_if", "replace_copy", "replace_copy_if", "fill", "fill_n", "generate", "generate_n", "remove", + "remove_if", "remove_copy", "remove_copy_if", + "unique", "unique_copy", "reverse", "reverse_copy", + "rotate", "rotate_copy", "random_shuffle", "partition", "stable_partition", + "sort", "stable_sort", "partial_sort", "partial_sort_copy", "nth_element", + "lower_bound", "upper_bound", "equal_range", "binary_search", "merge", "inplace_merge", "includes", + "set_union", "set_intersection", "set_difference", + "set_symmetric_difference", "push_heap", "pop_heap", "make_heap", "sort_heap", + "min", "max", "min_element", "max_element", "lexicographical_compare", "next_permutation", "prev_permutation", + "advance", "back_inserter", "distance", "front_inserter", "inserter", + "make_pair", "make_shared", "make_tuple" +}; // Add std:: in front of std classes, when using namespace std; was given @@ -9482,39 +9488,40 @@ namespace { std::string mbcs, unicode; }; - const std::map apis = make_container< std::map >() << - std::make_pair("_topen", triplet("open", "_wopen")) << - std::make_pair("_tsopen_s", triplet("_sopen_s", "_wsopen_s")) << - std::make_pair("_tfopen", triplet("fopen", "_wfopen")) << - std::make_pair("_tfopen_s", triplet("fopen_s", "_wfopen_s")) << - std::make_pair("_tfreopen", triplet("freopen", "_wfreopen")) << - std::make_pair("_tfreopen_s", triplet("freopen_s", "_wfreopen_s")) << - std::make_pair("_tcscat", triplet("strcat", "wcscat")) << - std::make_pair("_tcschr", triplet("strchr", "wcschr")) << - std::make_pair("_tcscmp", triplet("strcmp", "wcscmp")) << - std::make_pair("_tcsdup", triplet("strdup", "wcsdup")) << - std::make_pair("_tcscpy", triplet("strcpy", "wcscpy")) << - std::make_pair("_tcslen", triplet("strlen", "wcslen")) << - std::make_pair("_tcsncat", triplet("strncat", "wcsncat")) << - std::make_pair("_tcsncpy", triplet("strncpy", "wcsncpy")) << - std::make_pair("_tcsnlen", triplet("strnlen", "wcsnlen")) << - std::make_pair("_tcsrchr", triplet("strrchr", "wcsrchr")) << - std::make_pair("_tcsstr", triplet("strstr", "wcsstr")) << - std::make_pair("_tcstok", triplet("strtok", "wcstok")) << - std::make_pair("_ftprintf", triplet("fprintf", "fwprintf")) << - std::make_pair("_tprintf", triplet("printf", "wprintf")) << - std::make_pair("_stprintf", triplet("sprintf", "swprintf")) << - std::make_pair("_sntprintf", triplet("_snprintf", "_snwprintf")) << - std::make_pair("_ftscanf", triplet("fscanf", "fwscanf")) << - std::make_pair("_tscanf", triplet("scanf", "wscanf")) << - std::make_pair("_stscanf", triplet("sscanf", "swscanf")) << - std::make_pair("_ftprintf_s", triplet("fprintf_s", "fwprintf_s")) << - std::make_pair("_tprintf_s", triplet("printf_s", "wprintf_s")) << - std::make_pair("_stprintf_s", triplet("sprintf_s", "swprintf_s")) << - std::make_pair("_sntprintf_s", triplet("_snprintf_s", "_snwprintf_s")) << - std::make_pair("_ftscanf_s", triplet("fscanf_s", "fwscanf_s")) << - std::make_pair("_tscanf_s", triplet("scanf_s", "wscanf_s")) << - std::make_pair("_stscanf_s", triplet("sscanf_s", "swscanf_s")); + const std::map apis = { + std::make_pair("_topen", triplet("open", "_wopen")), + std::make_pair("_tsopen_s", triplet("_sopen_s", "_wsopen_s")), + std::make_pair("_tfopen", triplet("fopen", "_wfopen")), + std::make_pair("_tfopen_s", triplet("fopen_s", "_wfopen_s")), + std::make_pair("_tfreopen", triplet("freopen", "_wfreopen")), + std::make_pair("_tfreopen_s", triplet("freopen_s", "_wfreopen_s")), + std::make_pair("_tcscat", triplet("strcat", "wcscat")), + std::make_pair("_tcschr", triplet("strchr", "wcschr")), + std::make_pair("_tcscmp", triplet("strcmp", "wcscmp")), + std::make_pair("_tcsdup", triplet("strdup", "wcsdup")), + std::make_pair("_tcscpy", triplet("strcpy", "wcscpy")), + std::make_pair("_tcslen", triplet("strlen", "wcslen")), + std::make_pair("_tcsncat", triplet("strncat", "wcsncat")), + std::make_pair("_tcsncpy", triplet("strncpy", "wcsncpy")), + std::make_pair("_tcsnlen", triplet("strnlen", "wcsnlen")), + std::make_pair("_tcsrchr", triplet("strrchr", "wcsrchr")), + std::make_pair("_tcsstr", triplet("strstr", "wcsstr")), + std::make_pair("_tcstok", triplet("strtok", "wcstok")), + std::make_pair("_ftprintf", triplet("fprintf", "fwprintf")), + std::make_pair("_tprintf", triplet("printf", "wprintf")), + std::make_pair("_stprintf", triplet("sprintf", "swprintf")), + std::make_pair("_sntprintf", triplet("_snprintf", "_snwprintf")), + std::make_pair("_ftscanf", triplet("fscanf", "fwscanf")), + std::make_pair("_tscanf", triplet("scanf", "wscanf")), + std::make_pair("_stscanf", triplet("sscanf", "swscanf")), + std::make_pair("_ftprintf_s", triplet("fprintf_s", "fwprintf_s")), + std::make_pair("_tprintf_s", triplet("printf_s", "wprintf_s")), + std::make_pair("_stprintf_s", triplet("sprintf_s", "swprintf_s")), + std::make_pair("_sntprintf_s", triplet("_snprintf_s", "_snwprintf_s")), + std::make_pair("_ftscanf_s", triplet("fscanf_s", "fwscanf_s")), + std::make_pair("_tscanf_s", triplet("scanf_s", "wscanf_s")), + std::make_pair("_stscanf_s", triplet("sscanf_s", "swscanf_s")) + }; } void Tokenizer::simplifyMicrosoftStringFunctions() diff --git a/lib/utils.h b/lib/utils.h index b37a318d0..9f22194e3 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -26,38 +26,6 @@ #include #include -/*! Helper class to aid in the initializing global const data */ -template < typename Cont > -class make_container { -public: - typedef make_container< Cont > my_type; - typedef typename Cont::value_type T; - - my_type& operator<< (const T& val) { - data_.insert(data_.end(), val); - return *this; - } - my_type& operator<< (const Cont& other_container) { - for (typename Cont::const_iterator it=other_container.begin(); it!=other_container.end(); ++it) { - data_.insert(data_.end(), *it); - } - return *this; - } - my_type& operator<< (T&& val) { - data_.insert(data_.end(), val); - return *this; - } - my_type& operator<< (const char* val) { - data_.insert(data_.end(), val); - return *this; - } - operator Cont() const { - return data_; - } -private: - Cont data_; -}; - inline bool endsWith(const std::string &str, char c) { return str[str.size()-1U] == c; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index ab7dee342..2898c17bd 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -798,10 +798,8 @@ private: bool result = si.isVariableDeclaration(list.front(), vartok, typetok); ASSERT_EQUALS(true, result); Variable v(vartok, list.front(), list.back(), 0, Public, 0, 0, &settings1.library); - static const std::set types = make_container< std::set >() << - "string" << "wstring" ; - static const std::set no_types = make_container< std::set >() << - "set" ; + static const std::set types = { "string", "wstring" }; + static const std::set no_types = { "set" }; ASSERT_EQUALS(true, v.isStlType()); ASSERT_EQUALS(true, v.isStlType(types)); ASSERT_EQUALS(false, v.isStlType(no_types)); @@ -816,10 +814,8 @@ private: bool result = si.isVariableDeclaration(list.front(), vartok, typetok); ASSERT_EQUALS(true, result); Variable v(vartok, list.front(), list.back(), 0, Public, 0, 0, &settings1.library); - static const std::set types = make_container< std::set >() << - "bitset" << "set" << "vector" << "wstring" ; - static const std::set no_types = make_container< std::set >() << - "bitset" << "map" << "set" ; + static const std::set types = { "bitset", "set", "vector", "wstring" }; + static const std::set no_types = { "bitset", "map", "set" }; ASSERT_EQUALS(true, v.isStlType()); ASSERT_EQUALS(true, v.isStlType(types)); ASSERT_EQUALS(false, v.isStlType(no_types)); @@ -833,8 +829,7 @@ private: bool result = si.isVariableDeclaration(list.front(), vartok, typetok); ASSERT_EQUALS(true, result); Variable v(vartok, list.front(), list.back(), 0, Public, 0, 0, &settings1.library); - static const std::set types = make_container< std::set >() << - "bitset" << "set" << "vector" ; + static const std::set types = { "bitset", "set", "vector" }; ASSERT_EQUALS(false, v.isStlType()); ASSERT_EQUALS(false, v.isStlType(types)); ASSERT_EQUALS(false, v.isStlStringType());