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

Method checkScopeForVariable

lib/checkuninitvar.cpp:396–854  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

394}
395
396bool CheckUninitVarImpl::checkScopeForVariable(const Token *tok, const Variable& var, bool * const possibleInit, bool * const noreturn, Alloc* const alloc, const std::string &membervar, std::map<nonneg int, VariableValue>& variableValue)
397{
398 const bool suppressErrors(possibleInit && *possibleInit); // Assume that this is a variable declaration, rather than a fundef
399 const bool printDebug = mSettings.debugwarnings;
400
401 if (possibleInit)
402 *possibleInit = false;
403
404 int number_of_if = 0;
405
406 if (var.declarationId() == 0U)
407 return true;
408
409 for (; tok; tok = tok->next()) {
410 // End of scope..
411 if (tok->str() == "}") {
412 if (number_of_if && possibleInit)
413 *possibleInit = true;
414
415 // might be a noreturn function..
416 if (mTokenizer->isScopeNoReturn(tok)) {
417 if (noreturn)
418 *noreturn = true;
419 return false;
420 }
421
422 break;
423 }
424
425 // Unconditional inner scope, try, lambda, init list
426 if (tok->str() == "{" && Token::Match(tok->previous(), ",|;|{|}|]|try")) {
427 bool possibleInitInner = false;
428 if (checkScopeForVariable(tok->next(), var, &possibleInitInner, noreturn, alloc, membervar, variableValue))
429 return true;
430 tok = tok->link();
431 if (possibleInitInner) {
432 number_of_if = 1;
433 if (possibleInit)
434 *possibleInit = true;
435 }
436 continue;
437 }
438
439 // track values of other variables..
440 if (Token::Match(tok->previous(), "[;{}.] %var% =")) {
441 if (tok->next()->astOperand2() && tok->next()->astOperand2()->hasKnownIntValue())
442 variableValue[tok->varId()] = VariableValue(tok->next()->astOperand2()->getKnownIntValue());
443 else if (Token::Match(tok->previous(), "[;{}] %var% = - %name% ;"))
444 variableValue[tok->varId()] = !VariableValue(0);
445 else
446 variableValue.erase(tok->varId());
447 }
448
449 // Inner scope..
450 else if (Token::simpleMatch(tok, "if (")) {
451 bool alwaysTrue = false;
452 bool alwaysFalse = false;
453

Callers

nothing calls this directly

Calls 15

VariableValueClass · 0.85
astIsVariableComparisonFunction · 0.85
visitAstNodesFunction · 0.85
isUnevaluatedFunction · 0.85
isVariableUsedFunction · 0.85
uninitvarErrorFunction · 0.85
isVariableUsageFunction · 0.85
astIsLHSFunction · 0.85
nextMethod · 0.80
astOperand2Method · 0.80
getKnownIntValueMethod · 0.80

Tested by

no test coverage detected