Skip to content

Commit

Permalink
Fix #12647 Repeated typedef not handled (danmar#6355)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Apr 27, 2024
1 parent 5d0aa2a commit fae65de
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,8 @@ void Tokenizer::simplifyTypedef()

if (indentlevel == 0 && tok->str() == "typedef") {
TypedefSimplifier ts(tok);
if (!ts.fail() && numberOfTypedefs[ts.name()] == 1) {
if (!ts.fail() && numberOfTypedefs[ts.name()] == 1 &&
(numberOfTypedefs.find(ts.getTypedefToken()->strAt(1)) == numberOfTypedefs.end() || ts.getTypedefToken()->strAt(2) == "(")) {
if (mSettings.severity.isEnabled(Severity::portability) && ts.isInvalidConstFunctionType(typedefs))
reportError(tok->next(), Severity::portability, "invalidConstFunctionType",
"It is unspecified behavior to const qualify a function type.");
Expand Down
2 changes: 1 addition & 1 deletion test/testgarbage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ class TestGarbage : public TestFixture {
}

void garbageCode63() { // #6739
ASSERT_THROW_INTERNAL(checkCode("{ } { } typedef int u_array[]; typedef u_array &u_array_ref; (u_array_ref arg) { } u_array_ref u_array_ref_gbl_obj0"), INTERNAL);
ASSERT_THROW_INTERNAL(checkCode("{ } { } typedef int u_array[]; typedef u_array &u_array_ref; (u_array_ref arg) { } u_array_ref u_array_ref_gbl_obj0"), SYNTAX);
}

void garbageCode64() { // #6740
Expand Down
13 changes: 12 additions & 1 deletion test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedef150);
TEST_CASE(simplifyTypedef151);
TEST_CASE(simplifyTypedef152);
TEST_CASE(simplifyTypedef153);

TEST_CASE(simplifyTypedefFunction1);
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
Expand Down Expand Up @@ -1226,7 +1227,7 @@ class TestSimplifyTypedef : public TestFixture {
"LPCSTR ccp;";

const char expected[] =
"char c ; "
"; char c ; "
"char * cp ; "
"const char * ccp ;";

Expand Down Expand Up @@ -3563,6 +3564,16 @@ class TestSimplifyTypedef : public TestFixture {
ASSERT_EQUALS(exp, tok(code));
}

void simplifyTypedef153() {
const char* code{}, *exp{}; // #12647
code = "typedef unsigned long X;\n"
"typedef unsigned long X;\n"
"typedef X Y;\n"
"Y y;\n";
exp = "long y ;";
ASSERT_EQUALS(exp, tok(code));
}

void simplifyTypedefFunction1() {
{
const char code[] = "typedef void (*my_func)();\n"
Expand Down

0 comments on commit fae65de

Please sign in to comment.