Fix #10872 FP constStatement with variable called std (#3906)

This commit is contained in:
chrchr-github 2022-03-16 15:29:34 +01:00 committed by GitHub
parent b8ba0ae00e
commit fb1170b10b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 0 deletions

View File

@ -2082,6 +2082,8 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const
for (const Variable& var : scope->varlist) { for (const Variable& var : scope->varlist) {
if (var.name() == tok->str()) { if (var.name() == tok->str()) {
if (Token::Match(tok, "%name% ::"))
continue;
const Token* fqTok = tok; const Token* fqTok = tok;
while (Token::Match(fqTok->tokAt(-2), "%name% ::")) while (Token::Match(fqTok->tokAt(-2), "%name% ::"))
fqTok = fqTok->tokAt(-2); fqTok = fqTok->tokAt(-2);

View File

@ -3745,6 +3745,8 @@ void Tokenizer::setVarIdClassFunction(const std::string &classname,
continue; continue;
if (Token::Match(tok2->tokAt(-2), "!!this .") && !Token::simpleMatch(tok2->tokAt(-5), "( * this ) .")) if (Token::Match(tok2->tokAt(-2), "!!this .") && !Token::simpleMatch(tok2->tokAt(-5), "( * this ) ."))
continue; continue;
if (Token::Match(tok2, "%name% ::"))
continue;
const std::map<std::string,int>::const_iterator it = varlist.find(tok2->str()); const std::map<std::string,int>::const_iterator it = varlist.find(tok2->str());
if (it != varlist.end()) { if (it != varlist.end()) {

View File

@ -222,6 +222,7 @@ private:
TEST_CASE(constPtrToConstPtr); TEST_CASE(constPtrToConstPtr);
TEST_CASE(constTrailingReturnType); TEST_CASE(constTrailingReturnType);
TEST_CASE(staticArrayPtrOverload); TEST_CASE(staticArrayPtrOverload);
TEST_CASE(qualifiedNameMember); // #10872
TEST_CASE(initializerListOrder); TEST_CASE(initializerListOrder);
TEST_CASE(initializerListUsage); TEST_CASE(initializerListUsage);
@ -6823,6 +6824,22 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void qualifiedNameMember() { // #10872
Settings s;
s.severity.enable(Severity::style);
s.debugwarnings = true;
LOAD_LIB_2(s.library, "std.cfg");
checkConst("struct data {};\n"
" struct S {\n"
" std::vector<data> std;\n"
" void f();\n"
"};\n"
"void S::f() {\n"
" std::vector<data>::const_iterator end = std.end();\n"
"}\n", &s);
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:4]: (style, inconclusive) Technically the member function 'S::f' can be const.\n", errout.str());
}
#define checkInitializerListOrder(code) checkInitializerListOrder_(code, __FILE__, __LINE__) #define checkInitializerListOrder(code) checkInitializerListOrder_(code, __FILE__, __LINE__)
void checkInitializerListOrder_(const char code[], const char* file, int line) { void checkInitializerListOrder_(const char code[], const char* file, int line) {
// Clear the error log // Clear the error log

View File

@ -133,6 +133,7 @@ private:
TEST_CASE(varid_in_class19); TEST_CASE(varid_in_class19);
TEST_CASE(varid_in_class20); // #7267 TEST_CASE(varid_in_class20); // #7267
TEST_CASE(varid_in_class21); // #7788 TEST_CASE(varid_in_class21); // #7788
TEST_CASE(varid_in_class22); // #10872
TEST_CASE(varid_namespace_1); // #7272 TEST_CASE(varid_namespace_1); // #7272
TEST_CASE(varid_namespace_2); // #7000 TEST_CASE(varid_namespace_2); // #7000
TEST_CASE(varid_namespace_3); // #8627 TEST_CASE(varid_namespace_3); // #8627
@ -1910,6 +1911,30 @@ private:
ASSERT_EQUALS(expected, tokenize(code, "test.cpp")); ASSERT_EQUALS(expected, tokenize(code, "test.cpp"));
} }
void varid_in_class22() {
const char code[] = "struct data {};\n"
" struct S {\n"
" std::vector<data> std;\n"
" void f();\n"
"};\n"
"void S::f() {\n"
" std::vector<data>::const_iterator end = std.end();\n"
" for (std::vector<data>::const_iterator i = std.begin(); i != end; ++i) {}\n"
"}\n";
const char expected[] = "1: struct data { } ;\n"
"2: struct S {\n"
"3: std :: vector < data > std@1 ;\n"
"4: void f ( ) ;\n"
"5: } ;\n"
"6: void S :: f ( ) {\n"
"7: std :: vector < data > :: const_iterator end@2 ; end@2 = std@1 . end ( ) ;\n"
"8: for ( std :: vector < data > :: const_iterator i@3 = std@1 . begin ( ) ; i@3 != end@2 ; ++ i@3 ) { }\n"
"9: }\n";
ASSERT_EQUALS(expected, tokenize(code, "test.cpp"));
}
void varid_namespace_1() { // #7272 void varid_namespace_1() { // #7272
const char code[] = "namespace Blah {\n" const char code[] = "namespace Blah {\n"
" struct foo { int x;};\n" " struct foo { int x;};\n"