Tokenizer::setVarIdNew : enable more testcases. better handling of unions and struct members.

This commit is contained in:
Daniel Marjamäki 2012-04-15 13:42:13 +02:00
parent a420a36606
commit 95dafd30dd
2 changed files with 33 additions and 6 deletions

View File

@ -2815,7 +2815,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
bool hasstruct = false; // Is there a "struct" or "class"?
while (tok2) {
if (tok2->isName()) {
if (tok2->str() == "class" || tok2->str() == "struct") {
if (tok2->str() == "class" || tok2->str() == "struct" || tok2->str() == "union") {
hasstruct = true;
} else if (!hasstruct && variableId.find(tok2->str()) != variableId.end()) {
++typeCount;
@ -2861,6 +2861,7 @@ void Tokenizer::setVarIdNew()
// variable id
_varId = 0;
std::map<std::string, unsigned int> variableId;
std::map<unsigned int, std::map<std::string, unsigned int> > structMembers;
std::stack< std::map<std::string, unsigned int> > scopeInfo;
for (Token *tok = _tokens; tok; tok = tok->next()) {
@ -2906,8 +2907,34 @@ void Tokenizer::setVarIdNew()
if (tok->isName()) {
const std::map<std::string, unsigned int>::const_iterator it = variableId.find(tok->str());
if (it != variableId.end())
if (it != variableId.end()) {
tok->varId(it->second);
while (Token::Match(tok->next(), ". %var% !!(")) {
const unsigned int struct_varid = it->second;
tok = tok->tokAt(2);
if (struct_varid == 0)
continue;
std::map<unsigned int, std::map<std::string,unsigned int> >::iterator structIterator;
structIterator = structMembers.find(struct_varid);
if (structIterator == structMembers.end()) {
std::map<std::string,unsigned int> members;
members[tok->str()] = ++_varId;
structMembers[struct_varid] = members;
tok->varId(_varId);
} else {
std::map<std::string,unsigned int> &members = structIterator->second;
std::map<std::string,unsigned int>::const_iterator memberIterator;
memberIterator = members.find(tok->str());
if (memberIterator == members.end()) {
members[tok->str()] = ++_varId;
tok->varId(_varId);
} else {
tok->varId(memberIterator->second);
}
}
}
}
} else if (Token::Match(tok, "::|. %var%")) {
// Don't set varid after a :: or . token
tok = tok->next();

View File

@ -2585,7 +2585,7 @@ private:
" struct ABC abc;\n"
" abc.a = 3;\n"
" i = abc.a;\n"
"}\n");
"}\n", false, "test.c");
const std::string expected("\n\n##file 0\n"
"1: void f ( )\n"
@ -3083,7 +3083,7 @@ private:
"3: EventPtr event@3 ; event@3 = * eventP@1 ;\n"
"4: * actionsP@2 = & event@3 . actions@4 ;\n"
"5: }\n");
ASSERT_EQUALS(expected1, tokenizeDebugListing(code1));
ASSERT_EQUALS(expected1, tokenizeDebugListing(code1, false, "test.c"));
const std::string code2("void f(int b, int c) {\n"
" x(a*b*c,10);\n"
@ -3228,12 +3228,12 @@ private:
const std::string code1("union evt; void f(const evt & event);");
ASSERT_EQUALS("\n\n##file 0\n"
"1: union evt ; void f ( const evt & event@1 ) ;\n",
tokenizeDebugListing(code1));
tokenizeDebugListing(code1, false, "test.c"));
const std::string code2("struct evt; void f(const evt & event);");
ASSERT_EQUALS("\n\n##file 0\n"
"1: struct evt ; void f ( const evt & event@1 ) ;\n",
tokenizeDebugListing(code2));
tokenizeDebugListing(code2, false, "test.c"));
}
void varid42() {