Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,27 @@ namespace {
}
}

const auto checkForRecursion = [this]() {
if (Token::Match(mTypedefToken, "typedef %name% %name% ;"))
return;
for (const Token *tok = mTypedefToken; tok != mEndToken; tok = tok->next()) {
if (tok == mNameToken)
continue;
if (tok->str() != mNameToken->str())
continue;
if (Token::Match(tok->previous(), "struct|class|enum|union"))
continue;
throw InternalError(tok, "recursive typedef encountered");
}
};

for (Token* type = start; Token::Match(type, "%name%|*|&|&&"); type = type->next()) {
if (type != start && Token::Match(type, "%name% ;") && !type->isStandardType()) {
mRangeType.first = start;
mRangeType.second = type;
mNameToken = type;
mEndToken = mNameToken->next();
checkForRecursion();
return;
}
if (type != start && Token::Match(type, "%name% [")) {
Expand All @@ -609,6 +624,7 @@ namespace {
mEndToken = end->next();
mRangeAfterVar.first = mNameToken->next();
mRangeAfterVar.second = mEndToken;
checkForRecursion();
return;
}
if (Token::Match(type->next(), "( * const| %name% ) (") && Token::simpleMatch(type->linkAt(1)->linkAt(1), ") ;")) {
Expand All @@ -618,6 +634,7 @@ namespace {
mRangeType.second = mNameToken;
mRangeAfterVar.first = mNameToken->next();
mRangeAfterVar.second = mEndToken;
checkForRecursion();
return;
}
if (type != start && Token::Match(type, "%name% ( !!(") && Token::simpleMatch(type->linkAt(1), ") ;") && !type->isStandardType()) {
Expand All @@ -627,6 +644,7 @@ namespace {
mRangeType.second = type;
mRangeAfterVar.first = mNameToken->next();
mRangeAfterVar.second = mEndToken;
checkForRecursion();
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef const v*v,*;
10 changes: 8 additions & 2 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedef160);
TEST_CASE(simplifyTypedef161);
TEST_CASE(simplifyTypedef162);
TEST_CASE(simplifyTypedef163);

TEST_CASE(simplifyTypedefFunction1);
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
Expand Down Expand Up @@ -3836,11 +3837,11 @@ class TestSimplifyTypedef : public TestFixture {
"}";
ASSERT_EQUALS(exp, tok(code));

const char code2[] = "typedef stuct T* T;\n" // #14669
const char code2[] = "typedef struct T* T;\n" // #14669
"struct T {\n"
" T p;\n"
"};\n";
const char exp2[] = "struct T { stuct T * p ; } ;";
const char exp2[] = "struct T { struct T * p ; } ;";
ASSERT_EQUALS(exp2, simplifyTypedefC(code2));
}

Expand Down Expand Up @@ -3868,6 +3869,11 @@ class TestSimplifyTypedef : public TestFixture {
ASSERT_EQUALS(exp, tok(code));
}

void simplifyTypedef163() {
const char code[] = "typedef v *v;";
ASSERT_THROW_INTERNAL(tok(code), INTERNAL);
}

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