add operator support to ertra qualification check

This commit is contained in:
Robert Reif 2011-09-23 19:45:19 -04:00
parent ac070b90f2
commit a685f1f5b7
2 changed files with 57 additions and 20 deletions

View File

@ -10748,9 +10748,30 @@ void Tokenizer::removeUnnecessaryQualification()
if (tok == classInfo.back().classEnd)
classInfo.pop_back();
else if (tok->str() == classInfo.back().className &&
Token::Match(tok, "%type% :: %type% (") &&
Token::Match(tok->tokAt(3)->link(), ") const| {|;|:") &&
tok->previous()->str() != ":" && !classInfo.back().isNamespace)
!classInfo.back().isNamespace && tok->previous()->str() != ":" &&
(Token::Match(tok, "%type% :: %type% (") ||
Token::Match(tok, "%type% :: operator")))
{
int offset = 3;
if (tok->strAt(2) == "operator")
{
const Token *tok1 = tok->tokAt(offset);
// check for operator ()
if (tok1->str() == "(")
{
tok1 = tok1->next();
offset++;
}
while (tok1 && tok1->str() != "(")
{
tok1 = tok1->next();
offset++;
}
}
if (Token::Match(tok->tokAt(offset)->link(), ") const| {|;|:"))
{
std::string qualification = tok->str() + "::";
@ -10777,6 +10798,7 @@ void Tokenizer::removeUnnecessaryQualification()
}
}
}
}
}
void Tokenizer::unnecessaryQualificationError(const Token *tok, const std::string &qualification)

View File

@ -371,6 +371,7 @@ private:
TEST_CASE(removeUnnecessaryQualification5);
TEST_CASE(removeUnnecessaryQualification6); // ticket #2859
TEST_CASE(removeUnnecessaryQualification7); // ticket #2970
TEST_CASE(removeUnnecessaryQualification8);
TEST_CASE(simplifyIfNotNull);
TEST_CASE(simplifyVarDecl1); // ticket # 2682 segmentation fault
@ -7341,11 +7342,25 @@ private:
" TProcedure::TProcedure(long endAddress) : m_lEndAddr(endAddress){}\n"
"private:\n"
" long m_lEndAddr;\n"
"}\n";
"};\n";
tok(code, false);
ASSERT_EQUALS("[test.cpp:3]: (portability) Extra qualification 'TProcedure::' unnecessary and considered an error by many compilers.\n", errout.str());
}
void removeUnnecessaryQualification8()
{
const char code[] = "class Fred {\n"
"public:\n"
" Fred & Fred::operator = (const Fred &);\n"
" void Fred::operator () (void);\n"
" void Fred::operator delete[](void* x);\n"
"};\n";
tok(code, false);
ASSERT_EQUALS("[test.cpp:3]: (portability) Extra qualification 'Fred::' unnecessary and considered an error by many compilers.\n"
"[test.cpp:4]: (portability) Extra qualification 'Fred::' unnecessary and considered an error by many compilers.\n"
"[test.cpp:5]: (portability) Extra qualification 'Fred::' unnecessary and considered an error by many compilers.\n", errout.str());
}
void simplifyIfNotNull()
{
{