From 522f783ada7d43b88a9d1e168d87a0c7f39ba7b4 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 16 Aug 2009 22:02:11 +0700 Subject: [PATCH] CheckMemoryLeak: try to speed up call_func(). Using std::set::find() instead of sequence of Match() calls. --- src/checkmemoryleak.cpp | 66 +++++++++++++++++------------------------ src/checkmemoryleak.h | 16 ++++++++-- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 288c1172a..3b0a50562 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -369,6 +369,32 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) } +void CheckMemoryLeakInFunction::init() +{ + static const char * const white_list[] = { + "if", "for", "while", "return", "switch", "realloc", "delete" + , "strcpy", "strncpy", "strcat", "strncat", "strcmp", "strncmp" + , "strcasecmp", "stricmp", "sprintf", "strchr", "strrchr", "strstr" + , "memset", "memcpy", "memmove", "memchr", "fgets", "fgetc", "getc" + , "fputs", "fputc", "feof", "ferror", "clearerr", "printf", "fprintf" + , "fread", "fwrite", "fflush", "fseek", "fseeko", "ftell", "ftello" + , "fsetpos", "fgetpos", "setvbuf", "setbuf", "setbuffer", "setlinebuf" + , "rewind", "read", "readv", "pread", "readahead", "write", "writev" + , "pwrite", "lseek", "ioctl", "fchmod", "fcntl", "flock", "lockf" + , "ftruncate", "fsync", "fdatasync", "fstat", "sync_file_range" + , "posix_fallocate", "posix_fadvise", "readdir", "readdir_r" + , "rewinddir", "telldir", "seekdir", "scandir", "atoi", "atof", "atol" + , "strtol", "strtoul", "strtod" + , NULL + }; + + size_t i = 0; + while (white_list[i] != NULL) + { + call_func_white_list.insert(white_list[i]); + ++i; + } +} bool CheckMemoryLeakInFunction::matchFunctionsThatReturnArg(const Token *tok, const std::string &varname) { @@ -424,45 +450,7 @@ static int countParameters(const Token *tok) const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype, bool &all, unsigned int sz) { - // Keywords that are not function calls.. - if (Token::Match(tok, "if|for|while|return|switch")) - return 0; - - // Keywords that are not function calls.. - if (Token::Match(tok, "realloc")) - return 0; - - // String functions that are not allocating nor deallocating memory.. - if (Token::Match(tok, "strcpy|strncpy|strcat|strncat|strcmp|strncmp|strcasecmp|stricmp|sprintf|strchr|strrchr|strstr")) - return 0; - - // Memory functions that are not allocating nor deallocating memory.. - if (Token::Match(tok, "memset|memcpy|memmove|memchr")) - return 0; - - // I/O functions that are not allocating nor deallocating memory.. - if (Token::Match(tok, "fgets|fgetc|getc|fputs|fputc|feof|ferror|clearerr|printf|fprintf") || - Token::Match(tok, "fread|fwrite|fflush|fseek|fseeko|ftell|ftello|fsetpos|fgetpos") || - Token::Match(tok, "setvbuf|setbuf|setbuffer|setlinebuf|rewind")) - return 0; - - // I/O functions that are not allocating nor deallocating memory.. - if (Token::Match(tok, "read|readv|pread|readahead|write|writev|pwrite|lseek") || - Token::Match(tok, "ioctl|fchmod|fcntl|flock|lockf|ftruncate|fsync|fdatasync") || - Token::Match(tok, "fstat|sync_file_range|posix_fallocate|posix_fadvise")) - return 0; - - // Functions to work with directories that are not allocating nor - // deallocating memory.. - if (Token::Match(tok, "readdir|readdir_r|rewinddir|telldir|seekdir|scandir")) - return 0; - - // Convert functions that are not allocating nor deallocating memory.. - if (Token::Match(tok, "atoi|atof|atol|strtol|strtoul|strtod")) - return 0; - - // This is not an unknown function neither - if (tok->str() == "delete") + if (call_func_white_list.find(tok->str()) != call_func_white_list.end()) return 0; if (getAllocationType(tok, varnames[0]) != No || getReallocationType(tok, varnames[0]) != No || getDeallocationType(tok, varnames) != No) diff --git a/src/checkmemoryleak.h b/src/checkmemoryleak.h index fb090d899..341a8d0ff 100644 --- a/src/checkmemoryleak.h +++ b/src/checkmemoryleak.h @@ -37,6 +37,7 @@ #include "check.h" +#include #include #include #include @@ -133,12 +134,16 @@ class CheckMemoryLeakInFunction : private Check, private CheckMemoryLeak public: /** This constructor is used when registering this class */ CheckMemoryLeakInFunction() : Check(), CheckMemoryLeak(0, 0) - { } + { + init(); + } /** This constructor is used when running checks */ CheckMemoryLeakInFunction(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) : Check(tokenizer, settings, errorLogger), CheckMemoryLeak(tokenizer, errorLogger) - { } + { + init(); + } void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) { @@ -256,6 +261,13 @@ private: { return "Is there any allocated memory when a function goes out of scope"; } + + // Keywords and function names which not allocating nor deallocating memory + // (used by call_func()) + std::set call_func_white_list; + + // Called in all constructors + void init(); };