C++: Fix line number information after multiline comments.

Task-number: QTCREATORBUG-7702
Change-Id: I0ec2e1eb9bf1c556b0a426d4405df1c48b5653ed
Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
This commit is contained in:
Christian Kamm
2012-08-07 10:47:38 +02:00
parent 336511dd99
commit 41e140da04
2 changed files with 130 additions and 4 deletions

View File

@@ -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) void Preprocessor::synchronizeOutputLines(const PPToken &tk, bool forceLine)
{ {
if (m_state.m_expansionStatus != NotExpanding if (m_state.m_expansionStatus != NotExpanding
|| (!forceLine && m_env->currentLine == tk.lineno)) { || (!forceLine && m_env->currentLine == tk.lineno)) {
adjustForCommentNewlines(&m_env->currentLine, tk);
return; return;
} }
@@ -1157,8 +1164,7 @@ void Preprocessor::synchronizeOutputLines(const PPToken &tk, bool forceLine)
} }
m_env->currentLine = tk.lineno; m_env->currentLine = tk.lineno;
if (tk.is(T_COMMENT) || tk.is(T_DOXY_COMMENT)) adjustForCommentNewlines(&m_env->currentLine, tk);
m_env->currentLine += tk.asByteArrayRef().count('\n');
} }
void Preprocessor::removeTrailingOutputLines() void Preprocessor::removeTrailingOutputLines()

View File

@@ -306,7 +306,7 @@ protected:
static QString simplified(QByteArray buf); static QString simplified(QByteArray buf);
private: private:
void compare_input_output(); void compare_input_output(bool keepComments = false);
private slots: private slots:
void va_args(); void va_args();
@@ -332,6 +332,8 @@ private slots:
void comparisons(); void comparisons();
void comments_within(); void comments_within();
void comments_within_data(); void comments_within_data();
void comments_within2();
void comments_within2_data();
void multitokens_argument(); void multitokens_argument();
void multitokens_argument_data(); void multitokens_argument_data();
}; };
@@ -1179,13 +1181,131 @@ void tst_Preprocessor::comments_within_data()
QTest::newRow("case 4") << original << expected; 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, input);
QFETCH(QByteArray, output); QFETCH(QByteArray, output);
Environment env; Environment env;
Preprocessor preprocess(0, &env); Preprocessor preprocess(0, &env);
preprocess.setKeepComments(keepComments);
QByteArray prep = preprocess.run(QLatin1String("<stdin>"), input); QByteArray prep = preprocess.run(QLatin1String("<stdin>"), input);
QCOMPARE(output, prep); QCOMPARE(output, prep);
} }