diff --git a/src/libs/cplusplus/pp-engine.cpp b/src/libs/cplusplus/pp-engine.cpp index c17d36555d2..9c86b7e35d2 100644 --- a/src/libs/cplusplus/pp-engine.cpp +++ b/src/libs/cplusplus/pp-engine.cpp @@ -898,6 +898,11 @@ void Preprocessor::skipPreprocesorDirective(PPToken *tk) ScopedBoolSwap s(m_state.m_inPreprocessorDirective, true); while (isContinuationToken(*tk)) { + if (tk->isComment()) { + synchronizeOutputLines(*tk); + enforceSpacing(*tk, true); + currentOutputBuffer().append(tk->tokenStart(), tk->length()); + } lex(tk); } } @@ -1703,8 +1708,13 @@ void Preprocessor::handleDefineDirective(PPToken *tk) previousLine = tk->lineno; // Discard comments in macro definitions (keep comments flag doesn't apply here). - if (!tk->isComment()) + if (tk->isComment()) { + synchronizeOutputLines(*tk); + enforceSpacing(*tk, true); + currentOutputBuffer().append(tk->tokenStart(), tk->length()); + } else { bodyTokens.push_back(*tk); + } lex(tk); } diff --git a/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp b/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp index 380de2e861f..b262db3b40f 100644 --- a/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp +++ b/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp @@ -1357,7 +1357,7 @@ void tst_Preprocessor::comments_within_data() "}\n" ) << _( "# 1 \"\"\n" - "\n" + " //comment\n" "\n" "void foo() {\n" " if (\n" @@ -1368,6 +1368,75 @@ void tst_Preprocessor::comments_within_data() " ) {}\n" "}\n" ); + QTest::newRow("case 8") << _( + "#define FOO /* comment */ 0\n" + "FOO\n" + ) << _( + "# 1 \"\"\n" + "\n" + "# expansion begin 28,3 ~1\n" + "0\n" + "# expansion end\n" + "# 3 \"\"\n" + ) << _( + "# 1 \"\"\n" + " /* comment */\n" + "# expansion begin 28,3 ~1\n" + "0\n" + "# expansion end\n" + "# 3 \"\"\n" + ); + + QTest::newRow("case 9") << _( + "#define FOO /* comment1 */ /* comment2 */ 0\n" + "FOO\n" + ) << _( + "# 1 \"\"\n" + "\n" + "# expansion begin 44,3 ~1\n" + "0\n" + "# expansion end\n" + "# 3 \"\"\n" + ) << _( + "# 1 \"\"\n" + " /* comment1 */ /* comment2 */\n" + "# expansion begin 44,3 ~1\n" + "0\n" + "# expansion end\n" + "# 3 \"\"\n" + ); + + QTest::newRow("case 10") << _( + "#define FOO /* comment1 */ /* comment2 */ 0 /* comment3\n" + "comment4 */\n" + "FOO\n" + ) << _( + "# 1 \"\"\n" + "\n" + "\n" + "# expansion begin 70,3 ~1\n" + "0\n" + "# expansion end\n" + "# 4 \"\"\n" + ) << _( + "# 1 \"\"\n" + " /* comment1 */ /* comment2 */ /* comment3\n" + "comment4 */\n" + "# expansion begin 70,3 ~1\n" + "0\n" + "# expansion end\n" + "# 4 \"\"\n" + ); + + QTest::newRow("case 11") << _( + "#include // comment\n" + ) << _( + "# 1 \"\"\n" + "\n" + ) << _( + "# 1 \"\"\n" + " // comment\n" + ); QTest::newRow("joined") << _( "// comment \\\n"