diff --git a/src/libs/cplusplus/pp-engine.cpp b/src/libs/cplusplus/pp-engine.cpp index a7e06000273507ce2b719e1720ffb9005877cd8e..303205408aee2c37b8d87dff8cc4640b0ff9f14b 100644 --- a/src/libs/cplusplus/pp-engine.cpp +++ b/src/libs/cplusplus/pp-engine.cpp @@ -1137,10 +1137,17 @@ void Preprocessor::trackExpansionCycles(PPToken *tk) } } +static void adjustForCommentNewlines(unsigned *currentLine, const PPToken &tk) +{ + if (tk.is(T_COMMENT) || tk.is(T_DOXY_COMMENT)) + (*currentLine) += tk.asByteArrayRef().count('\n'); +} + void Preprocessor::synchronizeOutputLines(const PPToken &tk, bool forceLine) { if (m_state.m_expansionStatus != NotExpanding || (!forceLine && m_env->currentLine == tk.lineno)) { + adjustForCommentNewlines(&m_env->currentLine, tk); return; } @@ -1157,8 +1164,7 @@ void Preprocessor::synchronizeOutputLines(const PPToken &tk, bool forceLine) } m_env->currentLine = tk.lineno; - if (tk.is(T_COMMENT) || tk.is(T_DOXY_COMMENT)) - m_env->currentLine += tk.asByteArrayRef().count('\n'); + adjustForCommentNewlines(&m_env->currentLine, tk); } void Preprocessor::removeTrailingOutputLines() diff --git a/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp b/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp index af26d2915807cbd27df3b1fabaaed3a9b9e8cc1c..ae6f83708ce0c796fea49bb82546e1723927bcd5 100644 --- a/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp +++ b/tests/auto/cplusplus/preprocessor/tst_preprocessor.cpp @@ -306,7 +306,7 @@ protected: static QString simplified(QByteArray buf); private: - void compare_input_output(); + void compare_input_output(bool keepComments = false); private slots: void va_args(); @@ -332,6 +332,8 @@ private slots: void comparisons(); void comments_within(); void comments_within_data(); + void comments_within2(); + void comments_within2_data(); void multitokens_argument(); void multitokens_argument_data(); }; @@ -1179,13 +1181,131 @@ void tst_Preprocessor::comments_within_data() QTest::newRow("case 4") << original << expected; } -void tst_Preprocessor::compare_input_output() +void tst_Preprocessor::comments_within2() +{ + compare_input_output(true); +} + +void tst_Preprocessor::comments_within2_data() +{ + QTest::addColumn<QByteArray>("input"); + QTest::addColumn<QByteArray>("output"); + + QByteArray original; + QByteArray expected; + + original = "#define FOO int x;\n" + "\n" + " // comment\n" + " // comment\n" + " // comment\n" + " // comment\n" + "FOO\n" + "x = 10\n"; + expected = + "# 1 \"<stdin>\"\n" + "\n" + "\n" + " // comment\n" + " // comment\n" + " // comment\n" + " // comment\n" + "# expansion begin 76,3 ~3\n" + "int x;\n" + "# expansion end\n" + "# 8 \"<stdin>\"\n" + "x = 10\n"; + QTest::newRow("case 1") << original << expected; + + + original = "#define FOO int x;\n" + "\n" + " /* comment\n" + " comment\n" + " comment\n" + " comment */\n" + "FOO\n" + "x = 10\n"; + expected = + "# 1 \"<stdin>\"\n" + "\n" + "\n" + " /* comment\n" + " comment\n" + " comment\n" + " comment */\n" + "# expansion begin 79,3 ~3\n" + "int x;\n" + "# expansion end\n" + "# 8 \"<stdin>\"\n" + "x = 10\n"; + QTest::newRow("case 2") << original << expected; + + + original = "#define FOO int x;\n" + "\n" + " // comment\n" + " // comment\n" + " // comment\n" + " // comment\n" + "FOO\n" + "// test\n" + "// test again\n" + "x = 10\n"; + expected = + "# 1 \"<stdin>\"\n" + "\n" + "\n" + " // comment\n" + " // comment\n" + " // comment\n" + " // comment\n" + "# expansion begin 76,3 ~3\n" + "int x;\n" + "# expansion end\n" + "# 8 \"<stdin>\"\n" + "// test\n" + "// test again\n" + "x = 10\n"; + QTest::newRow("case 3") << original << expected; + + + original = "#define FOO int x;\n" + "\n" + "void foo() { /* comment\n" + " comment\n" + " comment\n" + " comment */\n" + "FOO\n" + "/* \n" + "*/\n" + "x = 10\n"; + expected = + "# 1 \"<stdin>\"\n" + "\n" + "\n" + "void foo() { /* comment\n" + " comment\n" + " comment\n" + " comment */\n" + "# expansion begin 91,3 ~3\n" + "int x;\n" + "# expansion end\n" + "# 8 \"<stdin>\"\n" + "/* \n" + "*/\n" + "x = 10\n"; + QTest::newRow("case 4") << original << expected; +} + +void tst_Preprocessor::compare_input_output(bool keepComments) { QFETCH(QByteArray, input); QFETCH(QByteArray, output); Environment env; Preprocessor preprocess(0, &env); + preprocess.setKeepComments(keepComments); QByteArray prep = preprocess.run(QLatin1String("<stdin>"), input); QCOMPARE(output, prep); }