Skip to content

Commit d252583

Browse files
committed
#7251 Remove checks variableHidingTypedef and variableHidingEnum
1 parent 7bd034c commit d252583

5 files changed

Lines changed: 19 additions & 100 deletions

File tree

lib/cppcheck.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,6 @@ void CppCheck::getErrorMessages()
636636
for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it)
637637
(*it)->getErrorMessages(this, &s);
638638

639-
Tokenizer::getErrorMessages(this, &s);
640639
Preprocessor::getErrorMessages(this, &s);
641640
}
642641

lib/tokenize.cpp

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,6 @@ Token *Tokenizer::copyTokens(Token *dest, const Token *first, const Token *last,
204204

205205
//---------------------------------------------------------------------------
206206

207-
void Tokenizer::duplicateTypedefError(const Token *tok1, const Token *tok2, const std::string &type) const
208-
{
209-
if (tok1 && !(_settings->isEnabled("style") && _settings->inconclusive))
210-
return;
211-
212-
std::list<const Token*> locationList;
213-
locationList.push_back(tok1);
214-
locationList.push_back(tok2);
215-
const std::string tok2_str = tok2 ? tok2->str() : std::string("name");
216-
217-
reportError(locationList, Severity::style, "variableHidingTypedef",
218-
std::string("The " + type + " '" + tok2_str + "' hides a typedef with the same name."), true);
219-
}
220-
221207
// check if this statement is a duplicate definition
222208
bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, const std::set<std::string>& structs) const
223209
{
@@ -260,7 +246,6 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
260246
if (!Token::Match(tok->tokAt(-3), ",|<"))
261247
return false;
262248

263-
duplicateTypedefError(*tokPtr, name, "template instantiation");
264249
*tokPtr = end->link();
265250
return true;
266251
}
@@ -271,7 +256,6 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
271256
// look backwards
272257
if (Token::Match(tok->previous(), "%type%") &&
273258
!Token::Match(tok->previous(), "return|new|const|struct")) {
274-
duplicateTypedefError(*tokPtr, name, "function parameter");
275259
// duplicate definition so skip entire function
276260
*tokPtr = end->next()->link();
277261
return true;
@@ -284,7 +268,6 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
284268
while (end && end->str() != "{")
285269
end = end->next();
286270
if (end) {
287-
duplicateTypedefError(*tokPtr, name, "template parameter");
288271
*tokPtr = end->link();
289272
return true;
290273
}
@@ -302,10 +285,8 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
302285
if (tok->previous()->str() == "}") {
303286
tok = tok->previous()->link();
304287
} else if (tok->previous()->str() == "typedef") {
305-
duplicateTypedefError(*tokPtr, name, "typedef");
306288
return true;
307289
} else if (tok->previous()->str() == "enum") {
308-
duplicateTypedefError(*tokPtr, name, "enum");
309290
return true;
310291
} else if (tok->previous()->str() == "struct") {
311292
if (tok->strAt(-2) == "typedef" &&
@@ -315,28 +296,24 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
315296
return true;
316297
} else if (tok->next()->str() == "{") {
317298
if (structs.find(name->strAt(-1)) == structs.end())
318-
duplicateTypedefError(*tokPtr, name, "struct");
319299
return true;
320300
} else if (Token::Match(tok->next(), ")|*")) {
321301
return true;
322302
} else if (tok->next()->str() == name->str()) {
323303
return true;
324304
} else if (tok->next()->str() != ";") {
325-
duplicateTypedefError(*tokPtr, name, "struct");
326305
return true;
327306
} else {
328307
return false;
329308
}
330309
} else if (tok->previous()->str() == "union") {
331310
if (tok->next()->str() != ";") {
332-
duplicateTypedefError(*tokPtr, name, "union");
333311
return true;
334312
} else {
335313
return false;
336314
}
337315
} else if (isCPP() && tok->previous()->str() == "class") {
338316
if (tok->next()->str() != ";") {
339-
duplicateTypedefError(*tokPtr, name, "class");
340317
return true;
341318
} else {
342319
return false;
@@ -347,7 +324,6 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
347324
}
348325

349326
if ((*tokPtr)->strAt(1) != "(" || !Token::Match((*tokPtr)->linkAt(1), ") .|(|[")) {
350-
duplicateTypedefError(*tokPtr, name, "variable");
351327
return true;
352328
}
353329
}
@@ -7224,24 +7200,10 @@ void Tokenizer::simplifyNestedStrcat()
72247200
}
72257201
}
72267202

7227-
void Tokenizer::duplicateEnumError(const Token * tok1, const Token * tok2, const std::string & type) const
7228-
{
7229-
if (tok1 && !(_settings->isEnabled("style")))
7230-
return;
7231-
7232-
std::list<const Token*> locationList;
7233-
locationList.push_back(tok1);
7234-
locationList.push_back(tok2);
7235-
const std::string tok2_str = tok2 ? tok2->str() : std::string("name");
7236-
7237-
reportError(locationList, Severity::style, "variableHidingEnum",
7238-
std::string(type + " '" + tok2_str + "' hides enumerator with same name"));
7239-
}
7240-
72417203
// Check if this statement is a duplicate definition. A duplicate
72427204
// definition will hide the enumerator within it's scope so just
72437205
// skip the entire scope of the duplicate.
7244-
bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const
7206+
bool Tokenizer::duplicateDefinition(Token ** tokPtr) const
72457207
{
72467208
// check for an end of definition
72477209
const Token * tok = *tokPtr;
@@ -7283,7 +7245,6 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const
72837245
(Token::Match(tok->previous(), "%type%") &&
72847246
tok->previous()->str() != "return") ||
72857247
Token::Match(tok->tokAt(-2), "%type% &|*")) {
7286-
duplicateEnumError(*tokPtr, name, "Function parameter");
72877248
// duplicate definition so skip entire function
72887249
*tokPtr = end->next()->link();
72897250
return true;
@@ -7297,22 +7258,19 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr, const Token * name) const
72977258
while (end && end->str() != "{")
72987259
end = end->next();
72997260
if (end) {
7300-
duplicateEnumError(*tokPtr, name, "Template parameter");
73017261
*tokPtr = end->link();
73027262
return true;
73037263
}
73047264
}
73057265
} else {
73067266
if (Token::Match(tok->previous(), "enum|,")) {
7307-
duplicateEnumError(*tokPtr, name, "Variable");
73087267
return true;
73097268
} else if (Token::Match(tok->previous(), "%type%")) {
73107269
// look backwards
73117270
const Token *back = tok;
73127271
while (back && back->isName())
73137272
back = back->previous();
73147273
if (!back || (Token::Match(back, "[(,;{}]") && !Token::Match(back->next(),"return|throw"))) {
7315-
duplicateEnumError(*tokPtr, name, "Variable");
73167274
return true;
73177275
}
73187276
}
@@ -7371,7 +7329,8 @@ class EnumValue {
73717329
}
73727330

73737331
// Simplify calculations..
7374-
while (start && start->previous() && TemplateSimplifier::simplifyNumericCalculations(start->previous())) { }
7332+
while (start && start->previous() && TemplateSimplifier::simplifyNumericCalculations(start->previous()))
7333+
{ }
73757334

73767335
if (Token::Match(start, "%num% [,}]")) {
73777336
value = start;
@@ -7390,7 +7349,6 @@ void Tokenizer::simplifyEnum()
73907349
std::string className;
73917350
int classLevel = 0;
73927351
bool goback = false;
7393-
const bool printStyle = _settings->isEnabled("style");
73947352
for (Token *tok = list.front(); tok; tok = tok->next()) {
73957353

73967354
if (goback) {
@@ -7646,10 +7604,6 @@ void Tokenizer::simplifyEnum()
76467604
if (prev->str() == "(" && (!Token::Match(prev->tokAt(-2), "%type%|::|*|& %type% (") || prev->strAt(-2) == "else"))
76477605
continue;
76487606
shadowVars.insert(arg->str());
7649-
if (inScope && printStyle) {
7650-
const EnumValue& enumValue = enumValues.find(arg->str())->second;
7651-
duplicateEnumError(arg, enumValue.name, "Function argument");
7652-
}
76537607
}
76547608
}
76557609
}
@@ -7666,10 +7620,6 @@ void Tokenizer::simplifyEnum()
76667620
(Token::Match(prev->previous(), "%type% *|&") && (prev->previous()->isStandardType() || prev->strAt(-1) == "const" || Token::Match(prev->tokAt(-2), ";|{|}")))) {
76677621
// variable declaration?
76687622
shadowVars.insert(tok3->str());
7669-
if (inScope && printStyle) {
7670-
const EnumValue& enumValue = enumValues.find(tok3->str())->second;
7671-
duplicateEnumError(tok3, enumValue.name, "Variable");
7672-
}
76737623
}
76747624
}
76757625
}
@@ -7702,7 +7652,7 @@ void Tokenizer::simplifyEnum()
77027652
(shadowId.empty() || shadowId.top().find(tok2->str()) == shadowId.top().end()) && // no shadow enum/var/etc of enum
77037653
enumValues.find(tok2->str()) != enumValues.end()) { // tok2 is a enum id with a known value
77047654
ev = &(enumValues.find(tok2->str())->second);
7705-
if (!duplicateDefinition(&tok2, ev->name)) {
7655+
if (!duplicateDefinition(&tok2)) {
77067656
if (tok2->strAt(-1) == "::" ||
77077657
Token::Match(tok2->next(), "::|[|=")) {
77087658
// Don't replace this enum if:
@@ -8734,14 +8684,7 @@ std::string Tokenizer::simplifyString(const std::string &source)
87348684

87358685
return str;
87368686
}
8737-
8738-
void Tokenizer::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
8739-
{
8740-
Tokenizer t(settings, errorLogger);
8741-
t.duplicateTypedefError(0, 0, "variable");
8742-
t.duplicateEnumError(0, 0, "variable");
8743-
}
8744-
8687+
87458688
void Tokenizer::simplifyWhile0()
87468689
{
87478690
for (Token *tok = list.front(); tok; tok = tok->next()) {

lib/tokenize.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,6 @@ class CPPCHECKLIB Tokenizer {
171171
*/
172172
bool isFunctionParameterPassedByValue(const Token *fpar) const;
173173

174-
/**
175-
* get error messages that the tokenizer generate
176-
*/
177-
static void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings);
178-
179174
/** Simplify assignment in function call "f(x=g());" => "x=g();f(x);"
180175
*/
181176
void simplifyAssignmentInFunctionCall();
@@ -701,21 +696,15 @@ class CPPCHECKLIB Tokenizer {
701696
/**
702697
* check for duplicate enum definition
703698
*/
704-
bool duplicateDefinition(Token **tokPtr, const Token *name) const;
699+
bool duplicateDefinition(Token **tokPtr) const;
705700

706701
/**
707702
* report error message
708703
*/
709704
void reportError(const Token* tok, const Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const;
710705
void reportError(const std::list<const Token*>& callstack, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const;
711706

712-
/**
713-
* duplicate enum definition error
714-
*/
715-
void duplicateEnumError(const Token *tok1, const Token *tok2, const std::string & type) const;
716-
717707
bool duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, const std::set<std::string>& structs) const;
718-
void duplicateTypedefError(const Token *tok1, const Token *tok2, const std::string & type) const;
719708

720709
void unsupportedTypedef(const Token *tok) const;
721710

test/testsimplifytokens.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,10 +3030,7 @@ class TestSimplifyTokens : public TestFixture {
30303030
"{\n"
30313031
" EF_Vector<float,6> d;\n"
30323032
"}");
3033-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name\n"
3034-
"[test.cpp:11] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name\n"
3035-
"[test.cpp:16] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name\n"
3036-
"[test.cpp:23] -> [test.cpp:1]: (style) Template parameter 'S' hides enumerator with same name\n", errout.str());
3033+
ASSERT_EQUALS("", errout.str());
30373034
}
30383035

30393036
void enum9() {
@@ -3075,8 +3072,7 @@ class TestSimplifyTokens : public TestFixture {
30753072
"}";
30763073
ASSERT_EQUALS(expected, checkSimplifyEnum(code));
30773074

3078-
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (style) Variable 'u' hides enumerator with same name\n"
3079-
"[test.cpp:4] -> [test.cpp:3]: (style) Variable 'v' hides enumerator with same name\n", errout.str());
3075+
ASSERT_EQUALS("", errout.str());
30803076
}
30813077

30823078
void enum12() {
@@ -3218,9 +3214,7 @@ class TestSimplifyTokens : public TestFixture {
32183214
" x+=1;\n"
32193215
"}\n";
32203216
checkSimplifyEnum(code);
3221-
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style) Variable 'x' hides enumerator with same name\n"
3222-
"[test.cpp:6] -> [test.cpp:1]: (style) Function argument 'x' hides enumerator with same name\n",
3223-
errout.str());
3217+
ASSERT_EQUALS("", errout.str());
32243218

32253219
// avoid false positive: in other scope
32263220
const char code2[] = "class C1 { enum en { x = 0 }; };\n"
@@ -3433,7 +3427,7 @@ class TestSimplifyTokens : public TestFixture {
34333427
std::istringstream istr("x ; return a not_eq x;");
34343428
tokenizer.tokenize(istr, "test.c");
34353429
Token *x_token = tokenizer.list.front()->tokAt(5);
3436-
ASSERT_EQUALS(false, tokenizer.duplicateDefinition(&x_token, tokenizer.tokens()));
3430+
ASSERT_EQUALS(false, tokenizer.duplicateDefinition(&x_token));
34373431
}
34383432

34393433
void removestd() {

test/testsimplifytypedef.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,11 +1079,7 @@ class TestSimplifyTypedef : public TestFixture {
10791079
"}";
10801080

10811081
ASSERT_EQUALS(expected, tok(code, false));
1082-
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n"
1083-
"[test.cpp:20] -> [test.cpp:1]: (style, inconclusive) The function parameter 'A' hides a typedef with the same name.\n"
1084-
"[test.cpp:21] -> [test.cpp:1]: (style, inconclusive) The variable 'A' hides a typedef with the same name.\n"
1085-
"[test.cpp:24] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n"
1086-
, errout.str());
1082+
ASSERT_EQUALS("", errout.str());
10871083
}
10881084

10891085
void simplifyTypedef36() {
@@ -1110,8 +1106,7 @@ class TestSimplifyTypedef : public TestFixture {
11101106
"typedef int B;";
11111107

11121108
checkSimplifyTypedef(code);
1113-
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n"
1114-
"[test.cpp:5] -> [test.cpp:3]: (style, inconclusive) The typedef 'B' hides a typedef with the same name.\n", errout.str());
1109+
ASSERT_EQUALS("", errout.str());
11151110
}
11161111

11171112
{
@@ -1150,16 +1145,15 @@ class TestSimplifyTypedef : public TestFixture {
11501145
"template <class A, class B> class C { };";
11511146
const char expected[] = "template < class A , class B > class C { } ;";
11521147
ASSERT_EQUALS(expected, tok(code, false));
1153-
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style, inconclusive) The template parameter 'A' hides a typedef with the same name.\n"
1154-
"[test.cpp:3] -> [test.cpp:2]: (style, inconclusive) The template parameter 'B' hides a typedef with the same name.\n", errout.str());
1148+
ASSERT_EQUALS("", errout.str());
11551149

11561150
checkSimplifyTypedef("typedef tuple<double&, const double&, const double, double*, const double*> t2;\n"
11571151
"void ordering_test()\n"
11581152
"{\n"
11591153
" tuple<short, float> t2(5, 3.3f);\n"
11601154
" BOOST_CHECK(t3 > t2);\n"
11611155
"}");
1162-
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (style, inconclusive) The template instantiation 't2' hides a typedef with the same name.\n", errout.str());
1156+
ASSERT_EQUALS("", errout.str());
11631157

11641158
checkSimplifyTypedef("class MyOverflowingUnsigned\n"
11651159
"{\n"
@@ -1223,7 +1217,7 @@ class TestSimplifyTypedef : public TestFixture {
12231217
"int alloclen ; "
12241218
"} ;";
12251219
ASSERT_EQUALS(expected, tok(code));
1226-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The struct 'A' hides a typedef with the same name.\n", errout.str());
1220+
ASSERT_EQUALS("", errout.str());
12271221
}
12281222

12291223
{
@@ -1239,7 +1233,7 @@ class TestSimplifyTypedef : public TestFixture {
12391233
"int alloclen ; "
12401234
"} ;";
12411235
ASSERT_EQUALS(expected, tok(code));
1242-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The union 'A' hides a typedef with the same name.\n", errout.str());
1236+
ASSERT_EQUALS("", errout.str());
12431237
}
12441238

12451239
{
@@ -1255,7 +1249,7 @@ class TestSimplifyTypedef : public TestFixture {
12551249
"int alloclen ; "
12561250
"} ;";
12571251
ASSERT_EQUALS(expected, tok(code));
1258-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The class 'A' hides a typedef with the same name.\n", errout.str());
1252+
ASSERT_EQUALS("", errout.str());
12591253
}
12601254
}
12611255

@@ -1472,14 +1466,14 @@ class TestSimplifyTypedef : public TestFixture {
14721466
"typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);";
14731467

14741468
checkSimplifyTypedef(code);
1475-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'PPDMarkOption' hides a typedef with the same name.\n", errout.str());
1469+
ASSERT_EQUALS("", errout.str());
14761470
}
14771471

14781472
{
14791473
const char code[] = "typedef int * A;\n"
14801474
"typedef int * A;";
14811475
checkSimplifyTypedef(code);
1482-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n", errout.str());
1476+
ASSERT_EQUALS("", errout.str());
14831477
}
14841478
}
14851479

0 commit comments

Comments
 (0)