Refactorization: Improved performance of Tokenizer::simplifyMicrosoftMemoryFunctions() and Tokenizer::simplifyMicrosoftStringFunctions()

- Added pre-check before doing complex Token::Match() calls
- Use std::map instead of std::set
This commit is contained in:
PKEuS 2016-05-24 21:19:00 +02:00
parent e76518d800
commit 686cc6640e
1 changed files with 49 additions and 51 deletions

View File

@ -9028,11 +9028,14 @@ void Tokenizer::simplifyMicrosoftMemoryFunctions()
return; return;
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "CopyMemory|RtlCopyMemory|RtlCopyBytes (")) { if (tok->strAt(1) != "(")
continue;
if (Token::Match(tok, "CopyMemory|RtlCopyMemory|RtlCopyBytes")) {
tok->str("memcpy"); tok->str("memcpy");
} else if (Token::Match(tok, "MoveMemory|RtlMoveMemory (")) { } else if (Token::Match(tok, "MoveMemory|RtlMoveMemory")) {
tok->str("memmove"); tok->str("memmove");
} else if (Token::Match(tok, "FillMemory|RtlFillMemory|RtlFillBytes (")) { } else if (Token::Match(tok, "FillMemory|RtlFillMemory|RtlFillBytes")) {
// FillMemory(dst, len, val) -> memset(dst, val, len) // FillMemory(dst, len, val) -> memset(dst, val, len)
tok->str("memset"); tok->str("memset");
@ -9045,7 +9048,7 @@ void Tokenizer::simplifyMicrosoftMemoryFunctions()
if (tok2) if (tok2)
Token::move(tok1->previous(), tok2->tokAt(-2), tok->next()->link()->previous()); // Swap third with second argument Token::move(tok1->previous(), tok2->tokAt(-2), tok->next()->link()->previous()); // Swap third with second argument
} }
} else if (Token::Match(tok, "ZeroMemory|RtlZeroMemory|RtlZeroBytes|RtlSecureZeroMemory (")) { } else if (Token::Match(tok, "ZeroMemory|RtlZeroMemory|RtlZeroBytes|RtlSecureZeroMemory")) {
// ZeroMemory(dst, len) -> memset(dst, 0, len) // ZeroMemory(dst, len) -> memset(dst, 0, len)
tok->str("memset"); tok->str("memset");
@ -9059,7 +9062,7 @@ void Tokenizer::simplifyMicrosoftMemoryFunctions()
tok1 = tok1->next(); tok1 = tok1->next();
tok1->insertToken(","); tok1->insertToken(",");
} }
} else if (Token::simpleMatch(tok, "RtlCompareMemory (")) { } else if (Token::simpleMatch(tok, "RtlCompareMemory")) {
// RtlCompareMemory(src1, src2, len) -> memcmp(src1, src2, len) // RtlCompareMemory(src1, src2, len) -> memcmp(src1, src2, len)
tok->str("memcmp"); tok->str("memcmp");
// For the record, when memcmp returns 0, both strings are equal. // For the record, when memcmp returns 0, both strings are equal.
@ -9073,48 +9076,43 @@ void Tokenizer::simplifyMicrosoftMemoryFunctions()
namespace { namespace {
struct triplet { struct triplet {
triplet(const char* t, const char* m, const char* u) : tchar(t), mbcs(m), unicode(u) {} triplet(const char* m, const char* u) : mbcs(m), unicode(u) {}
explicit triplet(const std::string& t) : tchar(t) {} std::string mbcs, unicode;
bool operator <(const triplet& rhs) const {
return tchar < rhs.tchar;
}
std::string tchar, mbcs, unicode;
}; };
const std::set<triplet> apis = make_container< std::set<triplet> >() << const std::map<std::string, triplet> apis = make_container< std::map<std::string, triplet> >() <<
triplet("_topen", "open", "_wopen") << std::make_pair("_topen", triplet("open", "_wopen")) <<
triplet("_tsopen_s", "_sopen_s", "_wsopen_s") << std::make_pair("_tsopen_s", triplet("_sopen_s", "_wsopen_s")) <<
triplet("_tfopen", "fopen", "_wfopen") << std::make_pair("_tfopen", triplet("fopen", "_wfopen")) <<
triplet("_tfopen_s", "fopen_s", "_wfopen_s") << std::make_pair("_tfopen_s", triplet("fopen_s", "_wfopen_s")) <<
triplet("_tfreopen", "freopen", "_wfreopen") << std::make_pair("_tfreopen", triplet("freopen", "_wfreopen")) <<
triplet("_tfreopen_s", "freopen_s", "_wfreopen_s") << std::make_pair("_tfreopen_s", triplet("freopen_s", "_wfreopen_s")) <<
triplet("_tcscat", "strcat", "wcscat") << std::make_pair("_tcscat", triplet("strcat", "wcscat")) <<
triplet("_tcschr", "strchr", "wcschr") << std::make_pair("_tcschr", triplet("strchr", "wcschr")) <<
triplet("_tcscmp", "strcmp", "wcscmp") << std::make_pair("_tcscmp", triplet("strcmp", "wcscmp")) <<
triplet("_tcsdup", "strdup", "wcsdup") << std::make_pair("_tcsdup", triplet("strdup", "wcsdup")) <<
triplet("_tcscpy", "strcpy", "wcscpy") << std::make_pair("_tcscpy", triplet("strcpy", "wcscpy")) <<
triplet("_tcslen", "strlen", "wcslen") << std::make_pair("_tcslen", triplet("strlen", "wcslen")) <<
triplet("_tcsncat", "strncat", "wcsncat") << std::make_pair("_tcsncat", triplet("strncat", "wcsncat")) <<
triplet("_tcsncpy", "strncpy", "wcsncpy") << std::make_pair("_tcsncpy", triplet("strncpy", "wcsncpy")) <<
triplet("_tcsnlen", "strnlen", "wcsnlen") << std::make_pair("_tcsnlen", triplet("strnlen", "wcsnlen")) <<
triplet("_tcsrchr", "strrchr", "wcsrchr") << std::make_pair("_tcsrchr", triplet("strrchr", "wcsrchr")) <<
triplet("_tcsstr", "strstr", "wcsstr") << std::make_pair("_tcsstr", triplet("strstr", "wcsstr")) <<
triplet("_tcstok", "strtok", "wcstok") << std::make_pair("_tcstok", triplet("strtok", "wcstok")) <<
triplet("_ftprintf", "fprintf", "fwprintf") << std::make_pair("_ftprintf", triplet("fprintf", "fwprintf")) <<
triplet("_tprintf", "printf", "wprintf") << std::make_pair("_tprintf", triplet("printf", "wprintf")) <<
triplet("_stprintf", "sprintf", "swprintf") << std::make_pair("_stprintf", triplet("sprintf", "swprintf")) <<
triplet("_sntprintf", "_snprintf", "_snwprintf") << std::make_pair("_sntprintf", triplet("_snprintf", "_snwprintf")) <<
triplet("_ftscanf", "fscanf", "fwscanf") << std::make_pair("_ftscanf", triplet("fscanf", "fwscanf")) <<
triplet("_tscanf", "scanf", "wscanf") << std::make_pair("_tscanf", triplet("scanf", "wscanf")) <<
triplet("_stscanf", "sscanf", "swscanf") << std::make_pair("_stscanf", triplet("sscanf", "swscanf")) <<
triplet("_ftprintf_s", "fprintf_s", "fwprintf_s") << std::make_pair("_ftprintf_s", triplet("fprintf_s", "fwprintf_s")) <<
triplet("_tprintf_s", "printf_s", "wprintf_s") << std::make_pair("_tprintf_s", triplet("printf_s", "wprintf_s")) <<
triplet("_stprintf_s", "sprintf_s", "swprintf_s") << std::make_pair("_stprintf_s", triplet("sprintf_s", "swprintf_s")) <<
triplet("_sntprintf_s", "_snprintf_s", "_snwprintf_s") << std::make_pair("_sntprintf_s", triplet("_snprintf_s", "_snwprintf_s")) <<
triplet("_ftscanf_s", "fscanf_s", "fwscanf_s") << std::make_pair("_ftscanf_s", triplet("fscanf_s", "fwscanf_s")) <<
triplet("_tscanf_s", "scanf_s", "wscanf_s") << std::make_pair("_tscanf_s", triplet("scanf_s", "wscanf_s")) <<
triplet("_stscanf_s", "sscanf_s", "swscanf_s") std::make_pair("_stscanf_s", triplet("sscanf_s", "swscanf_s"));
;
} }
void Tokenizer::simplifyMicrosoftStringFunctions() void Tokenizer::simplifyMicrosoftStringFunctions()
@ -9125,13 +9123,13 @@ void Tokenizer::simplifyMicrosoftStringFunctions()
const bool ansi = _settings->platformType == Settings::Win32A; const bool ansi = _settings->platformType == Settings::Win32A;
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
std::set<triplet>::const_iterator match = apis.find(triplet(tok->str())); if (tok->strAt(1) != "(")
continue;
std::map<std::string, triplet>::const_iterator match = apis.find(tok->str());
if (match!=apis.end()) { if (match!=apis.end()) {
const std::string pattern(match->tchar + " ("); tok->str(ansi ? match->second.mbcs : match->second.unicode);
if (Token::simpleMatch(tok, pattern.c_str())) { tok->originalName(match->first);
tok->str(ansi ? match->mbcs : match->unicode);
tok->originalName(match->tchar);
}
} else if (Token::Match(tok, "_T ( %char%|%str% )")) { } else if (Token::Match(tok, "_T ( %char%|%str% )")) {
tok->deleteNext(); tok->deleteNext();
tok->deleteThis(); tok->deleteThis();