Skip to content

Commit d475591

Browse files
authored
Tokenizer: simplification of typedefs in _Generic arguments (danmar#5059)
1 parent 793d6ae commit d475591

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/tokenize.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,18 @@ namespace {
935935
return true;
936936
if (Token::Match(tok->previous(), "public|protected|private"))
937937
return true;
938+
if (Token::Match(tok->previous(), ", %name% :")) {
939+
bool isGeneric = false;
940+
for (; tok; tok = tok->previous()) {
941+
if (Token::Match(tok, ")|]"))
942+
tok = tok->link();
943+
else if (Token::Match(tok, "[;{}(]")) {
944+
isGeneric = Token::simpleMatch(tok->previous(), "_Generic (");
945+
break;
946+
}
947+
}
948+
return isGeneric;
949+
}
938950
return false;
939951
}
940952
if (Token::Match(tok->previous(), "%name%") && !tok->previous()->isKeyword())

test/testsimplifytypedef.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class TestSimplifyTypedef : public TestFixture {
6969
TEST_CASE(carray2);
7070
TEST_CASE(cdonotreplace1);
7171
TEST_CASE(cppfp1);
72+
TEST_CASE(Generic1);
7273

7374
TEST_CASE(simplifyTypedef1);
7475
TEST_CASE(simplifyTypedef2);
@@ -463,6 +464,12 @@ class TestSimplifyTypedef : public TestFixture {
463464
ASSERT_EQUALS("void foo ( void ( * p ) ( void ) ) ;", tok(code));
464465
}
465466

467+
void Generic1() {
468+
const char code[] = "typedef void func(void);\n"
469+
"_Generic((x), func: 1, default: 2);";
470+
ASSERT_EQUALS("_Generic ( x , void ( ) : 1 , default : 2 ) ;", tok(code));
471+
}
472+
466473
void simplifyTypedef1() {
467474
const char code[] = "class A\n"
468475
"{\n"

0 commit comments

Comments
 (0)