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

Function valueFlowArray

lib/valueflow.cpp:635–688  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

633}
634
635static void valueFlowArray(TokenList& tokenlist, const Settings& settings)
636{
637 std::map<nonneg int, const Token*> constantArrays;
638
639 for (Token* tok = tokenlist.front(); tok; tok = tok->next()) {
640 if (tok->varId() > 0) {
641 // array
642 const auto it = utils::as_const(constantArrays).find(tok->varId());
643 if (it != constantArrays.end()) {
644 ValueFlow::Value value;
645 value.valueType = ValueFlow::Value::ValueType::TOK;
646 value.tokvalue = it->second;
647 value.setKnown();
648 setTokenValue(tok, std::move(value), settings);
649 }
650
651 // const array decl
652 else if (tok->variable() && tok->variable()->isArray() && tok->variable()->isConst() &&
653 tok->variable()->nameToken() == tok && Token::Match(tok, "%var% [ %num%| ] = {")) {
654 Token* rhstok = tok->linkAt(1)->tokAt(2);
655 constantArrays[tok->varId()] = rhstok;
656 tok = rhstok->link();
657 }
658
659 // pointer = array
660 else if (tok->variable() && tok->variable()->isArray() && Token::simpleMatch(tok->astParent(), "=") &&
661 astIsRHS(tok) && tok->astParent()->astOperand1() && tok->astParent()->astOperand1()->variable() &&
662 tok->astParent()->astOperand1()->variable()->isPointer()) {
663 ValueFlow::Value value;
664 value.valueType = ValueFlow::Value::ValueType::TOK;
665 value.tokvalue = tok;
666 value.setKnown();
667 setTokenValue(tok, std::move(value), settings);
668 }
669 continue;
670 }
671
672 if (Token::Match(tok, "const %type% %var% [ %num%| ] = {")) {
673 Token* vartok = tok->tokAt(2);
674 Token* rhstok = vartok->linkAt(1)->tokAt(2);
675 constantArrays[vartok->varId()] = rhstok;
676 tok = rhstok->link();
677 continue;
678 }
679
680 if (Token::Match(tok, "const char %var% [ %num%| ] = %str% ;")) {
681 Token* vartok = tok->tokAt(2);
682 Token* strtok = vartok->linkAt(1)->tokAt(2);
683 constantArrays[vartok->varId()] = strtok;
684 tok = strtok->next();
685 continue;
686 }
687 }
688}
689
690static bool isNonZero(const Token* tok)
691{

Callers 1

setValuesMethod · 0.85

Calls 15

setTokenValueFunction · 0.85
astIsRHSFunction · 0.85
frontMethod · 0.80
nextMethod · 0.80
variableMethod · 0.80
isArrayMethod · 0.80
isConstMethod · 0.80
linkAtMethod · 0.80
astParentMethod · 0.80
astOperand1Method · 0.80
simpleMatchFunction · 0.70
findMethod · 0.45

Tested by

no test coverage detected