Skip to content

Commit

Permalink
Add trimmer annotations to System.Data.Common (#52046)
Browse files Browse the repository at this point in the history
Add trimmer annotations to System.Data.Common

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
  • Loading branch information
krwq and eerhardt committed Jun 11, 2021
1 parent 6cf9af2 commit c2f559c
Show file tree
Hide file tree
Showing 87 changed files with 1,060 additions and 415 deletions.
423 changes: 255 additions & 168 deletions src/libraries/System.Data.Common/ref/System.Data.Common.cs

Large diffs are not rendered by default.

214 changes: 26 additions & 188 deletions src/libraries/System.Data.Common/src/ILLink/ILLink.Suppressions.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Numerics;
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Common
{
Expand Down Expand Up @@ -149,11 +150,13 @@ public override void SetCapacity(int capacity)
base.SetCapacity(capacity);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override object ConvertXmlToObject(string s)
{
return BigInteger.Parse(s, System.Globalization.CultureInfo.InvariantCulture);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override string ConvertObjectToXml(object value)
{
return ((BigInteger)value).ToString("D", System.Globalization.CultureInfo.InvariantCulture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Xml;
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Common
{
Expand Down Expand Up @@ -169,11 +170,13 @@ public override void SetCapacity(int capacity)
base.SetCapacity(capacity);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override object ConvertXmlToObject(string s)
{
return XmlConvert.ToBoolean(s);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override string ConvertObjectToXml(object value)
{
return XmlConvert.ToString((bool)value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Xml;
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Common
{
Expand Down Expand Up @@ -241,11 +242,13 @@ public override void SetCapacity(int capacity)
base.SetCapacity(capacity);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override object ConvertXmlToObject(string s)
{
return XmlConvert.ToByte(s);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override string ConvertObjectToXml(object value)
{
return XmlConvert.ToString((byte)value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Xml;
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Common
{
Expand Down Expand Up @@ -173,11 +174,13 @@ public override void SetCapacity(int capacity)
base.SetCapacity(capacity);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override object ConvertXmlToObject(string s)
{
return XmlConvert.ToChar(s);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override string ConvertObjectToXml(object value)
{
return XmlConvert.ToString((char)value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,7 @@ public DbCommand GetInsertCommand(bool useColumnsForParameterNames)
{
return GetInsertCommand(null, useColumnsForParameterNames);
}

internal DbCommand GetInsertCommand(DataRow? dataRow, bool useColumnsForParameterNames)
{
BuildCache(true, dataRow, useColumnsForParameterNames);
Expand All @@ -1329,10 +1330,12 @@ public DbCommand GetUpdateCommand()
{
return GetUpdateCommand(null, false);
}

public DbCommand GetUpdateCommand(bool useColumnsForParameterNames)
{
return GetUpdateCommand(null, useColumnsForParameterNames);
}

internal DbCommand GetUpdateCommand(DataRow? dataRow, bool useColumnsForParameterNames)
{
BuildCache(true, dataRow, useColumnsForParameterNames);
Expand All @@ -1344,10 +1347,12 @@ public DbCommand GetDeleteCommand()
{
return GetDeleteCommand(null, false);
}

public DbCommand GetDeleteCommand(bool useColumnsForParameterNames)
{
return GetDeleteCommand(null, useColumnsForParameterNames);
}

internal DbCommand GetDeleteCommand(DataRow? dataRow, bool useColumnsForParameterNames)
{
BuildCache(true, dataRow, useColumnsForParameterNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace System.Data.Common
Expand All @@ -27,8 +28,8 @@ internal static DbSchemaRow[] GetSortedSchemaRows(DataTable dataTable, bool retu
};
DbSchemaTable schemaTable = new DbSchemaTable(dataTable, returnProviderSpecificTypes);

const DataViewRowState rowStates = DataViewRowState.Unchanged | DataViewRowState.Added | DataViewRowState.ModifiedCurrent;
DataRow[] dataRows = dataTable.Select(null, "ColumnOrdinal ASC", rowStates);

DataRow[] dataRows = SelectRows(dataTable, returnProviderSpecificTypes);
Debug.Assert(null != dataRows, "GetSchemaRows: unexpected null dataRows");

DbSchemaRow[] schemaRows = new DbSchemaRow[dataRows.Length];
Expand All @@ -40,6 +41,14 @@ internal static DbSchemaRow[] GetSortedSchemaRows(DataTable dataTable, bool retu
return schemaRows;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Filter expression is null.")]
private static DataRow[] SelectRows(DataTable dataTable, bool returnProviderSpecificTypes)
{
const DataViewRowState rowStates = DataViewRowState.Unchanged | DataViewRowState.Added | DataViewRowState.ModifiedCurrent;
return dataTable.Select(null, "ColumnOrdinal ASC", rowStates);
}

internal DbSchemaRow(DbSchemaTable schemaTable, DataRow dataRow)
{
_schemaTable = schemaTable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ object ICloneable.Clone()
}

[EditorBrowsable(EditorBrowsableState.Advanced)]
public DataColumn? GetDataColumnBySchemaAction(DataTable dataTable, Type? dataType, MissingSchemaAction schemaAction)
public DataColumn? GetDataColumnBySchemaAction(DataTable dataTable, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type? dataType, MissingSchemaAction schemaAction)
{
return GetDataColumnBySchemaAction(SourceColumn, DataSetColumn, dataTable, dataType, schemaAction);
}

[EditorBrowsable(EditorBrowsableState.Advanced)]
public static DataColumn? GetDataColumnBySchemaAction(string? sourceColumn, string? dataSetColumn, DataTable dataTable, Type? dataType, MissingSchemaAction schemaAction)
public static DataColumn? GetDataColumnBySchemaAction(string? sourceColumn, string? dataSetColumn, DataTable dataTable, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type? dataType, MissingSchemaAction schemaAction)
{
if (null == dataTable)
{
Expand Down Expand Up @@ -112,7 +112,7 @@ object ICloneable.Clone()
return CreateDataColumnBySchemaAction(sourceColumn, dataSetColumn, dataTable, dataType, schemaAction);
}

internal static DataColumn? CreateDataColumnBySchemaAction(string? sourceColumn, string? dataSetColumn, DataTable dataTable, Type? dataType, MissingSchemaAction schemaAction)
internal static DataColumn? CreateDataColumnBySchemaAction(string? sourceColumn, string? dataSetColumn, DataTable dataTable, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type? dataType, MissingSchemaAction schemaAction)
{
Debug.Assert(dataTable != null, "Should not call with a null DataTable");
if (string.IsNullOrEmpty(dataSetColumn))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ internal void ValidateSourceColumn(int index, string? value)
}

[EditorBrowsable(EditorBrowsableState.Advanced)]
public static DataColumn? GetDataColumn(DataColumnMappingCollection? columnMappings, string sourceColumn, Type? dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
public static DataColumn? GetDataColumn(DataColumnMappingCollection? columnMappings, string sourceColumn, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type? dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
{
if (null != columnMappings)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Concurrent;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Xml;
using System.Xml.Serialization;

Expand Down Expand Up @@ -269,19 +270,25 @@ public virtual void SetCapacity(int capacity)
}
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public abstract object ConvertXmlToObject(string s);

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public virtual object ConvertXmlToObject(XmlReader xmlReader, XmlRootAttribute xmlAttrib)
{
return ConvertXmlToObject(xmlReader.Value);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public abstract string ConvertObjectToXml(object value);

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public virtual void ConvertObjectToXml(object value, XmlWriter xmlWriter, XmlRootAttribute? xmlAttrib)
{
xmlWriter.WriteString(ConvertObjectToXml(value)); // should it be NO OP?
}

public static DataStorage CreateStorage(DataColumn column, Type dataType, StorageType typeCode)
public static DataStorage CreateStorage(DataColumn column, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type dataType, StorageType typeCode)
{
Debug.Assert(typeCode == GetStorageType(dataType), "Incorrect storage type specified");
if ((StorageType.Empty == typeCode) && (null != dataType))
Expand Down Expand Up @@ -557,6 +564,7 @@ protected void SetNullStorage(BitArray nullbits)
/// Types like "System.Data.SqlTypes.SqlString" will load because they are in the same assembly as this code
/// Types like "System.Numerics.BigInteger" won't load because they are not special and not same assembly as this code
/// </remarks>
[RequiresUnreferencedCode("Calls Type.GetType")]
internal static Type GetType(string value)
{
Type? dataType = Type.GetType(value); // throwOnError=false, ignoreCase=fase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ object ICloneable.Clone()
}

[EditorBrowsable(EditorBrowsableState.Advanced)]
public DataColumn? GetDataColumn(string sourceColumn, Type? dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
public DataColumn? GetDataColumn(string sourceColumn, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type? dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
{
return DataColumnMappingCollection.GetDataColumn(_columnMappings, sourceColumn, dataType, dataTable, mappingAction, schemaAction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Xml;
using System.Collections;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Common
{
Expand Down Expand Up @@ -176,11 +177,13 @@ public override void SetCapacity(int capacity)
base.SetCapacity(capacity);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override object ConvertXmlToObject(string s)
{
return XmlConvert.ToDateTimeOffset(s);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override string ConvertObjectToXml(object value)
{
return XmlConvert.ToString((DateTimeOffset)value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Xml;
using System.Collections;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Common
{
Expand Down Expand Up @@ -215,6 +216,7 @@ public override void SetCapacity(int capacity)
base.SetCapacity(capacity);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override object ConvertXmlToObject(string s)
{
object retValue;
Expand All @@ -229,6 +231,7 @@ public override object ConvertXmlToObject(string s)
return retValue;
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override string ConvertObjectToXml(object value)
{
string retValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Data.ProviderBase;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Common
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ public static partial class DbProviderFactories
{
private struct ProviderRegistration
{
internal ProviderRegistration(string factoryTypeAssemblyQualifiedName, DbProviderFactory? factoryInstance)
internal ProviderRegistration([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] string factoryTypeAssemblyQualifiedName, DbProviderFactory? factoryInstance)
{
this.FactoryTypeAssemblyQualifiedName = factoryTypeAssemblyQualifiedName;
this.FactoryInstance = factoryInstance;
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
internal string FactoryTypeAssemblyQualifiedName { get; }

/// <summary>
Expand Down Expand Up @@ -51,6 +52,7 @@ public static DbProviderFactory GetFactory(string providerInvariantName)
return GetFactory(providerInvariantName, throwOnError: true)!;
}

[RequiresUnreferencedCode("Provider type and its members might be trimmed if not referenced directly.")]
public static DbProviderFactory GetFactory(DataRow providerRow)
{
ADP.CheckArgumentNull(providerRow, nameof(providerRow));
Expand Down Expand Up @@ -105,7 +107,7 @@ public static IEnumerable<string> GetProviderInvariantNames()
return _registeredFactories.Keys.ToList();
}

public static void RegisterFactory(string providerInvariantName, string factoryTypeAssemblyQualifiedName)
public static void RegisterFactory(string providerInvariantName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] string factoryTypeAssemblyQualifiedName)
{
ADP.CheckArgumentLength(providerInvariantName, nameof(providerInvariantName));
ADP.CheckArgumentLength(factoryTypeAssemblyQualifiedName, nameof(factoryTypeAssemblyQualifiedName));
Expand All @@ -114,7 +116,7 @@ public static void RegisterFactory(string providerInvariantName, string factoryT
_registeredFactories[providerInvariantName] = new ProviderRegistration(factoryTypeAssemblyQualifiedName, null);
}

public static void RegisterFactory(string providerInvariantName, Type providerFactoryClass)
public static void RegisterFactory(string providerInvariantName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type providerFactoryClass)
{
RegisterFactory(providerInvariantName, GetFactoryInstance(providerFactoryClass));
}
Expand Down Expand Up @@ -163,7 +165,7 @@ public static bool UnregisterFactory(string providerInvariantName)
return toReturn;
}

private static DbProviderFactory GetFactoryInstance(Type providerFactoryClass)
private static DbProviderFactory GetFactoryInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type providerFactoryClass)
{
ADP.CheckArgumentNull(providerFactoryClass, nameof(providerFactoryClass));
if (!providerFactoryClass.IsSubclassOf(typeof(DbProviderFactory)))
Expand All @@ -188,8 +190,8 @@ private static DbProviderFactory GetFactoryInstance(Type providerFactoryClass)
return (DbProviderFactory)factory;
}


private static Type GetProviderTypeFromTypeName(string assemblyQualifiedName)
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
private static Type GetProviderTypeFromTypeName([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] string assemblyQualifiedName)
{
Type? providerType = Type.GetType(assemblyQualifiedName);
if (null == providerType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace System.Data.Common
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
public abstract partial class DbProviderFactory
{
private bool? _canCreateDataAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Xml;
using System.Collections;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Common
{
Expand Down Expand Up @@ -237,11 +238,13 @@ public override void SetCapacity(int capacity)
base.SetCapacity(capacity);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override object ConvertXmlToObject(string s)
{
return XmlConvert.ToDecimal(s);
}

[RequiresUnreferencedCode(DataSet.RequiresUnreferencedCodeMessage)]
public override string ConvertObjectToXml(object value)
{
return XmlConvert.ToString((decimal)value);
Expand Down
Loading

0 comments on commit c2f559c

Please sign in to comment.