From 8eb92001b393c2047a8b0f21529c1f9d25ad996e Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 2 Feb 2011 07:40:08 +0100 Subject: [PATCH] Fixed #2530 (Tokenizer: Remove redundant 'MyClass::' inside MyClass class declaration) --- lib/tokenize.cpp | 60 +++++++++++++++++++++++++++++++++++++ lib/tokenize.h | 5 ++++ test/testsimplifytokens.cpp | 10 +++++++ 3 files changed, 75 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4c57f4016..8cc0b6945 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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; + 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 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(); + } + } + } +} + diff --git a/lib/tokenize.h b/lib/tokenize.h index 5662d7843..e9a280698 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -519,6 +519,11 @@ public: */ void simplifyBuiltinExpect(); + /** + * Remove unnecessary member qualification + */ + void removeUnnecessaryQualification(); + /** * Remove Microsoft MFC 'DECLARE_MESSAGE_MAP()' */ diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index e298bcfd0..9ba7bc197 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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)