From f7a415dbf30336173cb4f2c27e2e14d51684341a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Wed, 8 Feb 2023 21:07:16 +0100 Subject: [PATCH] Token: do not return non-`const` pointer from `const` methods - part 1 (#4761) --- lib/astutils.cpp | 6 +++--- lib/checkunusedvar.cpp | 4 ++-- lib/clangimport.cpp | 2 +- lib/templatesimplifier.cpp | 16 ++++++++-------- lib/token.cpp | 6 +++--- lib/token.h | 9 ++++++--- lib/tokenize.cpp | 19 ++++++++++--------- lib/tokenlist.cpp | 4 ++-- lib/valueflow.cpp | 18 +++++++++--------- 9 files changed, 44 insertions(+), 40 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 8d888179b..be81142b2 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -492,11 +492,11 @@ Token* previousBeforeAstLeftmostLeaf(Token* tok) template )> static T* nextAfterAstRightmostLeafGeneric(T* tok) { - const Token * rightmostLeaf = tok; + T * rightmostLeaf = tok; if (!rightmostLeaf || !rightmostLeaf->astOperand1()) return nullptr; do { - if (const Token* lam = findLambdaEndToken(rightmostLeaf)) { + if (T* lam = findLambdaEndToken(rightmostLeaf)) { rightmostLeaf = lam; break; } @@ -2890,7 +2890,7 @@ T* findLambdaEndTokenGeneric(T* first) return nullptr; if (first->astOperand1() != first->link()->next()) return nullptr; - const Token * tok = first; + T * tok = first; if (tok->astOperand1() && tok->astOperand1()->str() == "(") tok = tok->astOperand1(); diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 1baaa460d..8a35b6e74 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1556,7 +1556,7 @@ bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type) continue; // ignore default/deleted constructors const bool emptyBody = (f.functionScope && Token::simpleMatch(f.functionScope->bodyStart, "{ }")); - Token* nextToken = f.argDef->link(); + const Token* nextToken = f.argDef->link(); if (Token::simpleMatch(nextToken, ") :")) { // validating initialization list nextToken = nextToken->next(); // goto ":" @@ -1668,7 +1668,7 @@ bool CheckUnusedVar::isFunctionWithoutSideEffects(const Function& func, const To bool sideEffectReturnFound = false; std::set pointersToGlobals; - for (Token* bodyToken = func.functionScope->bodyStart->next(); bodyToken != func.functionScope->bodyEnd; + for (const Token* bodyToken = func.functionScope->bodyStart->next(); bodyToken != func.functionScope->bodyEnd; bodyToken = bodyToken->next()) { // check variable inside function body const Variable* bodyVariable = bodyToken->variable(); diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index a3e4de892..35461a402 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -1547,7 +1547,7 @@ static void setValues(Tokenizer *tokenizer, SymbolDatabase *symbolDatabase) if (sz <= 0) continue; long long mul = 1; - for (Token *arrtok = tok->linkAt(1)->previous(); arrtok; arrtok = arrtok->previous()) { + for (const Token *arrtok = tok->linkAt(1)->previous(); arrtok; arrtok = arrtok->previous()) { const std::string &a = arrtok->str(); if (a.size() > 2 && a[0] == '[' && a.back() == ']') mul *= std::atoi(a.substr(1).c_str()); diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index be2a35c95..45a532842 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1611,8 +1611,8 @@ void TemplateSimplifier::expandTemplate( Token * dstStart = dst->previous(); bool isStatic = false; std::string scope; - Token * start; - Token * end; + const Token * start; + const Token * end; auto it = mTemplateForwardDeclarationsMap.find(dst); if (!isSpecialization && it != mTemplateForwardDeclarationsMap.end()) { dst = it->second; @@ -1648,7 +1648,7 @@ void TemplateSimplifier::expandTemplate( if (end->str() == "(") end = end->link()->next(); else if (isVariable && end->str() == "=") { - Token *temp = end->next(); + const Token *temp = end->next(); while (temp && temp->str() != ";") { if (temp->link() && Token::Match(temp, "{|[|(")) temp = temp->link(); @@ -1765,7 +1765,7 @@ void TemplateSimplifier::expandTemplate( // check if type is a template if (start->strAt(1) == "<") { // get the instantiated name - Token * closing = start->next()->findClosingBracket(); + const Token * closing = start->next()->findClosingBracket(); if (closing) { std::string name; const Token * type = start; @@ -2044,7 +2044,7 @@ void TemplateSimplifier::expandTemplate( if (isVariadicTemplateArg && Token::Match(tok3, "%name% ... %name%")) tok3 = tok3->tokAt(2); const std::string endStr(isVariadicTemplateArg ? ">" : ",>"); - for (const Token *typetok = mTypesUsedInTemplateInstantiation[itype].token(); + for (Token *typetok = mTypesUsedInTemplateInstantiation[itype].token(); typetok && (typeindentlevel > 0 || endStr.find(typetok->str()[0]) == std::string::npos); typetok = typetok->next()) { if (typeindentlevel == 0 && typetok->str() == "*") @@ -2070,7 +2070,7 @@ void TemplateSimplifier::expandTemplate( mTokenList.addtoken(typetok, tok3); back = mTokenList.back(); } else - back = const_cast(typetok); + back = typetok; if (Token::Match(back, "{|(|[")) brackets1.push(back); else if (back->str() == "}") { @@ -3199,7 +3199,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations( Token *startToken = tok2; while (Token::Match(startToken->tokAt(-2), ">|%name% :: %name%")) { if (startToken->strAt(-2) == ">") { - const Token * tok3 = startToken->tokAt(-2)->findOpeningBracket(); + Token * tok3 = startToken->tokAt(-2)->findOpeningBracket(); if (tok3) startToken = tok3->previous(); else @@ -3336,7 +3336,7 @@ void TemplateSimplifier::replaceTemplateUsage( // matching template usage => replace tokens.. // Foo < int > => Foo - for (Token *tok = nameTok1->next(); tok != tok2; tok = tok->next()) { + for (const Token *tok = nameTok1->next(); tok != tok2; tok = tok->next()) { if (tok->isName() && tok->templateSimplifierPointers() && !tok->templateSimplifierPointers()->empty()) { std::list::iterator ti; for (ti = mTemplateInstantiations.begin(); ti != mTemplateInstantiations.end();) { diff --git a/lib/token.cpp b/lib/token.cpp index 9b1bdece3..0aeeedd1c 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -845,7 +845,7 @@ void Token::move(Token *srcStart, Token *srcEnd, Token *newLocation) tok->mImpl->mProgressValue = newLocation->mImpl->mProgressValue; } -Token* Token::nextArgument() const +const Token* Token::nextArgument() const { for (const Token* tok = this; tok; tok = tok->next()) { if (tok->str() == ",") @@ -858,7 +858,7 @@ Token* Token::nextArgument() const return nullptr; } -Token* Token::nextArgumentBeforeCreateLinks2() const +const Token* Token::nextArgumentBeforeCreateLinks2() const { for (const Token* tok = this; tok; tok = tok->next()) { if (tok->str() == ",") @@ -875,7 +875,7 @@ Token* Token::nextArgumentBeforeCreateLinks2() const return nullptr; } -Token* Token::nextTemplateArgument() const +const Token* Token::nextTemplateArgument() const { for (const Token* tok = this; tok; tok = tok->next()) { if (tok->str() == ",") diff --git a/lib/token.h b/lib/token.h index ef37d6bac..09581df40 100644 --- a/lib/token.h +++ b/lib/token.h @@ -1140,21 +1140,24 @@ public: * lists. Requires that Tokenizer::createLinks2() has been called before. * Returns 0, if there is no next argument. */ - Token* nextArgument() const; + const Token* nextArgument() const; + Token *nextArgument() { + return const_cast(const_cast(this)->nextArgument()); + } /** * @return the first token of the next argument. Does only work on argument * lists. Should be used only before Tokenizer::createLinks2() was called. * Returns 0, if there is no next argument. */ - Token* nextArgumentBeforeCreateLinks2() const; + const Token* nextArgumentBeforeCreateLinks2() const; /** * @return the first token of the next template argument. Does only work on template argument * lists. Requires that Tokenizer::createLinks2() has been called before. * Returns 0, if there is no next argument. */ - Token* nextTemplateArgument() const; + const Token* nextTemplateArgument() const; /** * Returns the closing bracket of opening '<'. Should only be used if link() diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2dea74e21..db1cfc18e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -240,9 +240,9 @@ nonneg int Tokenizer::sizeOfType(const Token *type) const bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef) const { // check for an end of definition - const Token * tok = *tokPtr; + Token * tok = *tokPtr; if (tok && Token::Match(tok->next(), ";|,|[|=|)|>|(|{")) { - const Token * end = tok->next(); + Token * end = tok->next(); if (end->str() == "[") { if (!end->link()) @@ -4405,11 +4405,11 @@ void Tokenizer::setVarIdPass2() std::list classnameTokens; classnameTokens.push_back(tok->next()); - const Token* tokStart = tok->tokAt(2); + Token* tokStart = tok->tokAt(2); while (Token::Match(tokStart, ":: %name%") || tokStart->str() == "<") { if (tokStart->str() == "<") { // skip the template part - const Token* closeTok = tokStart->findClosingBracket(); + Token* closeTok = tokStart->findClosingBracket(); if (!closeTok) syntaxError(tok); tokStart = closeTok->next(); @@ -7408,7 +7408,8 @@ static bool isAlignAttribute(const Token * tok) return Token::simpleMatch(tok, "alignas (") && tok->next()->link(); } -static const Token* skipCPPOrAlignAttribute(const Token * tok) +template +static T* skipCPPOrAlignAttribute(T * tok) { if (isCPPAttribute(tok)) { return tok->link(); @@ -8318,7 +8319,7 @@ void Tokenizer::simplifyCPPAttribute() } if (isCPPAttribute(tok)) { if (Token::findsimplematch(tok->tokAt(2), "noreturn", tok->link())) { - const Token * head = skipCPPOrAlignAttribute(tok); + Token * head = skipCPPOrAlignAttribute(tok); while (isCPPAttribute(head) || isAlignAttribute(head)) head = skipCPPOrAlignAttribute(head); head = head->next(); @@ -8328,7 +8329,7 @@ void Tokenizer::simplifyCPPAttribute() head->previous()->isAttributeNoreturn(true); } } else if (Token::findsimplematch(tok->tokAt(2), "nodiscard", tok->link())) { - const Token * head = skipCPPOrAlignAttribute(tok); + Token * head = skipCPPOrAlignAttribute(tok); while (isCPPAttribute(head) || isAlignAttribute(head)) head = skipCPPOrAlignAttribute(head); head = head->next(); @@ -8338,7 +8339,7 @@ void Tokenizer::simplifyCPPAttribute() head->previous()->isAttributeNodiscard(true); } } else if (Token::findsimplematch(tok->tokAt(2), "maybe_unused", tok->link())) { - const Token* head = skipCPPOrAlignAttribute(tok); + Token* head = skipCPPOrAlignAttribute(tok); while (isCPPAttribute(head) || isAlignAttribute(head)) head = skipCPPOrAlignAttribute(head); head->next()->isAttributeMaybeUnused(true); @@ -8903,7 +8904,7 @@ void Tokenizer::simplifyNamespaceStd() std::set userFunctions; - for (const Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) { + for (Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) { bool insert = false; if (Token::Match(tok, "enum class|struct| %name%| :|{")) { // Don't replace within enum definitions skipEnumBody(&tok); diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 0e72ce2c6..a521f9486 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1056,7 +1056,7 @@ static void compilePrecedence2(Token *&tok, AST_state& state) cast->astOperand1(tok1); tok = tok1->link()->next(); } else if (state.cpp && tok->str() == "{" && iscpp11init(tok)) { - const Token* end = tok->link(); + Token* end = tok->link(); if (Token::simpleMatch(tok, "{ }")) { compileUnaryOp(tok, state, nullptr); @@ -1677,7 +1677,7 @@ static Token * createAstAtToken(Token *tok, bool cpp) if (Token::Match(tok, "%name% (")) state.functionCallEndPar = tok->linkAt(1); compileExpression(tok, state); - const Token * const endToken = tok; + Token * const endToken = tok; if (endToken == tok1 || !endToken) return tok1; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 581561d67..13ca969ab 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1345,7 +1345,7 @@ static void valueFlowArray(TokenList *tokenlist) // const array decl else if (tok->variable() && tok->variable()->isArray() && tok->variable()->isConst() && tok->variable()->nameToken() == tok && Token::Match(tok, "%var% [ %num%| ] = {")) { - const Token* rhstok = tok->next()->link()->tokAt(2); + Token* rhstok = tok->next()->link()->tokAt(2); constantArrays[tok->varId()] = rhstok; tok = rhstok->link(); } @@ -1365,16 +1365,16 @@ static void valueFlowArray(TokenList *tokenlist) } if (Token::Match(tok, "const %type% %var% [ %num%| ] = {")) { - const Token *vartok = tok->tokAt(2); - const Token *rhstok = vartok->next()->link()->tokAt(2); + Token *vartok = tok->tokAt(2); + Token *rhstok = vartok->next()->link()->tokAt(2); constantArrays[vartok->varId()] = rhstok; tok = rhstok->link(); continue; } else if (Token::Match(tok, "const char %var% [ %num%| ] = %str% ;")) { - const Token *vartok = tok->tokAt(2); - const Token *strtok = vartok->next()->link()->tokAt(2); + Token *vartok = tok->tokAt(2); + Token *strtok = vartok->next()->link()->tokAt(2); constantArrays[vartok->varId()] = strtok; tok = strtok->next(); continue; @@ -4903,7 +4903,7 @@ static bool isStdMoveOrStdForwarded(Token * tok, ValueFlow::Value::MoveKind * mo variableToken = tok->tokAt(4); kind = ValueFlow::Value::MoveKind::MovedVariable; } else if (Token::simpleMatch(tok, "std :: forward <")) { - const Token * const leftAngle = tok->tokAt(3); + Token * const leftAngle = tok->tokAt(3); Token * rightAngle = leftAngle->link(); if (Token::Match(rightAngle, "> ( %var% )")) { variableToken = rightAngle->tokAt(2); @@ -5098,7 +5098,7 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { if (!Token::simpleMatch(tok, "if (")) continue; - Token * parenTok = tok->next(); + const Token * parenTok = tok->next(); if (!Token::simpleMatch(parenTok->link(), ") {")) continue; Token * blockTok = parenTok->link()->tokAt(1); @@ -6824,7 +6824,7 @@ static void valueFlowForLoopSimplify(Token* const bodyStart, if ((tok2->str() == "&&" && !conditionIsTrue(tok2->astOperand1(), programMemory)) || (tok2->str() == "||" && !conditionIsFalse(tok2->astOperand1(), programMemory))) { // Skip second expression.. - const Token *parent = tok2; + Token *parent = tok2; while (parent && parent->str() == tok2->str()) parent = parent->astParent(); // Jump to end of condition @@ -7233,7 +7233,7 @@ static void valueFlowSwitchVariable(TokenList *tokenlist, SymbolDatabase* symbol continue; } - for (Token *tok = scope.bodyStart->next(); tok != scope.bodyEnd; tok = tok->next()) { + for (const Token *tok = scope.bodyStart->next(); tok != scope.bodyEnd; tok = tok->next()) { if (tok->str() == "{") { tok = tok->link(); continue;