Skip to content

Commit 46e07c3

Browse files
chrchr-githubweb-flow
authored andcommitted
Fix #12623 FP constParameterReference for typedefed identifier (cppcheck-opensource#8341)
1 parent 8aba11e commit 46e07c3

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

lib/tokenize.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ static void skipEnumBody(T *&tok)
103103
/**
104104
* is tok the start brace { of a class, struct, union, or enum
105105
*/
106-
static bool isClassStructUnionEnumStart(const Token * tok)
106+
static const Token* isClassStructUnionEnumStart(const Token* tok)
107107
{
108108
if (!Token::Match(tok->previous(), "class|struct|union|enum|%name%|>|>> {"))
109-
return false;
109+
return nullptr;
110110
const Token * tok2 = tok->previous();
111111
while (tok2 && !Token::Match(tok2, "class|struct|union|enum|{|}|)|;"))
112112
tok2 = tok2->previous();
113-
return Token::Match(tok2, "class|struct|union|enum") && !Token::simpleMatch(tok2->tokAt(-1), "->");
113+
return (Token::Match(tok2, "class|struct|union|enum") && !Token::simpleMatch(tok2->tokAt(-1), "->")) ? tok2 : nullptr;
114114
}
115115

116116
//---------------------------------------------------------------------------
@@ -1028,6 +1028,13 @@ bool Tokenizer::isFunctionPointer(const Token* tok) {
10281028
return Token::Match(tok, "%name% ) (");
10291029
}
10301030

1031+
static bool matchCurrentType(const std::string& typeStr, const std::map<int, std::string>& types)
1032+
{
1033+
return std::any_of(types.begin(), types.end(), [&](const std::pair<int, std::string>& element) {
1034+
return typeStr == element.second;
1035+
});
1036+
}
1037+
10311038
void Tokenizer::simplifyTypedef()
10321039
{
10331040
// Simplify global typedefs that are not redefined with the fast 1-pass simplification.
@@ -1050,12 +1057,19 @@ void Tokenizer::simplifyTypedef()
10501057

10511058
int indentlevel = 0;
10521059
std::map<std::string, TypedefSimplifier> typedefs;
1060+
std::map<int, std::string> inType;
10531061
for (Token* tok = list.front(); tok; tok = tok->next()) {
10541062
if (!tok->isName()) {
1055-
if (tok->str()[0] == '{')
1063+
if (tok->str()[0] == '{') {
10561064
++indentlevel;
1057-
else if (tok->str()[0] == '}')
1065+
if (const Token* typeStart = isClassStructUnionEnumStart(tok)) {
1066+
inType.emplace(indentlevel, typeStart->strAt(1));
1067+
}
1068+
}
1069+
else if (tok->str()[0] == '}') {
1070+
inType.erase(indentlevel);
10581071
--indentlevel;
1072+
}
10591073
continue;
10601074
}
10611075

@@ -1072,7 +1086,7 @@ void Tokenizer::simplifyTypedef()
10721086
}
10731087

10741088
auto it = typedefs.find(tok->str());
1075-
if (it != typedefs.end() && it->second.canReplace(tok)) {
1089+
if (it != typedefs.end() && it->second.canReplace(tok) && !matchCurrentType(tok->str(), inType)) {
10761090
std::set<std::string> r;
10771091
std::string originalname;
10781092
while (it != typedefs.end() && r.insert(tok->str()).second) {

test/testsimplifytypedef.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ class TestSimplifyTypedef : public TestFixture {
230230
TEST_CASE(simplifyTypedef157);
231231
TEST_CASE(simplifyTypedef158);
232232
TEST_CASE(simplifyTypedef159);
233+
TEST_CASE(simplifyTypedef160);
233234

234235
TEST_CASE(simplifyTypedefFunction1);
235236
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
@@ -3812,6 +3813,28 @@ class TestSimplifyTypedef : public TestFixture {
38123813
ASSERT_EQUALS(exp, tok(code));
38133814
}
38143815

3816+
void simplifyTypedef160() {
3817+
const char code[] = "struct S1 {};\n"
3818+
"typedef struct S1 S2;\n"
3819+
"namespace N {\n"
3820+
" struct B {\n"
3821+
" explicit B(int& i);\n"
3822+
" };\n"
3823+
" struct S2 : B {\n"
3824+
" explicit S2(int& i) : B(i) {}\n"
3825+
" };\n"
3826+
"}\n";
3827+
const char exp[] = "struct S1 { } ; "
3828+
"namespace N { "
3829+
"struct B { "
3830+
"explicit B ( int & i ) ; } ; "
3831+
"struct S2 : B { "
3832+
"explicit S2 ( int & i ) : B ( i ) { } "
3833+
"} ; "
3834+
"}";
3835+
ASSERT_EQUALS(exp, tok(code));
3836+
}
3837+
38153838
void simplifyTypedefFunction1() {
38163839
{
38173840
const char code[] = "typedef void (*my_func)();\n"

0 commit comments

Comments
 (0)