Skip to content

Commit 16f44e2

Browse files
committed
Refactorization: Cleanup usage of std::multimap
Merged from LCppC.
1 parent 4fccd90 commit 16f44e2

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

lib/checkstl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,8 +1954,7 @@ void CheckStl::string_c_str()
19541954
if (var->isPointer())
19551955
string_c_strError(tok);
19561956
}
1957-
} else if (printPerformance && tok->function() && Token::Match(tok, "%name% ( !!)") && c_strFuncParam.find(tok->function()) != c_strFuncParam.end() &&
1958-
tok->str() != scope.className) {
1957+
} else if (printPerformance && tok->function() && Token::Match(tok, "%name% ( !!)") && tok->str() != scope.className) {
19591958
const std::pair<std::multimap<const Function*, int>::const_iterator, std::multimap<const Function*, int>::const_iterator> range = c_strFuncParam.equal_range(tok->function());
19601959
for (std::multimap<const Function*, int>::const_iterator i = range.first; i != range.second; ++i) {
19611960
if (i->second == 0)

lib/symboldatabase.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,9 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
646646
}
647647

648648
bool newFunc = true; // Is this function already in the database?
649-
for (std::multimap<std::string, const Function *>::const_iterator i = scope->functionMap.find(tok->str()); i != scope->functionMap.end() && i->first == tok->str(); ++i) {
650-
if (i->second->argsMatch(scope, i->second->argDef, argStart, emptyString, 0)) {
649+
auto range = scope->functionMap.equal_range(tok->str());
650+
for (std::multimap<std::string, const Function*>::const_iterator it = range.first; it != range.second; ++it) {
651+
if (it->second->argsMatch(scope, it->second->argDef, argStart, emptyString, 0)) {
651652
newFunc = false;
652653
break;
653654
}
@@ -2931,12 +2932,13 @@ Function* SymbolDatabase::addGlobalFunction(Scope*& scope, const Token*& tok, co
29312932
Function* function = nullptr;
29322933
// Lambda functions are always unique
29332934
if (tok->str() != "[") {
2934-
for (std::multimap<std::string, const Function *>::iterator i = scope->functionMap.find(tok->str()); i != scope->functionMap.end() && i->first == tok->str(); ++i) {
2935-
const Function *f = i->second;
2935+
auto range = scope->functionMap.equal_range(tok->str());
2936+
for (std::multimap<std::string, const Function*>::const_iterator it = range.first; it != range.second; ++it) {
2937+
const Function *f = it->second;
29362938
if (f->hasBody())
29372939
continue;
29382940
if (f->argsMatch(scope, f->argDef, argStart, emptyString, 0)) {
2939-
function = const_cast<Function *>(i->second);
2941+
function = const_cast<Function *>(it->second);
29402942
break;
29412943
}
29422944
}
@@ -3090,7 +3092,8 @@ void SymbolDatabase::addClassFunction(Scope **scope, const Token **tok, const To
30903092
}
30913093

30923094
if (match) {
3093-
for (std::multimap<std::string, const Function *>::iterator it = scope1->functionMap.find((*tok)->str()); it != scope1->functionMap.end() && it->first == (*tok)->str(); ++it) {
3095+
auto range = scope1->functionMap.equal_range((*tok)->str());
3096+
for (std::multimap<std::string, const Function*>::const_iterator it = range.first; it != range.second; ++it) {
30943097
Function * func = const_cast<Function *>(it->second);
30953098
if (!func->hasBody()) {
30963099
if (func->argsMatch(scope1, func->argDef, (*tok)->next(), path, path_length)) {
@@ -4150,7 +4153,8 @@ const Function * Function::getOverriddenFunctionRecursive(const ::Type* baseType
41504153
const Scope *parent = derivedFromType->classScope;
41514154

41524155
// check if function defined in base class
4153-
for (std::multimap<std::string, const Function *>::const_iterator it = parent->functionMap.find(tokenDef->str()); it != parent->functionMap.end() && it->first == tokenDef->str(); ++it) {
4156+
auto range = parent->functionMap.equal_range(tokenDef->str());
4157+
for (std::multimap<std::string, const Function*>::const_iterator it = range.first; it != range.second; ++it) {
41544158
const Function * func = it->second;
41554159
if (func->hasVirtualSpecifier()) { // Base is virtual and of same name
41564160
const Token *temp1 = func->tokenDef->previous();
@@ -4976,7 +4980,8 @@ void Scope::findFunctionInBase(const std::string & name, nonneg int args, std::v
49764980
if (base->classScope == this) // Ticket #5120, #5125: Recursive class; tok should have been found already
49774981
continue;
49784982

4979-
for (std::multimap<std::string, const Function *>::const_iterator it = base->classScope->functionMap.find(name); it != base->classScope->functionMap.end() && it->first == name; ++it) {
4983+
auto range = base->classScope->functionMap.equal_range(name);
4984+
for (std::multimap<std::string, const Function*>::const_iterator it = range.first; it != range.second; ++it) {
49804985
const Function *func = it->second;
49814986
if ((func->isVariadic() && args >= (func->argCount() - 1)) ||
49824987
(args == func->argCount() || (args < func->argCount() && args >= func->minArgCount()))) {
@@ -5117,7 +5122,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
51175122
const std::size_t args = arguments.size();
51185123

51195124
auto addMatchingFunctions = [&](const Scope *scope) {
5120-
for (std::multimap<std::string, const Function *>::const_iterator it = scope->functionMap.find(tok->str()); it != scope->functionMap.cend() && it->first == tok->str(); ++it) {
5125+
auto range = scope->functionMap.equal_range(tok->str());
5126+
for (std::multimap<std::string, const Function *>::const_iterator it = range.first; it != range.second; ++it) {
51215127
const Function *func = it->second;
51225128
if (!isCall || args == func->argCount() ||
51235129
(func->isVariadic() && args >= (func->minArgCount() - 1)) ||
@@ -5786,9 +5792,8 @@ Function * SymbolDatabase::findFunctionInScope(const Token *func, const Scope *n
57865792
const Function * function = nullptr;
57875793
const bool destructor = func->strAt(-1) == "~";
57885794

5789-
for (std::multimap<std::string, const Function *>::const_iterator it = ns->functionMap.find(func->str());
5790-
it != ns->functionMap.end() && it->first == func->str(); ++it) {
5791-
5795+
auto range = ns->functionMap.equal_range(func->str());
5796+
for (std::multimap<std::string, const Function*>::const_iterator it = range.first; it != range.second; ++it) {
57925797
if (it->second->argsMatch(ns, it->second->argDef, func->next(), path, path_length) &&
57935798
it->second->isDestructor() == destructor) {
57945799
function = it->second;

lib/templatesimplifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,8 @@ void TemplateSimplifier::getTemplateInstantiations()
822822
fullName = tok->str();
823823

824824
// get all declarations with this name
825-
for (auto pos = functionNameMap.lower_bound(tok->str());
826-
pos != functionNameMap.upper_bound(tok->str()); ++pos) {
825+
auto range = functionNameMap.equal_range(tok->str());
826+
for (auto pos = range.first; pos != range.second; ++pos) {; ++pos) {
827827
// look for declaration with same qualification or constructor with same qualification
828828
if (pos->second->fullName() == fullName ||
829829
(pos->second->scope() == fullName && tok->str() == pos->second->name())) {

lib/tokenize.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12399,7 +12399,7 @@ void Tokenizer::printUnknownTypes() const
1239912399
if (!mSymbolDatabase)
1240012400
return;
1240112401

12402-
std::multimap<std::string, const Token *> unknowns;
12402+
std::vector<std::pair<std::string, const Token *>> unknowns;
1240312403

1240412404
for (int i = 1; i <= mVarId; ++i) {
1240512405
const Variable *var = mSymbolDatabase->getVariableFromVarId(i);
@@ -12448,14 +12448,14 @@ void Tokenizer::printUnknownTypes() const
1244812448
}
1244912449
}
1245012450

12451-
unknowns.insert(std::pair<std::string, const Token *>(name, nameTok));
12451+
unknowns.emplace_back(std::make_pair(name, nameTok));
1245212452
}
1245312453

1245412454
if (!unknowns.empty()) {
1245512455
std::string last;
1245612456
int count = 0;
1245712457

12458-
for (std::multimap<std::string, const Token *>::const_iterator it = unknowns.begin(); it != unknowns.end(); ++it) {
12458+
for (auto it = unknowns.begin(); it != unknowns.end(); ++it) {
1245912459
// skip types is std namespace because they are not interesting
1246012460
if (it->first.find("std::") != 0) {
1246112461
if (it->first != last) {

0 commit comments

Comments
 (0)