diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8badaaa1e..fd52f71a3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3454,32 +3454,41 @@ void Tokenizer::removeMacrosInGlobalScope() tok->deleteNext(); } - if (Token::Match(tok, "[;{}] %type% (") && CheckNullPointer::isUpper(tok->next()->str())) { - unsigned int par = 0; - const Token *tok2; - for (tok2 = tok; tok2; tok2 = tok2->next()) { - if (tok2->str() == "(") - ++par; - else if (tok2->str() == ")") { - if (par <= 1) - break; - --par; + if (Token::Match(tok, "[;{}] %type%") && CheckNullPointer::isUpper(tok->next()->str())) { + const Token *tok2 = tok->tokAt(2); + if (tok2 && tok2->str() == "(") { + unsigned int par = 0; + for (; tok2; tok2 = tok2->next()) { + if (tok2->str() == "(") + ++par; + else if (tok2->str() == ")") { + if (par <= 1) + break; + --par; + } } + if (tok2 && tok2->str() == ")") + tok2 = tok2->next(); } // remove unknown macros before namespace|class|struct|union - if (Token::Match(tok2, ") namespace|class|struct|union")) { - Token::eraseTokens(tok, tok2->next()); + if (Token::Match(tok2, "namespace|class|struct|union")) { + // is there a "{" for? + const Token *tok3 = tok2; + while (tok3 && !Token::Match(tok3,"[;{}()]")) + tok3 = tok3->next(); + if (tok3 && tok3->str() == "{") + Token::eraseTokens(tok, tok2); continue; } // remove unknown macros before foo::foo( - if (Token::Match(tok2, ") %type% :: %type%")) { - const Token *tok3 = tok2->next(); + if (Token::Match(tok2, "%type% :: %type%")) { + const Token *tok3 = tok2; while (Token::Match(tok3, "%type% :: %type% ::")) tok3 = tok3->tokAt(2); if (Token::Match(tok3, "%type% :: %type% (") && tok3->str() == tok3->strAt(2)) - Token::eraseTokens(tok, tok2->next()); + Token::eraseTokens(tok, tok2); continue; } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 017d88d5e..20318f75b 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -6525,6 +6525,12 @@ private: // #3750 ASSERT_EQUALS("; foo :: foo ( ) { }", tokenizeAndStringify("; AB(foo*) foo::foo() { }")); + + // #3855 + ASSERT_EQUALS("; class foo { }", + tokenizeAndStringify("; AB class foo { }")); + ASSERT_EQUALS("; CONST struct ABC abc ;", + tokenizeAndStringify("; CONST struct ABC abc ;")); } void multipleAssignment() {