Skip to content

Commit ce12e1c

Browse files
committed
Remove unnecessaryForwardDeclaration check. It had false positives (e.g. cppcheck-opensource#3663), was implemented in the Tokenizer and of little value.
1 parent db6dfa2 commit ce12e1c

3 files changed

Lines changed: 12 additions & 57 deletions

File tree

lib/tokenize.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,6 @@ void Tokenizer::duplicateTypedefError(const Token *tok1, const Token *tok2, cons
209209
std::string("The " + type + " '" + tok2_str + "' hides a typedef with the same name."), true);
210210
}
211211

212-
void Tokenizer::duplicateDeclarationError(const Token *tok1, const Token *tok2, const std::string &type) const
213-
{
214-
if (tok1 && !(_settings->isEnabled("style")))
215-
return;
216-
217-
std::list<const Token*> locationList;
218-
locationList.push_back(tok1);
219-
locationList.push_back(tok2);
220-
const std::string tok2_str = tok2 ? tok2->str() : std::string("name");
221-
222-
reportError(locationList, Severity::style, "unnecessaryForwardDeclaration",
223-
std::string("The " + type + " '" + tok2_str + "' forward declaration is unnecessary. Type " + type + " is already declared earlier."));
224-
}
225-
226212
// check if this statement is a duplicate definition
227213
bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, const std::set<std::string>& structs) const
228214
{
@@ -330,26 +316,20 @@ bool Tokenizer::duplicateTypedef(Token **tokPtr, const Token *name, const Token
330316
duplicateTypedefError(*tokPtr, name, "struct");
331317
return true;
332318
} else {
333-
// forward declaration after declaration
334-
duplicateDeclarationError(*tokPtr, name, "struct");
335319
return false;
336320
}
337321
} else if (tok->previous()->str() == "union") {
338322
if (tok->next()->str() != ";") {
339323
duplicateTypedefError(*tokPtr, name, "union");
340324
return true;
341325
} else {
342-
// forward declaration after declaration
343-
duplicateDeclarationError(*tokPtr, name, "union");
344326
return false;
345327
}
346328
} else if (isCPP() && tok->previous()->str() == "class") {
347329
if (tok->next()->str() != ";") {
348330
duplicateTypedefError(*tokPtr, name, "class");
349331
return true;
350332
} else {
351-
// forward declaration after declaration
352-
duplicateDeclarationError(*tokPtr, name, "class");
353333
return false;
354334
}
355335
}
@@ -8746,7 +8726,6 @@ void Tokenizer::getErrorMessages(ErrorLogger *errorLogger, const Settings *setti
87468726
{
87478727
Tokenizer t(settings, errorLogger);
87488728
t.duplicateTypedefError(0, 0, "variable");
8749-
t.duplicateDeclarationError(0, 0, "variable");
87508729
t.duplicateEnumError(0, 0, "variable");
87518730
}
87528731

lib/tokenize.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -718,18 +718,25 @@ class CPPCHECKLIB Tokenizer {
718718
bool duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef, const std::set<std::string>& structs) const;
719719
void duplicateTypedefError(const Token *tok1, const Token *tok2, const std::string & type) const;
720720

721-
/**
722-
* Report error - duplicate declarations
723-
*/
724-
void duplicateDeclarationError(const Token *tok1, const Token *tok2, const std::string &type) const;
725-
726721
void unsupportedTypedef(const Token *tok) const;
727722

728723
void setVarIdClassDeclaration(Token * const startToken,
729724
const std::map<std::string, unsigned int> &variableId,
730725
const unsigned int scopeStartVarId,
731726
std::map<unsigned int, std::map<std::string,unsigned int> >& structMembers);
732727

728+
729+
/**
730+
* Simplify e.g. 'return(strncat(temp,"a",1));' into
731+
* strncat(temp,"a",1); return temp;
732+
*/
733+
void simplifyReturnStrncat();
734+
735+
/**
736+
* Output list of unknown types.
737+
*/
738+
void printUnknownTypes() const;
739+
733740
public:
734741

735742
/** Was there templates in the code? */
@@ -768,19 +775,6 @@ class CPPCHECKLIB Tokenizer {
768775
return _varId;
769776
}
770777

771-
772-
/**
773-
* Simplify e.g. 'return(strncat(temp,"a",1));' into
774-
* strncat(temp,"a",1); return temp;
775-
*/
776-
void simplifyReturnStrncat();
777-
778-
/**
779-
* Output list of unknown types.
780-
*/
781-
void printUnknownTypes() const;
782-
783-
784778
/**
785779
* Token list: stores all tokens.
786780
*/

test/testsimplifytypedef.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ class TestSimplifyTypedef : public TestFixture {
7878
TEST_CASE(simplifyTypedef39);
7979
TEST_CASE(simplifyTypedef40);
8080
TEST_CASE(simplifyTypedef41); // ticket #1488
81-
TEST_CASE(simplifyTypedef42); // ticket #1506
8281
TEST_CASE(simplifyTypedef43); // ticket #1588
8382
TEST_CASE(simplifyTypedef44);
8483
TEST_CASE(simplifyTypedef45); // ticket #1613
@@ -1209,23 +1208,6 @@ class TestSimplifyTypedef : public TestFixture {
12091208
ASSERT_EQUALS("", errout.str());
12101209
}
12111210

1212-
void simplifyTypedef42() {
1213-
// ticket #1506
1214-
checkSimplifyTypedef("typedef struct A { } A;\n"
1215-
"struct A;");
1216-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) The struct 'A' forward declaration is unnecessary. Type struct is already declared earlier.\n", errout.str());
1217-
1218-
checkSimplifyTypedef("typedef union A { int i; float f; } A;\n"
1219-
"union A;");
1220-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) The union 'A' forward declaration is unnecessary. Type union is already declared earlier.\n", errout.str());
1221-
1222-
const char code [] = "typedef std::map<std::string, int> A;\n"
1223-
"class A;";
1224-
checkSimplifyTypedef(code);
1225-
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) The class 'A' forward declaration is unnecessary. Type class is already declared earlier.\n", errout.str());
1226-
TODO_ASSERT_EQUALS("class A ;", "class std :: map < std :: string , int > ;", tok(code));
1227-
}
1228-
12291211
void simplifyTypedef43() {
12301212
// ticket #1588
12311213
{

0 commit comments

Comments
 (0)