add operator support to ertra qualification check
This commit is contained in:
parent
ac070b90f2
commit
a685f1f5b7
|
@ -10748,32 +10748,54 @@ void Tokenizer::removeUnnecessaryQualification()
|
||||||
if (tok == classInfo.back().classEnd)
|
if (tok == classInfo.back().classEnd)
|
||||||
classInfo.pop_back();
|
classInfo.pop_back();
|
||||||
else if (tok->str() == classInfo.back().className &&
|
else if (tok->str() == classInfo.back().className &&
|
||||||
Token::Match(tok, "%type% :: %type% (") &&
|
!classInfo.back().isNamespace && tok->previous()->str() != ":" &&
|
||||||
Token::Match(tok->tokAt(3)->link(), ") const| {|;|:") &&
|
(Token::Match(tok, "%type% :: %type% (") ||
|
||||||
tok->previous()->str() != ":" && !classInfo.back().isNamespace)
|
Token::Match(tok, "%type% :: operator")))
|
||||||
{
|
{
|
||||||
std::string qualification = tok->str() + "::";
|
int offset = 3;
|
||||||
|
if (tok->strAt(2) == "operator")
|
||||||
// check for extra qualification
|
|
||||||
/** @todo this should be made more generic to handle more levels */
|
|
||||||
if (Token::Match(tok->tokAt(-2), "%type% ::"))
|
|
||||||
{
|
{
|
||||||
if (classInfo.size() >= 2)
|
const Token *tok1 = tok->tokAt(offset);
|
||||||
|
|
||||||
|
// check for operator ()
|
||||||
|
if (tok1->str() == "(")
|
||||||
{
|
{
|
||||||
if (classInfo.at(classInfo.size() - 2).className != tok->strAt(-2))
|
tok1 = tok1->next();
|
||||||
continue;
|
offset++;
|
||||||
else
|
}
|
||||||
qualification = tok->strAt(-2) + "::" + qualification;
|
|
||||||
|
while (tok1 && tok1->str() != "(")
|
||||||
|
{
|
||||||
|
tok1 = tok1->next();
|
||||||
|
offset++;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_settings && _settings->isEnabled("portability"))
|
if (Token::Match(tok->tokAt(offset)->link(), ") const| {|;|:"))
|
||||||
unnecessaryQualificationError(tok, qualification);
|
{
|
||||||
|
std::string qualification = tok->str() + "::";
|
||||||
|
|
||||||
tok->deleteThis();
|
// check for extra qualification
|
||||||
tok->deleteThis();
|
/** @todo this should be made more generic to handle more levels */
|
||||||
|
if (Token::Match(tok->tokAt(-2), "%type% ::"))
|
||||||
|
{
|
||||||
|
if (classInfo.size() >= 2)
|
||||||
|
{
|
||||||
|
if (classInfo.at(classInfo.size() - 2).className != tok->strAt(-2))
|
||||||
|
continue;
|
||||||
|
else
|
||||||
|
qualification = tok->strAt(-2) + "::" + qualification;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_settings && _settings->isEnabled("portability"))
|
||||||
|
unnecessaryQualificationError(tok, qualification);
|
||||||
|
|
||||||
|
tok->deleteThis();
|
||||||
|
tok->deleteThis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -371,6 +371,7 @@ private:
|
||||||
TEST_CASE(removeUnnecessaryQualification5);
|
TEST_CASE(removeUnnecessaryQualification5);
|
||||||
TEST_CASE(removeUnnecessaryQualification6); // ticket #2859
|
TEST_CASE(removeUnnecessaryQualification6); // ticket #2859
|
||||||
TEST_CASE(removeUnnecessaryQualification7); // ticket #2970
|
TEST_CASE(removeUnnecessaryQualification7); // ticket #2970
|
||||||
|
TEST_CASE(removeUnnecessaryQualification8);
|
||||||
|
|
||||||
TEST_CASE(simplifyIfNotNull);
|
TEST_CASE(simplifyIfNotNull);
|
||||||
TEST_CASE(simplifyVarDecl1); // ticket # 2682 segmentation fault
|
TEST_CASE(simplifyVarDecl1); // ticket # 2682 segmentation fault
|
||||||
|
@ -7341,11 +7342,25 @@ private:
|
||||||
" TProcedure::TProcedure(long endAddress) : m_lEndAddr(endAddress){}\n"
|
" TProcedure::TProcedure(long endAddress) : m_lEndAddr(endAddress){}\n"
|
||||||
"private:\n"
|
"private:\n"
|
||||||
" long m_lEndAddr;\n"
|
" long m_lEndAddr;\n"
|
||||||
"}\n";
|
"};\n";
|
||||||
tok(code, false);
|
tok(code, false);
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (portability) Extra qualification 'TProcedure::' unnecessary and considered an error by many compilers.\n", errout.str());
|
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()
|
void simplifyIfNotNull()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue