Commit dabdb602 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh

C++: Preserve comments after preprocessor directives

Task-number: QTCREATORBUG-11216
Change-Id: Iac10e75f0f5c504b79e8466607dc1f478e578f99
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 6133920b
......@@ -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);
}
......
......@@ -1357,7 +1357,7 @@ void tst_Preprocessor::comments_within_data()
"}\n"
) << _(
"# 1 \"<stdin>\"\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 \"<stdin>\"\n"
"\n"
"# expansion begin 28,3 ~1\n"
"0\n"
"# expansion end\n"
"# 3 \"<stdin>\"\n"
) << _(
"# 1 \"<stdin>\"\n"
" /* comment */\n"
"# expansion begin 28,3 ~1\n"
"0\n"
"# expansion end\n"
"# 3 \"<stdin>\"\n"
);
QTest::newRow("case 9") << _(
"#define FOO /* comment1 */ /* comment2 */ 0\n"
"FOO\n"
) << _(
"# 1 \"<stdin>\"\n"
"\n"
"# expansion begin 44,3 ~1\n"
"0\n"
"# expansion end\n"
"# 3 \"<stdin>\"\n"
) << _(
"# 1 \"<stdin>\"\n"
" /* comment1 */ /* comment2 */\n"
"# expansion begin 44,3 ~1\n"
"0\n"
"# expansion end\n"
"# 3 \"<stdin>\"\n"
);
QTest::newRow("case 10") << _(
"#define FOO /* comment1 */ /* comment2 */ 0 /* comment3\n"
"comment4 */\n"
"FOO\n"
) << _(
"# 1 \"<stdin>\"\n"
"\n"
"\n"
"# expansion begin 70,3 ~1\n"
"0\n"
"# expansion end\n"
"# 4 \"<stdin>\"\n"
) << _(
"# 1 \"<stdin>\"\n"
" /* comment1 */ /* comment2 */ /* comment3\n"
"comment4 */\n"
"# expansion begin 70,3 ~1\n"
"0\n"
"# expansion end\n"
"# 4 \"<stdin>\"\n"
);
QTest::newRow("case 11") << _(
"#include <foo.h> // comment\n"
) << _(
"# 1 \"<stdin>\"\n"
"\n"
) << _(
"# 1 \"<stdin>\"\n"
" // comment\n"
);
QTest::newRow("joined") << _(
"// comment \\\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment