Add missing varid or variable for member variables.

Add an optional extended description…
This commit is contained in:
IOBYTE 2017-03-30 04:07:58 -04:00 committed by PKEuS
parent c9eb7f4556
commit 7cae057615
4 changed files with 61 additions and 15 deletions

View File

@ -1209,8 +1209,36 @@ void SymbolDatabase::createSymbolDatabaseSetTypePointers()
}
}
void SymbolDatabase::fixVarId(VarIdMap & varIds, const Token * vartok, Token * membertok, const Variable * membervar)
{
VarIdMap::iterator varId = varIds.find(vartok->varId());
if (varId == varIds.end()) {
MemberIdMap memberId;
if (membertok->varId() == 0) {
memberId[membervar->nameToken()->varId()] = const_cast<Tokenizer *>(_tokenizer)->newVarId();
_variableList.push_back(membervar);
} else
_variableList[membertok->varId()] = membervar;
varIds.insert(std::make_pair(vartok->varId(), memberId));
varId = varIds.find(vartok->varId());
}
MemberIdMap::iterator memberId = varId->second.find(membervar->nameToken()->varId());
if (memberId == varId->second.end()) {
if (membertok->varId() == 0) {
varId->second.insert(std::make_pair(membervar->nameToken()->varId(), const_cast<Tokenizer *>(_tokenizer)->newVarId()));
_variableList.push_back(membervar);
} else
_variableList[membertok->varId()] = membervar;
memberId = varId->second.find(membervar->nameToken()->varId());
}
if (membertok->varId() == 0)
membertok->varId(memberId->second);
}
void SymbolDatabase::createSymbolDatabaseSetVariablePointers()
{
VarIdMap varIds;
// Set variable pointers
for (const Token* tok = _tokenizer->list.front(); tok != _tokenizer->list.back(); tok = tok->next()) {
if (tok->varId())
@ -1239,6 +1267,8 @@ void SymbolDatabase::createSymbolDatabaseSetVariablePointers()
const Variable *membervar = var->typeScope()->getVariable(membertok->str());
if (membervar) {
membertok->variable(membervar);
if (membertok->varId() == 0 || _variableList[membertok->varId()] == nullptr)
fixVarId(varIds, tok, const_cast<Token *>(membertok), membervar);
}
} else if (var && tok->valueType() && tok->valueType()->type == ValueType::CONTAINER) {
if (Token::Match(var->typeStartToken(), "std :: %type% < %type% *| *| >")) {
@ -1247,6 +1277,8 @@ void SymbolDatabase::createSymbolDatabaseSetVariablePointers()
const Variable *membervar = type->classScope->getVariable(membertok->str());
if (membervar) {
membertok->variable(membervar);
if (membertok->varId() == 0 || _variableList[membertok->varId()] == nullptr)
fixVarId(varIds, tok, const_cast<Token *>(membertok), membervar);
}
}
}

View File

@ -1178,6 +1178,11 @@ private:
Function *findFunctionInScope(const Token *func, const Scope *ns);
const Type *findVariableTypeInBase(const Scope *scope, const Token *typeTok) const;
typedef std::map<unsigned int, unsigned int> MemberIdMap;
typedef std::map<unsigned int, MemberIdMap> VarIdMap;
void fixVarId(VarIdMap & varIds, const Token * vartok, Token * membertok, const Variable * membervar);
/** Whether iName is a keyword as defined in http://en.cppreference.com/w/c/keyword and http://en.cppreference.com/w/cpp/keyword*/
bool isReservedName(const std::string& iName) const;

View File

@ -44,6 +44,7 @@ class CPPCHECKLIB Tokenizer {
friend class TestSimplifyTokens;
friend class TestSimplifyTypedef;
friend class TestTokenizer;
friend class SymbolDatabase;
public:
Tokenizer();
Tokenizer(const Settings * settings, ErrorLogger *errorLogger);
@ -812,6 +813,14 @@ private:
Token *processFunc(Token *tok2, bool inOperator) const;
/**
* Get new variable id.
* @return new variable id
*/
unsigned int newVarId() {
return ++_varId;
}
/** Set pod types */
void setPodTypes();

View File

@ -817,7 +817,7 @@ private:
tok = tok ? tok->next() : nullptr;
ASSERT(db != nullptr);
ASSERT(tok && tok->variable() && Token::simpleMatch(tok->variable()->typeStartToken(), "int x ;"));
ASSERT(tok && tok->varId() == 0U); // It's possible to set a varId
ASSERT(tok && tok->varId() == 3U); // It's possible to set a varId
}
void arrayMemberVar2() {
@ -833,7 +833,7 @@ private:
tok = tok ? tok->next() : nullptr;
ASSERT(db != nullptr);
ASSERT(tok && tok->variable() && Token::simpleMatch(tok->variable()->typeStartToken(), "int x ;"));
ASSERT(tok && tok->varId() == 0U); // It's possible to set a varId
ASSERT(tok && tok->varId() == 3U); // It's possible to set a varId
}
void arrayMemberVar3() {
@ -849,7 +849,7 @@ private:
tok = tok ? tok->next() : nullptr;
ASSERT(db != nullptr);
ASSERT(tok && tok->variable() && Token::simpleMatch(tok->variable()->typeStartToken(), "int x ;"));
ASSERT(tok && tok->varId() == 0U); // It's possible to set a varId
ASSERT(tok && tok->varId() == 3U); // It's possible to set a varId
}
void staticMemberVar() {
@ -4588,10 +4588,10 @@ private:
vartok = Token::findsimplematch(tokenizer.tokens(), "return");
vartok = Token::findsimplematch(vartok, "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "i");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
@ -4709,31 +4709,31 @@ private:
ASSERT(db && vartok && vartok->variable() && !vartok->variable()->isPointer() && !vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok, "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "b . i");
ASSERT(db && vartok && vartok->variable() && !vartok->variable()->isPointer() && vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok->next(), "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "c . i");
ASSERT(db && vartok && vartok->variable() && !vartok->variable()->isPointer() && vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok->next(), "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "d . i");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->isPointer() && !vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok->next(), "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "e . i");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->isPointer() && !vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok->next(), "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "i");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
@ -4801,31 +4801,31 @@ private:
ASSERT(db && vartok && vartok->variable() && !vartok->variable()->isPointer() && !vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok, "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "b . i");
ASSERT(db && vartok && vartok->variable() && !vartok->variable()->isPointer() && vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok->next(), "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "c . i");
ASSERT(db && vartok && vartok->variable() && !vartok->variable()->isPointer() && vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok->next(), "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "d . i");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->isPointer() && !vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok->next(), "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "e . i");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->isPointer() && !vartok->variable()->isReference() && vartok->variable()->type() && vartok->variable()->type()->name() == "S");
vartok = Token::findsimplematch(vartok->next(), "i");
TODO_ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");
vartok = Token::findsimplematch(vartok->next(), "i");
ASSERT(db && vartok && vartok->variable() && vartok->variable()->typeStartToken()->str() == "int");