From 6cf0ad80bb8a168115772aa5c3b4abec3e372ccc Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 18 Aug 2010 18:10:01 +0200 Subject: moc: Error if the NOTIFY signal is invalid. Previously, an invalid NOTIFY signal would be silently ignored. Now it throws an error Reviewed-by: Joao Task-number: QTBUG-7684 --- src/tools/moc/generator.cpp | 48 ---------------------------- src/tools/moc/moc.cpp | 58 ++++++++++++++++++++++++++++++++++ src/tools/moc/moc.h | 1 + tests/auto/moc/error-on-wrong-notify.h | 53 +++++++++++++++++++++++++++++++ tests/auto/moc/tst_moc.cpp | 22 +++++++++++++ 5 files changed, 134 insertions(+), 48 deletions(-) create mode 100644 tests/auto/moc/error-on-wrong-notify.h diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index 9a982d0..c3bbba1 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -483,54 +483,6 @@ void Generator::generateFunctions(QList& list, const char *functype void Generator::generateProperties() { // - // specify get function, for compatibiliy we accept functions - // returning pointers, or const char * for QByteArray. - // - for (int i = 0; i < cdef->propertyList.count(); ++i) { - PropertyDef &p = cdef->propertyList[i]; - if (p.read.isEmpty()) - continue; - for (int j = 0; j < cdef->publicList.count(); ++j) { - const FunctionDef &f = cdef->publicList.at(j); - if (f.name != p.read) - continue; - if (!f.isConst) // get functions must be const - continue; - if (f.arguments.size()) // and must not take any arguments - continue; - PropertyDef::Specification spec = PropertyDef::ValueSpec; - QByteArray tmp = f.normalizedType; - if (p.type == "QByteArray" && tmp == "const char *") - tmp = "QByteArray"; - if (tmp.left(6) == "const ") - tmp = tmp.mid(6); - if (p.type != tmp && tmp.endsWith('*')) { - tmp.chop(1); - spec = PropertyDef::PointerSpec; - } else if (f.type.name.endsWith('&')) { // raw type, not normalized type - spec = PropertyDef::ReferenceSpec; - } - if (p.type != tmp) - continue; - p.gspec = spec; - break; - } - if(!p.notify.isEmpty()) { - int notifyId = -1; - for (int j = 0; j < cdef->signalList.count(); ++j) { - const FunctionDef &f = cdef->signalList.at(j); - if(f.name != p.notify) { - continue; - } else { - notifyId = j /* Signal indexes start from 0 */; - break; - } - } - p.notifyId = notifyId; - } - } - - // // Create meta data // diff --git a/src/tools/moc/moc.cpp b/src/tools/moc/moc.cpp index ac49d65..2c24165 100644 --- a/src/tools/moc/moc.cpp +++ b/src/tools/moc/moc.cpp @@ -727,6 +727,7 @@ void Moc::parse() error("Class declarations lacks Q_OBJECT macro."); checkSuperClasses(&def); + checkProperties(&def); classList += def; knownQObjectClasses.insert(def.classname); @@ -1312,5 +1313,62 @@ void Moc::checkSuperClasses(ClassDef *def) } } +void Moc::checkProperties(ClassDef *cdef) +{ + // + // specify get function, for compatibiliy we accept functions + // returning pointers, or const char * for QByteArray. + // + for (int i = 0; i < cdef->propertyList.count(); ++i) { + PropertyDef &p = cdef->propertyList[i]; + if (p.read.isEmpty()) + continue; + for (int j = 0; j < cdef->publicList.count(); ++j) { + const FunctionDef &f = cdef->publicList.at(j); + if (f.name != p.read) + continue; + if (!f.isConst) // get functions must be const + continue; + if (f.arguments.size()) // and must not take any arguments + continue; + PropertyDef::Specification spec = PropertyDef::ValueSpec; + QByteArray tmp = f.normalizedType; + if (p.type == "QByteArray" && tmp == "const char *") + tmp = "QByteArray"; + if (tmp.left(6) == "const ") + tmp = tmp.mid(6); + if (p.type != tmp && tmp.endsWith('*')) { + tmp.chop(1); + spec = PropertyDef::PointerSpec; + } else if (f.type.name.endsWith('&')) { // raw type, not normalized type + spec = PropertyDef::ReferenceSpec; + } + if (p.type != tmp) + continue; + p.gspec = spec; + break; + } + if(!p.notify.isEmpty()) { + int notifyId = -1; + for (int j = 0; j < cdef->signalList.count(); ++j) { + const FunctionDef &f = cdef->signalList.at(j); + if(f.name != p.notify) { + continue; + } else { + notifyId = j /* Signal indexes start from 0 */; + break; + } + } + p.notifyId = notifyId; + if (notifyId == -1) { + QByteArray msg = "NOTIFY signal '" + p.notify + "' of property '" + p.name + + "' does not exist in class " + cdef->classname + "."; + error(msg.constData()); + } + } + } +} + + QT_END_NAMESPACE diff --git a/src/tools/moc/moc.h b/src/tools/moc/moc.h index 9f349b5..5e47d9a 100644 --- a/src/tools/moc/moc.h +++ b/src/tools/moc/moc.h @@ -238,6 +238,7 @@ public: bool testFunctionAttribute(Token tok, FunctionDef *def); void checkSuperClasses(ClassDef *def); + void checkProperties(ClassDef* cdef); }; inline QByteArray noRef(const QByteArray &type) diff --git a/tests/auto/moc/error-on-wrong-notify.h b/tests/auto/moc/error-on-wrong-notify.h new file mode 100644 index 0000000..4b6284a --- /dev/null +++ b/tests/auto/moc/error-on-wrong-notify.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include + +class ClassWithWrongNOTIFY : public QObject +{ + Q_OBJECT + Q_PROPERTY(int foo READ foo WRITE setFoo NOTIFY fooChanged) + + int m_foo; +public: + void setFoo(int i) { m_foo = i; } + int foo() { return m_foo; } +}; + diff --git a/tests/auto/moc/tst_moc.cpp b/tests/auto/moc/tst_moc.cpp index d3a7e03..f9c3ccb 100644 --- a/tests/auto/moc/tst_moc.cpp +++ b/tests/auto/moc/tst_moc.cpp @@ -492,6 +492,7 @@ private slots: void warnOnVirtualSignal(); void QTBUG5590_dummyProperty(); void QTBUG12260_defaultTemplate(); + void notifyError(); signals: void sigWithUnsignedArg(unsigned foo); void sigWithSignedArg(signed foo); @@ -1362,6 +1363,27 @@ void tst_Moc::QTBUG12260_defaultTemplate() QVERIFY(QTBUG12260_defaultTemplate_Object::staticMetaObject.indexOfSlot("doAnotherThing(bool,bool)") != -1); } +void tst_Moc::notifyError() +{ +#ifdef MOC_CROSS_COMPILED + QSKIP("Not tested when cross-compiled", SkipAll); +#endif +#if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(QT_NO_PROCESS) + QProcess proc; + proc.start("moc", QStringList(srcify("error-on-wrong-notify.h"))); + QVERIFY(proc.waitForFinished()); + QCOMPARE(proc.exitCode(), 1); + QCOMPARE(proc.exitStatus(), QProcess::NormalExit); + QByteArray mocOut = proc.readAllStandardOutput(); + QVERIFY(mocOut.isEmpty()); + QString mocError = QString::fromLocal8Bit(proc.readAllStandardError()); + QCOMPARE(mocError, QString(SRCDIR) + + QString("/error-on-wrong-notify.h:52: Error: NOTIFY signal 'fooChanged' of property 'foo' does not exist in class ClassWithWrongNOTIFY.\n")); +#else + QSKIP("Only tested on linux/gcc", SkipAll); +#endif +} + QTEST_APPLESS_MAIN(tst_Moc) #include "tst_moc.moc" -- cgit v0.12