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

Method arraySizeAfterValueFlow

lib/tokenize.cpp:3990–4027  ·  view source on GitHub ↗

cppcheck-suppress functionConst

Source from the content-addressed store, hash-verified

3988
3989// cppcheck-suppress functionConst
3990void Tokenizer::arraySizeAfterValueFlow()
3991{
3992 // After ValueFlow, adjust array sizes.
3993 for (const Variable* var: mSymbolDatabase->variableList()) {
3994 if (!var || !var->isArray())
3995 continue;
3996 if (!Token::Match(var->nameToken(), "%name% [ ] = { ["))
3997 continue;
3998 MathLib::bigint maxIndex = -1;
3999 const Token* const startToken = var->nameToken()->tokAt(4);
4000 const Token* const endToken = startToken->link();
4001 for (const Token* tok = startToken; tok != endToken; tok = tok->next()) {
4002 if (!Token::Match(tok, "[{,] [") || !Token::simpleMatch(tok->linkAt(1), "] ="))
4003 continue;
4004 const Token* expr = tok->next()->astOperand1();
4005 if (expr && expr->hasKnownIntValue())
4006 maxIndex = std::max(maxIndex, expr->getKnownIntValue());
4007 }
4008 if (maxIndex >= 0) {
4009 // insert array size
4010 auto* tok = const_cast<Token*>(var->nameToken()->next());
4011 tok->insertToken(MathLib::toString(maxIndex + 1));
4012 // ast
4013 tok->astOperand2(tok->next());
4014 // Token::scope
4015 tok->next()->scope(tok->scope());
4016 // Value flow
4017 ValueFlow::Value value(maxIndex + 1);
4018 value.setKnown();
4019 tok->next()->addValue(value);
4020 // Set array dimensions
4021 Dimension d;
4022 d.num = maxIndex + 1;
4023 std::vector<Dimension> dimensions{d};
4024 const_cast<Variable*>(var)->setDimensions(dimensions);
4025 }
4026 }
4027}
4028
4029static Token *skipTernaryOp(Token *tok)
4030{

Callers

nothing calls this directly

Calls 14

isArrayMethod · 0.80
nextMethod · 0.80
linkAtMethod · 0.80
astOperand1Method · 0.80
getKnownIntValueMethod · 0.80
insertTokenMethod · 0.80
astOperand2Method · 0.80
scopeMethod · 0.80
addValueMethod · 0.80
simpleMatchFunction · 0.70
toStringFunction · 0.70
nameTokenMethod · 0.45

Tested by

no test coverage detected