Support namespaces and static member variables in setVarId (only one depth) (#6061)

This commit is contained in:
PKEuS 2014-08-18 10:24:29 +02:00
parent 0bf7b03f96
commit 5c54f8d0d8
2 changed files with 33 additions and 8 deletions

View File

@ -2331,7 +2331,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
static void setVarIdStructMembers(Token **tok1,
std::map<unsigned int, std::map<std::string,unsigned int> > *structMembers,
std::map<unsigned int, std::map<std::string, unsigned int> > *structMembers,
unsigned int *_varId)
{
Token *tok = *tok1;
@ -2345,7 +2345,7 @@ static void setVarIdStructMembers(Token **tok1,
if (TemplateSimplifier::templateParameters(tok->next()) > 0)
break;
std::map<std::string,unsigned int>& members = (*structMembers)[struct_varid];
std::map<std::string, unsigned int>& members = (*structMembers)[struct_varid];
if (members.empty() || members.find(tok->str()) == members.end()) {
members[tok->str()] = ++(*_varId);
tok->varId(*_varId);
@ -2422,7 +2422,7 @@ static 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,
std::map<unsigned int, std::map<std::string, unsigned int> > *structMembers,
unsigned int *_varId)
{
for (Token *tok2 = startToken; tok2 && tok2 != endToken; tok2 = tok2->next()) {
@ -2644,7 +2644,7 @@ void Tokenizer::setVarId()
if (Token::Match(tok2, "%var% :: %var%")) {
if (tok2->strAt(3) == "(")
allMemberFunctions.push_back(tok2);
else if (tok2->tokAt(2)->varId() != 0)
else if (tok2->strAt(3) != "::" && tok2->strAt(-1) != "::") // Support only one depth
allMemberVars.push_back(tok2);
}
}
@ -2652,9 +2652,11 @@ void Tokenizer::setVarId()
// class members..
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "class|struct %var% {|:")) {
if (Token::Match(tok, "namespace|class|struct %var% {|:")) {
const std::string &classname(tok->next()->str());
bool namesp = tok->str() == "namespace";
// What member variables are there in this class?
std::map<std::string, unsigned int> varlist;
const Token* tokStart = Token::findsimplematch(tok, "{");
@ -2690,6 +2692,9 @@ void Tokenizer::setVarId()
tok2->varId(varlist[tok2->str()]);
}
if (namesp)
continue;
// Set variable ids in member functions for this class..
const std::string funcpattern(classname + " :: %var% (");
for (std::list<Token *>::iterator func = allMemberFunctions.begin(); func != allMemberFunctions.end(); ++func) {

View File

@ -334,7 +334,9 @@ private:
TEST_CASE(varidclass14);
TEST_CASE(varidclass15); // initializer list
TEST_CASE(varidclass16); // #4577
TEST_CASE(varid_classnameshaddowsvariablename) // #3990
TEST_CASE(varid_classnameshaddowsvariablename); // #3990
TEST_CASE(varidnamespace1);
TEST_CASE(file1);
TEST_CASE(file2);
@ -5072,8 +5074,7 @@ private:
"10: A :: buf@1 [ 10 ] = 0 ;\n"
"11: }\n");
const char current[] = "\n\n##file 0\n1: class A\n2: {\n3: public:\n4: static char buf@1 [ 20 ] ;\n5: } ;\n6: char A :: buf [ 20 ] ;\n7: int main ( )\n8: {\n9: char buf@2 [ 2 ] ;\n10: A :: buf [ 10 ] = 0 ;\n11: }\n";
TODO_ASSERT_EQUALS(wanted, current, actual);
ASSERT_EQUALS(wanted, actual);
}
void varidclass7() {
@ -5302,6 +5303,25 @@ private:
}
void varidnamespace1() {
const char code[] = "namespace A {\n"
" char buf[20];\n"
"}\n"
"int main() {\n"
" return foo(A::buf);\n"
"}";
const char expected[] = "\n\n##file 0\n"
"1: namespace A {\n"
"2: char buf@1 [ 20 ] ;\n"
"3: }\n"
"4: int main ( ) {\n"
"5: return foo ( A :: buf@1 ) ;\n"
"6: }\n";
ASSERT_EQUALS(expected, tokenizeDebugListing(code));
}
void file1() {
const char code[] = "a1\n"
"#file \"b\"\n"