From c20b3587038c8834e439057c791684c5f636cd07 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 22 Jul 2013 13:01:59 +0200 Subject: moc: Issue a warning instead of an error when macro argument mismatch moc's C++ is not 100% accurate, so better process the invalid macro with a warning rather than an error. Such errors occurred in the QSKIP macro with variadic arguments since that macro is defined conditionally. It is also causing problem in boost header (cf task QTBUG-29331) Task-number: QTBUG-29331 Change-Id: Ice6a01b675286540d6470c8e36920b7efd39b540 Reviewed-by: Lars Knoll --- src/tools/moc/preprocessor.cpp | 4 ++-- tests/auto/tools/moc/tst_moc.cpp | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/tools/moc/preprocessor.cpp b/src/tools/moc/preprocessor.cpp index 8f4b84a9c8..06758e67bd 100644 --- a/src/tools/moc/preprocessor.cpp +++ b/src/tools/moc/preprocessor.cpp @@ -645,7 +645,7 @@ Symbols Preprocessor::macroExpandIdentifier(Preprocessor *that, SymbolStack &sym // 0 argument macros are a bit special. They are ok if the // argument is pure whitespace or empty (macro.arguments.size() != 0 || arguments.size() != 1 || !arguments.at(0).isEmpty())) - that->error("Macro argument mismatch."); + that->warning("Macro argument mismatch."); // now replace the macro arguments with the expanded arguments enum Mode { @@ -662,7 +662,7 @@ Symbols Preprocessor::macroExpandIdentifier(Preprocessor *that, SymbolStack &sym } int index = macro.arguments.indexOf(s); if (mode == Normal) { - if (index >= 0) { + if (index >= 0 && index < arguments.size()) { // each argument undoergoes macro expansion if it's not used as part of a # or ## if (i == macro.symbols.size() - 1 || macro.symbols.at(i + 1).token != PP_HASHHASH) { Symbols arg = arguments.at(index); diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index 3459bede85..612ce3cd7e 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -1718,6 +1718,14 @@ void tst_Moc::warnings_data() << 1 << QString() << QString("standard input:1: Error: Class contains Q_OBJECT macro but does not inherit from QObject"); + + QTest::newRow("Warning on invalid macro") + << QByteArray("#define Foo(a, b)\n class X : public QObject { Q_OBJECT }; \n Foo(a) \n Foo(a,b,c) \n") + << QStringList() + << 0 + << QString("IGNORE_ALL_STDOUT") + << QString(":3: Warning: Macro argument mismatch.\n:4: Warning: Macro argument mismatch."); + } void tst_Moc::warnings() @@ -1759,7 +1767,7 @@ void tst_Moc::warnings() // magic value "IGNORE_ALL_STDOUT" ignores stdout if (expectedStdOut != "IGNORE_ALL_STDOUT") QCOMPARE(QString::fromLocal8Bit(proc.readAllStandardOutput()).trimmed(), expectedStdOut); - QCOMPARE(QString::fromLocal8Bit(proc.readAllStandardError()).trimmed(), expectedStdErr); + QCOMPARE(QString::fromLocal8Bit(proc.readAllStandardError()).trimmed().remove('\r'), expectedStdErr); } class tst_Moc::PrivateClass : public QObject { -- cgit v1.2.3