From 1286898fa059ff78c69f13f0ea4eea903409f10a Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Tue, 28 Jun 2011 21:46:54 -0400 Subject: [PATCH] fix #2864 (--errorlist missing errors: variableHidingTypedef and Extra qualification) --- lib/tokenize.cpp | 119 ++++++++++++++++++++++++++++++----------------- lib/tokenize.h | 5 ++ 2 files changed, 81 insertions(+), 43 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 218f22561..e52111471 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -486,21 +486,28 @@ void Tokenizer::createTokens(std::istream &code) void Tokenizer::duplicateTypedefError(const Token *tok1, const Token *tok2, const std::string &type) { - if (!(_settings->_checkCodingStyle)) + if (tok1 && !(_settings->_checkCodingStyle)) return; std::list locationList; - ErrorLogger::ErrorMessage::FileLocation loc; - loc.line = tok1->linenr(); - loc.setfile(file(tok1)); - locationList.push_back(loc); - loc.line = tok2->linenr(); - loc.setfile(file(tok2)); - locationList.push_back(loc); + std::string tok2_str; + if (tok1 && tok2) + { + ErrorLogger::ErrorMessage::FileLocation loc; + loc.line = tok1->linenr(); + loc.setfile(file(tok1)); + locationList.push_back(loc); + loc.line = tok2->linenr(); + loc.setfile(file(tok2)); + locationList.push_back(loc); + tok2_str = tok2->str(); + } + else + tok2_str = "name"; const ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, - std::string(type + " '" + tok2->str() + + std::string(type + " '" + tok2_str + "' hides typedef with same name"), "variableHidingTypedef", false); @@ -513,21 +520,28 @@ void Tokenizer::duplicateTypedefError(const Token *tok1, const Token *tok2, cons void Tokenizer::duplicateDeclarationError(const Token *tok1, const Token *tok2, const std::string &type) { - if (!(_settings->_checkCodingStyle)) + if (tok1 && !(_settings->_checkCodingStyle)) return; std::list locationList; - ErrorLogger::ErrorMessage::FileLocation loc; - loc.line = tok1->linenr(); - loc.setfile(file(tok1)); - locationList.push_back(loc); - loc.line = tok2->linenr(); - loc.setfile(file(tok2)); - locationList.push_back(loc); + std::string tok2_str; + if (tok1 && tok2) + { + ErrorLogger::ErrorMessage::FileLocation loc; + loc.line = tok1->linenr(); + loc.setfile(file(tok1)); + locationList.push_back(loc); + loc.line = tok2->linenr(); + loc.setfile(file(tok2)); + locationList.push_back(loc); + tok2_str = tok2->str(); + } + else + tok2_str = "name"; const ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, - std::string(type + " '" + tok2->str() + + std::string(type + " '" + tok2_str + "' forward declaration unnecessary, already declared"), "unnecessaryForwardDeclaration", false); @@ -7878,21 +7892,28 @@ void Tokenizer::simplifyNestedStrcat() void Tokenizer::duplicateEnumError(const Token * tok1, const Token * tok2, const std::string & type) { - if (!(_settings->_checkCodingStyle)) + if (tok1 && !(_settings->_checkCodingStyle)) return; std::list locationList; - ErrorLogger::ErrorMessage::FileLocation loc; - loc.line = tok1->linenr(); - loc.setfile(file(tok1)); - locationList.push_back(loc); - loc.line = tok2->linenr(); - loc.setfile(file(tok2)); - locationList.push_back(loc); + std::string tok2_str; + if (tok1 && tok2) + { + ErrorLogger::ErrorMessage::FileLocation loc; + loc.line = tok1->linenr(); + loc.setfile(file(tok1)); + locationList.push_back(loc); + loc.line = tok2->linenr(); + loc.setfile(file(tok2)); + locationList.push_back(loc); + tok2_str = tok2->str(); + } + else + tok2_str = "name"; const ErrorLogger::ErrorMessage errmsg(locationList, Severity::style, - std::string(type + " '" + tok2->str() + + std::string(type + " '" + tok2_str + "' hides enumerator with same name"), "variableHidingEnum", false); @@ -9083,6 +9104,10 @@ void Tokenizer::getErrorMessages(ErrorLogger *errorLogger, const Settings *setti Tokenizer t(settings, errorLogger); t.syntaxError(0, ' '); t.cppcheckError(0); + t.duplicateTypedefError(0, 0, "Variable"); + t.duplicateDeclarationError(0, 0, "Variable"); + t.duplicateEnumError(0, 0, "Variable"); + t.unnecessaryQualificationError(0, "type"); } /** find pattern */ @@ -9869,22 +9894,7 @@ void Tokenizer::removeUnnecessaryQualification() continue; } - 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 \'" + qualification + "\' unnecessary and considered an error by many compilers.", - "portability", - false); - - if (_errorLogger) - _errorLogger->reportErr(errmsg); - else - Check::reportError(errmsg); + unnecessaryQualificationError(tok, qualification); tok->deleteThis(); tok->deleteThis(); @@ -9893,6 +9903,29 @@ void Tokenizer::removeUnnecessaryQualification() } } +void Tokenizer::unnecessaryQualificationError(const Token *tok, const std::string &qualification) +{ + std::list locationList; + if (tok) + { + 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 \'" + qualification + "\' unnecessary and considered an error by many compilers.", + "unnecessaryQualification", + false); + + if (_errorLogger) + _errorLogger->reportErr(errmsg); + else + Check::reportError(errmsg); +} + void Tokenizer::simplifyReturn() { for (Token *tok = _tokens; tok; tok = tok->next()) diff --git a/lib/tokenize.h b/lib/tokenize.h index 577bbbef4..eb0155947 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -591,6 +591,11 @@ public: */ void removeUnnecessaryQualification(); + /** + * unnecessary member qualification error + */ + void unnecessaryQualificationError(const Token *tok, const std::string &qualification); + /** * Remove Microsoft MFC 'DECLARE_MESSAGE_MAP()' */