Skip to content

Commit c331d49

Browse files
authored
prevent unnecessary utils::as_const() calls (danmar#8316)
1 parent 498b2c6 commit c331d49

9 files changed

Lines changed: 31 additions & 30 deletions

File tree

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/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
@@ -3864,7 +3864,7 @@ void SymbolDatabase::returnImplicitIntError(const Token *tok) const
38643864
const Function* Type::getFunction(const std::string& funcName) const
38653865
{
38663866
if (classScope) {
3867-
const auto it = utils::as_const(classScope->functionMap).find(funcName);
3867+
const auto it = classScope->functionMap.find(funcName);
38683868
if (it != classScope->functionMap.end())
38693869
return it->second;
38703870
}
@@ -4838,7 +4838,7 @@ std::vector<const Function*> Function::getOverloadedFunctions() const
48384838

48394839
while (scope) {
48404840
const bool isMemberFunction = scope->isClassOrStruct() && !isStatic();
4841-
for (auto it = utils::as_const(scope->functionMap).find(tokenDef->str());
4841+
for (auto it = scope->functionMap.find(tokenDef->str());
48424842
it != scope->functionMap.end() && it->first == tokenDef->str();
48434843
++it) {
48444844
const Function* func = it->second;
@@ -4889,7 +4889,7 @@ const Function * Function::getOverriddenFunctionRecursive(const ::Type* baseType
48894889
const Scope *parent = derivedFromType->classScope;
48904890

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

5839-
auto range = utils::as_const(base->classScope->functionMap).equal_range(tok->str());
5839+
auto range = base->classScope->functionMap.equal_range(tok->str());
58405840
for (auto it = range.first; it != range.second; ++it) {
58415841
const Function *func = it->second;
58425842
if (func->isDestructor() && !Token::simpleMatch(tok->tokAt(-1), "~"))
@@ -5998,7 +5998,7 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst, Referen
59985998
const std::size_t args = arguments.size();
59995999

60006000
auto addMatchingFunctions = [&](const Scope *scope) {
6001-
auto range = utils::as_const(scope->functionMap).equal_range(tok->str());
6001+
auto range = scope->functionMap.equal_range(tok->str());
60026002
for (auto it = range.first; it != range.second; ++it) {
60036003
const Function *func = it->second;
60046004
if (ref == Reference::LValue && func->hasRvalRefQualifier())
@@ -6766,7 +6766,7 @@ Function * SymbolDatabase::findFunctionInScope(const Token *func, const Scope *n
67666766
const Function * function = nullptr;
67676767
const bool destructor = func->strAt(-1) == "~";
67686768

6769-
auto range = utils::as_const(ns->functionMap).equal_range(func->str());
6769+
auto range = ns->functionMap.equal_range(func->str());
67706770
for (auto it = range.first; it != range.second; ++it) {
67716771
if (it->second->argsMatch(ns, it->second->argDef, func->next(), path, path_length) &&
67726772
it->second->isDestructor() == destructor) {
@@ -7685,14 +7685,14 @@ static const Function *getOperatorFunction(const Token * const tok)
76857685

76867686
const Scope *classScope = getClassScope(tok->astOperand1());
76877687
if (classScope) {
7688-
auto it = utils::as_const(classScope->functionMap).find(functionName);
7688+
auto it = classScope->functionMap.find(functionName);
76897689
if (it != classScope->functionMap.end())
76907690
return it->second;
76917691
}
76927692

76937693
classScope = getClassScope(tok->astOperand2());
76947694
if (classScope) {
7695-
auto it = utils::as_const(classScope->functionMap).find(functionName);
7695+
auto it = classScope->functionMap.find(functionName);
76967696
if (it != classScope->functionMap.end())
76977697
return it->second;
76987698
}

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)
@@ -4630,7 +4630,7 @@ void Tokenizer::setVarIdClassFunction(const std::string &classname,
46304630
if (Token::Match(tok2, "%name% ::"))
46314631
continue;
46324632

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

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

@@ -7842,7 +7842,7 @@ bool Tokenizer::simplifyCAlternativeTokens()
78427842
return false;
78437843

78447844
for (Token *tok: alt) {
7845-
const auto cOpIt = utils::as_const(cAlternativeTokens).find(tok->str());
7845+
const auto cOpIt = cAlternativeTokens.find(tok->str());
78467846
if (cOpIt != cAlternativeTokens.end())
78477847
tok->str(cOpIt->second);
78487848
else if (tok->str() == "not")
@@ -10452,7 +10452,7 @@ void Tokenizer::simplifyMicrosoftStringFunctions()
1045210452
if (tok->strAt(1) != "(")
1045310453
continue;
1045410454

10455-
const auto match = utils::as_const(apis).find(tok->str());
10455+
const auto match = apis.find(tok->str());
1045610456
if (match!=apis.end()) {
1045710457
tok->str(ansi ? match->second.mbcs : match->second.unicode);
1045810458
tok->originalName(match->first);

lib/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ namespace utils {
418418
template<class T>
419419
constexpr typename std::add_const<T>::type & as_const(T& t) noexcept
420420
{
421+
static_assert(!std::is_const<T>::value, "object is already const");
421422
// NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter) - potential false positive
422423
return t;
423424
}

0 commit comments

Comments
 (0)