Skip to content

Commit f8bf2b5

Browse files
committed
Removed rest of variableHidingTypedef and variableHidingEnum checking
1 parent 42278dd commit f8bf2b5

4 files changed

Lines changed: 20 additions & 236 deletions

File tree

lib/tokenize.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Token *Tokenizer::copyTokens(Token *dest, const Token *first, const Token *last,
205205
//---------------------------------------------------------------------------
206206

207207
// check if this statement is a duplicate definition
208-
bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, const std::set<std::string>& structs) const
208+
bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef) const
209209
{
210210
// check for an end of definition
211211
const Token * tok = *tokPtr;
@@ -295,7 +295,6 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
295295
// declaration after forward declaration
296296
return true;
297297
} else if (tok->next()->str() == "{") {
298-
if (structs.find(name->strAt(-1)) == structs.end())
299298
return true;
300299
} else if (Token::Match(tok->next(), ")|*")) {
301300
return true;
@@ -323,9 +322,8 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
323322
tok = tok->previous();
324323
}
325324

326-
if ((*tokPtr)->strAt(1) != "(" || !Token::Match((*tokPtr)->linkAt(1), ") .|(|[")) {
325+
if ((*tokPtr)->strAt(1) != "(" || !Token::Match((*tokPtr)->linkAt(1), ") .|(|["))
327326
return true;
328-
}
329327
}
330328
}
331329
}
@@ -535,13 +533,6 @@ Token *Tokenizer::processFunc(Token *tok2, bool inOperator) const
535533

536534
void Tokenizer::simplifyTypedef()
537535
{
538-
// Collect all structs for later detection of undefined structs
539-
std::set<std::string> structs;
540-
for (const Token* tok = list.front(); tok; tok = tok->next()) {
541-
if (Token::Match(tok, "struct %type% {|:"))
542-
structs.insert(tok->strAt(1));
543-
}
544-
545536
std::vector<Space> spaceInfo;
546537
bool isNamespace = false;
547538
std::string className;
@@ -1163,7 +1154,7 @@ void Tokenizer::simplifyTypedef()
11631154
}
11641155
} else if (Token::Match(tok2->previous(), "case|;|{|} %type% :")) {
11651156
tok2 = tok2->next();
1166-
} else if (duplicateTypedef(&tok2, typeName, typeDef, structs)) {
1157+
} else if (duplicateTypedef(&tok2, typeName, typeDef)) {
11671158
// skip to end of scope if not already there
11681159
if (tok2->str() != "}") {
11691160
while (tok2->next()) {
@@ -7264,16 +7255,15 @@ bool Tokenizer::duplicateDefinition(Token ** tokPtr) const
72647255
}
72657256
}
72667257
} else {
7267-
if (Token::Match(tok->previous(), "enum|,")) {
7258+
if (Token::Match(tok->previous(), "enum|,"))
72687259
return true;
7269-
} else if (Token::Match(tok->previous(), "%type%")) {
7260+
else if (Token::Match(tok->previous(), "%type%")) {
72707261
// look backwards
72717262
const Token *back = tok;
72727263
while (back && back->isName())
72737264
back = back->previous();
7274-
if (!back || (Token::Match(back, "[(,;{}]") && !Token::Match(back->next(),"return|throw"))) {
7265+
if (!back || (Token::Match(back, "[(,;{}]") && !Token::Match(back->next(),"return|throw")))
72757266
return true;
7276-
}
72777267
}
72787268
}
72797269
}
@@ -7330,8 +7320,8 @@ class EnumValue {
73307320
}
73317321

73327322
// Simplify calculations..
7333-
while (start && start->previous() && TemplateSimplifier::simplifyNumericCalculations(start->previous()))
7334-
{ }
7323+
while (start && start->previous() && TemplateSimplifier::simplifyNumericCalculations(start->previous())) {
7324+
}
73357325

73367326
if (Token::Match(start, "%num% [,}]")) {
73377327
value = start;
@@ -7351,7 +7341,6 @@ void Tokenizer::simplifyEnum()
73517341
int classLevel = 0;
73527342
bool goback = false;
73537343
for (Token *tok = list.front(); tok; tok = tok->next()) {
7354-
73557344
if (goback) {
73567345
//jump back once, see the comment at the end of the function
73577346
goback = false;
@@ -8685,7 +8674,7 @@ std::string Tokenizer::simplifyString(const std::string &source)
86858674

86868675
return str;
86878676
}
8688-
8677+
86898678
void Tokenizer::simplifyWhile0()
86908679
{
86918680
for (Token *tok = list.front(); tok; tok = tok->next()) {

lib/tokenize.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ class CPPCHECKLIB Tokenizer {
704704
void reportError(const Token* tok, const Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const;
705705
void reportError(const std::list<const Token*>& callstack, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const;
706706

707-
bool duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, const std::set<std::string>& structs) const;
707+
bool duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef) const;
708708

709709
void unsupportedTypedef(const Token *tok) const;
710710

test/testsimplifytokens.cpp

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ class TestSimplifyTokens : public TestFixture {
168168
TEST_CASE(enum5);
169169
TEST_CASE(enum6);
170170
TEST_CASE(enum7);
171-
TEST_CASE(enum8);
172-
TEST_CASE(enum9); // ticket 1404
173171
TEST_CASE(enum10); // ticket 1445
174172
TEST_CASE(enum11);
175173
TEST_CASE(enum12);
@@ -182,7 +180,6 @@ class TestSimplifyTokens : public TestFixture {
182180
TEST_CASE(enum19); // ticket #2536
183181
TEST_CASE(enum20); // ticket #2600
184182
TEST_CASE(enum21); // ticket #2720
185-
TEST_CASE(enum22); // ticket #2745
186183
TEST_CASE(enum23); // ticket #2804
187184
TEST_CASE(enum24); // ticket #2828
188185
TEST_CASE(enum25); // ticket #2966
@@ -198,8 +195,6 @@ class TestSimplifyTokens : public TestFixture {
198195
TEST_CASE(enum35); // ticket #3953 (avoid simplification of type)
199196
TEST_CASE(enum36); // ticket #4378
200197
TEST_CASE(enum37); // ticket #4280 (shadow variable)
201-
TEST_CASE(enum38); // ticket #4463 (when throwing enum id, don't warn about shadow variable)
202-
TEST_CASE(enum39); // ticket #5145 (fp variable hides enum)
203198
TEST_CASE(enum40);
204199
TEST_CASE(enum41); // ticket #5212 (valgrind errors during enum simplification)
205200
TEST_CASE(enum42); // ticket #5182 (template function call in enum value)
@@ -2995,59 +2990,6 @@ class TestSimplifyTokens : public TestFixture {
29952990
return tokenizer.tokens()->stringifyList(0, true);
29962991
}
29972992

2998-
void enum8() {
2999-
// ticket 1388
3000-
checkSimplifyEnum("enum Direction {N=100,E,S,W,ALL};\n"
3001-
"template<class T,int S> class EF_Vector{\n"
3002-
" T v_v[S];\n"
3003-
"\n"
3004-
"public:\n"
3005-
" EF_Vector();\n"
3006-
" explicit EF_Vector(const T &);\n"
3007-
" explicit EF_Vector(const T arr[S]);\n"
3008-
"};\n"
3009-
"\n"
3010-
"template<class T,int S>\n"
3011-
"EF_Vector<T,S>::EF_Vector()\n"
3012-
"{\n"
3013-
"}\n"
3014-
"\n"
3015-
"template<class T,int S>\n"
3016-
"EF_Vector<T,S>::EF_Vector(const T &t)\n"
3017-
"{\n"
3018-
" for(int i=0;i<S;i++)\n"
3019-
" v_v[i]=t;\n"
3020-
"}\n"
3021-
"\n"
3022-
"template<class T,int S>\n"
3023-
"EF_Vector<T,S>::EF_Vector(const T arr[S])\n"
3024-
"{\n"
3025-
" for(int i=0;i<S;i++)\n"
3026-
" v_v[i]=arr[i];\n"
3027-
"}\n"
3028-
"\n"
3029-
"void initialize()\n"
3030-
"{\n"
3031-
" EF_Vector<float,6> d;\n"
3032-
"}");
3033-
ASSERT_EQUALS("", errout.str());
3034-
}
3035-
3036-
void enum9() {
3037-
// ticket 1404
3038-
checkSimplifyEnum("class XX {\n"
3039-
"public:\n"
3040-
"static void Set(const int &p){m_p=p;}\n"
3041-
"static int m_p;\n"
3042-
"};\n"
3043-
"int XX::m_p=0;\n"
3044-
"int main() {\n"
3045-
" enum { XX };\n"
3046-
" XX::Set(std::numeric_limits<X>::digits());\n"
3047-
"}");
3048-
ASSERT_EQUALS("", errout.str());
3049-
}
3050-
30512993
void enum10() {
30522994
// ticket 1445
30532995
const char code[] = "enum {\n"
@@ -3204,42 +3146,6 @@ class TestSimplifyTokens : public TestFixture {
32043146
ASSERT_EQUALS("", errout.str());
32053147
}
32063148

3207-
void enum22() { // ticket #2745
3208-
const char code[] = "enum en { x = 0 };\n"
3209-
"void f() {\n"
3210-
" int x = 0;\n"
3211-
" g(x);\n"
3212-
"}\n"
3213-
"void f2(int &x) {\n"
3214-
" x+=1;\n"
3215-
"}\n";
3216-
checkSimplifyEnum(code);
3217-
ASSERT_EQUALS("", errout.str());
3218-
3219-
// avoid false positive: in other scope
3220-
const char code2[] = "class C1 { enum en { x = 0 }; };\n"
3221-
"class C2 { bool x; };\n";
3222-
checkSimplifyEnum(code2);
3223-
ASSERT_EQUALS("", errout.str());
3224-
3225-
// avoid false positive: inner if-scope
3226-
const char code3[] = "enum en { x = 0 };\n"
3227-
"void f() { if (aa) ; else if (bb==x) df; }\n";
3228-
checkSimplifyEnum(code3);
3229-
ASSERT_EQUALS("", errout.str());
3230-
3231-
// avoid false positive: Initializer list
3232-
const char code4[] = "struct S {\n"
3233-
" enum { E = 1 };\n"
3234-
" explicit S(float f)\n"
3235-
" : f_(f * E)\n"
3236-
" {}\n"
3237-
" float f_;\n"
3238-
"};";
3239-
checkSimplifyEnum(code4);
3240-
ASSERT_EQUALS("", errout.str());
3241-
}
3242-
32433149
void enum23() { // ticket #2804
32443150
const char code[] = "enum Enumerator : std::uint8_t { ITEM1, ITEM2, ITEM3 };\n"
32453151
"Enumerator e = ITEM3;\n";
@@ -3350,22 +3256,6 @@ class TestSimplifyTokens : public TestFixture {
33503256

33513257
const char code4[] = "enum { a, b }; void f() { int &a=x; }";
33523258
ASSERT_EQUALS("void f ( ) { int & a = x ; }", checkSimplifyEnum(code4));
3353-
3354-
// #4857 - not shadow variable
3355-
checkSimplifyEnum("enum { a,b }; void f() { if (x) { } else if ( x & a ) {} }");
3356-
ASSERT_EQUALS("", errout.str());
3357-
}
3358-
3359-
void enum38() { // #4463
3360-
const char code[] = "enum { a,b }; void f() { throw a; }";
3361-
checkSimplifyEnum(code);
3362-
ASSERT_EQUALS("", errout.str());
3363-
}
3364-
3365-
void enum39() { // #5145 - fp variable hides enum
3366-
const char code[] = "enum { A }; void f() { int a = 1 * A; }";
3367-
checkSimplifyEnum(code);
3368-
ASSERT_EQUALS("", errout.str());
33693259
}
33703260

33713261
void enum40() {

0 commit comments

Comments
 (0)