diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index fd52f71a3..a2899436a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4466,7 +4466,10 @@ void Tokenizer::simplifyCasts() // *((char *)a + 1) = 0; // #3596 : remove cast when casting a function pointer: // (*(void (*)(char *))fp)(x); - if (!tok->isName() && Token::simpleMatch(tok->next(), "* (") && !Token::Match(tok->linkAt(2), ") %var%")) { + if (!tok->isName() && + Token::simpleMatch(tok->next(), "* (") && + !Token::Match(tok->linkAt(2), ") %var%") && + !Token::simpleMatch(tok->linkAt(2), ") &")) { tok = tok->linkAt(2); continue; } @@ -4483,6 +4486,12 @@ void Tokenizer::simplifyCasts() // Remove cast.. Token::eraseTokens(tok, tok->next()->link()->next()); + // Remove '* &' + if (Token::simpleMatch(tok, "* &")) { + tok->deleteNext(); + tok->deleteThis(); + } + if (tok->str() == ")" && tok->link()->previous()) { // If there was another cast before this, go back // there to check it also. e.g. "(int)(char)x" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 50367e51e..c9ce837c5 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -78,6 +78,7 @@ private: TEST_CASE(removeCast8); TEST_CASE(removeCast9); TEST_CASE(removeCast10); + TEST_CASE(removeCast11); TEST_CASE(inlineasm); @@ -788,6 +789,10 @@ private: ASSERT_EQUALS("; ( * f ) ( p ) ;", tokenizeAndStringify("; (*(void (*)(char *))f)(p);", true)); } + void removeCast11() { + ASSERT_EQUALS("; x = 0 ;", tokenizeAndStringify("; *(int *)&x = 0;", true)); + } + void inlineasm() { ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("asm { mov ax,bx };")); ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("_asm { mov ax,bx };"));