C++: Fix handling of #undef

* If the macro is defined before, track its reference
* Synchronize environment line before calling remove, which
  currently sets incorrect line
* Set macro offset

Task-number: QTCREATORBUG-10454
Change-Id: I480d16423a976a025bb8c71046610a46f9d7b0fd
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
This commit is contained in:
Orgad Shaneh
2014-02-06 23:18:18 +02:00
committed by Orgad Shaneh
parent 8ff63de728
commit 5d2cd2e56d
2 changed files with 52 additions and 2 deletions

View File

@@ -374,6 +374,7 @@ private slots:
void include_guard_data();
void empty_trailing_lines();
void empty_trailing_lines_data();
void undef();
};
// Remove all #... lines, and 'simplify' string, to allow easily comparing the result
@@ -1739,6 +1740,46 @@ void tst_Preprocessor::empty_trailing_lines_data()
);
}
void tst_Preprocessor::undef()
{
Environment env;
QByteArray output;
MockClient client(&env, &output);
Preprocessor preprocess(&client, &env);
QByteArray input =
"#define FOO\n"
"#define FOO2\n"
"#undef FOO\n"
"#undef BAR\n";
preprocess.run(QLatin1String("<stdin>"), input);
QCOMPARE(env.macroCount(), 4U);
Macro *macro = env.macroAt(0);
QCOMPARE(macro->name(), QByteArray("FOO"));
QCOMPARE(macro->offset(), 8U);
QCOMPARE(macro->line(), 1U);
QVERIFY(!macro->isHidden());
macro = env.macroAt(1);
QCOMPARE(macro->name(), QByteArray("FOO2"));
QCOMPARE(macro->offset(), 20U);
QCOMPARE(macro->line(), 2U);
QVERIFY(!macro->isHidden());
macro = env.macroAt(2);
QCOMPARE(macro->name(), QByteArray("FOO"));
QCOMPARE(macro->offset(), 32U);
QCOMPARE(macro->line(), 3U);
QVERIFY(macro->isHidden());
macro = env.macroAt(3);
QCOMPARE(macro->name(), QByteArray("BAR"));
QCOMPARE(macro->offset(), 43U);
QCOMPARE(macro->line(), 4U);
QVERIFY(macro->isHidden());
QList<QByteArray> macros = client.definedMacros();
QVERIFY(macros.contains("FOO"));
QVERIFY(macros.contains("FOO2"));
QCOMPARE(client.macroUsesLine()["FOO"], (QList<unsigned>() << 3U));
QVERIFY(client.macroUsesLine()["BAR"].isEmpty());
}
void tst_Preprocessor::compare_input_output(bool keepComments)
{
QFETCH(QByteArray, input);