Tokenizer; Fixed varid for base class member variable

This commit is contained in:
Daniel Marjamäki 2020-12-08 17:25:50 +01:00
parent 7a4462dac5
commit 245284acde
2 changed files with 56 additions and 1 deletions

View File

@ -3830,7 +3830,22 @@ void Tokenizer::setVarIdPass2()
continue;
}
if (Token::Match(tokStart, "%name% ,|{")) {
const std::map<std::string, int>& baseClassVars = varsByClass[tokStart->str()];
std::string baseClassName = tokStart->str();
std::string scopeName3(scopeName2);
while (!scopeName3.empty()) {
const std::string name = scopeName3 + baseClassName;
if (varsByClass.find(name) != varsByClass.end()) {
baseClassName = name;
break;
}
if (scopeName3.size() < 8)
break;
const std::string::size_type pos = scopeName3.rfind(" :: ", scopeName.size()-5);
if (pos == std::string::npos)
break;
scopeName3.erase(pos + 4);
}
const std::map<std::string, int>& baseClassVars = varsByClass[baseClassName];
thisClassVars.insert(baseClassVars.begin(), baseClassVars.end());
}
tokStart = tokStart->next();

View File

@ -135,6 +135,8 @@ private:
TEST_CASE(varid_namespace_1); // #7272
TEST_CASE(varid_namespace_2); // #7000
TEST_CASE(varid_namespace_3); // #8627
TEST_CASE(varid_namespace_4);
TEST_CASE(varid_namespace_5);
TEST_CASE(varid_initList);
TEST_CASE(varid_initListWithBaseTemplate);
TEST_CASE(varid_initListWithScope);
@ -1970,6 +1972,44 @@ private:
ASSERT_EQUALS("11: type@2 = 0 ;", getLine(actual,11));
}
void varid_namespace_4() {
const char code[] = "namespace X {\n"
" struct foo { int x;};\n"
" struct bar: public foo {\n"
" void dostuff();\n"
" };\n"
" void bar::dostuff() { int x2 = x * 2; }\n"
"}";
ASSERT_EQUALS("1: namespace X {\n"
"2: struct foo { int x@1 ; } ;\n"
"3: struct bar : public foo {\n"
"4: void dostuff ( ) ;\n"
"5: } ;\n"
"6: void bar :: dostuff ( ) { int x2@2 ; x2@2 = x@1 * 2 ; }\n"
"7: }\n", tokenize(code, "test.cpp"));
}
void varid_namespace_5() {
const char code[] = "namespace X {\n"
" struct foo { int x;};\n"
" namespace Y {\n"
" struct bar: public foo {\n"
" void dostuff();\n"
" };\n"
" void bar::dostuff() { int x2 = x * 2; }\n"
" }\n"
"}";
ASSERT_EQUALS("1: namespace X {\n"
"2: struct foo { int x@1 ; } ;\n"
"3: namespace Y {\n"
"4: struct bar : public foo {\n"
"5: void dostuff ( ) ;\n"
"6: } ;\n"
"7: void bar :: dostuff ( ) { int x2@2 ; x2@2 = x@1 * 2 ; }\n"
"8: }\n"
"9: }\n", tokenize(code, "test.cpp"));
}
void varid_initList() {
const char code1[] = "class A {\n"
" A() : x(0) {}\n"