From d4755916659412a9a5cb4c2208469760b20ae446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 14 May 2023 15:14:52 +0200 Subject: [PATCH] Tokenizer: simplification of typedefs in _Generic arguments (#5059) --- lib/tokenize.cpp | 12 ++++++++++++ test/testsimplifytypedef.cpp | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index dc6438bdb..dad5b14f9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -935,6 +935,18 @@ namespace { return true; if (Token::Match(tok->previous(), "public|protected|private")) return true; + if (Token::Match(tok->previous(), ", %name% :")) { + bool isGeneric = false; + for (; tok; tok = tok->previous()) { + if (Token::Match(tok, ")|]")) + tok = tok->link(); + else if (Token::Match(tok, "[;{}(]")) { + isGeneric = Token::simpleMatch(tok->previous(), "_Generic ("); + break; + } + } + return isGeneric; + } return false; } if (Token::Match(tok->previous(), "%name%") && !tok->previous()->isKeyword()) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 52f3ca5c4..bc8d46a3a 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -69,6 +69,7 @@ private: TEST_CASE(carray2); TEST_CASE(cdonotreplace1); TEST_CASE(cppfp1); + TEST_CASE(Generic1); TEST_CASE(simplifyTypedef1); TEST_CASE(simplifyTypedef2); @@ -463,6 +464,12 @@ private: ASSERT_EQUALS("void foo ( void ( * p ) ( void ) ) ;", tok(code)); } + void Generic1() { + const char code[] = "typedef void func(void);\n" + "_Generic((x), func: 1, default: 2);"; + ASSERT_EQUALS("_Generic ( x , void ( ) : 1 , default : 2 ) ;", tok(code)); + } + void simplifyTypedef1() { const char code[] = "class A\n" "{\n"