Skip to content
6 changes: 3 additions & 3 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,11 @@ Token* previousBeforeAstLeftmostLeaf(Token* tok)
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
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;
}
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ":"
Expand Down Expand Up @@ -1668,7 +1668,7 @@ bool CheckUnusedVar::isFunctionWithoutSideEffects(const Function& func, const To

bool sideEffectReturnFound = false;
std::set<const Variable*> 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();
Expand Down
2 changes: 1 addition & 1 deletion lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
16 changes: 8 additions & 8 deletions lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Comment thread
danmar marked this conversation as resolved.
typetok && (typeindentlevel > 0 || endStr.find(typetok->str()[0]) == std::string::npos);
typetok = typetok->next()) {
if (typeindentlevel == 0 && typetok->str() == "*")
Expand All @@ -2070,7 +2070,7 @@ void TemplateSimplifier::expandTemplate(
mTokenList.addtoken(typetok, tok3);
back = mTokenList.back();
} else
back = const_cast<Token *>(typetok);
back = typetok;
if (Token::Match(back, "{|(|["))
brackets1.push(back);
else if (back->str() == "}") {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3336,7 +3336,7 @@ void TemplateSimplifier::replaceTemplateUsage(

// matching template usage => replace tokens..
// Foo < int > => Foo<int>
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<TokenAndName>::iterator ti;
for (ti = mTemplateInstantiations.begin(); ti != mTemplateInstantiations.end();) {
Expand Down
6 changes: 3 additions & 3 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() == ",")
Expand All @@ -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() == ",")
Expand All @@ -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() == ",")
Expand Down
9 changes: 6 additions & 3 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -1140,21 +1140,24 @@ class CPPCHECKLIB Token {
* 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<Token *>(const_cast<const Token *>(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()
Expand Down
19 changes: 10 additions & 9 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -4405,11 +4405,11 @@ void Tokenizer::setVarIdPass2()

std::list<const Token *> 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();
Expand Down Expand Up @@ -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<typename T>
static T* skipCPPOrAlignAttribute(T * tok)
{
if (isCPPAttribute(tok)) {
return tok->link();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -8903,7 +8904,7 @@ void Tokenizer::simplifyNamespaceStd()

std::set<std::string> 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);
Expand Down
4 changes: 2 additions & 2 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
18 changes: 9 additions & 9 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these can stay const.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are all based straight on compiler errors when the signature has been fixed - so no, they cannot.

Also from looking at the code it is used to get a new value for tok two lines down and that is not const.

constantArrays[tok->varId()] = rhstok;
tok = rhstok->link();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down