332 ContentEqFn contentEq)
335 const auto &oldAccessor = oldSource.accessor;
336 const auto &newAccessor = newSource.accessor;
337 const quint32 oldCount = oldSource.count;
338 const quint32 newCount = newSource.count;
340 const auto disambiguatedKeys = [&](
auto accessor, quint32 count,
const Unit *unit) {
343 QHash<QString,
int> seen;
344 for (quint32 i = 0; i < count; ++i) {
345 const QString base = keyFn(accessor(i), unit);
346 keys.append(base + QChar(QChar::Null) + QString::number(seen[base]++));
351 const QList<QString> oldKeys = disambiguatedKeys(oldAccessor, oldCount, oldUnit);
352 const QList<QString> newKeys = disambiguatedKeys(newAccessor, newCount, newUnit);
354 QHash<QString, quint32> oldByKey;
355 oldByKey.reserve(oldCount);
356 for (quint32 i = 0; i < oldCount; ++i)
357 oldByKey.insert(oldKeys[i], i);
359 QList<
bool> oldMatched(oldCount,
false);
360 for (quint32 j = 0; j < newCount; ++j) {
361 const auto it = oldByKey.constFind(newKeys[j]);
362 if (it == oldByKey.constEnd()) {
363 changes.append(Change{ Traits::Added, m_currentObjectIndex, j, j,
364 extractData(newAccessor(j)) });
368 const quint32 i = it.value();
369 oldMatched[i] =
true;
370 const ElementT &oldElem = oldAccessor(i);
371 const ElementT &newElem = newAccessor(j);
373 if (!contentEq(oldElem, newElem)) {
374 changes.append(Change{ Traits::Changed, m_currentObjectIndex, j, i,
375 extractData(newElem) });
378 if (Traits::LocationChanged != ChangeType::None
379 && LocationExtractor<ElementT>::extract(oldElem)
380 != LocationExtractor<ElementT>::extract(newElem)) {
381 changes.append(Change{ Traits::LocationChanged, m_currentObjectIndex, j, i,
382 extractData(newElem) });
386 changes.append(Change{ Traits::IndexChanged, m_currentObjectIndex, j, i,
387 extractData(newElem) });
391 for (quint32 i = 0; i < oldCount; ++i) {
393 changes.append(Change{ Traits::Removed, m_currentObjectIndex, i, i, NoHunk{} });
529 static_assert(
sizeof(Binding) == 24,
"Update bindingContentEqual when Binding layout changes");
530 static_assert(
sizeof(TranslationData) == 16,
"Update bindingContentEqual when TranslationData layout changes");
531 if (oldBinding.type() != newBinding.type() || oldBinding.flags() != newBinding.flags())
534 if (!stringsEqual(oldBinding.propertyNameIndex, newBinding.propertyNameIndex))
537 switch (oldBinding.type()) {
538 case Binding::Type_Invalid:
540 case Binding::Type_Boolean:
541 return oldBinding.valueAsBoolean() == newBinding.valueAsBoolean();
542 case Binding::Type_Number: {
543 const uint oldIdx = oldBinding.value.constantValueIndex;
544 const uint newIdx = newBinding.value.constantValueIndex;
545 Q_ASSERT(oldIdx < oldUnit->constantTableSize);
546 Q_ASSERT(newIdx < newUnit->constantTableSize);
547 return oldUnit->constants()[oldIdx] == newUnit->constants()[newIdx];
549 case Binding::Type_String:
550 return stringsEqual(oldBinding.stringIndex, newBinding.stringIndex);
551 case Binding::Type_Null:
553 case Binding::Type_Translation:
554 case Binding::Type_TranslationById: {
555 const auto &oldTrans = oldUnit->translations()[oldBinding.value.translationDataIndex];
556 const auto &newTrans = newUnit->translations()[newBinding.value.translationDataIndex];
560 const bool oldHasCtx = oldTrans.contextIndex != TranslationData::NoContextIndex;
561 const bool newHasCtx = newTrans.contextIndex != TranslationData::NoContextIndex;
563 if (oldHasCtx != newHasCtx)
566 if (oldHasCtx && !stringsEqual(oldTrans.contextIndex, newTrans.contextIndex))
569 return stringsEqual(oldBinding.stringIndex, newBinding.stringIndex)
570 && stringsEqual(oldTrans.stringIndex, newTrans.stringIndex)
571 && stringsEqual(oldTrans.commentIndex, newTrans.commentIndex)
572 && oldTrans.number == newTrans.number;
574 case Binding::Type_Script:
575 return oldBinding.value.compiledScriptIndex == newBinding.value.compiledScriptIndex;
576 case Binding::Type_Object:
577 case Binding::Type_AttachedProperty:
578 case Binding::Type_GroupedProperty:
579 return oldBinding.value.objectIndex == newBinding.value.objectIndex
580 && objectContentEqual(
581 *oldUnit->qmlUnit()->objectAt(oldBinding.value.objectIndex),
582 *newUnit->qmlUnit()->objectAt(newBinding.value.objectIndex));
857 const QmlUnit *oldQml = oldUnit->qmlUnit();
858 const QmlUnit *newQml = newUnit->qmlUnit();
860 if (!oldQml || !newQml)
867 change.data = extractData(*newUnit);
868 changes.append(change);
871 compareIndexedElements<Object>(
873 [oldQml](quint32 i) ->
const Object & {
return *oldQml->objectAt(i); },
876 [newQml](quint32 i) ->
const Object & {
return *newQml->objectAt(i); },
878 [
this](
const Object &a,
const Object &b) {
return objectContentEqual(a, b); });
881 const quint32 commonCount = std::min(oldQml->nObjects, newQml->nObjects);
882 for (quint32 objectIndex = 0; objectIndex < commonCount; ++objectIndex) {
884 const Object *oldObj = oldQml->objectAt(objectIndex);
885 const Object *newObj = newQml->objectAt(objectIndex);
887 compareObjectProperties(oldObj, newObj);
888 compareObjectAliases(oldObj, newObj);
889 compareObjectEnums(oldObj, newObj);
890 compareObjectSignals(oldObj, newObj);
891 compareObjectInlineComponents(oldObj, newObj);
892 compareObjectRequiredPropertyExtraData(oldObj, newObj);
893 compareObjectBindings(oldObj, newObj);
896 compareIndexedElements<quint64_le>(
897 IndexedElements{ [
this](quint32 i) {
return oldUnit->constants()[i]; },
898 oldUnit->constantTableSize },
899 IndexedElements{ [
this](quint32 i) {
return newUnit->constants()[i]; },
900 newUnit->constantTableSize },
901 [](
const quint64_le &a,
const quint64_le &b) {
return a == b; });
903 compareIndexedElements<QString>(
904 IndexedElements{ [
this](quint32 i) {
return oldUnit->stringAtInternal(i); },
905 oldUnit->stringTableSize },
906 IndexedElements{ [
this](quint32 i) {
return newUnit->stringAtInternal(i); },
907 newUnit->stringTableSize },
908 [](
const QString &a,
const QString &b) {
return a == b; });
910 compareIndexedElements<Lookup>(
911 IndexedElements{ [
this](quint32 i) {
return oldUnit->lookupTable()[i]; },
912 oldUnit->lookupTableSize },
913 IndexedElements{ [
this](quint32 i) {
return newUnit->lookupTable()[i]; },
914 newUnit->lookupTableSize },
915 [
this](
const Lookup &a,
const Lookup &b) {
916 return a.type() == b.type() && a.mode() == b.mode()
917 && stringsEqual(a.nameIndex(), b.nameIndex());
920 compareIndexedElements<Import>(
921 IndexedElements{ [oldQml](quint32 i) {
return *oldQml->importAt(i); },
923 IndexedElements{ [newQml](quint32 i) {
return *newQml->importAt(i); },
925 [
this](
const Import &a,
const Import &b) {
926 return a.type == b.type && stringsEqual(a.uriIndex, b.uriIndex)
927 && stringsEqual(a.qualifierIndex, b.qualifierIndex)
928 && a.version == b.version;
936 const QHash<
const Function *, QString> oldFunctionKeys = functionOwnerKeys(oldUnit);
937 const QHash<
const Function *, QString> newFunctionKeys = functionOwnerKeys(newUnit);
938 compareNamedElements<Function>(
940 [
this](quint32 i) ->
const Function & {
return *oldUnit->functionAt(i); },
941 oldUnit->functionTableSize },
943 [
this](quint32 i) ->
const Function & {
return *newUnit->functionAt(i); },
944 newUnit->functionTableSize },
945 [&](
const Function &f,
const Unit *unit) {
946 const QHash<
const Function *, QString> &keys =
947 unit == oldUnit ? oldFunctionKeys : newFunctionKeys;
948 return keys.value(&f, unit->stringAtInternal(f.nameIndex));
950 [
this](
const Function &a,
const Function &b) {
951 return functionContentEqual(a, b);
954 compareIndexedElements<TranslationData>(
955 IndexedElements{ [
this](quint32 i) {
return oldUnit->translations()[i]; },
956 oldUnit->translationTableSize },
957 IndexedElements{ [
this](quint32 i) {
return newUnit->translations()[i]; },
958 newUnit->translationTableSize },
959 [
this](
const TranslationData &a,
const TranslationData &b) {
960 return translationContentEqual(a, b);
963 compareIndexedElements<RegExp>(
965 [
this](quint32 i) ->
const RegExp & {
return *oldUnit->regexpAt(i); },
966 oldUnit->regexpTableSize },
968 [
this](quint32 i) ->
const RegExp & {
return *newUnit->regexpAt(i); },
969 newUnit->regexpTableSize },
970 [
this](
const RegExp &a,
const RegExp &b) {
971 return a.flags() == b.flags() && stringsEqual(a.stringIndex(), b.stringIndex());
974 compareIndexedElements<Class>(
976 [
this](quint32 i) ->
const Class & {
return *oldUnit->classAt(i); },
977 oldUnit->classTableSize },
979 [
this](quint32 i) ->
const Class & {
return *newUnit->classAt(i); },
980 newUnit->classTableSize },
981 [
this](
const Class &a,
const Class &b) {
return classContentEqual(a, b); });
983 compareIndexedElements<TemplateObject>(
984 IndexedElements{ [
this](quint32 i) ->
const TemplateObject & {
985 return *oldUnit->templateObjectAt(i);
987 oldUnit->templateObjectTableSize },
988 IndexedElements{ [
this](quint32 i) ->
const TemplateObject & {
989 return *newUnit->templateObjectAt(i);
991 newUnit->templateObjectTableSize },
992 [
this](
const TemplateObject &a,
const TemplateObject &b) {
993 return templateObjectContentEqual(a, b);
997 auto jsClassFromUnit = [](
const Unit *unit, quint32 idx) ->
const JSClass & {
998 const quint32_le *offsetTable =
reinterpret_cast<
const quint32_le *>(
999 reinterpret_cast<
const char *>(unit) + unit->offsetToJSClassTable);
1000 return *
reinterpret_cast<
const JSClass *>(
reinterpret_cast<
const char *>(unit)
1001 + offsetTable[idx]);
1003 compareIndexedElements<JSClass>(
1004 IndexedElements{ [
this, jsClassFromUnit](quint32 i) ->
const JSClass & {
1005 return jsClassFromUnit(oldUnit, i);
1007 oldUnit->jsClassTableSize },
1008 IndexedElements{ [
this, jsClassFromUnit](quint32 i) ->
const JSClass & {
1009 return jsClassFromUnit(newUnit, i);
1011 newUnit->jsClassTableSize },
1012 [
this](
const JSClass &a,
const JSClass &b) {
1013 return jsClassContentEqual(a, b);
1017 compareIndexedElements<Block>(
1019 [
this](quint32 i) ->
const Block & {
return *oldUnit->blockAt(i); },
1020 oldUnit->blockTableSize },
1022 [
this](quint32 i) ->
const Block & {
return *newUnit->blockAt(i); },
1023 newUnit->blockTableSize },
1024 [
this](
const Block &a,
const Block &b) {
return blockContentEqual(a, b); });
1026 return CompilationUnitDiff{ std::move(changes),
true };