Improved const correctness of local variables. There are no functional changes intended. (#1392)

This commit is contained in:
orbitcowboy 2018-09-23 20:24:51 +02:00 committed by GitHub
parent 6ceab1f245
commit d08b39c915
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 32 deletions

View File

@ -1987,7 +1987,7 @@ void CheckStl::useStlAlgorithm()
const Token *memberAccessTok = singleMemberCallInScope(bodyTok, loopVar->varId(), useLoopVarInMemCall); const Token *memberAccessTok = singleMemberCallInScope(bodyTok, loopVar->varId(), useLoopVarInMemCall);
if (memberAccessTok) { if (memberAccessTok) {
const Token *memberCallTok = memberAccessTok->astOperand2(); const Token *memberCallTok = memberAccessTok->astOperand2();
unsigned int contVarId = memberAccessTok->astOperand1()->varId(); const unsigned int contVarId = memberAccessTok->astOperand1()->varId();
if (contVarId == loopVar->varId()) if (contVarId == loopVar->varId())
continue; continue;
if (memberCallTok->str() == "push_back" || if (memberCallTok->str() == "push_back" ||
@ -2022,7 +2022,7 @@ void CheckStl::useStlAlgorithm()
// Check for single assign // Check for single assign
assignTok = singleAssignInScope(condBodyTok, loopVar->varId(), useLoopVarInAssign); assignTok = singleAssignInScope(condBodyTok, loopVar->varId(), useLoopVarInAssign);
if (assignTok) { if (assignTok) {
unsigned int assignVarId = assignTok->astOperand1()->varId(); const unsigned int assignVarId = assignTok->astOperand1()->varId();
std::string algo; std::string algo;
if (assignVarId == loopVar->varId()) { if (assignVarId == loopVar->varId()) {
if (useLoopVarInAssign) if (useLoopVarInAssign)
@ -2045,7 +2045,7 @@ void CheckStl::useStlAlgorithm()
memberAccessTok = singleMemberCallInScope(condBodyTok, loopVar->varId(), useLoopVarInMemCall); memberAccessTok = singleMemberCallInScope(condBodyTok, loopVar->varId(), useLoopVarInMemCall);
if (memberAccessTok) { if (memberAccessTok) {
const Token *memberCallTok = memberAccessTok->astOperand2(); const Token *memberCallTok = memberAccessTok->astOperand2();
unsigned int contVarId = memberAccessTok->astOperand1()->varId(); const unsigned int contVarId = memberAccessTok->astOperand1()->varId();
if (contVarId == loopVar->varId()) if (contVarId == loopVar->varId())
continue; continue;
if (memberCallTok->str() == "push_back" || if (memberCallTok->str() == "push_back" ||

View File

@ -927,7 +927,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
tok = tok->next(); tok = tok->next();
} }
Variables::VariableUsage *var = variables.find(varid); const Variables::VariableUsage *const var = variables.find(varid);
if (var && !var->_allocateMemory) { if (var && !var->_allocateMemory) {
variables.readAll(varid, tok); variables.readAll(varid, tok);
} }
@ -998,13 +998,13 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.use(varid1, tok); variables.use(varid1, tok);
if (dereference) { if (dereference) {
Variables::VariableUsage *var = variables.find(varid1); const Variables::VariableUsage *const var = variables.find(varid1);
if (var && var->mType == Variables::array) if (var && var->mType == Variables::array)
variables.write(varid1, tok); variables.write(varid1, tok);
variables.writeAliases(varid1, tok); variables.writeAliases(varid1, tok);
variables.read(varid1, tok); variables.read(varid1, tok);
} else { } else {
Variables::VariableUsage *var = variables.find(varid1); const Variables::VariableUsage *const var = variables.find(varid1);
if (var && (inwhile || start->strAt(-1) == ",")) { if (var && (inwhile || start->strAt(-1) == ",")) {
variables.use(varid1, tok); variables.use(varid1, tok);
} else if (var && var->mType == Variables::reference) { } else if (var && var->mType == Variables::reference) {
@ -1051,7 +1051,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
} }
} }
Variables::VariableUsage *var2 = variables.find(tok->varId()); const Variables::VariableUsage * const var2 = variables.find(tok->varId());
if (var2) { if (var2) {
if (var2->mType == Variables::reference) { if (var2->mType == Variables::reference) {
variables.writeAliases(tok->varId(), tok); variables.writeAliases(tok->varId(), tok);
@ -1069,7 +1069,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
// checked for chained assignments // checked for chained assignments
if (tok != start && equal && equal->str() == "=") { if (tok != start && equal && equal->str() == "=") {
const unsigned int varId = tok->varId(); const unsigned int varId = tok->varId();
Variables::VariableUsage *var = variables.find(varId); const Variables::VariableUsage * const var = variables.find(varId);
if (var && var->mType != Variables::reference) { if (var && var->mType != Variables::reference) {
variables.read(varId,tok); variables.read(varId,tok);

View File

@ -522,7 +522,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
// nested class or friend function? // nested class or friend function?
else { else {
/** @todo check entire qualification for match */ /** @todo check entire qualification for match */
Scope * nested = scope->findInNestedListRecursive(tok->strAt(-2)); const Scope * const nested = scope->findInNestedListRecursive(tok->strAt(-2));
if (nested) if (nested)
addClassFunction(&scope, &tok, argStart); addClassFunction(&scope, &tok, argStart);
@ -579,7 +579,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
// regular function // regular function
else { else {
Function* function = addGlobalFunction(scope, tok, argStart, funcStart); const Function* const function = addGlobalFunction(scope, tok, argStart, funcStart);
if (!function) if (!function)
mTokenizer->syntaxError(tok); mTokenizer->syntaxError(tok);
@ -679,7 +679,7 @@ void SymbolDatabase::createSymbolDatabaseClassInfo()
// only find if not already found // only find if not already found
if (i->scope == nullptr) { if (i->scope == nullptr) {
// check scope for match // check scope for match
Scope *scope = findScope(i->start->tokAt(2), &(*it)); const Scope * const scope = findScope(i->start->tokAt(2), &(*it));
if (scope) { if (scope) {
// set found scope // set found scope
i->scope = scope; i->scope = scope;

View File

@ -550,7 +550,7 @@ std::list<TemplateSimplifier::TokenAndName> TemplateSimplifier::getTemplateDecla
!Token::Match(tok->tokAt(3), "%name%|.|,|=|>")) !Token::Match(tok->tokAt(3), "%name%|.|,|=|>"))
syntaxError(tok->next()); syntaxError(tok->next());
codeWithTemplates = true; codeWithTemplates = true;
Token *parmEnd = tok->next()->findClosingBracket(); const Token * const parmEnd = tok->next()->findClosingBracket();
for (const Token *tok2 = parmEnd; tok2; tok2 = tok2->next()) { for (const Token *tok2 = parmEnd; tok2; tok2 = tok2->next()) {
if (tok2->str() == "(") if (tok2->str() == "(")
tok2 = tok2->link(); tok2 = tok2->link();
@ -848,7 +848,7 @@ void TemplateSimplifier::simplifyTemplateAliases()
} }
tok2 = aliasUsage.token->next(); // the '<' tok2 = aliasUsage.token->next(); // the '<'
const Token * const endToken1 = templateAlias.token->next()->findClosingBracket(); const Token * const endToken1 = templateAlias.token->next()->findClosingBracket();
Token * const endToken2 = TokenList::copyTokens(tok2, templateAlias.token->tokAt(2), endToken1->previous(), false); const Token * const endToken2 = TokenList::copyTokens(tok2, templateAlias.token->tokAt(2), endToken1->previous(), false);
for (const Token *tok1 = templateAlias.token->next(); tok2 != endToken2; tok1 = tok1->next(), tok2 = tok2->next()) { for (const Token *tok1 = templateAlias.token->next(); tok2 != endToken2; tok1 = tok1->next(), tok2 = tok2->next()) {
if (!tok2->isName()) if (!tok2->isName())
continue; continue;
@ -1758,7 +1758,7 @@ void TemplateSimplifier::replaceTemplateUsage(Token * const instantiationToken,
// matching template usage => replace tokens.. // matching template usage => replace tokens..
// Foo < int > => Foo<int> // Foo < int > => Foo<int>
if (tok2->str() == ">" && typeCountInInstantiation == typesUsedInTemplateInstantiation.size()) { if (tok2->str() == ">" && typeCountInInstantiation == typesUsedInTemplateInstantiation.size()) {
Token * const nameTok1 = nameTok; const Token * const nameTok1 = nameTok;
while (Token::Match(nameTok->tokAt(-2), "%name% :: %name%")) while (Token::Match(nameTok->tokAt(-2), "%name% :: %name%"))
nameTok = nameTok->tokAt(-2); nameTok = nameTok->tokAt(-2);
nameTok->str(newName); nameTok->str(newName);

View File

@ -4286,13 +4286,13 @@ void Tokenizer::removeRedundantAssignment()
if (tok->str() == "{") if (tok->str() == "{")
tok = tok->link(); tok = tok->link();
Token * start = const_cast<Token *>(startOfExecutableScope(tok)); const Token * const start = const_cast<Token *>(startOfExecutableScope(tok));
if (start) { if (start) {
tok = start->previous(); tok = start->previous();
// parse in this function.. // parse in this function..
std::set<unsigned int> localvars; std::set<unsigned int> localvars;
const Token * const end = tok->next()->link(); const Token * const end = tok->next()->link();
for (Token *tok2 = tok->next(); tok2 && tok2 != end; tok2 = tok2->next()) { for (Token * tok2 = tok->next(); tok2 && tok2 != end; tok2 = tok2->next()) {
// skip local class or struct // skip local class or struct
if (Token::Match(tok2, "class|struct %type% {|:")) { if (Token::Match(tok2, "class|struct %type% {|:")) {
// skip to '{' // skip to '{'
@ -8666,8 +8666,8 @@ void Tokenizer::simplifyFuncInWhile()
continue; continue;
Token *func = tok->tokAt(2); Token *func = tok->tokAt(2);
Token *var = tok->tokAt(4); const Token * const var = tok->tokAt(4);
Token *end = tok->next()->link()->next()->link(); Token * const end = tok->next()->link()->next()->link();
const unsigned int varid = ++mVarId; // Create new variable const unsigned int varid = ++mVarId; // Create new variable
const std::string varname("cppcheck:r" + MathLib::toString(++count)); const std::string varname("cppcheck:r" + MathLib::toString(++count));
@ -8745,7 +8745,7 @@ void Tokenizer::simplifyStructDecl()
Token *start = tok; Token *start = tok;
while (Token::Match(start->previous(), "%type%")) while (Token::Match(start->previous(), "%type%"))
start = start->previous(); start = start->previous();
Token *type = tok->next(); const Token * const type = tok->next();
Token *next = tok->tokAt(2); Token *next = tok->tokAt(2);
while (next && next->str() != "{") while (next && next->str() != "{")
@ -9378,10 +9378,10 @@ void Tokenizer::simplifyBitfields()
} }
if (last && last->str() == ",") { if (last && last->str() == ",") {
Token *tok1 = last; Token * tok1 = last;
tok1->str(";"); tok1->str(";");
Token *tok2 = tok->next(); const Token *const tok2 = tok->next();
tok1->insertToken(tok2->str()); tok1->insertToken(tok2->str());
tok1 = tok1->next(); tok1 = tok1->next();
tok1->isSigned(tok2->isSigned()); tok1->isSigned(tok2->isSigned());
@ -9950,7 +9950,7 @@ void Tokenizer::simplifyReturnStrncat()
tok2->insertToken(";"); tok2->insertToken(";");
//the last token of the first argument before ',' //the last token of the first argument before ','
Token *end = tok->next()->nextArgument()->tokAt(-2); const Token * const end = tok->next()->nextArgument()->tokAt(-2);
//all the first argument is copied //all the first argument is copied
TokenList::copyTokens(tok2, tok->next(), end); TokenList::copyTokens(tok2, tok->next(), end);
@ -10057,7 +10057,7 @@ void Tokenizer::simplifyMathExpressions()
const std::string& leftExponent = tok2->strAt(2); const std::string& leftExponent = tok2->strAt(2);
if (!isTwoNumber(leftExponent)) if (!isTwoNumber(leftExponent))
continue; // left exponent is not 2 continue; // left exponent is not 2
Token * const tok3 = tok2->tokAt(8); const Token * const tok3 = tok2->tokAt(8);
Token * const tok4 = tok3->link(); Token * const tok4 = tok3->link();
if (!Token::Match(tok4, ") , %num% )")) if (!Token::Match(tok4, ") , %num% )"))
continue; continue;
@ -10075,7 +10075,7 @@ void Tokenizer::simplifyMathExpressions()
const std::string& leftExponent = tok2->strAt(2); const std::string& leftExponent = tok2->strAt(2);
if (!isTwoNumber(leftExponent)) if (!isTwoNumber(leftExponent))
continue; // left exponent is not 2 continue; // left exponent is not 2
Token * const tok3 = tok2->tokAt(8); const Token * const tok3 = tok2->tokAt(8);
Token * const tok4 = tok3->link(); Token * const tok4 = tok3->link();
if (!Token::Match(tok4, ") , %num% )")) if (!Token::Match(tok4, ") , %num% )"))
continue; continue;
@ -10093,7 +10093,7 @@ void Tokenizer::simplifyMathExpressions()
const std::string& leftExponent = tok2->strAt(2); const std::string& leftExponent = tok2->strAt(2);
if (!isTwoNumber(leftExponent)) if (!isTwoNumber(leftExponent))
continue; // left exponent is not 2 continue; // left exponent is not 2
Token * const tok3 = tok2->tokAt(8); const Token * const tok3 = tok2->tokAt(8);
Token * const tok4 = tok3->link(); Token * const tok4 = tok3->link();
if (!Token::Match(tok4, ") , %num% )")) if (!Token::Match(tok4, ") , %num% )"))
continue; continue;
@ -10111,7 +10111,7 @@ void Tokenizer::simplifyMathExpressions()
const std::string& leftExponent = tok2->strAt(2); const std::string& leftExponent = tok2->strAt(2);
if (!isTwoNumber(leftExponent)) if (!isTwoNumber(leftExponent))
continue; // left exponent is not 2 continue; // left exponent is not 2
Token * const tok3 = tok2->tokAt(8); const Token * const tok3 = tok2->tokAt(8);
Token * const tok4 = tok3->link(); Token * const tok4 = tok3->link();
if (!Token::Match(tok4, ") , %num% )")) if (!Token::Match(tok4, ") , %num% )"))
continue; continue;

View File

@ -695,7 +695,7 @@ static void compilePrecedence2(Token *&tok, AST_state& state)
} }
} }
Token* tok2 = tok; const Token* const tok2 = tok;
if (tok->strAt(1) != "]") if (tok->strAt(1) != "]")
compileBinOp(tok, state, compileExpression); compileBinOp(tok, state, compileExpression);
else else
@ -1152,7 +1152,7 @@ static Token * createAstAtToken(Token *tok, bool cpp)
Token * const tok1 = tok; Token * const tok1 = tok;
AST_state state(cpp); AST_state state(cpp);
compileExpression(tok, state); compileExpression(tok, state);
Token * const endToken = tok; const Token * const endToken = tok;
if (endToken == tok1 || !endToken) if (endToken == tok1 || !endToken)
return tok1; return tok1;

View File

@ -1735,7 +1735,7 @@ static bool valueFlowForward(Token * const startToken,
} }
if (truevalues.size() != values.size() || condAlwaysTrue) { if (truevalues.size() != values.size() || condAlwaysTrue) {
// '{' // '{'
Token * const startToken1 = tok2->linkAt(1)->next(); const Token * const startToken1 = tok2->linkAt(1)->next();
bool vfresult = valueFlowForward(startToken1->next(), bool vfresult = valueFlowForward(startToken1->next(),
startToken1->link(), startToken1->link(),
@ -1763,7 +1763,7 @@ static bool valueFlowForward(Token * const startToken,
} }
if (Token::simpleMatch(tok2, "} else {")) { if (Token::simpleMatch(tok2, "} else {")) {
Token * const startTokenElse = tok2->tokAt(2); const Token * const startTokenElse = tok2->tokAt(2);
vfresult = valueFlowForward(startTokenElse->next(), vfresult = valueFlowForward(startTokenElse->next(),
startTokenElse->link(), startTokenElse->link(),
@ -2219,7 +2219,7 @@ static bool isStdMoveOrStdForwarded(Token * tok, ValueFlow::Value::MoveKind * mo
variableToken = tok->tokAt(4); variableToken = tok->tokAt(4);
kind = ValueFlow::Value::MovedVariable; kind = ValueFlow::Value::MovedVariable;
} else if (Token::simpleMatch(tok, "std :: forward <")) { } else if (Token::simpleMatch(tok, "std :: forward <")) {
Token * leftAngle = tok->tokAt(3); const Token * const leftAngle = tok->tokAt(3);
Token * rightAngle = leftAngle->link(); Token * rightAngle = leftAngle->link();
if (Token::Match(rightAngle, "> ( %var% )")) { if (Token::Match(rightAngle, "> ( %var% )")) {
variableToken = rightAngle->tokAt(2); variableToken = rightAngle->tokAt(2);
@ -2602,7 +2602,7 @@ static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symbol
bool bail = false; bool bail = false;
for (int i=0; i<2; i++) { for (int i=0; i<2; i++) {
Token * startToken = startTokens[i]; const Token * const startToken = startTokens[i];
if (!startToken) if (!startToken)
continue; continue;
std::list<ValueFlow::Value> & values = (i==0 ? true_values : false_values); std::list<ValueFlow::Value> & values = (i==0 ? true_values : false_values);