Repository: microsoft/onnxruntime
File: onnxruntime/core/framework/tensor_type_and_shape.cc (C++)

Code before:
  return type;
}

ONNXTensorElementDataType MLDataTypeToOnnxRuntimeTensorElementDataType(
    const onnxruntime::DataTypeImpl* cpp_type) {
  auto prim_type = cpp_type->AsPrimitiveDataType();
  if (prim_type == nullptr) {
    return ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED;
  }
  return TensorDataTypeToOnnxRuntimeTensorElementDataType(prim_type->GetDataType());
}

std::unique_ptr<OrtTensorTypeAndShapeInfo> OrtTensorTypeAndShapeInfo::GetTensorShapeAndTypeHelper(
    ONNXTensorElementDataType type,
    const onnxruntime::TensorShape* shape,
    const std::vector<std::string>* dim_params) {
  auto type_and_shape = std::make_unique<OrtTensorTypeAndShapeInfo>();

  type_and_shape->SetElementType(type);

  if (shape != nullptr) {
    ShapeInfo shape_info;
    shape_info.shape = *shape;
    if (dim_params != nullptr) {
      shape_info.dim_params = *dim_params;
    }
    shape_info.dim_params.resize(shape_info.shape.NumDimensions());
    type_and_shape->SetShape(std::move(shape_info));
  }
  return type_and_shape;
}

std::unique_ptr<OrtTensorTypeAndShapeInfo> OrtTensorTypeAndShapeInfo::GetTensorShapeAndType(
    const onnxruntime::TensorShape* shape,
    const onnxruntime::DataTypeImpl& tensor_data_type) {
  ONNXTensorElementDataType type = MLDataTypeToOnnxRuntimeTensorElementDataType(&tensor_data_type);
  if (ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED == type) {
    ORT_NOT_IMPLEMENTED("Tensor type is undefined");
  }
  return GetTensorShapeAndTypeHelper(type, shape, nullptr);
}

std::unique_ptr<OrtTensorTypeAndShapeInfo> OrtTensorTypeAndShapeInfo::GetTensorShapeAndType(
    const onnxruntime::TensorShape* shape,
    const std::vector<std::string>* dim_params,
    const ONNX_NAMESPACE::TypeProto& type_proto) {
  auto value_case = type_proto.value_case();
  assert(value_case == ONNX_NAMESPACE::TypeProto::kTensorType ||
         value_case == ONNX_NAMESPACE::TypeProto::kSparseTensorType);

  auto dtype = (value_case == ONNX_NAMESPACE::TypeProto::kTensorType) ? type_proto.tensor_type().elem_type()

Review comment (refactor):
If `GetTensorShapeAndTypeHelper` is called with a null `shape` and a valid (non-null) `dim_params` argument, would the final `OrtTensorTypeAndShapeInfo` have a shape? It looks like it wouldn't, but maybe it should have a shape with all -1?

Code after:
ONNXTensorElementDataType MLDataTypeToOnnxRuntimeTensorElementDataType(
    const onnxruntime::DataTypeImpl* cpp_type) {
  auto prim_type = cpp_type->AsPrimitiveDataType();
  if (prim_type == nullptr) {
    return ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED;
  }
  return TensorDataTypeToOnnxRuntimeTensorElementDataType(prim_type->GetDataType());
}

std::unique_ptr<OrtTensorTypeAndShapeInfo> OrtTensorTypeAndShapeInfo::GetTensorShapeAndTypeHelper(
    ONNXTensorElementDataType type,
    const onnxruntime::TensorShape* shape,
    const std::vector<std::string>* dim_params) {
  auto type_and_shape = std::make_unique<OrtTensorTypeAndShapeInfo>();
  type_and_shape->SetElementType(type);

  if (shape == nullptr && dim_params == nullptr) {
    return type_and_shape;
  }

  ShapeInfo shape_info;
  size_t num_dims = (shape != nullptr) ? shape->NumDimensions() : 0;
  num_dims = std::max(num_dims, (dim_params != nullptr) ? dim_params->size() : 0);

  onnxruntime::TensorShapeVector shape_vec;
  if (shape != nullptr) {
    shape_vec = shape->AsShapeVector();
  }
  shape_vec.resize(num_dims, -1);
  shape_info.shape = onnxruntime::TensorShape(shape_vec);

  if (dim_params != nullptr) {
    shape_info.dim_params = *dim_params;
  }
  shape_info.dim_params.resize(num_dims);

  type_and_shape->SetShape(std::move(shape_info));
  return type_and_shape;
}

std::unique_ptr<OrtTensorTypeAndShapeInfo> OrtTensorTypeAndShapeInfo::GetTensorShapeAndType(
    const onnxruntime::TensorShape* shape,
    const onnxruntime::DataTypeImpl& tensor_data_type) {
  ONNXTensorElementDataType type = MLDataTypeToOnnxRuntimeTensorElementDataType(&tensor_data_type);
  if (ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED == type) {
    ORT_NOT_IMPLEMENTED("Tensor type is undefined");
  }
  return GetTensorShapeAndTypeHelper(type, shape, nullptr);
}

std::unique_ptr<OrtTensorTypeAndShapeInfo> OrtTensorTypeAndShapeInfo::GetTensorShapeAndType(