diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c54bc1ef688..51991f1f9e7 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6225,6 +6225,28 @@ void Tokenizer::simplifyHeadersAndUnusedTemplates() for (Token *tok = list.front(); tok; tok = tok->next()) { const bool isIncluded = (tok->fileIndex() != 0); + // Remove constructor initializer + if (isIncluded && !mSettings.checkHeaders && Token::Match(tok, "%name% (")) { + Token *start = tok->next()->link(); + if (start) { + start = start->next(); + } + if (start && start->str() == ":") { + const Token *next = start; + while (Token::Match(next, "[,:] %name%")) { + next = next->next(); // Go to %name% + next = next->next(); // Go to opening '(' or '{' + if (next && next->link()) { + next = next->link(); // Go to closing ')' or '}' + next = next->next(); // Go to next, either ',' or '{' + } + } + if (next && next->str() == "{") { + Token::eraseTokens(start->previous(), next); + } + } + } + // Remove executable code if (isIncluded && !mSettings.checkHeaders && tok->str() == "{") { // TODO: We probably need to keep the executable code if this function is called from the source file. diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index b07114d7f79..4d1415a30b8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -433,6 +433,8 @@ class TestTokenizer : public TestFixture { TEST_CASE(cppcast); TEST_CASE(checkHeader1); + TEST_CASE(checkHeader2); + TEST_CASE(checkHeader3); TEST_CASE(removeExtraTemplateKeywords); @@ -7750,6 +7752,52 @@ class TestTokenizer : public TestFixture { checkHdrs(code, false)); } + void checkHeader2() { + // #9977 + const char code[] = "# 1 \"test.h\"\n" + "A::A(const int &value)\n" + " :data(value),\n" + " data2(value)\n" + " {}\n"; + + ASSERT_EQUALS("\n\n##file 1\n" + "1: A :: A ( const int & value )\n" + "2: : data ( value ) ,\n" + "3: data2 ( value )\n" + "4: { }\n", + checkHdrs(code, true)); + + ASSERT_EQUALS("\n\n##file 1\n" + "1: A :: A ( const int & value )\n" + "2:\n" + "3:\n" + "4: ;\n", + checkHdrs(code, false)); + } + + void checkHeader3() { + // #9977 + const char code[] = "# 1 \"test.h\"\n" + "A::A(const int &value)\n" + " :data{value},\n" + " data2{value}\n" + " {}\n"; + + ASSERT_EQUALS("\n\n##file 1\n" + "1: A :: A ( const int & value )\n" + "2: : data { value } ,\n" + "3: data2 { value }\n" + "4: { }\n", + checkHdrs(code, true)); + + ASSERT_EQUALS("\n\n##file 1\n" + "1: A :: A ( const int & value )\n" + "2:\n" + "3:\n" + "4: ;\n", + checkHdrs(code, false)); + } + void removeExtraTemplateKeywords() { const char code1[] = "typename GridView::template Codim<0>::Iterator iterator;"; const char expected1[] = "GridView :: Codim < 0 > :: Iterator iterator ;";