Fixed #2530 (Tokenizer: Remove redundant 'MyClass::' inside MyClass class declaration)

This commit is contained in:
Robert Reif 2011-02-02 07:40:08 +01:00 committed by Daniel Marjamäki
parent defeded4b5
commit 8eb92001b3
3 changed files with 75 additions and 0 deletions

View File

@ -2382,6 +2382,9 @@ bool Tokenizer::tokenize(std::istream &code,
// remove __attribute__((?))
simplifyAttribute();
// remove unnecessary member qualification..
removeUnnecessaryQualification();
// remove Microsoft MFC..
simplifyMicrosoftMFC();
@ -9325,3 +9328,60 @@ void Tokenizer::simplifyOperatorName()
}
}
}
// remove unnecessary member qualification..
struct ClassInfo
{
std::string className;
Token *end;
};
void Tokenizer::removeUnnecessaryQualification()
{
std::stack<ClassInfo> classInfo;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "class|struct %type% :|{"))
{
tok = tok->next();
ClassInfo info;
info.className = tok->str();
tok = tok->next();
while (tok && tok->str() != "{")
tok = tok->next();
if (!tok)
return;
info.end = tok->link();
classInfo.push(info);
}
else if (!classInfo.empty())
{
if (tok == classInfo.top().end)
classInfo.pop();
else if (tok->str() == classInfo.top().className &&
Token::Match(tok, "%type% :: %type% (") &&
Token::Match(tok->tokAt(3)->link(), ") const| {|;"))
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr();
loc.setfile(file(tok));
locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::portability,
"Extra qualification \'" + tok->str() + "::\' unnecessary and considered an error by many compilers.",
"portability");
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
tok->deleteThis();
tok->deleteThis();
}
}
}
}

View File

@ -519,6 +519,11 @@ public:
*/
void simplifyBuiltinExpect();
/**
* Remove unnecessary member qualification
*/
void removeUnnecessaryQualification();
/**
* Remove Microsoft MFC 'DECLARE_MESSAGE_MAP()'
*/

View File

@ -315,6 +315,8 @@ private:
TEST_CASE(redundant_semicolon);
TEST_CASE(simplifyFunctionReturn);
TEST_CASE(removeUnnecessaryQualification);
}
std::string tok(const char code[], bool simplify = true)
@ -6408,6 +6410,14 @@ private:
"} ;";
ASSERT_EQUALS(expected, tok(code, false));
}
void removeUnnecessaryQualification()
{
const char code[] = "class Fred { Fred::Fred() {} };";
const char expected[] = "class Fred { Fred ( ) { } } ;";
ASSERT_EQUALS(expected, tok(code, false));
ASSERT_EQUALS("[test.cpp:1]: (portability) Extra qualification 'Fred::' unnecessary and considered an error by many compilers.\n", errout.str());
}
};
REGISTER_TEST(TestSimplifyTokens)