Token: do not return non-`const` pointer from `const` methods - part 1 (#4761)

This commit is contained in:
Oliver Stöneberg 2023-02-08 21:07:16 +01:00 committed by GitHub
parent 8ef14dad98
commit f7a415dbf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 44 additions and 40 deletions

View File

@ -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;
}
@ -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();

View File

@ -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<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();

View File

@ -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());

View File

@ -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<Token *>(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<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();) {

View File

@ -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() == ",")

View File

@ -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<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()

View File

@ -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<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();
@ -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();
@ -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<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);

View File

@ -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;

View File

@ -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;