From 8e9d7290b216988bdb6256ce8d8eb44bf8a38677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 8 Apr 2020 22:40:45 +0200 Subject: [PATCH] Tokenizer::simplifyTypedef: Better handling of r-value references --- lib/tokenize.cpp | 6 ++++-- test/testsimplifytypedef.cpp | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9136f941a..6707f155a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1534,13 +1534,15 @@ void Tokenizer::simplifyTypedef() tok2 = tok2->next(); // reference or pointer to array? - if (tok2->str() == "&" || tok2->str() == "*") { + if (Token::Match(tok2, "&|*|&&")) { tok2 = tok2->previous(); tok2->insertToken("("); Token *tok3 = tok2->next(); // handle missing variable name - if (tok2->strAt(3) == ")" || tok2->strAt(3) == "," || tok2->strAt(3) == "(") + if (Token::Match(tok3, "( *|&|&& *|&|&& %name%")) + tok2 = tok3->tokAt(3); + else if (Token::Match(tok2->tokAt(3), "[(),]")) tok2 = tok2->tokAt(2); else tok2 = tok2->tokAt(3); diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 27f7573a0..ce21da9b6 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -166,6 +166,7 @@ private: TEST_CASE(simplifyTypedef128); // ticket #9053 TEST_CASE(simplifyTypedef129); TEST_CASE(simplifyTypedef130); // ticket #9446 + TEST_CASE(simplifyTypedef131); // ticket #9446 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -2616,6 +2617,22 @@ private: ASSERT_EQUALS(exp, tok(code, false)); } + void simplifyTypedef131() { + const char code[] = "typedef unsigned char a4[4];\n" + "a4 a4obj;\n" + "a4 && a4_rref = std::move(a4obj);\n" + "a4* a4p = &(a4obj);\n" + "a4*&& a4p_rref = std::move(a4p);"; + + const char exp [] = "unsigned char a4obj [ 4 ] ; " + "unsigned char ( && a4_rref ) [ 4 ] = std :: move ( a4obj ) ; " + "unsigned char ( * a4p ) [ 4 ] ; " + "a4p = & ( a4obj ) ; " + "unsigned char ( * && a4p_rref ) [ 4 ] = std :: move ( a4p ) ;"; + + ASSERT_EQUALS(exp, tok(code, false)); + } + void simplifyTypedefFunction1() { { const char code[] = "typedef void (*my_func)();\n"