Skip to content

Commit 2287c6b

Browse files
committed
prevent unnecessary utils::as_const() calls
1 parent 5aa8739 commit 2287c6b

10 files changed

Lines changed: 33 additions & 32 deletions

cli/signalhandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static void CppcheckSignalHandler(int signo, siginfo_t * info, void * context)
124124
killid = getpid();
125125
#endif
126126

127-
const auto it = utils::as_const(listofsignals).find(signo);
127+
const auto it = listofsignals.find(signo);
128128
const char * const signame = (it==listofsignals.end()) ? "unknown" : it->second.c_str();
129129
bool unexpectedSignal=true; // unexpected indicates program failure
130130
bool terminate=true; // exit process/thread

lib/checkleakautovar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ void CheckLeakAutoVar::leakIfAllocated(const Token *vartok,
11631163
const std::map<int, VarInfo::AllocInfo> &alloctype = varInfo.alloctype;
11641164
const auto& possibleUsage = varInfo.possibleUsage;
11651165

1166-
const auto var = utils::as_const(alloctype).find(vartok->varId());
1166+
const auto var = alloctype.find(vartok->varId());
11671167
if (var != alloctype.cend() && var->second.status == VarInfo::ALLOC) {
11681168
const auto use = possibleUsage.find(vartok->varId());
11691169
if (use == possibleUsage.end()) {

lib/checkuninitvar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::map<nonneg i
304304
if (tok->isName() || tok->str() == ".") {
305305
while (tok && tok->str() == ".")
306306
tok = tok->astOperand2();
307-
const auto it = utils::as_const(variableValue).find(tok ? tok->varId() : ~0U);
307+
const auto it = variableValue.find(tok ? tok->varId() : ~0U);
308308
if (it != variableValue.end()) {
309309
*alwaysTrue = (it->second != 0LL);
310310
*alwaysFalse = (it->second == 0LL);
@@ -330,7 +330,7 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::map<nonneg i
330330
while (vartok && vartok->str() == ".")
331331
vartok = vartok->astOperand2();
332332

333-
const auto it = utils::as_const(variableValue).find(vartok ? vartok->varId() : ~0U);
333+
const auto it = variableValue.find(vartok ? vartok->varId() : ~0U);
334334
if (it == variableValue.end())
335335
return;
336336

lib/ctu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ static bool findPath(const std::string &callId,
512512
if (index >= maxCtuDepth)
513513
return false; // TODO: add bailout message?
514514

515-
const auto it = utils::as_const(callsMap).find(callId);
515+
const auto it = callsMap.find(callId);
516516
if (it == callsMap.end())
517517
return false;
518518

lib/library.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Library::LibraryData
4646
{
4747
struct Platform {
4848
const PlatformType *platform_type(const std::string &name) const {
49-
const auto it = utils::as_const(mPlatformTypes).find(name);
49+
const auto it = mPlatformTypes.find(name);
5050
return (it != mPlatformTypes.end()) ? &(it->second) : nullptr;
5151
}
5252
std::map<std::string, PlatformType> mPlatformTypes;
@@ -1323,10 +1323,10 @@ const Library::ArgumentChecks * Library::getarg(const Token *ftok, int argnr) co
13231323
const Function* func = nullptr;
13241324
if (isNotLibraryFunction(ftok, &func))
13251325
return nullptr;
1326-
const auto it2 = utils::as_const(func->argumentChecks).find(argnr);
1326+
const auto it2 = func->argumentChecks.find(argnr);
13271327
if (it2 != func->argumentChecks.cend())
13281328
return &it2->second;
1329-
const auto it3 = utils::as_const(func->argumentChecks).find(-1);
1329+
const auto it3 = func->argumentChecks.find(-1);
13301330
if (it3 != func->argumentChecks.cend())
13311331
return &it3->second;
13321332
return nullptr;

lib/library.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,21 +242,21 @@ class CPPCHECKLIB Library {
242242
bool startPatternHasStd{};
243243

244244
Action getAction(const std::string& function) const {
245-
const auto i = utils::as_const(functions).find(function);
245+
const auto i = functions.find(function);
246246
if (i != functions.end())
247247
return i->second.action;
248248
return Action::NO_ACTION;
249249
}
250250

251251
Yield getYield(const std::string& function) const {
252-
const auto i = utils::as_const(functions).find(function);
252+
const auto i = functions.find(function);
253253
if (i != functions.end())
254254
return i->second.yield;
255255
return Yield::NO_YIELD;
256256
}
257257

258258
const std::string& getReturnType(const std::string& function) const {
259-
const auto i = utils::as_const(functions).find(function);
259+
const auto i = functions.find(function);
260260
return (i != functions.end()) ? i->second.returnType : mEmptyString;
261261
}
262262

@@ -482,7 +482,7 @@ class CPPCHECKLIB Library {
482482
std::string getFunctionName(const Token *ftok, bool &error) const;
483483

484484
static const AllocFunc* getAllocDealloc(const std::map<std::string, AllocFunc> &data, const std::string &name) {
485-
const auto it = utils::as_const(data).find(name);
485+
const auto it = data.find(name);
486486
return (it == data.end()) ? nullptr : &it->second;
487487
}
488488

lib/settings.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppress
109109
}
110110
const picojson::object& obj = json.get<picojson::object>();
111111
{
112-
const auto it = utils::as_const(obj).find("productName");
112+
const auto it = obj.find("productName");
113113
if (it != obj.cend()) {
114114
const auto& v = it->second;
115115
if (!v.is<std::string>())
@@ -118,7 +118,7 @@ std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppress
118118
}
119119
}
120120
{
121-
const auto it = utils::as_const(obj).find("manualUrl");
121+
const auto it = obj.find("manualUrl");
122122
if (it != obj.cend()) {
123123
const auto& v = it->second;
124124
if (!v.is<std::string>())
@@ -127,7 +127,7 @@ std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppress
127127
}
128128
}
129129
{
130-
const auto it = utils::as_const(obj).find("about");
130+
const auto it = obj.find("about");
131131
if (it != obj.cend()) {
132132
const auto& v = it->second;
133133
if (!v.is<std::string>())
@@ -136,7 +136,7 @@ std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppress
136136
}
137137
}
138138
{
139-
const auto it = utils::as_const(obj).find("addons");
139+
const auto it = obj.find("addons");
140140
if (it != obj.cend()) {
141141
const auto& entry = it->second;
142142
if (!entry.is<picojson::array>())
@@ -154,7 +154,7 @@ std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppress
154154
}
155155
}
156156
{
157-
const auto it = utils::as_const(obj).find("suppressions");
157+
const auto it = obj.find("suppressions");
158158
if (it != obj.cend()) {
159159
const auto& entry = it->second;
160160
if (!entry.is<picojson::array>())
@@ -171,7 +171,7 @@ std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppress
171171
}
172172
}
173173
{
174-
const auto it = utils::as_const(obj).find("safety");
174+
const auto it = obj.find("safety");
175175
if (it != obj.cend()) {
176176
const auto& v = it->second;
177177
if (!v.is<bool>())

lib/symboldatabase.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,7 +3860,7 @@ void SymbolDatabase::returnImplicitIntError(const Token *tok) const
38603860
const Function* Type::getFunction(const std::string& funcName) const
38613861
{
38623862
if (classScope) {
3863-
const auto it = utils::as_const(classScope->functionMap).find(funcName);
3863+
const auto it = classScope->functionMap.find(funcName);
38643864
if (it != classScope->functionMap.end())
38653865
return it->second;
38663866
}
@@ -4834,7 +4834,7 @@ std::vector<const Function*> Function::getOverloadedFunctions() const
48344834

48354835
while (scope) {
48364836
const bool isMemberFunction = scope->isClassOrStruct() && !isStatic();
4837-
for (auto it = utils::as_const(scope->functionMap).find(tokenDef->str());
4837+
for (auto it = scope->functionMap.find(tokenDef->str());
48384838
it != scope->functionMap.end() && it->first == tokenDef->str();
48394839
++it) {
48404840
const Function* func = it->second;
@@ -4885,7 +4885,7 @@ const Function * Function::getOverriddenFunctionRecursive(const ::Type* baseType
48854885
const Scope *parent = derivedFromType->classScope;
48864886

48874887
// check if function defined in base class
4888-
auto range = utils::as_const(parent->functionMap).equal_range(tokenDef->str());
4888+
auto range = parent->functionMap.equal_range(tokenDef->str());
48894889
for (auto it = range.first; it != range.second; ++it) {
48904890
const Function * func = it->second;
48914891
if (func->isImplicitlyVirtual()) { // Base is virtual and of same name
@@ -5832,7 +5832,7 @@ void Scope::findFunctionInBase(const Token* tok, nonneg int args, std::vector<co
58325832
if (base->classScope == this) // Ticket #5120, #5125: Recursive class; tok should have been found already
58335833
continue;
58345834

5835-
auto range = utils::as_const(base->classScope->functionMap).equal_range(tok->str());
5835+
auto range = base->classScope->functionMap.equal_range(tok->str());
58365836
for (auto it = range.first; it != range.second; ++it) {
58375837
const Function *func = it->second;
58385838
if (func->isDestructor() && !Token::simpleMatch(tok->tokAt(-1), "~"))
@@ -5994,7 +5994,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst, Referen
59945994
const std::size_t args = arguments.size();
59955995

59965996
auto addMatchingFunctions = [&](const Scope *scope) {
5997-
auto range = utils::as_const(scope->functionMap).equal_range(tok->str());
5997+
auto range = scope->functionMap.equal_range(tok->str());
59985998
for (auto it = range.first; it != range.second; ++it) {
59995999
const Function *func = it->second;
60006000
if (ref == Reference::LValue && func->hasRvalRefQualifier())
@@ -6762,7 +6762,7 @@ Function * SymbolDatabase::findFunctionInScope(const Token *func, const Scope *n
67626762
const Function * function = nullptr;
67636763
const bool destructor = func->strAt(-1) == "~";
67646764

6765-
auto range = utils::as_const(ns->functionMap).equal_range(func->str());
6765+
auto range = ns->functionMap.equal_range(func->str());
67666766
for (auto it = range.first; it != range.second; ++it) {
67676767
if (it->second->argsMatch(ns, it->second->argDef, func->next(), path, path_length) &&
67686768
it->second->isDestructor() == destructor) {
@@ -7681,14 +7681,14 @@ static const Function *getOperatorFunction(const Token * const tok)
76817681

76827682
const Scope *classScope = getClassScope(tok->astOperand1());
76837683
if (classScope) {
7684-
auto it = utils::as_const(classScope->functionMap).find(functionName);
7684+
auto it = classScope->functionMap.find(functionName);
76857685
if (it != classScope->functionMap.end())
76867686
return it->second;
76877687
}
76887688

76897689
classScope = getClassScope(tok->astOperand2());
76907690
if (classScope) {
7691-
auto it = utils::as_const(classScope->functionMap).find(functionName);
7691+
auto it = classScope->functionMap.find(functionName);
76927692
if (it != classScope->functionMap.end())
76937693
return it->second;
76947694
}

lib/tokenize.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Tokenizer::~Tokenizer()
135135

136136
nonneg int Tokenizer::sizeOfType(const std::string& type) const
137137
{
138-
const auto it = utils::as_const(mTypeSize).find(type);
138+
const auto it = mTypeSize.find(type);
139139
if (it == mTypeSize.end()) {
140140
const Library::PodType* podtype = mSettings.library.podtype(type);
141141
if (!podtype)
@@ -154,7 +154,7 @@ nonneg int Tokenizer::sizeOfType(const Token *type) const
154154
if (type->tokType() == Token::eString)
155155
return Token::getStrLength(type) + 1U;
156156

157-
const auto it = utils::as_const(mTypeSize).find(type->str());
157+
const auto it = mTypeSize.find(type->str());
158158
if (it == mTypeSize.end()) {
159159
const Library::PodType* podtype = mSettings.library.podtype(type->str());
160160
if (!podtype)
@@ -4629,7 +4629,7 @@ void Tokenizer::setVarIdClassFunction(const std::string &classname,
46294629
if (Token::Match(tok2, "%name% ::"))
46304630
continue;
46314631

4632-
const auto it = utils::as_const(varlist).find(tok2->str());
4632+
const auto it = varlist.find(tok2->str());
46334633
if (it != varlist.end()) {
46344634
tok2->varId(it->second);
46354635
setVarIdStructMembers(tok2, structMembers, varId_);
@@ -7799,7 +7799,7 @@ bool Tokenizer::simplifyCAlternativeTokens()
77997799
if (!tok->isName())
78007800
continue;
78017801

7802-
const auto cOpIt = utils::as_const(cAlternativeTokens).find(tok->str());
7802+
const auto cOpIt = cAlternativeTokens.find(tok->str());
78037803
if (cOpIt != cAlternativeTokens.end()) {
78047804
alt.push_back(tok);
78057805

@@ -7841,7 +7841,7 @@ bool Tokenizer::simplifyCAlternativeTokens()
78417841
return false;
78427842

78437843
for (Token *tok: alt) {
7844-
const auto cOpIt = utils::as_const(cAlternativeTokens).find(tok->str());
7844+
const auto cOpIt = cAlternativeTokens.find(tok->str());
78457845
if (cOpIt != cAlternativeTokens.end())
78467846
tok->str(cOpIt->second);
78477847
else if (tok->str() == "not")
@@ -10442,7 +10442,7 @@ void Tokenizer::simplifyMicrosoftStringFunctions()
1044210442
if (tok->strAt(1) != "(")
1044310443
continue;
1044410444

10445-
const auto match = utils::as_const(apis).find(tok->str());
10445+
const auto match = apis.find(tok->str());
1044610446
if (match!=apis.end()) {
1044710447
tok->str(ansi ? match->second.mbcs : match->second.unicode);
1044810448
tok->originalName(match->first);

lib/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ namespace utils {
414414
template<class T>
415415
constexpr typename std::add_const<T>::type & as_const(T& t) noexcept
416416
{
417+
static_assert(!std::is_const<T>::value, "object is already const");
417418
// NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter) - potential false positive
418419
return t;
419420
}

0 commit comments

Comments
 (0)