CppEditor: Use qFuzzyCompare when comparing member variable for qproperty refactoring

For a line like

    Q_PROPERTY(qreal foo READ foo WRITE setFoo NOTIFY fooChanged)

the generated setter will now use "if(qFuzzyCompare(m_foo, foo))" instead of "if (m_foo == foo)"
for types that are supported by qFuzzyCompare (that is: qreal, double and float).

A warning stating that "Floating point comparison needs context sanity check" is as well added
to remind the user to check/fix the generated code.

Change-Id: I8d274d5c072d107f0d04b1e153b5cc183e6317fc
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Christian Gagneraud
2017-02-25 11:45:13 +13:00
parent d42c35ab07
commit 972b48ef24

View File

@@ -195,6 +195,13 @@ inline bool isQtStringTranslation(const QByteArray &id)
return id == "tr" || id == "trUtf8" || id == "translate" || id == "QT_TRANSLATE_NOOP";
}
inline bool isQtFuzzyComparable(const QString &typeName)
{
return typeName == QLatin1String("double")
|| typeName == QLatin1String("float")
|| typeName == QLatin1String("qreal");
}
Class *isMemberFunction(const LookupContext &context, Function *function)
{
QTC_ASSERT(function, return 0);
@@ -4461,8 +4468,13 @@ public:
if (m_signalName.isEmpty()) {
setter << m_storageName << " = " << baseName << ";\n}\n";
} else {
setter << "if (" << m_storageName << " == " << baseName << ")\nreturn;\n\n"
<< m_storageName << " = " << baseName << ";\nemit " << m_signalName
if (isQtFuzzyComparable(typeName)) {
setter << "qWarning(\"Floating point comparison needs context sanity check\");\n";
setter << "if (qFuzzyCompare(" << m_storageName << ", " << baseName << "))\nreturn;\n\n";
}
else
setter << "if (" << m_storageName << " == " << baseName << ")\nreturn;\n\n";
setter << m_storageName << " = " << baseName << ";\nemit " << m_signalName
<< '(' << m_storageName << ");\n}\n";
}
InsertionLocation setterLoc = locator.methodDeclarationInClass(file->fileName(), m_class, InsertionPointLocator::PublicSlot);