Skip to content

Commit 662283c

Browse files
committed
Refactorization: Avoid iterations over whole token list, limited several checks to function scopes.
1 parent 5bc775e commit 662283c

5 files changed

Lines changed: 242 additions & 211 deletions

File tree

lib/checkmemoryleak.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
172172
return No;
173173

174174
// is there a user function with this name?
175-
if (tokenizer && Token::findmatch(tokenizer->tokens(), ("%type% *|&| " + tok2->str()).c_str()))
175+
if (tok2->function())
176176
return No;
177177
return Fd;
178178
}

lib/checknonreentrantfunctions.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//---------------------------------------------------------------------------
2222

2323
#include "checknonreentrantfunctions.h"
24+
#include "symboldatabase.h"
2425

2526
//---------------------------------------------------------------------------
2627

@@ -35,30 +36,34 @@ void CheckNonReentrantFunctions::nonReentrantFunctions()
3536
if (!_settings->standards.posix || !_settings->isEnabled("portability"))
3637
return;
3738

38-
std::map<std::string,std::string>::const_iterator nonReentrant_end = _nonReentrantFunctions.end();
39-
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
40-
// Look for function invocations
41-
if (!tok->isName() || tok->strAt(1) != "(" || tok->varId() != 0)
42-
continue;
43-
44-
// Check for non-reentrant function name
45-
std::map<std::string,std::string>::const_iterator it = _nonReentrantFunctions.find(tok->str());
46-
if (it == nonReentrant_end)
47-
continue;
48-
49-
const Token *prev = tok->previous();
50-
if (prev) {
51-
// Ignore function definitions, class members or class definitions
52-
if (prev->isName() || Token::Match(prev, ".|:"))
39+
const SymbolDatabase * const symbolDatabase = _tokenizer->getSymbolDatabase();
40+
const std::size_t functions = symbolDatabase->functionScopes.size();
41+
for (std::size_t i = 0; i < functions; ++i) {
42+
const Scope * scope = symbolDatabase->functionScopes[i];
43+
for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
44+
// Look for function invocations
45+
if (tok->varId() != 0 || !tok->isName() || tok->strAt(1) != "(")
5346
continue;
5447

55-
// Check for "std" or global namespace, ignore other namespaces
56-
if (prev->str() == "::" && prev->previous() && prev->previous()->str() != "std" && prev->previous()->isName())
48+
// Check for non-reentrant function name
49+
std::map<std::string, std::string>::const_iterator it = _nonReentrantFunctions.find(tok->str());
50+
if (it == _nonReentrantFunctions.end())
5751
continue;
58-
}
5952

60-
// Only affecting multi threaded code, therefore this is "portability"
61-
reportError(tok, Severity::portability, "nonreentrantFunctions"+it->first, it->second);
53+
const Token *prev = tok->previous();
54+
if (prev) {
55+
// Ignore function definitions, class members or class definitions
56+
if (prev->str() == ".")
57+
continue;
58+
59+
// Check for "std" or global namespace, ignore other namespaces
60+
if (prev->str() == "::" && prev->previous() && prev->previous()->str() != "std" && prev->previous()->isName())
61+
continue;
62+
}
63+
64+
// Only affecting multi threaded code, therefore this is "portability"
65+
reportError(tok, Severity::portability, "nonreentrantFunctions" + it->first, it->second);
66+
}
6267
}
6368
}
6469
//---------------------------------------------------------------------------

0 commit comments

Comments
 (0)