From f762affea03e7b7fcd61d8d5c7984027e61bcd53 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Mon, 30 Nov 2015 22:13:49 +0100 Subject: [PATCH] Small refactoring: replace NULL by nullptr, remove redundant static keyword, Tokenizer::setVarId() uses const variable 'notstart' --- cli/cppcheckexecutor.cpp | 10 +++++----- cli/filelister.cpp | 4 ++-- cli/main.cpp | 2 +- cli/threadexecutor.cpp | 2 +- lib/checkclass.cpp | 2 +- lib/checkio.cpp | 10 +++++----- lib/checknullpointer.cpp | 4 ++-- lib/checkstl.cpp | 20 ++++++++------------ lib/checkuninitvar.cpp | 6 +++--- lib/path.cpp | 2 +- lib/tokenize.cpp | 20 ++++++++++---------- tools/reduce.cpp | 4 ++-- 12 files changed, 41 insertions(+), 45 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 497343f74..88ccf6596 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -580,10 +580,10 @@ namespace { hThread, &stack, &context, - NULL, + nullptr, pSymFunctionTableAccess64, pSymGetModuleBase64, - NULL + nullptr ); if (!result) // official end... break; @@ -748,7 +748,7 @@ int CppCheckExecutor::check_wrapper(CppCheck& cppcheck, int argc, const char* co segv_stack.ss_sp = mytstack; segv_stack.ss_flags = 0; segv_stack.ss_size = MYSTACKSIZE; - sigaltstack(&segv_stack, NULL); + sigaltstack(&segv_stack, nullptr); // install signal handler struct sigaction act; @@ -756,7 +756,7 @@ int CppCheckExecutor::check_wrapper(CppCheck& cppcheck, int argc, const char* co act.sa_flags=SA_SIGINFO|SA_ONSTACK; act.sa_sigaction=CppcheckSignalHandler; for (std::map::const_iterator sig=listofsignals.begin(); sig!=listofsignals.end(); ++sig) { - sigaction(sig->first, &act, NULL); + sigaction(sig->first, &act, nullptr); } return check_internal(cppcheck, argc, argv); #else @@ -912,7 +912,7 @@ void CppCheckExecutor::reportProgress(const std::string &filename, const char st return; // Report progress messages every 10 seconds - const std::time_t time2 = std::time(NULL); + const std::time_t time2 = std::time(nullptr); if (time2 >= (time1 + 10)) { time1 = time2; diff --git a/cli/filelister.cpp b/cli/filelister.cpp index c117ba39f..61e83076d 100644 --- a/cli/filelister.cpp +++ b/cli/filelister.cpp @@ -176,11 +176,11 @@ std::string FileLister::getAbsolutePath(const std::string& path) #ifdef PATH_MAX char buf[PATH_MAX]; - if (realpath(path.c_str(), buf) != NULL) + if (realpath(path.c_str(), buf) != nullptr) absolute_path = buf; #else char *dynamic_buf; - if ((dynamic_buf = realpath(path.c_str(), NULL)) != NULL) { + if ((dynamic_buf = realpath(path.c_str(), nullptr)) != nullptr) { absolute_path = dynamic_buf; free(dynamic_buf); } diff --git a/cli/main.cpp b/cli/main.cpp index 9e32158cd..1c268f71c 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -126,7 +126,7 @@ int main(int argc, char* argv[]) CppCheckExecutor exec; #ifdef _WIN32 - GetModuleFileNameA(NULL, exename, sizeof(exename)/sizeof(exename[0])-1); + GetModuleFileNameA(nullptr, exename, sizeof(exename)/sizeof(exename[0])-1); argv[0] = exename; #endif diff --git a/cli/threadexecutor.cpp b/cli/threadexecutor.cpp index 861c51f24..37316118b 100644 --- a/cli/threadexecutor.cpp +++ b/cli/threadexecutor.cpp @@ -238,7 +238,7 @@ unsigned int ThreadExecutor::check() struct timeval tv; // for every second polling of load average condition tv.tv_sec = 1; tv.tv_usec = 0; - int r = select(*std::max_element(rpipes.begin(), rpipes.end()) + 1, &rfds, NULL, NULL, &tv); + int r = select(*std::max_element(rpipes.begin(), rpipes.end()) + 1, &rfds, nullptr, nullptr, &tv); if (r > 0) { std::list::iterator rp = rpipes.begin(); diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 43408f18b..654dddb88 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1847,7 +1847,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const namespace { // The container contains the STL types whose operator[] is not a const. - static const std::set stl_containers_not_const = make_container< std::set >() << "map" << "unordered_map"; + const std::set stl_containers_not_const = make_container< std::set >() << "map" << "unordered_map"; } bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& memberAccessed) const diff --git a/lib/checkio.cpp b/lib/checkio.cpp index b828a667e..191619a95 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -95,8 +95,8 @@ struct Filepointer { }; namespace { - static const std::set whitelist = make_container< std::set > () - << "clearerr" << "feof" << "ferror" << "fgetpos" << "ftell" << "setbuf" << "setvbuf" << "ungetc" << "ungetwc"; + const std::set whitelist = make_container< std::set > () + << "clearerr" << "feof" << "ferror" << "fgetpos" << "ftell" << "setbuf" << "setvbuf" << "ungetc" << "ungetwc"; } void CheckIO::checkFileUsage() @@ -1552,8 +1552,8 @@ CheckIO::ArgumentInfo::~ArgumentInfo() } namespace { - static const std::set stl_vector = make_container< std::set >() << "array" << "vector"; - static const std::set stl_string = make_container< std::set >() << "string" << "u16string" << "u32string" << "wstring"; + const std::set stl_vector = make_container< std::set >() << "array" << "vector"; + const std::set stl_string = make_container< std::set >() << "string" << "u16string" << "u32string" << "wstring"; } bool CheckIO::ArgumentInfo::isStdVectorOrString() @@ -1614,7 +1614,7 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString() } namespace { - static const std::set stl_container = make_container< std::set >() << + const std::set stl_container = make_container< std::set >() << "array" << "bitset" << "deque" << "forward_list" << "hash_map" << "hash_multimap" << "hash_set" << "list" << "map" << "multimap" << "multiset" << diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 36913b4af..9b3627ac2 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -138,7 +138,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list stl_stream = make_container< std::set >() << + const std::set stl_stream = make_container< std::set >() << "fstream" << "ifstream" << "iostream" << "istream" << "istringstream" << "ofstream" << "ostream" << "ostringstream" << "stringstream" << "wistringstream" << "wostringstream" << "wstringstream"; @@ -376,7 +376,7 @@ void CheckNullPointer::nullPointer() } namespace { - static const std::set stl_istream = make_container< std::set >() << + const std::set stl_istream = make_container< std::set >() << "fstream" << "ifstream" << "iostream" << "istream" << "istringstream" << "stringstream" << "wistringstream" << "wstringstream"; } diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index dc464125c..bc47005f3 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -254,17 +254,16 @@ namespace { const std::set algorithm1x1 = make_container< std::set >() // func(begin1 << x << end1 << "inplace_merge" << "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"; + const std::string iteratorBeginFuncPattern = "begin|cbegin|rbegin|crbegin"; + const std::string iteratorEndFuncPattern = "end|cend|rend|crend"; - static const std::string pattern1x1_1 = "%name% . " + iteratorBeginFuncPattern + " ( ) , "; - static const std::string pattern1x1_2 = "%name% . " + iteratorEndFuncPattern + " ( ) ,|)"; - static const std::string pattern2 = pattern1x1_1 + pattern1x1_2; + const std::string pattern1x1_1 = "%name% . " + iteratorBeginFuncPattern + " ( ) , "; + const std::string pattern1x1_2 = "%name% . " + iteratorEndFuncPattern + " ( ) ,|)"; + const std::string pattern2 = pattern1x1_1 + pattern1x1_2; } void CheckStl::mismatchingContainers() { - // Check if different containers are used in various calls of standard functions const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const std::size_t functions = symbolDatabase->functionScopes.size(); @@ -593,8 +592,6 @@ void CheckStl::invalidPointerError(const Token *tok, const std::string &func, co } - - void CheckStl::stlBoundaries() { const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase(); @@ -924,7 +921,7 @@ static bool isLocal(const Token *tok) } namespace { - static const std::set stl_string_stream = make_container< std::set >() << + const std::set stl_string_stream = make_container< std::set >() << "istringstream" << "ostringstream" << "stringstream" << "wstringstream" ; } @@ -1115,7 +1112,7 @@ void CheckStl::checkAutoPointer() { std::set autoPtrVarId; std::map mallocVarId; // variables allocated by the malloc-like function - static const char STL_CONTAINER_LIST[] = "array|bitset|deque|list|forward_list|map|multimap|multiset|priority_queue|queue|set|stack|vector|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset|basic_string"; + const char STL_CONTAINER_LIST[] = "array|bitset|deque|list|forward_list|map|multimap|multiset|priority_queue|queue|set|stack|vector|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset|basic_string"; const int malloc = _settings->library.alloc("malloc"); // allocation function, which are not compatible with auto_ptr const bool printStyle = _settings->isEnabled("style"); @@ -1229,7 +1226,7 @@ void CheckStl::autoPointerMallocError(const Token *tok, const std::string& alloc } namespace { - static const std::set stl_containers_with_empty_and_clear = make_container< std::set >() << + 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" << @@ -1389,7 +1386,6 @@ void CheckStl::dereferenceInvalidIteratorError(const Token* deref, const std::st - void CheckStl::readingEmptyStlContainer_parseUsage(const Token* tok, const Library::Container* container, std::map& empty, bool noerror) { // Check for various conditions for the way stl containers and variables can be used diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 23c53128f..d705e868f 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1067,12 +1067,12 @@ bool CheckUninitVar::isMemberVariableAssignment(const Token *tok, const std::str } // is this a function call? - ftok = ftok ? ftok->previous() : NULL; + ftok = ftok ? ftok->previous() : nullptr; if (Token::Match(ftok, "%name% (")) { // check how function handle uninitialized data arguments.. const Function *function = ftok->function(); - const Variable *arg = function ? function->getArgumentVar(argumentNumber) : NULL; - const Token *argStart = arg ? arg->typeStartToken() : NULL; + const Variable *arg = function ? function->getArgumentVar(argumentNumber) : nullptr; + const Token *argStart = arg ? arg->typeStartToken() : nullptr; while (argStart && argStart->previous() && argStart->previous()->isName()) argStart = argStart->previous(); if (Token::Match(argStart, "const struct| %type% * const| %name% [,)]")) diff --git a/lib/path.cpp b/lib/path.cpp index 363ae6a14..cbd75a581 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -249,7 +249,7 @@ std::string Path::getAbsoluteFilePath(const std::string& filePath) if (_fullpath(absolute, filePath.c_str(), _MAX_PATH)) absolute_path = absolute; #elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__) - char * absolute = realpath(filePath.c_str(), NULL); + char * absolute = realpath(filePath.c_str(), nullptr); if (absolute) absolute_path = absolute; free(absolute); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ba70b15d5..5cd4844cb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2658,6 +2658,15 @@ static void setVarIdClassFunction(const std::string &classname, } } +namespace { + // Variable declarations can't start with "return" etc. + const std::set notstart_c = make_container< std::set > () + << "goto" << "NOT" << "return" << "sizeof"<< "typedef"; + 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" ; +} + void Tokenizer::setVarId() { // Clear all variable ids @@ -2669,16 +2678,7 @@ void Tokenizer::setVarId() setPodTypes(); // Variable declarations can't start with "return" etc. - std::set notstart; - notstart.insert("goto"); - notstart.insert("NOT"); - notstart.insert("return"); - notstart.insert("sizeof"); - notstart.insert("typedef"); - if (!isC()) { - static const char *str[] = {"delete","friend","new","throw","using","virtual","explicit","const_cast","dynamic_cast","reinterpret_cast","static_cast"}; - notstart.insert(str, str+(sizeof(str)/sizeof(*str))); - } + const std::set& notstart = (isC()) ? notstart_c : notstart_cpp; // variable id _varId = 0; diff --git a/tools/reduce.cpp b/tools/reduce.cpp index 1bc6f700a..4376d705c 100644 --- a/tools/reduce.cpp +++ b/tools/reduce.cpp @@ -733,7 +733,7 @@ int main(int argc, char *argv[]) } maxconfigs = true; - } else if (settings.filename==NULL && strchr(argv[i],'.')) + } else if (settings.filename==nullptr && strchr(argv[i],'.')) settings.filename = argv[i]; else if (settings.linenr == 0U && MathLib::isInt(argv[i])) settings.linenr = std::atoi(argv[i]); @@ -749,7 +749,7 @@ int main(int argc, char *argv[]) if (settings._force) settings._maxConfigs = ~0U; - if ((!settings.hang && settings.linenr == 0U) || settings.filename == NULL) { + if ((!settings.hang && settings.linenr == 0U) || settings.filename == nullptr) { std::cerr << "Syntax:" << std::endl << argv[0] << " [--stdout] [--cfg=X] [--hang] [--maxtime=60] [-D define] [-I includepath] [--force] [--enable=] [--inconclusive] [--debug-warnings] [--max-configs=] filename [linenr]" << std::endl; return EXIT_FAILURE;