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

Method checkScope

lib/checkuninitvar.cpp:130–233  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128}
129
130void CheckUninitVarImpl::checkScope(const Scope* scope, const std::set<std::string> &arrayTypeDefs)
131{
132 for (const Variable &var : scope->varlist) {
133 if ((mTokenizer->isCPP() && var.type() && !var.isPointer() && var.type()->needInitialization != Type::NeedInitialization::True) ||
134 var.isStatic() || var.isExtern() || var.isReference())
135 continue;
136
137 // don't warn for try/catch exception variable
138 if (var.isThrow())
139 continue;
140
141 if (Token::Match(var.nameToken()->next(), "[({:]"))
142 continue;
143
144 if (Token::Match(var.nameToken(), "%name% =")) { // Variable is initialized, but Rhs might be not
145 checkRhs(var.nameToken(), var, NO_ALLOC, 0U, "");
146 continue;
147 }
148 if (Token::Match(var.nameToken(), "%name% ) (") && Token::simpleMatch(var.nameToken()->linkAt(2), ") =")) { // Function pointer is initialized, but Rhs might be not
149 checkRhs(var.nameToken()->linkAt(2)->next(), var, NO_ALLOC, 0U, "");
150 continue;
151 }
152
153 if (var.isArray() || var.isPointerToArray()) {
154 const Token *tok = var.nameToken()->next();
155 if (var.isPointerToArray())
156 tok = tok->next();
157 while (Token::simpleMatch(tok->link(), "] ["))
158 tok = tok->link()->next();
159 if (Token::Match(tok->link(), "] =|{|("))
160 continue;
161 }
162
163 bool stdtype = var.typeStartToken()->isC() && arrayTypeDefs.find(var.typeStartToken()->str()) == arrayTypeDefs.end();
164 const Token* tok = var.typeStartToken();
165 for (; tok != var.nameToken() && tok->str() != "<"; tok = tok->next()) {
166 if (tok->isStandardType() || tok->isEnumType())
167 stdtype = true;
168 }
169 if (var.isArray() && !stdtype) { // std::array
170 if (!(var.isStlType() && Token::simpleMatch(var.typeStartToken(), "std :: array") && var.valueType() &&
171 var.valueType()->containerTypeToken && var.valueType()->containerTypeToken->isStandardType()))
172 continue;
173 }
174
175 while (tok && tok->str() != ";")
176 tok = tok->next();
177 if (!tok)
178 continue;
179
180 if (tok->astParent() && Token::simpleMatch(tok->astParent()->previous(), "for (") && Token::simpleMatch(tok->astParent()->link()->next(), "{") &&
181 checkLoopBody(tok->astParent()->link()->next(), var, var.isArray() ? ARRAY : NO_ALLOC, "", true))
182 continue;
183
184 if (var.isArray()) {
185 bool init = false;
186 for (const Token *parent = var.nameToken(); parent; parent = parent->astParent()) {
187 if (parent->str() == "=") {

Callers

nothing calls this directly

Calls 15

getVariableValuesFunction · 0.85
findAllocFuncCallTokenFunction · 0.85
isCPPMethod · 0.80
typeMethod · 0.80
nextMethod · 0.80
linkAtMethod · 0.80
isArrayMethod · 0.80
isCMethod · 0.80
isEnumTypeMethod · 0.80
astParentMethod · 0.80
astOperand2Method · 0.80
getAllocFuncInfoMethod · 0.80

Tested by

no test coverage detected