#7251 Remove checks variableHidingTypedef and variableHidingEnum

This commit is contained in:
amai2012 2016-02-06 15:37:58 +01:00
parent 7bd034c009
commit d25258359a
5 changed files with 19 additions and 100 deletions

View File

@ -636,7 +636,6 @@ void CppCheck::getErrorMessages()
for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it)
(*it)->getErrorMessages(this, &s);
Tokenizer::getErrorMessages(this, &s);
Preprocessor::getErrorMessages(this, &s);
}

View File

@ -204,20 +204,6 @@ Token *Tokenizer::copyTokens(Token *dest, const Token *first, const Token *last,
//---------------------------------------------------------------------------
void Tokenizer::duplicateTypedefError(const Token *tok1, const Token *tok2, const std::string &type) const
{
if (tok1 && !(_settings->isEnabled("style") && _settings->inconclusive))
return;
std::list<const Token*> locationList;
locationList.push_back(tok1);
locationList.push_back(tok2);
const std::string tok2_str = tok2 ? tok2->str() : std::string("name");
reportError(locationList, Severity::style, "variableHidingTypedef",
std::string("The " + type + " '" + tok2_str + "' hides a typedef with the same name."), true);
}
// check if this statement is a duplicate definition
bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, const std::set<std::string>& structs) const
{
@ -260,7 +246,6 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
if (!Token::Match(tok->tokAt(-3), ",|<"))
return false;
duplicateTypedefError(*tokPtr, name, "template instantiation");
*tokPtr = end->link();
return true;
}
@ -271,7 +256,6 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
// look backwards
if (Token::Match(tok->previous(), "%type%") &&
!Token::Match(tok->previous(), "return|new|const|struct")) {
duplicateTypedefError(*tokPtr, name, "function parameter");
// duplicate definition so skip entire function
*tokPtr = end->next()->link();
return true;
@ -284,7 +268,6 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
while (end && end->str() != "{")
end = end->next();
if (end) {
duplicateTypedefError(*tokPtr, name, "template parameter");
*tokPtr = end->link();
return true;
}
@ -302,10 +285,8 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
if (tok->previous()->str() == "}") {
tok = tok->previous()->link();
} else if (tok->previous()->str() == "typedef") {
duplicateTypedefError(*tokPtr, name, "typedef");
return true;
} else if (tok->previous()->str() == "enum") {
duplicateTypedefError(*tokPtr, name, "enum");
return true;
} else if (tok->previous()->str() == "struct") {
if (tok->strAt(-2) == "typedef" &&
@ -315,28 +296,24 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
return true;
} else if (tok->next()->str() == "{") {
if (structs.find(name->strAt(-1)) == structs.end())
duplicateTypedefError(*tokPtr, name, "struct");
return true;
} else if (Token::Match(tok->next(), ")|*")) {
return true;
} else if (tok->next()->str() == name->str()) {
return true;
} else if (tok->next()->str() != ";") {
duplicateTypedefError(*tokPtr, name, "struct");
return true;
} else {
return false;
}
} else if (tok->previous()->str() == "union") {
if (tok->next()->str() != ";") {
duplicateTypedefError(*tokPtr, name, "union");
return true;
} else {
return false;
}
} else if (isCPP() && tok->previous()->str() == "class") {
if (tok->next()->str() != ";") {
duplicateTypedefError(*tokPtr, name, "class");
return true;
} else {
return false;
@ -347,7 +324,6 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
}
if ((*tokPtr)->strAt(1) != "(" || !Token::Match((*tokPtr)->linkAt(1), ") .|(|[")) {
duplicateTypedefError(*tokPtr, name, "variable");
return true;
}
}
@ -7224,24 +7200,10 @@ void Tokenizer::simplifyNestedStrcat()
}
}
void Tokenizer::duplicateEnumError(const Token * tok1, const Token * tok2, const std::string & type) const
{
if (tok1 && !(_settings->isEnabled("style")))
return;
std::list<const Token*> locationList;
locationList.push_back(tok1);
locationList.push_back(tok2);
const std::string tok2_str = tok2 ? tok2->str() : std::string("name");
reportError(locationList, Severity::style, "variableHidingEnum",
std::string(type + " '" + tok2_str + "' hides enumerator with same name"));
}
// Check if this statement is a duplicate definition. A duplicate
// definition will hide the enumerator within it's scope so just
// skip the entire scope of the duplicate.
bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const
bool Tokenizer::duplicateDefinition(Token ** tokPtr) const
{
// check for an end of definition
const Token * tok = *tokPtr;
@ -7283,7 +7245,6 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const
(Token::Match(tok->previous(), "%type%") &&
tok->previous()->str() != "return") ||
Token::Match(tok->tokAt(-2), "%type% &|*")) {
duplicateEnumError(*tokPtr, name, "Function parameter");
// duplicate definition so skip entire function
*tokPtr = end->next()->link();
return true;
@ -7297,14 +7258,12 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const
while (end && end->str() != "{")
end = end->next();
if (end) {
duplicateEnumError(*tokPtr, name, "Template parameter");
*tokPtr = end->link();
return true;
}
}
} else {
if (Token::Match(tok->previous(), "enum|,")) {
duplicateEnumError(*tokPtr, name, "Variable");
return true;
} else if (Token::Match(tok->previous(), "%type%")) {
// look backwards
@ -7312,7 +7271,6 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const
while (back && back->isName())
back = back->previous();
if (!back || (Token::Match(back, "[(,;{}]") && !Token::Match(back->next(),"return|throw"))) {
duplicateEnumError(*tokPtr, name, "Variable");
return true;
}
}
@ -7371,7 +7329,8 @@ public:
}
// Simplify calculations..
while (start && start->previous() && TemplateSimplifier::simplifyNumericCalculations(start->previous())) { }
while (start && start->previous() && TemplateSimplifier::simplifyNumericCalculations(start->previous()))
{ }
if (Token::Match(start, "%num% [,}]")) {
value = start;
@ -7390,7 +7349,6 @@ void Tokenizer::simplifyEnum()
std::string className;
int classLevel = 0;
bool goback = false;
const bool printStyle = _settings->isEnabled("style");
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (goback) {
@ -7646,10 +7604,6 @@ void Tokenizer::simplifyEnum()
if (prev->str() == "(" && (!Token::Match(prev->tokAt(-2), "%type%|::|*|& %type% (") || prev->strAt(-2) == "else"))
continue;
shadowVars.insert(arg->str());
if (inScope && printStyle) {
const EnumValue& enumValue = enumValues.find(arg->str())->second;
duplicateEnumError(arg, enumValue.name, "Function argument");
}
}
}
}
@ -7666,10 +7620,6 @@ void Tokenizer::simplifyEnum()
(Token::Match(prev->previous(), "%type% *|&") && (prev->previous()->isStandardType() || prev->strAt(-1) == "const" || Token::Match(prev->tokAt(-2), ";|{|}")))) {
// variable declaration?
shadowVars.insert(tok3->str());
if (inScope && printStyle) {
const EnumValue& enumValue = enumValues.find(tok3->str())->second;
duplicateEnumError(tok3, enumValue.name, "Variable");
}
}
}
}
@ -7702,7 +7652,7 @@ void Tokenizer::simplifyEnum()
(shadowId.empty() || shadowId.top().find(tok2->str()) == shadowId.top().end()) && // no shadow enum/var/etc of enum
enumValues.find(tok2->str()) != enumValues.end()) { // tok2 is a enum id with a known value
ev = &(enumValues.find(tok2->str())->second);
if (!duplicateDefinition(&tok2, ev->name)) {
if (!duplicateDefinition(&tok2)) {
if (tok2->strAt(-1) == "::" ||
Token::Match(tok2->next(), "::|[|=")) {
// Don't replace this enum if:
@ -8734,14 +8684,7 @@ std::string Tokenizer::simplifyString(const std::string &source)
return str;
}
void Tokenizer::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
Tokenizer t(settings, errorLogger);
t.duplicateTypedefError(0, 0, "variable");
t.duplicateEnumError(0, 0, "variable");
}
void Tokenizer::simplifyWhile0()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {

View File

@ -171,11 +171,6 @@ public:
*/
bool isFunctionParameterPassedByValue(const Token *fpar) const;
/**
* get error messages that the tokenizer generate
*/
static void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings);
/** Simplify assignment in function call "f(x=g());" => "x=g();f(x);"
*/
void simplifyAssignmentInFunctionCall();
@ -701,7 +696,7 @@ private:
/**
* check for duplicate enum definition
*/
bool duplicateDefinition(Token **tokPtr, const Token *name) const;
bool duplicateDefinition(Token **tokPtr) const;
/**
* report error message
@ -709,13 +704,7 @@ private:
void reportError(const Token* tok, const Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const;
void reportError(const std::list<const Token*>& callstack, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const;
/**
* duplicate enum definition error
*/
void duplicateEnumError(const Token *tok1, const Token *tok2, const std::string & type) const;
bool duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, const std::set<std::string>& structs) const;
void duplicateTypedefError(const Token *tok1, const Token *tok2, const std::string & type) const;
void unsupportedTypedef(const Token *tok) const;

View File

@ -3030,10 +3030,7 @@ private:
"{\n"
" EF_Vector<float,6> d;\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name\n"
"[test.cpp:11] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name\n"
"[test.cpp:16] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name\n"
"[test.cpp:23] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
void enum9() {
@ -3075,8 +3072,7 @@ private:
"}";
ASSERT_EQUALS(expected, checkSimplifyEnum(code));
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (style) Variable 'u' hides enumerator with same name\n"
"[test.cpp:4] -> [test.cpp:3]: (style) Variable 'v' hides enumerator with same name\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
void enum12() {
@ -3218,9 +3214,7 @@ private:
" x+=1;\n"
"}\n";
checkSimplifyEnum(code);
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style) Variable 'x' hides enumerator with same name\n"
"[test.cpp:6] -> [test.cpp:1]: (style) Function argument 'x' hides enumerator with same name\n",
errout.str());
ASSERT_EQUALS("", errout.str());
// avoid false positive: in other scope
const char code2[] = "class C1 { enum en { x = 0 }; };\n"
@ -3433,7 +3427,7 @@ private:
std::istringstream istr("x ; return a not_eq x;");
tokenizer.tokenize(istr, "test.c");
Token *x_token = tokenizer.list.front()->tokAt(5);
ASSERT_EQUALS(false, tokenizer.duplicateDefinition(&x_token, tokenizer.tokens()));
ASSERT_EQUALS(false, tokenizer.duplicateDefinition(&x_token));
}
void removestd() {

View File

@ -1079,11 +1079,7 @@ private:
"}";
ASSERT_EQUALS(expected, tok(code, false));
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n"
"[test.cpp:20] -> [test.cpp:1]: (style, inconclusive) The function parameter 'A' hides a typedef with the same name.\n"
"[test.cpp:21] -> [test.cpp:1]: (style, inconclusive) The variable 'A' hides a typedef with the same name.\n"
"[test.cpp:24] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n"
, errout.str());
ASSERT_EQUALS("", errout.str());
}
void simplifyTypedef36() {
@ -1110,8 +1106,7 @@ private:
"typedef int B;";
checkSimplifyTypedef(code);
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n"
"[test.cpp:5] -> [test.cpp:3]: (style, inconclusive) The typedef 'B' hides a typedef with the same name.\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
{
@ -1150,8 +1145,7 @@ private:
"template <class A, class B> class C { };";
const char expected[] = "template < class A , class B > class C { } ;";
ASSERT_EQUALS(expected, tok(code, false));
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style, inconclusive) The template parameter 'A' hides a typedef with the same name.\n"
"[test.cpp:3] -> [test.cpp:2]: (style, inconclusive) The template parameter 'B' hides a typedef with the same name.\n", errout.str());
ASSERT_EQUALS("", errout.str());
checkSimplifyTypedef("typedef tuple<double&, const double&, const double, double*, const double*> t2;\n"
"void ordering_test()\n"
@ -1159,7 +1153,7 @@ private:
" tuple<short, float> t2(5, 3.3f);\n"
" BOOST_CHECK(t3 > t2);\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (style, inconclusive) The template instantiation 't2' hides a typedef with the same name.\n", errout.str());
ASSERT_EQUALS("", errout.str());
checkSimplifyTypedef("class MyOverflowingUnsigned\n"
"{\n"
@ -1223,7 +1217,7 @@ private:
"int alloclen ; "
"} ;";
ASSERT_EQUALS(expected, tok(code));
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The struct 'A' hides a typedef with the same name.\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
{
@ -1239,7 +1233,7 @@ private:
"int alloclen ; "
"} ;";
ASSERT_EQUALS(expected, tok(code));
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The union 'A' hides a typedef with the same name.\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
{
@ -1255,7 +1249,7 @@ private:
"int alloclen ; "
"} ;";
ASSERT_EQUALS(expected, tok(code));
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The class 'A' hides a typedef with the same name.\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
}
@ -1472,14 +1466,14 @@ private:
"typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);";
checkSimplifyTypedef(code);
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'PPDMarkOption' hides a typedef with the same name.\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
{
const char code[] = "typedef int * A;\n"
"typedef int * A;";
checkSimplifyTypedef(code);
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
}