forked from qt-creator/qt-creator
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user