Skip to content

Commit f7a415d

Browse files
authored
Token: do not return non-const pointer from const methods - part 1 (#4761)
1 parent 8ef14da commit f7a415d

9 files changed

Lines changed: 44 additions & 40 deletions

File tree

lib/astutils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,11 @@ Token* previousBeforeAstLeftmostLeaf(Token* tok)
492492
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
493493
static T* nextAfterAstRightmostLeafGeneric(T* tok)
494494
{
495-
const Token * rightmostLeaf = tok;
495+
T * rightmostLeaf = tok;
496496
if (!rightmostLeaf || !rightmostLeaf->astOperand1())
497497
return nullptr;
498498
do {
499-
if (const Token* lam = findLambdaEndToken(rightmostLeaf)) {
499+
if (T* lam = findLambdaEndToken(rightmostLeaf)) {
500500
rightmostLeaf = lam;
501501
break;
502502
}
@@ -2890,7 +2890,7 @@ T* findLambdaEndTokenGeneric(T* first)
28902890
return nullptr;
28912891
if (first->astOperand1() != first->link()->next())
28922892
return nullptr;
2893-
const Token * tok = first;
2893+
T * tok = first;
28942894

28952895
if (tok->astOperand1() && tok->astOperand1()->str() == "(")
28962896
tok = tok->astOperand1();

lib/checkunusedvar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type)
15561556
continue; // ignore default/deleted constructors
15571557
const bool emptyBody = (f.functionScope && Token::simpleMatch(f.functionScope->bodyStart, "{ }"));
15581558

1559-
Token* nextToken = f.argDef->link();
1559+
const Token* nextToken = f.argDef->link();
15601560
if (Token::simpleMatch(nextToken, ") :")) {
15611561
// validating initialization list
15621562
nextToken = nextToken->next(); // goto ":"
@@ -1668,7 +1668,7 @@ bool CheckUnusedVar::isFunctionWithoutSideEffects(const Function& func, const To
16681668

16691669
bool sideEffectReturnFound = false;
16701670
std::set<const Variable*> pointersToGlobals;
1671-
for (Token* bodyToken = func.functionScope->bodyStart->next(); bodyToken != func.functionScope->bodyEnd;
1671+
for (const Token* bodyToken = func.functionScope->bodyStart->next(); bodyToken != func.functionScope->bodyEnd;
16721672
bodyToken = bodyToken->next()) {
16731673
// check variable inside function body
16741674
const Variable* bodyVariable = bodyToken->variable();

lib/clangimport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ static void setValues(Tokenizer *tokenizer, SymbolDatabase *symbolDatabase)
15471547
if (sz <= 0)
15481548
continue;
15491549
long long mul = 1;
1550-
for (Token *arrtok = tok->linkAt(1)->previous(); arrtok; arrtok = arrtok->previous()) {
1550+
for (const Token *arrtok = tok->linkAt(1)->previous(); arrtok; arrtok = arrtok->previous()) {
15511551
const std::string &a = arrtok->str();
15521552
if (a.size() > 2 && a[0] == '[' && a.back() == ']')
15531553
mul *= std::atoi(a.substr(1).c_str());

lib/templatesimplifier.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,8 +1611,8 @@ void TemplateSimplifier::expandTemplate(
16111611
Token * dstStart = dst->previous();
16121612
bool isStatic = false;
16131613
std::string scope;
1614-
Token * start;
1615-
Token * end;
1614+
const Token * start;
1615+
const Token * end;
16161616
auto it = mTemplateForwardDeclarationsMap.find(dst);
16171617
if (!isSpecialization && it != mTemplateForwardDeclarationsMap.end()) {
16181618
dst = it->second;
@@ -1648,7 +1648,7 @@ void TemplateSimplifier::expandTemplate(
16481648
if (end->str() == "(")
16491649
end = end->link()->next();
16501650
else if (isVariable && end->str() == "=") {
1651-
Token *temp = end->next();
1651+
const Token *temp = end->next();
16521652
while (temp && temp->str() != ";") {
16531653
if (temp->link() && Token::Match(temp, "{|[|("))
16541654
temp = temp->link();
@@ -1765,7 +1765,7 @@ void TemplateSimplifier::expandTemplate(
17651765
// check if type is a template
17661766
if (start->strAt(1) == "<") {
17671767
// get the instantiated name
1768-
Token * closing = start->next()->findClosingBracket();
1768+
const Token * closing = start->next()->findClosingBracket();
17691769
if (closing) {
17701770
std::string name;
17711771
const Token * type = start;
@@ -2044,7 +2044,7 @@ void TemplateSimplifier::expandTemplate(
20442044
if (isVariadicTemplateArg && Token::Match(tok3, "%name% ... %name%"))
20452045
tok3 = tok3->tokAt(2);
20462046
const std::string endStr(isVariadicTemplateArg ? ">" : ",>");
2047-
for (const Token *typetok = mTypesUsedInTemplateInstantiation[itype].token();
2047+
for (Token *typetok = mTypesUsedInTemplateInstantiation[itype].token();
20482048
typetok && (typeindentlevel > 0 || endStr.find(typetok->str()[0]) == std::string::npos);
20492049
typetok = typetok->next()) {
20502050
if (typeindentlevel == 0 && typetok->str() == "*")
@@ -2070,7 +2070,7 @@ void TemplateSimplifier::expandTemplate(
20702070
mTokenList.addtoken(typetok, tok3);
20712071
back = mTokenList.back();
20722072
} else
2073-
back = const_cast<Token *>(typetok);
2073+
back = typetok;
20742074
if (Token::Match(back, "{|(|["))
20752075
brackets1.push(back);
20762076
else if (back->str() == "}") {
@@ -3199,7 +3199,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
31993199
Token *startToken = tok2;
32003200
while (Token::Match(startToken->tokAt(-2), ">|%name% :: %name%")) {
32013201
if (startToken->strAt(-2) == ">") {
3202-
const Token * tok3 = startToken->tokAt(-2)->findOpeningBracket();
3202+
Token * tok3 = startToken->tokAt(-2)->findOpeningBracket();
32033203
if (tok3)
32043204
startToken = tok3->previous();
32053205
else
@@ -3336,7 +3336,7 @@ void TemplateSimplifier::replaceTemplateUsage(
33363336

33373337
// matching template usage => replace tokens..
33383338
// Foo < int > => Foo<int>
3339-
for (Token *tok = nameTok1->next(); tok != tok2; tok = tok->next()) {
3339+
for (const Token *tok = nameTok1->next(); tok != tok2; tok = tok->next()) {
33403340
if (tok->isName() && tok->templateSimplifierPointers() && !tok->templateSimplifierPointers()->empty()) {
33413341
std::list<TokenAndName>::iterator ti;
33423342
for (ti = mTemplateInstantiations.begin(); ti != mTemplateInstantiations.end();) {

lib/token.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ void Token::move(Token *srcStart, Token *srcEnd, Token *newLocation)
845845
tok->mImpl->mProgressValue = newLocation->mImpl->mProgressValue;
846846
}
847847

848-
Token* Token::nextArgument() const
848+
const Token* Token::nextArgument() const
849849
{
850850
for (const Token* tok = this; tok; tok = tok->next()) {
851851
if (tok->str() == ",")
@@ -858,7 +858,7 @@ Token* Token::nextArgument() const
858858
return nullptr;
859859
}
860860

861-
Token* Token::nextArgumentBeforeCreateLinks2() const
861+
const Token* Token::nextArgumentBeforeCreateLinks2() const
862862
{
863863
for (const Token* tok = this; tok; tok = tok->next()) {
864864
if (tok->str() == ",")
@@ -875,7 +875,7 @@ Token* Token::nextArgumentBeforeCreateLinks2() const
875875
return nullptr;
876876
}
877877

878-
Token* Token::nextTemplateArgument() const
878+
const Token* Token::nextTemplateArgument() const
879879
{
880880
for (const Token* tok = this; tok; tok = tok->next()) {
881881
if (tok->str() == ",")

lib/token.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,21 +1140,24 @@ class CPPCHECKLIB Token {
11401140
* lists. Requires that Tokenizer::createLinks2() has been called before.
11411141
* Returns 0, if there is no next argument.
11421142
*/
1143-
Token* nextArgument() const;
1143+
const Token* nextArgument() const;
1144+
Token *nextArgument() {
1145+
return const_cast<Token *>(const_cast<const Token *>(this)->nextArgument());
1146+
}
11441147

11451148
/**
11461149
* @return the first token of the next argument. Does only work on argument
11471150
* lists. Should be used only before Tokenizer::createLinks2() was called.
11481151
* Returns 0, if there is no next argument.
11491152
*/
1150-
Token* nextArgumentBeforeCreateLinks2() const;
1153+
const Token* nextArgumentBeforeCreateLinks2() const;
11511154

11521155
/**
11531156
* @return the first token of the next template argument. Does only work on template argument
11541157
* lists. Requires that Tokenizer::createLinks2() has been called before.
11551158
* Returns 0, if there is no next argument.
11561159
*/
1157-
Token* nextTemplateArgument() const;
1160+
const Token* nextTemplateArgument() const;
11581161

11591162
/**
11601163
* Returns the closing bracket of opening '<'. Should only be used if link()

lib/tokenize.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ nonneg int Tokenizer::sizeOfType(const Token *type) const
240240
bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef) const
241241
{
242242
// check for an end of definition
243-
const Token * tok = *tokPtr;
243+
Token * tok = *tokPtr;
244244
if (tok && Token::Match(tok->next(), ";|,|[|=|)|>|(|{")) {
245-
const Token * end = tok->next();
245+
Token * end = tok->next();
246246

247247
if (end->str() == "[") {
248248
if (!end->link())
@@ -4405,11 +4405,11 @@ void Tokenizer::setVarIdPass2()
44054405

44064406
std::list<const Token *> classnameTokens;
44074407
classnameTokens.push_back(tok->next());
4408-
const Token* tokStart = tok->tokAt(2);
4408+
Token* tokStart = tok->tokAt(2);
44094409
while (Token::Match(tokStart, ":: %name%") || tokStart->str() == "<") {
44104410
if (tokStart->str() == "<") {
44114411
// skip the template part
4412-
const Token* closeTok = tokStart->findClosingBracket();
4412+
Token* closeTok = tokStart->findClosingBracket();
44134413
if (!closeTok)
44144414
syntaxError(tok);
44154415
tokStart = closeTok->next();
@@ -7408,7 +7408,8 @@ static bool isAlignAttribute(const Token * tok)
74087408
return Token::simpleMatch(tok, "alignas (") && tok->next()->link();
74097409
}
74107410

7411-
static const Token* skipCPPOrAlignAttribute(const Token * tok)
7411+
template<typename T>
7412+
static T* skipCPPOrAlignAttribute(T * tok)
74127413
{
74137414
if (isCPPAttribute(tok)) {
74147415
return tok->link();
@@ -8318,7 +8319,7 @@ void Tokenizer::simplifyCPPAttribute()
83188319
}
83198320
if (isCPPAttribute(tok)) {
83208321
if (Token::findsimplematch(tok->tokAt(2), "noreturn", tok->link())) {
8321-
const Token * head = skipCPPOrAlignAttribute(tok);
8322+
Token * head = skipCPPOrAlignAttribute(tok);
83228323
while (isCPPAttribute(head) || isAlignAttribute(head))
83238324
head = skipCPPOrAlignAttribute(head);
83248325
head = head->next();
@@ -8328,7 +8329,7 @@ void Tokenizer::simplifyCPPAttribute()
83288329
head->previous()->isAttributeNoreturn(true);
83298330
}
83308331
} else if (Token::findsimplematch(tok->tokAt(2), "nodiscard", tok->link())) {
8331-
const Token * head = skipCPPOrAlignAttribute(tok);
8332+
Token * head = skipCPPOrAlignAttribute(tok);
83328333
while (isCPPAttribute(head) || isAlignAttribute(head))
83338334
head = skipCPPOrAlignAttribute(head);
83348335
head = head->next();
@@ -8338,7 +8339,7 @@ void Tokenizer::simplifyCPPAttribute()
83388339
head->previous()->isAttributeNodiscard(true);
83398340
}
83408341
} else if (Token::findsimplematch(tok->tokAt(2), "maybe_unused", tok->link())) {
8341-
const Token* head = skipCPPOrAlignAttribute(tok);
8342+
Token* head = skipCPPOrAlignAttribute(tok);
83428343
while (isCPPAttribute(head) || isAlignAttribute(head))
83438344
head = skipCPPOrAlignAttribute(head);
83448345
head->next()->isAttributeMaybeUnused(true);
@@ -8903,7 +8904,7 @@ void Tokenizer::simplifyNamespaceStd()
89038904

89048905
std::set<std::string> userFunctions;
89058906

8906-
for (const Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) {
8907+
for (Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) {
89078908
bool insert = false;
89088909
if (Token::Match(tok, "enum class|struct| %name%| :|{")) { // Don't replace within enum definitions
89098910
skipEnumBody(&tok);

lib/tokenlist.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ static void compilePrecedence2(Token *&tok, AST_state& state)
10561056
cast->astOperand1(tok1);
10571057
tok = tok1->link()->next();
10581058
} else if (state.cpp && tok->str() == "{" && iscpp11init(tok)) {
1059-
const Token* end = tok->link();
1059+
Token* end = tok->link();
10601060
if (Token::simpleMatch(tok, "{ }"))
10611061
{
10621062
compileUnaryOp(tok, state, nullptr);
@@ -1677,7 +1677,7 @@ static Token * createAstAtToken(Token *tok, bool cpp)
16771677
if (Token::Match(tok, "%name% ("))
16781678
state.functionCallEndPar = tok->linkAt(1);
16791679
compileExpression(tok, state);
1680-
const Token * const endToken = tok;
1680+
Token * const endToken = tok;
16811681
if (endToken == tok1 || !endToken)
16821682
return tok1;
16831683

lib/valueflow.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ static void valueFlowArray(TokenList *tokenlist)
13451345
// const array decl
13461346
else if (tok->variable() && tok->variable()->isArray() && tok->variable()->isConst() &&
13471347
tok->variable()->nameToken() == tok && Token::Match(tok, "%var% [ %num%| ] = {")) {
1348-
const Token* rhstok = tok->next()->link()->tokAt(2);
1348+
Token* rhstok = tok->next()->link()->tokAt(2);
13491349
constantArrays[tok->varId()] = rhstok;
13501350
tok = rhstok->link();
13511351
}
@@ -1365,16 +1365,16 @@ static void valueFlowArray(TokenList *tokenlist)
13651365
}
13661366

13671367
if (Token::Match(tok, "const %type% %var% [ %num%| ] = {")) {
1368-
const Token *vartok = tok->tokAt(2);
1369-
const Token *rhstok = vartok->next()->link()->tokAt(2);
1368+
Token *vartok = tok->tokAt(2);
1369+
Token *rhstok = vartok->next()->link()->tokAt(2);
13701370
constantArrays[vartok->varId()] = rhstok;
13711371
tok = rhstok->link();
13721372
continue;
13731373
}
13741374

13751375
else if (Token::Match(tok, "const char %var% [ %num%| ] = %str% ;")) {
1376-
const Token *vartok = tok->tokAt(2);
1377-
const Token *strtok = vartok->next()->link()->tokAt(2);
1376+
Token *vartok = tok->tokAt(2);
1377+
Token *strtok = vartok->next()->link()->tokAt(2);
13781378
constantArrays[vartok->varId()] = strtok;
13791379
tok = strtok->next();
13801380
continue;
@@ -4903,7 +4903,7 @@ static bool isStdMoveOrStdForwarded(Token * tok, ValueFlow::Value::MoveKind * mo
49034903
variableToken = tok->tokAt(4);
49044904
kind = ValueFlow::Value::MoveKind::MovedVariable;
49054905
} else if (Token::simpleMatch(tok, "std :: forward <")) {
4906-
const Token * const leftAngle = tok->tokAt(3);
4906+
Token * const leftAngle = tok->tokAt(3);
49074907
Token * rightAngle = leftAngle->link();
49084908
if (Token::Match(rightAngle, "> ( %var% )")) {
49094909
variableToken = rightAngle->tokAt(2);
@@ -5098,7 +5098,7 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase*
50985098
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
50995099
if (!Token::simpleMatch(tok, "if ("))
51005100
continue;
5101-
Token * parenTok = tok->next();
5101+
const Token * parenTok = tok->next();
51025102
if (!Token::simpleMatch(parenTok->link(), ") {"))
51035103
continue;
51045104
Token * blockTok = parenTok->link()->tokAt(1);
@@ -6824,7 +6824,7 @@ static void valueFlowForLoopSimplify(Token* const bodyStart,
68246824
if ((tok2->str() == "&&" && !conditionIsTrue(tok2->astOperand1(), programMemory)) ||
68256825
(tok2->str() == "||" && !conditionIsFalse(tok2->astOperand1(), programMemory))) {
68266826
// Skip second expression..
6827-
const Token *parent = tok2;
6827+
Token *parent = tok2;
68286828
while (parent && parent->str() == tok2->str())
68296829
parent = parent->astParent();
68306830
// Jump to end of condition
@@ -7233,7 +7233,7 @@ static void valueFlowSwitchVariable(TokenList *tokenlist, SymbolDatabase* symbol
72337233
continue;
72347234
}
72357235

7236-
for (Token *tok = scope.bodyStart->next(); tok != scope.bodyEnd; tok = tok->next()) {
7236+
for (const Token *tok = scope.bodyStart->next(); tok != scope.bodyEnd; tok = tok->next()) {
72377237
if (tok->str() == "{") {
72387238
tok = tok->link();
72397239
continue;

0 commit comments

Comments
 (0)