MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / checkShadowVariables

Method checkShadowVariables

lib/checkother.cpp:4156–4217  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4154}
4155
4156void CheckOtherImpl::checkShadowVariables()
4157{
4158 if (!mSettings.severity.isEnabled(Severity::style) && !mSettings.isPremiumEnabled("shadowVariable"))
4159 return;
4160 logChecker("CheckOther::checkShadowVariables"); // style
4161 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
4162 for (const Scope & scope : symbolDatabase->scopeList) {
4163 if (!scope.isExecutable() || scope.type == ScopeType::eLambda)
4164 continue;
4165 const Scope *functionScope = &scope;
4166 while (functionScope && functionScope->type != ScopeType::eFunction && functionScope->type != ScopeType::eLambda)
4167 functionScope = functionScope->nestedIn;
4168 const auto checkVar = [&](const Variable &var) {
4169 if (!var.nameToken())
4170 return;
4171
4172 if (var.nameToken()->isExpandedMacro()) // #8903
4173 return;
4174
4175 if (!var.isArgument() && functionScope && functionScope->type == ScopeType::eFunction && functionScope->function) {
4176 const auto & argList = functionScope->function->argumentList;
4177 auto it = std::find_if(argList.cbegin(), argList.cend(), [&](const Variable& arg) {
4178 return arg.nameToken() && var.name() == arg.name();
4179 });
4180 if (it != argList.end()) {
4181 shadowError(var.nameToken(), "local variable", it->nameToken(), "argument");
4182 return;
4183 }
4184 }
4185
4186 const Token *shadowed = findShadowed(scope.nestedIn, var, var.nameToken()->linenr());
4187 if (!shadowed)
4188 shadowed = findShadowed(scope.functionOf, var, var.nameToken()->linenr());
4189 if (!shadowed)
4190 return;
4191 if (scope.type == ScopeType::eFunction && scope.className == var.name())
4192 return;
4193 if (functionScope->functionOf && functionScope->functionOf->isClassOrStructOrUnion() && functionScope->function &&
4194 (functionScope->function->isStatic() || functionScope->function->isFriend()) &&
4195 shadowed->variable() && !shadowed->variable()->isLocal())
4196 return;
4197 if (functionScope->functionOf && functionScope->functionOf->isClassOrStructOrUnion() && functionScope->function &&
4198 functionScope->function->isStatic() && shadowed->function() && !shadowed->function()->isStatic())
4199 return;
4200 if (var.scope() && var.scope()->function && var.scope()->function->isConstructor()) {
4201 if (shadowed->variable() && shadowed->variable()->isMember())
4202 return;
4203 if (shadowed->function() && shadowed->function()->nestedIn &&
4204 shadowed->function()->nestedIn->isClassOrStruct())
4205 return;
4206 }
4207 shadowError(var.nameToken(), var.isArgument() ? "argument" : "local variable",
4208 shadowed, (shadowed->varId() != 0) ?
4209 (shadowed->variable()->isMember() ? "member" : "variable") : "function");
4210 };
4211 for (const Variable &var : scope.varlist)
4212 checkVar(var);
4213 if (functionScope && functionScope->type == ScopeType::eFunction && functionScope->function)

Callers 1

runChecksMethod · 0.80

Calls 11

findShadowedFunction · 0.85
isPremiumEnabledMethod · 0.80
isFriendMethod · 0.80
variableMethod · 0.80
scopeMethod · 0.80
isMemberMethod · 0.80
isEnabledMethod · 0.45
nameTokenMethod · 0.45
nameMethod · 0.45
endMethod · 0.45
functionMethod · 0.45

Tested by

no test coverage detected