Skip to content

Avoid useless setField calls when value is default #2443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
} else {
this.resultModel = resultModel
val expected = variableConstructor.getOrCreateVariable(resultModel, "expected")
emptyLineIfNeeded()
assertEquality(expected, actual)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import org.utbot.framework.plugin.api.UtPrimitiveModel
import org.utbot.framework.plugin.api.UtReferenceModel
import org.utbot.framework.plugin.api.UtStatementCallModel
import org.utbot.framework.plugin.api.UtVoidModel
import org.utbot.framework.plugin.api.util.booleanClassId
import org.utbot.framework.plugin.api.util.booleanWrapperClassId
import org.utbot.framework.plugin.api.util.classClassId
import org.utbot.framework.plugin.api.util.defaultValueModel
import org.utbot.framework.plugin.api.util.jField
Expand All @@ -51,6 +53,8 @@ import org.utbot.framework.plugin.api.util.isArray
import org.utbot.framework.plugin.api.util.isEnum
import org.utbot.framework.plugin.api.util.isPrimitiveWrapperOrString
import org.utbot.framework.plugin.api.util.isStatic
import org.utbot.framework.plugin.api.util.primitiveWrappers
import org.utbot.framework.plugin.api.util.primitives
import org.utbot.framework.plugin.api.util.stringClassId
import org.utbot.framework.plugin.api.util.supertypeOfAnonymousClass
import org.utbot.framework.plugin.api.util.wrapperByPrimitive
Expand Down Expand Up @@ -171,12 +175,13 @@ open class CgVariableConstructor(val context: CgContext) :

for ((fieldId, fieldModel) in model.fields) {
val variableForField = getOrCreateVariable(fieldModel)
setFieldValue(obj, fieldId, variableForField)
if (!variableForField.hasDefaultValue())
setFieldValue(obj, fieldId, variableForField)
}
return obj
}

fun setFieldValue(obj: CgValue, fieldId: FieldId, variableForField: CgValue){
fun setFieldValue(obj: CgValue, fieldId: FieldId, valueForField: CgValue) {
val field = fieldId.jField
val fieldFromVariableSpecifiedType = obj.type.findFieldByIdOrNull(fieldId)

Expand All @@ -187,14 +192,27 @@ open class CgVariableConstructor(val context: CgContext) :
// branchRegisterRequest.byteBuffer = heapByteBuffer;
// byteBuffer is field of type ByteBuffer and upper line is incorrect
val canFieldBeDirectlySetByVariableAndFieldTypeRestrictions =
fieldFromVariableSpecifiedType != null && fieldFromVariableSpecifiedType.type.id == variableForField.type
fieldFromVariableSpecifiedType != null && fieldFromVariableSpecifiedType.type.id == valueForField.type
if (canFieldBeDirectlySetByVariableAndFieldTypeRestrictions && fieldId.canBeSetFrom(context, obj.type)) {
// TODO: check if it is correct to use declaringClass of a field here
val fieldAccess = if (field.isStatic) CgStaticFieldAccess(fieldId) else CgFieldAccess(obj, fieldId)
fieldAccess `=` variableForField
fieldAccess `=` valueForField
} else {
// composite models must not have info about static fields, hence only non-static fields are set here
+utilsClassId[setField](obj, fieldId.declaringClass.name, fieldId.name, variableForField)
+utilsClassId[setField](obj, fieldId.declaringClass.name, fieldId.name, valueForField)
}
}

private fun CgValue.hasDefaultValue(): Boolean {
if (this !is CgLiteral) {
return false;
}

return when {
this.value == null -> true
(this.type == booleanClassId || this.type == booleanWrapperClassId) && this.value == false -> true
(this.type in primitives || this.type in primitiveWrappers) && this.value == 0 -> true
else -> false
}
}

Expand Down