Refactorization: Use varid instead of variable name in CheckClass::initializeVarList() and fixed issue in setVarId().

Fixed internal warning.
This commit is contained in:
PKEuS 2015-11-09 23:00:14 +01:00
parent 49a9b011eb
commit 5cd36d6f8a
4 changed files with 35 additions and 24 deletions

View File

@ -415,26 +415,26 @@ bool CheckClass::canNotMove(const Scope *scope)
return constructor && !(publicAssign || publicCopy || publicMove);
}
void CheckClass::assignVar(const std::string &varname, const Scope *scope, std::vector<Usage> &usage)
void CheckClass::assignVar(unsigned int varid, const Scope *scope, std::vector<Usage> &usage)
{
std::list<Variable>::const_iterator var;
unsigned int count = 0;
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) {
if (var->name() == varname) {
if (var->declarationId() == varid) {
usage[count].assign = true;
return;
}
}
}
void CheckClass::initVar(const std::string &varname, const Scope *scope, std::vector<Usage> &usage)
void CheckClass::initVar(unsigned int varid, const Scope *scope, std::vector<Usage> &usage)
{
std::list<Variable>::const_iterator var;
unsigned int count = 0;
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) {
if (var->name() == varname) {
if (var->declarationId() == varid) {
usage[count].init = true;
return;
}
@ -493,7 +493,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
if (initList) {
if (level == 0 && Token::Match(ftok, "%name% {|(") && Token::Match(ftok->linkAt(1), "}|) ,|{")) {
if (ftok->str() != func.name()) {
initVar(ftok->str(), scope, usage);
initVar(ftok->varId(), scope, usage);
} else { // c++11 delegate constructor
const Function *member = ftok->function();
// member function found
@ -521,7 +521,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
}
}
} else if (level != 0 && Token::Match(ftok, "%name% =")) // assignment in the initializer: var(value = x)
assignVar(ftok->str(), scope, usage);
assignVar(ftok->varId(), scope, usage);
// Level handling
if (ftok->link() && Token::Match(ftok, "(|<"))
@ -541,7 +541,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
// Variable getting value from stream?
if (Token::Match(ftok, ">> %name%")) {
assignVar(ftok->strAt(1), scope, usage);
assignVar(ftok->next()->varId(), scope, usage);
}
// Before a new statement there is "[{};()=[]"
@ -569,7 +569,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
if (var->declarationId() == ftok->next()->varId()) {
/** @todo false negative: we assume function changes variable state */
assignVar(ftok->next()->str(), scope, usage);
assignVar(ftok->next()->varId(), scope, usage);
break;
}
}
@ -615,7 +615,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
else if (Token::Match(ftok, "::| memset ( %name% ,")) {
if (ftok->str() == "::")
ftok = ftok->next();
assignVar(ftok->strAt(2), scope, usage);
assignVar(ftok->tokAt(2)->varId(), scope, usage);
ftok = ftok->linkAt(1);
continue;
}
@ -690,7 +690,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
tok2 = tok2->next();
if (tok2->str() == "&")
tok2 = tok2->next();
assignVar(tok2->str(), scope, usage);
assignVar(tok2->varId(), scope, usage);
}
}
}
@ -720,7 +720,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
else {
for (const Token *tok = ftok->tokAt(2); tok && tok != ftok->next()->link(); tok = tok->next()) {
if (tok->isName()) {
assignVar(tok->str(), scope, usage);
assignVar(tok->varId(), scope, usage);
}
}
}
@ -729,7 +729,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
// Assignment of member variable?
else if (Token::Match(ftok, "%name% =")) {
assignVar(ftok->str(), scope, usage);
assignVar(ftok->varId(), scope, usage);
bool bailout = ftok->variable() && ftok->variable()->isReference();
const Token* tok2 = ftok->tokAt(2);
if (tok2->str() == "&") {
@ -737,7 +737,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
bailout = true;
}
if (tok2->variable() && (bailout || tok2->variable()->isArray()) && tok2->strAt(1) != "[")
assignVar(tok2->str(), scope, usage);
assignVar(tok2->varId(), scope, usage);
}
// Assignment of array item of member variable?
@ -752,19 +752,19 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
break;
}
if (tok2 && tok2->strAt(1) == "=")
assignVar(ftok->str(), scope, usage);
assignVar(ftok->varId(), scope, usage);
}
// Assignment of array item of member variable?
else if (Token::Match(ftok, "* %name% =")) {
assignVar(ftok->next()->str(), scope, usage);
assignVar(ftok->next()->varId(), scope, usage);
} else if (Token::Match(ftok, "* this . %name% =")) {
assignVar(ftok->strAt(3), scope, usage);
assignVar(ftok->tokAt(3)->varId(), scope, usage);
}
// The functions 'clear' and 'Clear' are supposed to initialize variable.
if (Token::Match(ftok, "%name% . clear|Clear (")) {
assignVar(ftok->str(), scope, usage);
assignVar(ftok->varId(), scope, usage);
}
}
}

View File

@ -260,7 +260,7 @@ private:
* @param scope pointer to variable Scope
* @param usage reference to usage vector
*/
static void assignVar(const std::string &varname, const Scope *scope, std::vector<Usage> &usage);
static void assignVar(unsigned int varid, const Scope *scope, std::vector<Usage> &usage);
/**
* @brief initialize a variable in the varlist
@ -268,7 +268,7 @@ private:
* @param scope pointer to variable Scope
* @param usage reference to usage vector
*/
static void initVar(const std::string &varname, const Scope *scope, std::vector<Usage> &usage);
static void initVar(unsigned int varid, const Scope *scope, std::vector<Usage> &usage);
/**
* @brief set all variables in list assigned

View File

@ -2605,8 +2605,8 @@ void Tokenizer::setVarIdClassDeclaration(Token * const startToken,
tok->varId(it->second);
}
} else if (tok->isName() && tok->varId() <= scopeStartVarId) {
if (indentlevel > 0) {
if (Token::Match(tok->previous(), "::|.") && tok->strAt(-2) != "this" && !Token::Match(tok->tokAt(-5), "( * this ) ."))
if (indentlevel > 0 || initList) {
if (Token::Match(tok->previous(), "::|.") && tok->strAt(-2) != "this" && !Token::simpleMatch(tok->tokAt(-5), "( * this ) ."))
continue;
if (tok->next()->str() == "::") {
if (tok->str() == className)
@ -2644,7 +2644,7 @@ static void setVarIdClassFunction(const std::string &classname,
continue;
if (Token::Match(tok2->tokAt(-4), "%name% :: %name% ::")) // Currently unsupported
continue;
if (Token::Match(tok2->tokAt(-2), "!!this .") && !Token::Match(tok2->tokAt(-5), "( * this ) ."))
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());

View File

@ -1790,8 +1790,19 @@ private:
"4: } ;\n",
tokenize(code6));
const char code7[] = "class Foo : public Bar {\n"
" explicit Foo(int i) : Bar(mi = i) { }\n"
" int mi;\n"
"};";
ASSERT_EQUALS("\n\n##file 0\n"
"1: class Foo : public Bar {\n"
"2: explicit Foo ( int i@1 ) : Bar ( mi@2 = i@1 ) { }\n"
"3: int mi@2 ;\n"
"4: } ;\n",
tokenize(code7));
// #6520
const char code7[] = "class A {\n"
const char code8[] = "class A {\n"
" A(int x) : y(a?0:1), x(x) {}\n"
" int x, y;\n"
"};";
@ -1800,7 +1811,7 @@ private:
"2: A ( int x@1 ) : y@3 ( a ? 0 : 1 ) , x@2 ( x@1 ) { }\n"
"3: int x@2 ; int y@3 ;\n"
"4: } ;\n",
tokenize(code7));
tokenize(code8));
}
void varid_initListWithBaseTemplate() {