From 66ebc187f6cd41daa434d438ad1ae47412140a2b Mon Sep 17 00:00:00 2001 From: Ken-Patrick Date: Sun, 16 Jun 2019 10:09:38 +0200 Subject: [PATCH] Cleanup some const_cast of Token* (#1886) * Add non const version of some methods of Token The aim is to reduce the (ab)use of const_cast. * Cleanup some more const_cast in valueflow * Remove useless const_cast * Remove some const_cast from templatesimplifier * Remove some const_cast from valueflow --- lib/symboldatabase.cpp | 12 ++-- lib/templatesimplifier.cpp | 20 +++---- lib/token.h | 16 ++++++ lib/tokenlist.cpp | 10 ++-- lib/valueflow.cpp | 112 ++++++++++++++++++------------------- lib/valueflow.h | 2 +- test/testtoken.cpp | 2 +- 7 files changed, 95 insertions(+), 79 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index d23708d62..19d2cbbf4 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1243,7 +1243,7 @@ void SymbolDatabase::createSymbolDatabaseEnums() // look for possible constant folding expressions // rhs of operator: - const Token *rhs = enumerator.start->previous()->astOperand2(); + Token *rhs = enumerator.start->previous()->astOperand2(); // constant folding of expression: ValueFlow::valueFlowConstantFoldAST(rhs, mSettings); @@ -2469,7 +2469,7 @@ bool Variable::arrayDimensions(const Settings* settings) while (tok->astParent() && !Token::Match(tok->astParent(), "[,<>]")) tok = tok->astParent(); dimension_.tok = tok; - ValueFlow::valueFlowConstantFoldAST(dimension_.tok, settings); + ValueFlow::valueFlowConstantFoldAST(const_cast(dimension_.tok), settings); if (tok->hasKnownIntValue()) { dimension_.num = tok->getKnownIntValue(); dimension_.known = true; @@ -2500,7 +2500,7 @@ bool Variable::arrayDimensions(const Settings* settings) // check for empty array dimension [] if (dim->next()->str() != "]") { dimension_.tok = dim->astOperand2(); - ValueFlow::valueFlowConstantFoldAST(dimension_.tok, settings); + ValueFlow::valueFlowConstantFoldAST(const_cast(dimension_.tok), settings); if (dimension_.tok && dimension_.tok->hasKnownIntValue()) { dimension_.num = dimension_.tok->getKnownIntValue(); dimension_.known = true; @@ -4889,7 +4889,7 @@ static void setAutoTokenProperties(Token * const autoTok) void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype) { tok->setValueType(new ValueType(valuetype)); - Token *parent = const_cast(tok->astParent()); + Token *parent = tok->astParent(); if (!parent || parent->valueType()) return; if (!parent->astOperand1()) @@ -5019,7 +5019,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype) !parent->previous()->valueType() && Token::simpleMatch(parent->astParent()->astOperand1(), "for")) { const bool isconst = Token::simpleMatch(parent->astParent()->next(), "const"); - Token * const autoToken = const_cast(parent->astParent()->tokAt(isconst ? 2 : 1)); + Token * const autoToken = parent->astParent()->tokAt(isconst ? 2 : 1); if (vt2->pointer) { ValueType autovt(*vt2); autovt.pointer--; @@ -5091,7 +5091,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype) if (ternary) { if (vt2 && vt1->pointer == vt2->pointer && vt1->type == vt2->type && vt1->sign == vt2->sign) setValueType(parent, *vt2); - parent = const_cast(parent->astParent()); + parent = parent->astParent(); } if (ternary || parent->isArithmeticalOp() || parent->tokType() == Token::eIncDecOp) { diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 8b093a18c..0b33b0403 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -757,9 +757,9 @@ void TemplateSimplifier::getTemplateInstantiations() const bool isUsing = tok->strAt(1) == "using"; if (isUsing && Token::Match(tok->tokAt(2), "%name% <")) { // Cant have specialized type alias so ignore it - const Token *tok2 = Token::findsimplematch(tok->tokAt(3), ";"); + Token *tok2 = Token::findsimplematch(tok->tokAt(3), ";"); if (tok2) - tok = const_cast(tok2); + tok = tok2; } else if (tok->strAt(-1) == "<") { // Don't ignore user specialization but don't consider it an instantiation. // Instantiations in return type, function parameters, and executable code @@ -771,22 +771,22 @@ void TemplateSimplifier::getTemplateInstantiations() // #7914 // Ignore template instantiations within template definitions: they will only be // handled if the definition is actually instantiated - const Token *tok2 = Token::findmatch(tok, "{|;"); + Token *tok2 = Token::findmatch(tok, "{|;"); if (tok2 && tok2->str() == "{") tok = tok2->link(); else if (tok2 && tok2->str() == ";") - tok = const_cast(tok2); + tok = tok2; } } else if (Token::Match(tok, "template using %name% <")) { // Cant have specialized type alias so ignore it - const Token *tok2 = Token::findsimplematch(tok->tokAt(3), ";"); + Token *tok2 = Token::findsimplematch(tok->tokAt(3), ";"); if (tok2) - tok = const_cast(tok2); + tok = tok2; } else if (Token::Match(tok, "using %name% <")) { // Cant have specialized type alias so ignore it - const Token *tok2 = Token::findsimplematch(tok->tokAt(2), ";"); + Token *tok2 = Token::findsimplematch(tok->tokAt(2), ";"); if (tok2) - tok = const_cast(tok2); + tok = tok2; } else if (Token::Match(tok->previous(), "(|{|}|;|=|>|<<|:|.|*|&|return|<|, %name% ::|<|(") || Token::Match(tok->previous(), "%type% %name% ::|<") || Token::Match(tok->tokAt(-2), "[,:] private|protected|public %name% ::|<")) { @@ -879,7 +879,7 @@ void TemplateSimplifier::getTemplateInstantiations() // Add inner template instantiations first => go to the ">" // and then parse backwards, adding all seen instantiations - const Token *tok2 = tok->next()->findClosingBracket(); + Token *tok2 = tok->next()->findClosingBracket(); // parse backwards and add template instantiations // TODO @@ -888,7 +888,7 @@ void TemplateSimplifier::getTemplateInstantiations() templateParameters(tok2->tokAt(2))) { addInstantiation(tok2->next(), getScopeName(scopeList)); } else if (Token::Match(tok2->next(), "class|struct")) - const_cast(tok2)->deleteNext(); + tok2->deleteNext(); } // Add outer template.. diff --git a/lib/token.h b/lib/token.h index a9a0b7b6e..822726a2c 100644 --- a/lib/token.h +++ b/lib/token.h @@ -1088,15 +1088,31 @@ public: void astOperand1(Token *tok); void astOperand2(Token *tok); + Token * astOperand1() { + return mImpl->mAstOperand1; + } const Token * astOperand1() const { return mImpl->mAstOperand1; } + Token * astOperand2() { + return mImpl->mAstOperand2; + } const Token * astOperand2() const { return mImpl->mAstOperand2; } + Token * astParent() { + return mImpl->mAstParent; + } const Token * astParent() const { return mImpl->mAstParent; } + Token *astTop() { + Token *ret = this; + while (ret->mImpl->mAstParent) + ret = ret->mImpl->mAstParent; + return ret; + } + const Token *astTop() const { const Token *ret = this; while (ret->mImpl->mAstParent) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 53b29f5ba..040aa8cea 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1071,9 +1071,9 @@ static void createAstAtTokenInner(Token * const tok1, const Token *endToken, boo tok = createAstAtToken(tok, cpp); } else if (tok->str() == "[") { if (isLambdaCaptureList(tok)) { - tok = const_cast(tok->astOperand1()); + tok = tok->astOperand1(); if (tok->str() == "(") - tok = const_cast(tok->astOperand1()); + tok = tok->astOperand1(); const Token * const endToken2 = tok->link(); for (; tok && tok != endToken && tok != endToken2; tok = tok ? tok->next() : nullptr) tok = createAstAtToken(tok, cpp); @@ -1086,7 +1086,7 @@ static Token * findAstTop(Token *tok1, Token *tok2) { for (Token *tok = tok1; tok && (tok != tok2); tok = tok->next()) { if (tok->astParent() || tok->astOperand1() || tok->astOperand2()) - return const_cast(tok->astTop()); + return tok->astTop(); if (Token::simpleMatch(tok, "( {")) tok = tok->link(); } @@ -1149,7 +1149,7 @@ static Token * createAstAtToken(Token *tok, bool cpp) compileExpression(tok2, state3); if (init != semicolon1) - semicolon1->astOperand1(const_cast(init->astTop())); + semicolon1->astOperand1(init->astTop()); tok2 = findAstTop(semicolon1->next(), semicolon2); if (tok2) semicolon2->astOperand1(tok2); @@ -1157,7 +1157,7 @@ static Token * createAstAtToken(Token *tok, bool cpp) if (tok2) semicolon2->astOperand2(tok2); else if (!state3.op.empty()) - semicolon2->astOperand2(const_cast(state3.op.top())); + semicolon2->astOperand2(state3.op.top()); semicolon1->astOperand2(semicolon2); tok->next()->astOperand1(tok); diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 2856e8849..3be9faa60 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -402,7 +402,7 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti if (value.isUninitValue()) return; - Token *parent = const_cast(tok->astParent()); + Token *parent = tok->astParent(); if (!parent) return; @@ -436,12 +436,12 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti if (yields == Library::Container::Yield::SIZE) { ValueFlow::Value v(value); v.valueType = ValueFlow::Value::ValueType::INT; - setTokenValue(const_cast(parent->astParent()), v, settings); + setTokenValue(parent->astParent(), v, settings); } else if (yields == Library::Container::Yield::EMPTY) { ValueFlow::Value v(value); v.intvalue = !v.intvalue; v.valueType = ValueFlow::Value::ValueType::INT; - setTokenValue(const_cast(parent->astParent()), v, settings); + setTokenValue(parent->astParent(), v, settings); } } @@ -853,30 +853,30 @@ static size_t getSizeOf(const ValueType &vt, const Settings *settings) } // Handle various constants.. -static Token * valueFlowSetConstantValue(const Token *tok, const Settings *settings, bool cpp) +static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, bool cpp) { if ((tok->isNumber() && MathLib::isInt(tok->str())) || (tok->tokType() == Token::eChar)) { ValueFlow::Value value(MathLib::toLongNumber(tok->str())); if (!tok->isTemplateArg()) value.setKnown(); - setTokenValue(const_cast(tok), value, settings); + setTokenValue(tok, value, settings); } else if (tok->isNumber() && MathLib::isFloat(tok->str())) { ValueFlow::Value value; value.valueType = ValueFlow::Value::FLOAT; value.floatValue = MathLib::toDoubleNumber(tok->str()); if (!tok->isTemplateArg()) value.setKnown(); - setTokenValue(const_cast(tok), value, settings); + setTokenValue(tok, value, settings); } else if (tok->enumerator() && tok->enumerator()->value_known) { ValueFlow::Value value(tok->enumerator()->value); if (!tok->isTemplateArg()) value.setKnown(); - setTokenValue(const_cast(tok), value, settings); + setTokenValue(tok, value, settings); } else if (tok->str() == "NULL" || (cpp && tok->str() == "nullptr")) { ValueFlow::Value value(0); if (!tok->isTemplateArg()) value.setKnown(); - setTokenValue(const_cast(tok), value, settings); + setTokenValue(tok, value, settings); } else if (Token::simpleMatch(tok, "sizeof (")) { const Token *tok2 = tok->tokAt(2); // skip over tokens to find variable or type @@ -893,7 +893,7 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti ValueFlow::Value value(sz); if (!tok2->isTemplateArg() && settings->platformType != cppcheck::Platform::Unspecified) value.setKnown(); - setTokenValue(const_cast(tok->next()), value, settings); + setTokenValue(tok->next(), value, settings); } } else if (tok2->enumerator() && tok2->enumerator()->scope) { long long size = settings->sizeof_int; @@ -904,8 +904,8 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti ValueFlow::Value value(size); if (!tok2->isTemplateArg() && settings->platformType != cppcheck::Platform::Unspecified) value.setKnown(); - setTokenValue(const_cast(tok), value, settings); - setTokenValue(const_cast(tok->next()), value, settings); + setTokenValue(tok, value, settings); + setTokenValue(tok->next(), value, settings); } else if (tok2->type() && tok2->type()->isEnumType()) { long long size = settings->sizeof_int; if (tok2->type()->classScope) { @@ -917,8 +917,8 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti ValueFlow::Value value(size); if (!tok2->isTemplateArg() && settings->platformType != cppcheck::Platform::Unspecified) value.setKnown(); - setTokenValue(const_cast(tok), value, settings); - setTokenValue(const_cast(tok->next()), value, settings); + setTokenValue(tok, value, settings); + setTokenValue(tok->next(), value, settings); } else if (Token::Match(tok, "sizeof ( %var% ) / sizeof (") && tok->next()->astParent() == tok->tokAt(4)) { // Get number of elements in array const Token *sz1 = tok->tokAt(2); @@ -933,7 +933,7 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti ValueFlow::Value value(sz1->variable()->dimension(0)); if (!tok2->isTemplateArg() && settings->platformType != cppcheck::Platform::Unspecified) value.setKnown(); - setTokenValue(const_cast(tok->tokAt(4)), value, settings); + setTokenValue(tok->tokAt(4), value, settings); } } else if (Token::Match(tok2, "%var% )")) { const Variable *var = tok2->variable(); @@ -962,8 +962,8 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti ValueFlow::Value value(count * size); if (settings->platformType != cppcheck::Platform::Unspecified) value.setKnown(); - setTokenValue(const_cast(tok), value, settings); - setTokenValue(const_cast(tok->next()), value, settings); + setTokenValue(tok, value, settings); + setTokenValue(tok->next(), value, settings); } } } else if (!tok2->type()) { @@ -973,7 +973,7 @@ static Token * valueFlowSetConstantValue(const Token *tok, const Settings *setti ValueFlow::Value value(sz); if (!tok2->isTemplateArg() && settings->platformType != cppcheck::Platform::Unspecified) value.setKnown(); - setTokenValue(const_cast(tok->next()), value, settings); + setTokenValue(tok->next(), value, settings); } } // skip over enum @@ -1447,14 +1447,14 @@ static void valueFlowOppositeCondition(SymbolDatabase *symboldatabase, const Set tok2 = tok2->linkAt(1); if (!Token::simpleMatch(tok2, "} else { if (")) break; - const Token *ifOpenBraceTok = tok2->tokAt(4); - const Token *cond2 = ifOpenBraceTok->astOperand2(); + Token *ifOpenBraceTok = tok2->tokAt(4); + Token *cond2 = ifOpenBraceTok->astOperand2(); if (!cond2 || !cond2->isComparisonOp()) continue; if (isOppositeCond(true, cpp, cond1, cond2, settings->library, true, true)) { ValueFlow::Value value(1); value.setKnown(); - setTokenValue(const_cast(cond2), value, settings); + setTokenValue(cond2, value, settings); } tok2 = ifOpenBraceTok->link(); } @@ -1879,7 +1879,7 @@ static void valueFlowAST(Token *tok, unsigned int varid, const ValueFlow::Value return; if (tok->varId() == varid) setTokenValue(tok, value, settings); - valueFlowAST(const_cast(tok->astOperand1()), varid, value, settings); + valueFlowAST(tok->astOperand1(), varid, value, settings); if (tok->str() == "&&" && tok->astOperand1() && tok->astOperand1()->getValue(0)) { ProgramMemory pm; pm.setValue(varid,value); @@ -1898,7 +1898,7 @@ static void valueFlowAST(Token *tok, unsigned int varid, const ValueFlow::Value if (conditionIsTrue(tok->astOperand1(), pm)) return; } - valueFlowAST(const_cast(tok->astOperand2()), varid, value, settings); + valueFlowAST(tok->astOperand2(), varid, value, settings); } /** if known variable is changed in loop body, change it to a possible value */ @@ -2405,26 +2405,26 @@ static bool valueFlowForward(Token * const startToken, // If a ? is seen and it's known that the condition is true/false.. else if (tok2->str() == "?") { const Token *condition = tok2->astOperand1(); - const Token *op2 = tok2->astOperand2(); + Token *op2 = tok2->astOperand2(); if (!condition || !op2) // Ticket #6713 continue; if (condition->hasKnownIntValue()) { const ValueFlow::Value &condValue = condition->values().front(); - const Token *expr = (condValue.intvalue != 0) ? op2->astOperand1() : op2->astOperand2(); + Token *expr = (condValue.intvalue != 0) ? op2->astOperand1() : op2->astOperand2(); for (const ValueFlow::Value &v : values) - valueFlowAST(const_cast(expr), varid, v, settings); + valueFlowAST(expr, varid, v, settings); if (isVariableChangedByFunctionCall(expr, varid, settings, nullptr)) changeKnownToPossible(values); } else { for (const ValueFlow::Value &v : values) { const ProgramMemory programMemory(getProgramMemory(tok2, varid, v)); if (conditionIsTrue(condition, programMemory)) - valueFlowAST(const_cast(op2->astOperand1()), varid, v, settings); + valueFlowAST(op2->astOperand1(), varid, v, settings); else if (conditionIsFalse(condition, programMemory)) - valueFlowAST(const_cast(op2->astOperand2()), varid, v, settings); + valueFlowAST(op2->astOperand2(), varid, v, settings); else - valueFlowAST(const_cast(op2), varid, v, settings); + valueFlowAST(op2, varid, v, settings); } const Token * const expr0 = op2->astOperand1() ? op2->astOperand1() : tok2->astOperand1(); @@ -2449,9 +2449,9 @@ static bool valueFlowForward(Token * const startToken, const Token * const questionToken = tok2; while (tok2->astOperand1() || tok2->astOperand2()) { if (tok2->astOperand2()) - tok2 = const_cast(tok2->astOperand2()); + tok2 = tok2->astOperand2(); else if (tok2->isUnaryPreOp()) - tok2 = const_cast(tok2->astOperand1()); + tok2 = tok2->astOperand1(); else break; } @@ -2499,7 +2499,7 @@ static bool valueFlowForward(Token * const startToken, else if (Token::Match(tok2->previous(), "!!* %name% %assign%")) { // simplify rhs std::stack rhs; - rhs.push(const_cast(tok2->next()->astOperand2())); + rhs.push(tok2->next()->astOperand2()); while (!rhs.empty()) { Token *rtok = rhs.top(); rhs.pop(); @@ -2513,8 +2513,8 @@ static bool valueFlowForward(Token * const startToken, for (const ValueFlow::Value &v : values) setTokenValue(rtok, v, settings); } - rhs.push(const_cast(rtok->astOperand1())); - rhs.push(const_cast(rtok->astOperand2())); + rhs.push(rtok->astOperand1()); + rhs.push(rtok->astOperand2()); } if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok2, "assignment of " + tok2->str()); @@ -2537,7 +2537,7 @@ static bool valueFlowForward(Token * const startToken, "no simplification of " + tok2->str() + " within " + (Token::Match(parent,"[?:]") ? "?:" : parent->str()) + " expression"); const Token *astTop = parent->astTop(); if (Token::simpleMatch(astTop->astOperand1(), "for (")) - tok2 = const_cast(astTop->link()); + tok2 = astTop->link(); // bailout if address of var is taken.. if (tok2->astParent() && tok2->astParent()->isUnaryOp("&")) { @@ -2935,7 +2935,7 @@ static void valueFlowLifetimeConstructor(Token *tok, static void valueFlowForwardLifetime(Token * tok, TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings) { - const Token *parent = tok->astParent(); + Token *parent = tok->astParent(); while (parent && (parent->isArithmeticalOp() || parent->str() == ",")) parent = parent->astParent(); if (!parent) @@ -2979,8 +2979,8 @@ static void valueFlowForwardLifetime(Token * tok, TokenList *tokenlist, ErrorLog if (tok->astTop() && Token::simpleMatch(tok->astTop()->previous(), "for (") && Token::simpleMatch(tok->astTop()->link(), ") {")) { - const Token *start = tok->astTop()->link()->next(); - valueFlowForward(const_cast(start), + Token *start = tok->astTop()->link()->next(); + valueFlowForward(start, start->link(), var, var->declarationId(), @@ -2993,10 +2993,10 @@ static void valueFlowForwardLifetime(Token * tok, TokenList *tokenlist, ErrorLog } // Constructor } else if (Token::Match(parent->previous(), "=|return|%type%|%var% {")) { - valueFlowLifetimeConstructor(const_cast(parent), tokenlist, errorLogger, settings); + valueFlowLifetimeConstructor(parent, tokenlist, errorLogger, settings); // Function call } else if (Token::Match(parent->previous(), "%name% (")) { - valueFlowLifetimeFunction(const_cast(parent->previous()), tokenlist, errorLogger, settings); + valueFlowLifetimeFunction(parent->previous(), tokenlist, errorLogger, settings); // Variable } else if (tok->variable()) { const Variable *var = tok->variable(); @@ -3666,7 +3666,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat std::list values = truncateValues(tok->astOperand2()->values(), tok->astOperand1()->valueType(), settings); const bool constValue = tok->astOperand2()->isNumber(); const bool init = var->nameToken() == tok->astOperand1(); - valueFlowForwardAssign(const_cast(tok->astOperand2()), var, values, constValue, init, tokenlist, errorLogger, settings); + valueFlowForwardAssign(tok->astOperand2(), var, values, constValue, init, tokenlist, errorLogger, settings); } } } @@ -3714,22 +3714,22 @@ struct ValueFlowConditionHandler { } if (Token::Match(tok->astParent(), "%oror%|&&")) { - Token *parent = const_cast(tok->astParent()); + Token *parent = tok->astParent(); const std::string &op(parent->str()); if (parent->astOperand1() == tok && ((op == "&&" && Token::Match(tok, "==|>=|<=|!")) || (op == "||" && Token::Match(tok, "%name%|!=")))) { - for (; parent && parent->str() == op; parent = const_cast(parent->astParent())) { + for (; parent && parent->str() == op; parent = parent->astParent()) { std::stack tokens; - tokens.push(const_cast(parent->astOperand2())); + tokens.push(parent->astOperand2()); bool assign = false; while (!tokens.empty()) { Token *rhstok = tokens.top(); tokens.pop(); if (!rhstok) continue; - tokens.push(const_cast(rhstok->astOperand1())); - tokens.push(const_cast(rhstok->astOperand2())); + tokens.push(rhstok->astOperand1()); + tokens.push(rhstok->astOperand2()); if (rhstok->varId() == varid) setTokenValue(rhstok, cond.true_values.front(), settings); else if (Token::Match(rhstok, "++|--|=") && @@ -3741,7 +3741,7 @@ struct ValueFlowConditionHandler { if (assign) break; while (parent->astParent() && parent == parent->astParent()->astOperand2()) - parent = const_cast(parent->astParent()); + parent = parent->astParent(); } } } @@ -4947,7 +4947,7 @@ static bool isContainerSizeChangedByFunction(const Token *tok) return false; } -static void valueFlowContainerReverse(const Token *tok, unsigned int containerId, const ValueFlow::Value &value, const Settings *settings) +static void valueFlowContainerReverse(Token *tok, unsigned int containerId, const ValueFlow::Value &value, const Settings *settings) { while (nullptr != (tok = tok->previous())) { if (Token::Match(tok, "[{}]")) @@ -4965,11 +4965,11 @@ static void valueFlowContainerReverse(const Token *tok, unsigned int containerId if (Token::Match(tok, "%name% . %name% (") && tok->valueType()->container->getAction(tok->strAt(2)) != Library::Container::Action::NO_ACTION) break; if (!hasContainerSizeGuard(tok, containerId)) - setTokenValue(const_cast(tok), value, settings); + setTokenValue(tok, value, settings); } } -static void valueFlowContainerForward(const Token *tok, unsigned int containerId, ValueFlow::Value value, const Settings *settings, bool cpp) +static void valueFlowContainerForward(Token *tok, unsigned int containerId, ValueFlow::Value value, const Settings *settings, bool cpp) { while (nullptr != (tok = tok->next())) { if (Token::Match(tok, "[{}]")) @@ -5013,7 +5013,7 @@ static void valueFlowContainerForward(const Token *tok, unsigned int containerId if (Token::Match(tok, "%name% . %name% (") && tok->valueType()->container->getAction(tok->strAt(2)) != Library::Container::Action::NO_ACTION) break; if (!hasContainerSizeGuard(tok, containerId)) - setTokenValue(const_cast(tok), value, settings); + setTokenValue(tok, value, settings); } } @@ -5063,10 +5063,10 @@ static void valueFlowSmartPointer(TokenList *tokenlist, ErrorLogger * errorLogge continue; if (var->nameToken() == tok) { if (Token::Match(tok, "%var% (|{") && tok->next()->astOperand2() && tok->next()->astOperand2()->str() != ",") { - const Token * inTok = tok->next()->astOperand2(); + Token * inTok = tok->next()->astOperand2(); std::list values = inTok->values(); const bool constValue = inTok->isNumber(); - valueFlowForwardAssign(const_cast(inTok), var, values, constValue, true, tokenlist, errorLogger, settings); + valueFlowForwardAssign(inTok, var, values, constValue, true, tokenlist, errorLogger, settings); } else if (Token::Match(tok, "%var% ;")) { std::list values; @@ -5084,12 +5084,12 @@ static void valueFlowSmartPointer(TokenList *tokenlist, ErrorLogger * errorLogge valueFlowForwardAssign(tok->tokAt(4), var, values, false, false, tokenlist, errorLogger, settings); } else { tok->removeValues(std::mem_fn(&ValueFlow::Value::isIntValue)); - const Token * inTok = tok->tokAt(3)->astOperand2(); + Token * inTok = tok->tokAt(3)->astOperand2(); if (!inTok) continue; std::list values = inTok->values(); const bool constValue = inTok->isNumber(); - valueFlowForwardAssign(const_cast(inTok), var, values, constValue, false, tokenlist, errorLogger, settings); + valueFlowForwardAssign(inTok, var, values, constValue, false, tokenlist, errorLogger, settings); } } else if (Token::Match(tok, "%var% . release ( )")) { std::list values; @@ -5180,7 +5180,7 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; // possible value before condition - valueFlowContainerReverse(scope.classDef, tok->varId(), value, settings); + valueFlowContainerReverse(const_cast(scope.classDef), tok->varId(), value, settings); } } } @@ -5395,7 +5395,7 @@ std::string ValueFlow::Value::infoString() const throw InternalError(nullptr, "Invalid ValueFlow Value type"); } -const ValueFlow::Value *ValueFlow::valueFlowConstantFoldAST(const Token *expr, const Settings *settings) +const ValueFlow::Value *ValueFlow::valueFlowConstantFoldAST(Token *expr, const Settings *settings) { if (expr && expr->values().empty()) { valueFlowConstantFoldAST(expr->astOperand1(), settings); diff --git a/lib/valueflow.h b/lib/valueflow.h index 3bd3a04d1..062c94d4c 100644 --- a/lib/valueflow.h +++ b/lib/valueflow.h @@ -228,7 +228,7 @@ namespace ValueFlow { }; /// Constant folding of expression. This can be used before the full ValueFlow has been executed (ValueFlow::setValues). - const ValueFlow::Value * valueFlowConstantFoldAST(const Token *expr, const Settings *settings); + const ValueFlow::Value * valueFlowConstantFoldAST(Token *expr, const Settings *settings); /// Perform valueflow analysis. void setValues(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings); diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 0c1970dba..65dedc587 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -936,7 +936,7 @@ private: void canFindMatchingBracketsInnerPair() const { givenACodeSampleToTokenize var("std::deque > intsets;"); - const Token * const t = const_cast(var.tokens()->tokAt(7))->findClosingBracket(); + const Token * const t = var.tokens()->tokAt(7)->findClosingBracket(); ASSERT_EQUALS(">", t->str()); ASSERT(var.tokens()->tokAt(9) == t); }