Avoid 'unsigned' in Tokenizer. Use 'nonneg' instead for arguments and members.

This commit is contained in:
Daniel Marjamäki 2019-07-14 15:48:20 +02:00
parent 0014fe880e
commit 534659e596
3 changed files with 130 additions and 130 deletions

View File

@ -1127,7 +1127,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
if (!tok->valueType())
continue;
bool check = false;
switch(tok->valueType()->type) {
switch (tok->valueType()->type) {
case ValueType::Type::UNKNOWN_TYPE:
case ValueType::Type::NONSTD:
case ValueType::Type::RECORD:

View File

@ -52,14 +52,14 @@ namespace {
VarIdScopeInfo()
:isExecutable(false), isStructInit(false), isEnum(false), startVarid(0) {
}
VarIdScopeInfo(bool _isExecutable, bool _isStructInit, bool _isEnum, unsigned int _startVarid)
:isExecutable(_isExecutable), isStructInit(_isStructInit), isEnum(_isEnum), startVarid(_startVarid) {
VarIdScopeInfo(bool isExecutable, bool isStructInit, bool isEnum, nonneg int startVarid)
:isExecutable(isExecutable), isStructInit(isStructInit), isEnum(isEnum), startVarid(startVarid) {
}
const bool isExecutable;
const bool isStructInit;
const bool isEnum;
const unsigned int startVarid;
const nonneg int startVarid;
};
}
@ -189,7 +189,7 @@ Tokenizer::~Tokenizer()
// SizeOfType - gives the size of a type
//---------------------------------------------------------------------------
unsigned int Tokenizer::sizeOfType(const Token *type) const
nonneg int Tokenizer::sizeOfType(const Token *type) const
{
if (!type || type->str().empty())
return 0;
@ -197,7 +197,7 @@ unsigned int Tokenizer::sizeOfType(const Token *type) const
if (type->tokType() == Token::eString)
return Token::getStrLength(type) + 1U;
const std::map<std::string, unsigned int>::const_iterator it = mTypeSize.find(type->str());
const std::map<std::string, int>::const_iterator it = mTypeSize.find(type->str());
if (it == mTypeSize.end()) {
const Library::PodType* podtype = mSettings->library.podtype(type->str());
if (!podtype)
@ -410,7 +410,7 @@ namespace {
};
}
static Token *splitDefinitionFromTypedef(Token *tok, unsigned int *unnamedCount)
static Token *splitDefinitionFromTypedef(Token *tok, nonneg int *unnamedCount)
{
Token *tok1;
std::string name;
@ -2415,7 +2415,7 @@ void Tokenizer::combineOperators()
} else if (cpp && (c1 == 'p' || c1 == '_') &&
Token::Match(tok, "private|protected|public|__published : !!:")) {
bool simplify = false;
unsigned int par = 0U;
int par = 0;
for (const Token *prev = tok->previous(); prev; prev = prev->previous()) {
if (prev->str() == ")") {
++par;
@ -2687,7 +2687,7 @@ void Tokenizer::arraySize()
static Token *skipTernaryOp(Token *tok)
{
unsigned int colonLevel = 1;
int colonLevel = 1;
while (nullptr != (tok = tok->next())) {
if (tok->str() == "?") {
++colonLevel;
@ -2730,7 +2730,7 @@ void Tokenizer::simplifyLabelsCaseDefault()
{
const bool cpp = isCPP();
bool executablescope = false;
unsigned int indentLevel = 0;
int indentLevel = 0;
for (Token *tok = list.front(); tok; tok = tok->next()) {
// Simplify labels in the executable scope..
Token *start = const_cast<Token *>(startOfExecutableScope(tok));
@ -2847,14 +2847,14 @@ void Tokenizer::simplifyTemplates()
//---------------------------------------------------------------------------
static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::string,unsigned int> &variableId, bool executableScope, bool cpp, bool c)
static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::string,int> &variableId, bool executableScope, bool cpp, bool c)
{
const Token *tok2 = *tok;
if (!tok2->isName())
return false;
unsigned int typeCount = 0;
unsigned int singleNameCount = 0;
int typeCount = 0;
int singleNameCount = 0;
bool hasstruct = false; // Is there a "struct" or "class"?
bool bracket = false;
bool ref = false;
@ -2967,17 +2967,17 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
void Tokenizer::setVarIdStructMembers(Token **tok1,
std::map<unsigned int, std::map<std::string, unsigned int> >& structMembers,
unsigned int *varId)
std::map<int, std::map<std::string, int> >& structMembers,
nonneg int *varId)
{
Token *tok = *tok1;
if (Token::Match(tok, "%name% = { . %name% =")) {
const unsigned int struct_varid = tok->varId();
const int struct_varid = tok->varId();
if (struct_varid == 0)
return;
std::map<std::string, unsigned int>& members = structMembers[struct_varid];
std::map<std::string, int>& members = structMembers[struct_varid];
tok = tok->tokAt(3);
while (tok->str() != "}") {
@ -2985,7 +2985,7 @@ void Tokenizer::setVarIdStructMembers(Token **tok1,
tok = tok->link();
if (Token::Match(tok->previous(), "[,{] . %name% =")) {
tok = tok->next();
const std::map<std::string, unsigned int>::iterator it = members.find(tok->str());
const std::map<std::string, int>::iterator it = members.find(tok->str());
if (it == members.end()) {
members[tok->str()] = ++(*varId);
tok->varId(*varId);
@ -3006,7 +3006,7 @@ void Tokenizer::setVarIdStructMembers(Token **tok1,
tok = tok->tokAt(3);
continue;
}
const unsigned int struct_varid = tok->varId();
const int struct_varid = tok->varId();
tok = tok->tokAt(2);
if (struct_varid == 0)
continue;
@ -3018,8 +3018,8 @@ void Tokenizer::setVarIdStructMembers(Token **tok1,
if (TemplateSimplifier::templateParameters(tok->next()) > 0)
break;
std::map<std::string, unsigned int>& members = structMembers[struct_varid];
const std::map<std::string, unsigned int>::iterator it = members.find(tok->str());
std::map<std::string, int>& members = structMembers[struct_varid];
const std::map<std::string, int>::iterator it = members.find(tok->str());
if (it == members.end()) {
members[tok->str()] = ++(*varId);
tok->varId(*varId);
@ -3034,8 +3034,8 @@ void Tokenizer::setVarIdStructMembers(Token **tok1,
void Tokenizer::setVarIdClassDeclaration(const Token * const startToken,
const VariableMap &variableMap,
const unsigned int scopeStartVarId,
std::map<unsigned int, std::map<std::string,unsigned int> >& structMembers)
const nonneg int scopeStartVarId,
std::map<int, std::map<std::string,int> >& structMembers)
{
// end of scope
const Token * const endToken = startToken->link();
@ -3052,7 +3052,7 @@ void Tokenizer::setVarIdClassDeclaration(const Token * const startToken,
}
// replace varids..
unsigned int indentlevel = 0;
int indentlevel = 0;
bool initList = false;
bool inEnum = false;
const Token *initListArgLastToken = nullptr;
@ -3076,7 +3076,7 @@ void Tokenizer::setVarIdClassDeclaration(const Token * const startToken,
--indentlevel;
inEnum = false;
} else if (initList && indentlevel == 0 && Token::Match(tok->previous(), "[,:] %name% [({]")) {
const std::map<std::string, unsigned int>::const_iterator it = variableMap.find(tok->str());
const std::map<std::string, int>::const_iterator it = variableMap.find(tok->str());
if (it != variableMap.end()) {
tok->varId(it->second);
}
@ -3094,7 +3094,7 @@ void Tokenizer::setVarIdClassDeclaration(const Token * const startToken,
}
if (!inEnum) {
const std::map<std::string, unsigned int>::const_iterator it = variableMap.find(tok->str());
const std::map<std::string, int>::const_iterator it = variableMap.find(tok->str());
if (it != variableMap.end()) {
tok->varId(it->second);
setVarIdStructMembers(&tok, structMembers, variableMap.getVarId());
@ -3113,9 +3113,9 @@ void Tokenizer::setVarIdClassDeclaration(const Token * const startToken,
void Tokenizer::setVarIdClassFunction(const std::string &classname,
Token * const startToken,
const Token * const endToken,
const std::map<std::string, unsigned int> &varlist,
std::map<unsigned int, std::map<std::string, unsigned int> >& structMembers,
unsigned int *varId_)
const std::map<std::string, int> &varlist,
std::map<int, std::map<std::string, int> >& structMembers,
nonneg int *varId_)
{
for (Token *tok2 = startToken; tok2 && tok2 != endToken; tok2 = tok2->next()) {
if (tok2->varId() != 0 || !tok2->isName())
@ -3127,7 +3127,7 @@ void Tokenizer::setVarIdClassFunction(const std::string &classname,
if (Token::Match(tok2->tokAt(-2), "!!this .") && !Token::simpleMatch(tok2->tokAt(-5), "( * this ) ."))
continue;
const std::map<std::string,unsigned int>::const_iterator it = varlist.find(tok2->str());
const std::map<std::string,int>::const_iterator it = varlist.find(tok2->str());
if (it != varlist.end()) {
tok2->varId(it->second);
setVarIdStructMembers(&tok2, structMembers, varId_);
@ -3166,7 +3166,7 @@ void Tokenizer::setVarIdPass1()
const std::set<std::string>& notstart = (isC()) ? notstart_c : notstart_cpp;
VariableMap variableMap;
std::map<unsigned int, std::map<std::string, unsigned int> > structMembers;
std::map<int, std::map<std::string, int> > structMembers;
std::stack<VarIdScopeInfo> scopeStack;
@ -3359,7 +3359,7 @@ void Tokenizer::setVarIdPass1()
const Token *end = tok->findClosingBracket();
while (tok != end) {
if (tok->isName()) {
const std::map<std::string, unsigned int>::const_iterator it = variableMap.find(tok->str());
const std::map<std::string, int>::const_iterator it = variableMap.find(tok->str());
if (it != variableMap.end())
tok->varId(it->second);
}
@ -3400,7 +3400,7 @@ void Tokenizer::setVarIdPass1()
if (!Token::Match(start, "[(,] %type% %name%|*|&"))
break;
}
if (start->varId() > 0U)
if (start->varId() > 0)
break;
}
@ -3417,7 +3417,7 @@ void Tokenizer::setVarIdPass1()
}
if (!scopeStack.top().isEnum) {
const std::map<std::string, unsigned int>::const_iterator it = variableMap.find(tok->str());
const std::map<std::string, int>::const_iterator it = variableMap.find(tok->str());
if (it != variableMap.end()) {
tok->varId(it->second);
setVarIdStructMembers(&tok, structMembers, variableMap.getVarId());
@ -3542,7 +3542,7 @@ static Token * matchMemberFunctionName(const Member &func, const std::list<Scope
void Tokenizer::setVarIdPass2()
{
std::map<unsigned int, std::map<std::string, unsigned int> > structMembers;
std::map<int, std::map<std::string, int> > structMembers;
// Member functions and variables in this source
std::list<Member> allMemberFunctions;
@ -3604,7 +3604,7 @@ void Tokenizer::setVarIdPass2()
std::list<ScopeInfo2> scopeInfo;
// class members..
std::map<std::string, std::map<std::string, unsigned int> > varsByClass;
std::map<std::string, std::map<std::string, int> > varsByClass;
for (Token *tok = list.front(); tok; tok = tok->next()) {
while (tok->str() == "}" && !scopeInfo.empty() && tok == scopeInfo.back().bodyEnd)
scopeInfo.pop_back();
@ -3627,7 +3627,7 @@ void Tokenizer::setVarIdPass2()
for (const Token *it : classnameTokens)
classname += (classname.empty() ? "" : " :: ") + it->str();
std::map<std::string, unsigned int> &thisClassVars = varsByClass[scopeName2 + classname];
std::map<std::string, int> &thisClassVars = varsByClass[scopeName2 + classname];
while (Token::Match(tokStart, ":|::|,|%name%")) {
if (Token::Match(tokStart, "%name% <")) {
tokStart = tokStart->next()->findClosingBracket();
@ -3636,7 +3636,7 @@ void Tokenizer::setVarIdPass2()
continue;
}
if (Token::Match(tokStart, "%name% ,|{")) {
const std::map<std::string, unsigned int>& baseClassVars = varsByClass[tokStart->str()];
const std::map<std::string, int>& baseClassVars = varsByClass[tokStart->str()];
thisClassVars.insert(baseClassVars.begin(), baseClassVars.end());
}
tokStart = tokStart->next();
@ -3719,7 +3719,7 @@ void Tokenizer::setVarIdPass2()
break;
// set varid
const std::map<std::string, unsigned int>::const_iterator varpos = thisClassVars.find(tok3->str());
const std::map<std::string, int>::const_iterator varpos = thisClassVars.find(tok3->str());
if (varpos != thisClassVars.end())
tok3->varId(varpos->second);
@ -3941,16 +3941,16 @@ void Tokenizer::sizeofAddParentheses()
bool Tokenizer::simplifySizeof()
{
// Locate variable declarations and calculate the size
std::map<unsigned int, unsigned int> sizeOfVar;
std::map<unsigned int, const Token *> declTokOfVar;
std::map<int, int> sizeOfVar;
std::map<int, const Token *> declTokOfVar;
for (const Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->varId() != 0 && sizeOfVar.find(tok->varId()) == sizeOfVar.end()) {
const unsigned int varId = tok->varId();
const int varId = tok->varId();
if (Token::Match(tok->tokAt(-3), "[;{}(,] %type% * %name% [;,)]") ||
Token::Match(tok->tokAt(-4), "[;{}(,] const %type% * %name% [;),]") ||
Token::Match(tok->tokAt(-2), "[;{}(,] %type% %name% [;),]") ||
Token::Match(tok->tokAt(-3), "[;{}(,] const %type% %name% [;),]")) {
const unsigned int size = sizeOfType(tok->previous());
const int size = sizeOfType(tok->previous());
if (size == 0) {
continue;
}
@ -3961,7 +3961,7 @@ bool Tokenizer::simplifySizeof()
else if (Token::Match(tok->previous(), "%type% %name% [ %num% ] [[;=]") ||
Token::Match(tok->tokAt(-2), "%type% * %name% [ %num% ] [[;=]")) {
unsigned int size = sizeOfType(tok->previous());
int size = sizeOfType(tok->previous());
if (size == 0)
continue;
@ -4039,7 +4039,7 @@ bool Tokenizer::simplifySizeof()
// sizeof( a )
else if (Token::Match(tok->next(), "( %var% )")) {
const std::map<unsigned int, unsigned int>::const_iterator sizeOfVarPos = sizeOfVar.find(tok->tokAt(2)->varId());
const std::map<int, int>::const_iterator sizeOfVarPos = sizeOfVar.find(tok->tokAt(2)->varId());
if (sizeOfVarPos != sizeOfVar.end()) {
tok->deleteNext();
tok->deleteThis();
@ -4053,7 +4053,7 @@ bool Tokenizer::simplifySizeof()
}
else if (Token::Match(tok->next(), "( %type% )")) {
const unsigned int size = sizeOfType(tok->tokAt(2));
const int size = sizeOfType(tok->tokAt(2));
if (size > 0) {
tok->str(MathLib::toString(size));
tok->deleteNext(3);
@ -4062,7 +4062,7 @@ bool Tokenizer::simplifySizeof()
}
else if (Token::simpleMatch(tok->next(), "( *") || Token::Match(tok->next(), "( %name% [")) {
unsigned int derefs = 0;
int derefs = 0;
const Token* nametok = tok->tokAt(2);
if (nametok->str() == "*") {
@ -4087,7 +4087,7 @@ bool Tokenizer::simplifySizeof()
// Some default value
MathLib::biguint size = 0;
const unsigned int varid = nametok->varId();
const int varid = nametok->varId();
if (derefs != 0 && varid != 0 && declTokOfVar.find(varid) != declTokOfVar.end()) {
// Try to locate variable declaration..
const Token *decltok = declTokOfVar[varid];
@ -4099,7 +4099,7 @@ bool Tokenizer::simplifySizeof()
// Multi-dimensional array..
if (Token::Match(decltok, "%name% [") && Token::simpleMatch(decltok->linkAt(1), "] [")) {
const Token *tok2 = decltok;
for (unsigned int i = 0; i < derefs; i++)
for (int i = 0; i < derefs; i++)
tok2 = tok2->linkAt(1); // Skip all dimensions that are dereferenced before the sizeof call
while (Token::Match(tok2, "] [ %num% ]")) {
size *= MathLib::toULongNumber(tok2->strAt(2));
@ -4681,7 +4681,7 @@ bool Tokenizer::simplifyTokenList2()
}
//---------------------------------------------------------------------------
void Tokenizer::printDebugOutput(unsigned int simplification) const
void Tokenizer::printDebugOutput(int simplification) const
{
const bool debug = (simplification != 1U && mSettings->debugSimplified) ||
(simplification != 2U && mSettings->debugnormal);
@ -4774,7 +4774,7 @@ void Tokenizer::dump(std::ostream &out) const
}
if (tok->link())
out << " link=\"" << tok->link() << '\"';
if (tok->varId() > 0U)
if (tok->varId() > 0)
out << " varId=\"" << MathLib::toString(tok->varId()) << '\"';
if (tok->variable())
out << " variable=\"" << tok->variable() << '\"';
@ -5051,7 +5051,7 @@ void Tokenizer::removeRedundantAssignment()
if (start) {
tok = start->previous();
// parse in this function..
std::set<unsigned int> localvars;
std::set<int> localvars;
const Token * const end = tok->next()->link();
for (Token * tok2 = tok->next(); tok2 && tok2 != end; tok2 = tok2->next()) {
// skip local class or struct
@ -5185,7 +5185,7 @@ void Tokenizer::simplifyFlowControl()
continue;
Token* end = begin->linkAt(1+(begin->next()->str() == "{" ? 0 : 1));
unsigned int indentLevel = 0;
int indentLevel = 0;
bool stilldead = false;
for (Token *tok = begin; tok && tok != end; tok = tok->next()) {
@ -5325,7 +5325,7 @@ void Tokenizer::removeRedundantFor()
if (type)
varTok = varTok->next();
const std::string varname(varTok->str());
const unsigned int varid(varTok->varId());
const int varid(varTok->varId());
if (varname != varTok->strAt(4))
continue;
const Token *vartok2 = tok->linkAt(2)->previous();
@ -5878,7 +5878,7 @@ bool Tokenizer::simplifyConstTernaryOp()
// delete the condition token and the "?"
tok->deleteNext(2);
unsigned int ternaryOplevel = 0;
int ternaryOplevel = 0;
for (const Token *endTok = colon; endTok; endTok = endTok->next()) {
if (Token::Match(endTok, "(|[|{")) {
endTok = endTok->link();
@ -5916,7 +5916,7 @@ void Tokenizer::simplifyUndefinedSizeArray()
tok = tok2->previous();
Token *end = tok2->next();
unsigned int count = 0;
int count = 0;
do {
end = end->tokAt(2);
++count;
@ -5955,7 +5955,7 @@ void Tokenizer::simplifyCasts()
// #4164 : ((unsigned char)1) => (1)
if (Token::Match(tok->next(), "( %type% ) %num%") && tok->next()->link()->previous()->isStandardType()) {
const MathLib::bigint value = MathLib::toLongNumber(tok->next()->link()->next()->str());
unsigned int bits = mSettings->char_bit * mTypeSize[tok->next()->link()->previous()->str()];
int bits = mSettings->char_bit * mTypeSize[tok->next()->link()->previous()->str()];
if (!tok->tokAt(2)->isUnsigned() && bits > 0)
bits--;
if (bits < 31 && value >= 0 && value < (1LL << bits)) {
@ -6406,7 +6406,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
bool isconst = false;
bool isstatic = false;
Token *tok2 = type0;
unsigned int typelen = 1;
int typelen = 1;
if (Token::Match(tok2, "::|extern")) {
tok2 = tok2->next();
@ -6469,8 +6469,8 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
tok2 = tok2->tokAt(2);
if (tok2 && tok2->previous()->str() == "::")
continue;
unsigned int indentlevel = 0;
unsigned int parens = 0;
int indentlevel = 0;
int parens = 0;
for (Token *tok3 = tok2; tok3; tok3 = tok3->next()) {
++typelen;
@ -6484,7 +6484,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
}
--indentlevel;
} else if (!parens && tok3->str() == ">>") {
if (indentlevel <= 1U) {
if (indentlevel <= 1) {
tok2 = tok3->next();
break;
}
@ -6781,7 +6781,7 @@ void Tokenizer::simplifyIfAndWhileAssign()
// If it's a while loop, insert the assignment in the loop
if (iswhile && !isDoWhile) {
unsigned int indentlevel = 0;
int indentlevel = 0;
Token *tok3 = tok2;
for (; tok3; tok3 = tok3->next()) {
@ -6879,7 +6879,7 @@ static const std::map<std::string, std::string> cAlternativeTokens = {
bool Tokenizer::simplifyCAlternativeTokens()
{
/* executable scope level */
unsigned int executableScopeLevel = 0;
int executableScopeLevel = 0;
bool ret = false;
for (Token *tok = list.front(); tok; tok = tok->next()) {
@ -6947,7 +6947,7 @@ void Tokenizer::simplifyInitVar()
tok1 = tok1->next();
tok1->str(";");
const unsigned int numTokens = (Token::Match(tok, "class|struct|union")) ? 2U : 1U;
const int numTokens = (Token::Match(tok, "class|struct|union")) ? 2U : 1U;
list.insertTokens(tok1, tok, numTokens);
tok = initVar(tok);
}
@ -7007,9 +7007,9 @@ bool Tokenizer::simplifyKnownVariables()
// constants..
{
std::unordered_map<unsigned int, std::string> constantValues;
std::map<unsigned int, Token*> constantVars;
std::unordered_map<unsigned int, std::list<Token*>> constantValueUsages;
std::unordered_map<int, std::string> constantValues;
std::map<int, Token*> constantVars;
std::unordered_map<int, std::list<Token*>> constantValueUsages;
bool goback = false;
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (goback) {
@ -7121,9 +7121,9 @@ bool Tokenizer::simplifyKnownVariables()
}
// variable id for local, float/double, array variables
std::set<unsigned int> localvars;
std::set<unsigned int> floatvars;
std::set<unsigned int> arrays;
std::set<int> localvars;
std::set<int> floatvars;
std::set<int> arrays;
// auto variables..
for (Token *tok = list.front(); tok; tok = tok->next()) {
@ -7178,7 +7178,7 @@ bool Tokenizer::simplifyKnownVariables()
Token::Match(tok2, "%name% [ %num%| ] = %str% ;") ||
Token::Match(tok2, "%name% = & %name% ;") ||
(Token::Match(tok2, "%name% = & %name% [ 0 ] ;") && arrays.find(tok2->tokAt(3)->varId()) != arrays.end()))) {
const unsigned int varid = tok2->varId();
const int varid = tok2->varId();
if (varid == 0)
continue;
@ -7221,7 +7221,7 @@ bool Tokenizer::simplifyKnownVariables()
const Token * const valueToken = tok2->tokAt(2);
std::string value;
unsigned int valueVarId = 0;
int valueVarId = 0;
Token *tok3 = nullptr;
bool valueIsPointer = false;
@ -7240,7 +7240,7 @@ bool Tokenizer::simplifyKnownVariables()
}
else if (Token::Match(tok2, "strcpy|sprintf ( %name% , %str% ) ;")) {
const unsigned int varid(tok2->tokAt(2)->varId());
const int varid(tok2->tokAt(2)->varId());
if (varid == 0)
continue;
@ -7254,7 +7254,7 @@ bool Tokenizer::simplifyKnownVariables()
++n;
}
}
const unsigned int valueVarId(0);
const int valueVarId(0);
const bool valueIsPointer(false);
Token *tok3 = tok2->tokAt(6);
ret |= simplifyKnownVariablesSimplify(&tok2, tok3, varid, emptyString, value, valueVarId, valueIsPointer, valueToken, indentlevel);
@ -7272,7 +7272,7 @@ bool Tokenizer::simplifyKnownVariables()
return ret;
}
bool Tokenizer::simplifyKnownVariablesGetData(unsigned int varid, Token **_tok2, Token **_tok3, std::string &value, unsigned int &valueVarId, bool &valueIsPointer, bool floatvar)
bool Tokenizer::simplifyKnownVariablesGetData(nonneg int varid, Token **_tok2, Token **_tok3, std::string &value, nonneg int &valueVarId, bool &valueIsPointer, bool floatvar)
{
Token *tok2 = *_tok2;
Token *tok3 = nullptr;
@ -7341,7 +7341,7 @@ bool Tokenizer::simplifyKnownVariablesGetData(unsigned int varid, Token **_tok2,
return true;
}
bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsigned int varid, const std::string &structname, std::string &value, unsigned int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel) const
bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, nonneg int varid, const std::string &structname, std::string &value, nonneg int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel) const
{
const bool pointeralias(valueToken->isName() || Token::Match(valueToken, "& %name% ["));
const bool varIsGlobal = (indentlevel == 0);
@ -7587,7 +7587,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
// don't simplify buffer value
"memcmp","memcpy","memmove","memset","strcpy","strncmp","strncpy"
};
for (unsigned int i = 0; i < (sizeof(functionName) / sizeof(*functionName)); ++i) {
for (int i = 0; i < (sizeof(functionName) / sizeof(*functionName)); ++i) {
if (valueVarId == 0U && i >= 2)
break;
if (tok3->str() == functionName[i]) {
@ -7611,7 +7611,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
// don't simplify buffer value
"memcmp","memcpy","memmove"
};
for (unsigned int i = 0; i < (sizeof(functionName) / sizeof(*functionName)); ++i) {
for (int i = 0; i < (sizeof(functionName) / sizeof(*functionName)); ++i) {
if (valueVarId == 0U && i >= 4)
break;
if (tok3->str() == functionName[i]) {
@ -8033,7 +8033,7 @@ void Tokenizer::simplifyReference()
for (Token *tok2 = tok; tok2 && tok2 != end; tok2 = tok2->next()) {
// found a reference..
if (Token::Match(tok2, "[;{}] %type% & %name% (|= %name% )| ;")) {
const unsigned int refId = tok2->tokAt(3)->varId();
const int refId = tok2->tokAt(3)->varId();
if (!refId)
continue;
@ -8094,7 +8094,7 @@ void Tokenizer::simplifyOffsetPointerDereference()
void Tokenizer::simplifyOffsetPointerReference()
{
std::set<unsigned int> pod;
std::set<int> pod;
for (const Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->isStandardType()) {
tok = tok->next();
@ -8326,7 +8326,7 @@ bool Tokenizer::isFunctionParameterPassedByValue(const Token *fpar) const
const Token *ftok;
// Look at function call, what parameter number is it?
unsigned int parameter = 1;
int parameter = 1;
for (ftok = fpar->previous(); ftok; ftok = ftok->previous()) {
if (ftok->str() == "(")
break;
@ -8352,7 +8352,7 @@ bool Tokenizer::isFunctionParameterPassedByValue(const Token *fpar) const
else if (Token::Match(tok, "%type% (") && tok->str() == functionName) {
// Goto parameter
tok = tok->tokAt(2);
unsigned int par = 1;
int par = 1;
while (tok && par < parameter) {
if (tok->str() == ")")
break;
@ -8390,13 +8390,13 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end)
if (!begin)
return;
const bool isgoto = Token::Match(begin->tokAt(-2), "goto %name% ;");
unsigned int indentlevel = 1,
indentcase = 0,
indentswitch = 0,
indentlabel = 0,
roundbraces = 0,
indentcheck = 0;
std::vector<unsigned int> switchindents;
int indentlevel = 1;
int indentcase = 0;
int indentswitch = 0;
int indentlabel = 0;
int roundbraces = 0;
int indentcheck = 0;
std::vector<int> switchindents;
bool checklabel = false;
Token *tok = begin;
Token *tokcheck = nullptr;
@ -8517,7 +8517,7 @@ void Tokenizer::eraseDeadCode(Token *begin, const Token *end)
//instruction is removed, there's no sense to keep the
//case instructions. Remove them, if there are any.
Token *tok2 = tok->tokAt(3);
unsigned int indentlevel2 = indentlevel;
int indentlevel2 = indentlevel;
while (tok2->next() && tok2->next() != end) {
if (Token::Match(tok2->next(), "{|[|(")) {
tok2 = tok2->next()->link();
@ -9077,7 +9077,7 @@ void Tokenizer::validate() const
cppcheckError(lastTok);
}
static const Token *findUnmatchedTernaryOp(const Token * const begin, const Token * const end, unsigned depth = 0)
static const Token *findUnmatchedTernaryOp(const Token * const begin, const Token * const end, int depth = 0)
{
std::stack<const Token *> ternaryOp;
for (const Token *tok = begin; tok != end && tok->str() != ";"; tok = tok->next()) {
@ -9207,7 +9207,7 @@ void Tokenizer::findGarbageCode() const
if (!Token::simpleMatch(tok, "for (")) // find for loops
continue;
// count number of semicolons
unsigned int semicolons = 0;
int semicolons = 0;
const Token* const startTok = tok;
tok = tok->next()->link()->previous(); // find ")" of the for-loop
// walk backwards until we find the beginning (startTok) of the for() again
@ -9337,7 +9337,7 @@ std::string Tokenizer::simplifyString(const std::string &source)
continue;
int c = 'a'; // char
unsigned int sz = 0; // size of stringdata
int sz = 0; // size of stringdata
if (str[i+1] == 'x') {
sz = 2;
while (sz < 4 && std::isxdigit((unsigned char)str[i+sz]))
@ -9452,7 +9452,7 @@ void Tokenizer::simplifyErrNoInWhile()
void Tokenizer::simplifyFuncInWhile()
{
unsigned int count = 0;
int count = 0;
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (!Token::Match(tok, "while ( %name% ( %name% ) ) {"))
continue;
@ -9461,7 +9461,7 @@ void Tokenizer::simplifyFuncInWhile()
const Token * const var = tok->tokAt(4);
Token * const end = tok->next()->link()->next()->link();
const unsigned int varid = ++mVarId; // Create new variable
const int varid = ++mVarId; // Create new variable
const std::string varname("cppcheck:r" + MathLib::toString(++count));
tok->str("int");
tok->next()->insertToken(varname);
@ -9492,7 +9492,7 @@ void Tokenizer::simplifyStructDecl()
const bool cpp = isCPP();
// A counter that is used when giving unique names for anonymous structs.
unsigned int count = 0;
int count = 0;
// Skip simplification of unions in class definition
std::stack<bool> skip; // true = in function, false = not in function
@ -10002,7 +10002,7 @@ void Tokenizer::simplifyAssignmentBlock()
const std::string &varname = tok->next()->str();
// goto the "} )"
unsigned int indentlevel = 0;
int indentlevel = 0;
Token *tok2 = tok;
while (nullptr != (tok2 = tok2->next())) {
if (Token::Match(tok2, "(|{"))
@ -10052,7 +10052,7 @@ void Tokenizer::simplifyAsm()
else if (Token::Match(tok, "_asm|__asm")) {
Token *endasm = tok->next();
const Token *firstSemiColon = nullptr;
unsigned int comment = 0;
int comment = 0;
while (Token::Match(endasm, "%num%|%name%|,|:|;") || (endasm && endasm->linenr() == comment)) {
if (Token::Match(endasm, "_asm|__asm|__endasm"))
break;
@ -10097,7 +10097,7 @@ void Tokenizer::simplifyAsm()
tok = tok->tokAt(2);
if (tok->next() && tok->next()->str() == ";" &&
tok->next()->linenr() != tok->linenr()) {
const unsigned int endposition = tok->next()->linenr();
const int endposition = tok->next()->linenr();
tok = tok->tokAt(-3);
for (int i = 0; i < 4; ++i) {
tok = tok->next();
@ -10567,7 +10567,7 @@ void Tokenizer::simplifyQtSignalsSlots()
}
// count { and } for tok2
unsigned int indentlevel = 0;
int indentlevel = 0;
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "{") {
++indentlevel;
@ -10850,7 +10850,7 @@ void Tokenizer::printUnknownTypes() const
std::multimap<std::string, const Token *> unknowns;
for (unsigned int i = 1; i <= mVarId; ++i) {
for (int i = 1; i <= mVarId; ++i) {
const Variable *var = mSymbolDatabase->getVariableFromVarId(i);
if (!var)
continue;
@ -11033,7 +11033,7 @@ void Tokenizer::prepareTernaryOpForAST()
for (Token* tok = list.front(); tok; tok = tok->next()) {
if (tok->str() == "?") {
bool parenthesesNeeded = false;
unsigned int depth = 0;
int depth = 0;
Token* tok2 = tok->next();
for (; tok2; tok2 = tok2->next()) {
if (tok2->link() && Token::Match(tok2, "[|(|<"))
@ -11281,7 +11281,7 @@ Tokenizer::VariableMap::VariableMap() : mVarId(0) {}
void Tokenizer::VariableMap::enterScope()
{
mScopeInfo.push(std::list<std::pair<std::string, unsigned int>>());
mScopeInfo.push(std::list<std::pair<std::string, int>>());
}
bool Tokenizer::VariableMap::leaveScope()
@ -11289,7 +11289,7 @@ bool Tokenizer::VariableMap::leaveScope()
if (mScopeInfo.empty())
return false;
for (const std::pair<std::string, unsigned int> &outerVariable : mScopeInfo.top()) {
for (const std::pair<std::string, int> &outerVariable : mScopeInfo.top()) {
if (outerVariable.second != 0)
mVariableId[outerVariable.first] = outerVariable.second;
else
@ -11305,13 +11305,13 @@ void Tokenizer::VariableMap::addVariable(const std::string &varname)
mVariableId[varname] = ++mVarId;
return;
}
std::map<std::string, unsigned int>::iterator it = mVariableId.find(varname);
std::map<std::string, int>::iterator it = mVariableId.find(varname);
if (it == mVariableId.end()) {
mScopeInfo.top().push_back(std::pair<std::string, unsigned int>(varname, 0));
mScopeInfo.top().push_back(std::pair<std::string, int>(varname, 0));
mVariableId[varname] = ++mVarId;
return;
}
mScopeInfo.top().push_back(std::pair<std::string, unsigned int>(varname, it->second));
mScopeInfo.top().push_back(std::pair<std::string, int>(varname, it->second));
it->second = ++mVarId;
}

View File

@ -58,25 +58,25 @@ class CPPCHECKLIB Tokenizer {
/** Class used in Tokenizer::setVarIdPass1 */
class VariableMap {
private:
std::map<std::string, unsigned int> mVariableId;
std::stack<std::list<std::pair<std::string, unsigned int> > > mScopeInfo;
mutable unsigned int mVarId;
std::map<std::string, int> mVariableId;
std::stack<std::list<std::pair<std::string,int> > > mScopeInfo;
mutable nonneg int mVarId;
public:
VariableMap();
void enterScope();
bool leaveScope();
void addVariable(const std::string &varname);
bool hasVariable(const std::string &varname) const;
std::map<std::string, unsigned int>::const_iterator find(const std::string &varname) const {
std::map<std::string,int>::const_iterator find(const std::string &varname) const {
return mVariableId.find(varname);
}
std::map<std::string, unsigned int>::const_iterator end() const {
std::map<std::string,int>::const_iterator end() const {
return mVariableId.end();
}
const std::map<std::string, unsigned int> &map() const {
const std::map<std::string,int> &map() const {
return mVariableId;
}
unsigned int *getVarId() const {
nonneg int *getVarId() const {
return &mVarId;
}
};
@ -199,7 +199,7 @@ public:
* @param type Token which will contain e.g. "int", "*", or string.
* @return sizeof for given type, or 0 if it can't be calculated.
*/
unsigned int sizeOfType(const Token *type) const;
nonneg int sizeOfType(const Token *type) const;
/**
* Try to determine if function parameter is passed by value by looking
@ -403,13 +403,13 @@ public:
* Utility function for simplifyKnownVariables. Get data about an
* assigned variable.
*/
static bool simplifyKnownVariablesGetData(unsigned int varid, Token **_tok2, Token **_tok3, std::string &value, unsigned int &valueVarId, bool &valueIsPointer, bool floatvar);
static bool simplifyKnownVariablesGetData(nonneg int varid, Token **_tok2, Token **_tok3, std::string &value, nonneg int &valueVarId, bool &valueIsPointer, bool floatvar);
/**
* utility function for simplifyKnownVariables. Perform simplification
* of a given variable
*/
bool simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsigned int varid, const std::string &structname, std::string &value, unsigned int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel) const;
bool simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, nonneg int varid, const std::string &structname, std::string &value, nonneg int valueVarId, bool valueIsPointer, const Token * const valueToken, int indentlevel) const;
/** Simplify useless C++ empty namespaces, like: 'namespace %name% { }'*/
void simplifyEmptyNamespaces();
@ -765,19 +765,19 @@ private:
void setVarIdClassDeclaration(const Token * const startToken,
const VariableMap &variableMap,
const unsigned int scopeStartVarId,
std::map<unsigned int, std::map<std::string,unsigned int> >& structMembers);
const nonneg int scopeStartVarId,
std::map<int, std::map<std::string,int> >& structMembers);
void setVarIdStructMembers(Token **tok1,
std::map<unsigned int, std::map<std::string, unsigned int> >& structMembers,
unsigned int *varId);
std::map<int, std::map<std::string, int> >& structMembers,
nonneg int *varId);
void setVarIdClassFunction(const std::string &classname,
Token * const startToken,
const Token * const endToken,
const std::map<std::string, unsigned int> &varlist,
std::map<unsigned int, std::map<std::string, unsigned int> >& structMembers,
unsigned int *varId_);
const std::map<std::string,int> &varlist,
std::map<int, std::map<std::string,int> >& structMembers,
nonneg int *varId_);
/**
* Simplify e.g. 'return(strncat(temp,"a",1));' into
@ -817,7 +817,7 @@ public:
* 1=1st simplifications
* 2=2nd simplifications
*/
void printDebugOutput(unsigned int simplification) const;
void printDebugOutput(int simplification) const;
void dump(std::ostream &out) const;
@ -827,7 +827,7 @@ public:
* Get variable count.
* @return number of variables
*/
unsigned int varIdCount() const {
nonneg int varIdCount() const {
return mVarId;
}
@ -895,7 +895,7 @@ private:
* Get new variable id.
* @return new variable id
*/
unsigned int newVarId() {
nonneg int newVarId() {
return ++mVarId;
}
@ -918,13 +918,13 @@ private:
std::string mConfiguration;
/** sizeof information for known types */
std::map<std::string, unsigned int> mTypeSize;
std::map<std::string, int> mTypeSize;
/** variable count */
unsigned int mVarId;
nonneg int mVarId;
/** unnamed count "Unnamed0", "Unnamed1", "Unnamed2", ... */
unsigned int mUnnamedCount;
nonneg int mUnnamedCount;
/**
* was there any templates? templates that are "unused" are