Skip to content

Commit

Permalink
Add SYSLIB* analyzers to global configs (#80930)
Browse files Browse the repository at this point in the history
* Add SYSLIB* analyzers to global configs

* Address PR feedback
  • Loading branch information
stephentoub authored Jan 26, 2023
1 parent 26b58b9 commit fb7d8b1
Show file tree
Hide file tree
Showing 28 changed files with 222 additions and 118 deletions.
24 changes: 24 additions & 0 deletions eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ dotnet_diagnostic.BCL0015.severity = none
# BCL0020: Invalid SR.Format call
dotnet_diagnostic.BCL0020.severity = warning

# SYSLIB1045: Convert to 'GeneratedRegexAttribute'.
dotnet_diagnostic.SYSLIB1045.severity = warning

# SYSLIB1054: Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
dotnet_diagnostic.SYSLIB1054.severity = warning

# SYSLIB1055: Invalid 'CustomMarshallerAttribute' usage
dotnet_diagnostic.SYSLIB1055.severity = error

# SYSLIB1056:Specified marshaller type is invalid
dotnet_diagnostic.SYSLIB1056.severity = error

# SYSLIB1057: Marshaller type does not have the required shape
dotnet_diagnostic.SYSLIB1057.severity = error

# SYSLIB1058: Invalid 'NativeMarshallingAttribute' usage
dotnet_diagnostic.SYSLIB1058.severity = error

# SYSLIB1060: Specified marshaller type is invalid
dotnet_diagnostic.SYSLIB1060.severity = error

# SYSLIB1061: Marshaller type has incompatible method signatures
dotnet_diagnostic.SYSLIB1061.severity = error

# CA1000: Do not declare static members on generic types
dotnet_diagnostic.CA1000.severity = none

Expand Down
24 changes: 24 additions & 0 deletions eng/CodeAnalysis.test.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ dotnet_diagnostic.BCL0015.severity = none
# BCL0020: Invalid SR.Format call
dotnet_diagnostic.BCL0020.severity = none

# SYSLIB1045: Convert to 'GeneratedRegexAttribute'.
dotnet_diagnostic.SYSLIB1045.severity = none

# SYSLIB1054: Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
dotnet_diagnostic.SYSLIB1054.severity = none

# SYSLIB1055: Invalid 'CustomMarshallerAttribute' usage
dotnet_diagnostic.SYSLIB1055.severity = error

# SYSLIB1056:Specified marshaller type is invalid
dotnet_diagnostic.SYSLIB1056.severity = error

# SYSLIB1057: Marshaller type does not have the required shape
dotnet_diagnostic.SYSLIB1057.severity = error

# SYSLIB1058: Invalid 'NativeMarshallingAttribute' usage
dotnet_diagnostic.SYSLIB1058.severity = error

# SYSLIB1060: Specified marshaller type is invalid
dotnet_diagnostic.SYSLIB1060.severity = error

# SYSLIB1061: Marshaller type has incompatible method signatures
dotnet_diagnostic.SYSLIB1061.severity = error

# CA1000: Do not declare static members on generic types
dotnet_diagnostic.CA1000.severity = none

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

using Internal.Runtime;

#pragma warning disable SYSLIB1054 // Use DllImport here instead of LibraryImport because this file is used by Test.CoreLib

namespace System.Runtime
{
internal enum DispatchCellType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,11 @@ public static unsafe int RhGetCurrentThreadStackTrace(IntPtr[] outputBuffer)
return RhpGetCurrentThreadStackTrace(pOutputBuffer, (uint)((outputBuffer != null) ? outputBuffer.Length : 0), new UIntPtr(&pOutputBuffer));
}

// Use DllImport here instead of LibraryImport because this file is used by Test.CoreLib.
#pragma warning disable SYSLIB1054 // Use DllImport here instead of LibraryImport because this file is used by Test.CoreLib.
[DllImport(Redhawk.BaseName)]
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
private static extern unsafe int RhpGetCurrentThreadStackTrace(IntPtr* pOutputBuffer, uint outputBufferLength, UIntPtr addressInCurrentFrame);
#pragma warning restore SYSLIB1054

// Worker for RhGetCurrentThreadStackTrace. RhGetCurrentThreadStackTrace just allocates a transition
// frame that will be used to seed the stack trace and this method does all the real work.
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/tools/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Code Style Analyzers

[*.cs]

# TODO: Update tools to use LibraryImport instead of DllImport
dotnet_diagnostic.SYSLIB1054.severity = suggestion
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);SYSLIB1045</NoWarn>

<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

Expand All @@ -13,19 +12,6 @@ namespace System.Data.Common
internal partial class DbConnectionOptions
{
#if DEBUG
/*private const string ConnectionStringPatternV1 =
"[\\s;]*"
+"(?<key>([^=\\s]|\\s+[^=\\s]|\\s+==|==)+)"
+ "\\s*=(?!=)\\s*"
+"(?<value>("
+ "(" + "\"" + "([^\"]|\"\")*" + "\"" + ")"
+ "|"
+ "(" + "'" + "([^']|'')*" + "'" + ")"
+ "|"
+ "(" + "(?![\"'])" + "([^\\s;]|\\s+[^\\s;])*" + "(?<![\"'])" + ")"
+ "))"
+ "[\\s;]*"
;*/
private const string ConnectionStringPattern = // may not contain embedded null except trailing last value
"([\\s;]*" // leading whitespace and extra semicolons
+ "(?![\\s;])" // key does not start with space or semicolon
Expand Down Expand Up @@ -61,16 +47,38 @@ internal partial class DbConnectionOptions
+ "[\\s;]*[\u0000\\s]*" // trailing whitespace/semicolons (DataSourceLocator), embedded nulls are allowed only in the end
;

private static readonly Regex s_connectionStringRegex = new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static readonly Regex s_connectionStringRegexOdbc = new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static readonly Regex s_connectionStringRegex = CreateConnectionStringRegex();
private static readonly Regex s_connectionStringRegexOdbc = CreateConnectionStringRegexOdbc();

#if NET7_0_OR_GREATER
[GeneratedRegex(ConnectionStringPattern, RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringRegex();

[GeneratedRegex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringRegexOdbc();
#else
private static Regex CreateConnectionStringRegex() => new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex CreateConnectionStringRegexOdbc() => new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
#endif
#endif
internal const string DataDirectory = "|datadirectory|";

#pragma warning disable CA1823 // used in some compilations and not others
private static readonly Regex s_connectionStringValidKeyRegex = new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled); // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
private static readonly Regex s_connectionStringQuoteValueRegex = new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled); // generally do not quote the value if it matches the pattern
private static readonly Regex s_connectionStringQuoteOdbcValueRegex = new Regex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled); // do not quote odbc value if it matches this pattern
#pragma warning restore CA1823
private static readonly Regex s_connectionStringValidKeyRegex = CreateConnectionStringValidKeyRegex(); // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
private static readonly Regex s_connectionStringQuoteValueRegex = CreateConnectionStringQuoteValueRegex(); // generally do not quote the value if it matches the pattern
private static readonly Regex s_connectionStringQuoteOdbcValueRegex = CreateConnectionStringQuoteOdbcValueRegex(); // do not quote odbc value if it matches this pattern

#if NET7_0_OR_GREATER
[GeneratedRegex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$")]
private static partial Regex CreateConnectionStringValidKeyRegex();
[GeneratedRegex("^[^\"'=;\\s\\p{Cc}]*$")]
private static partial Regex CreateConnectionStringQuoteValueRegex();
[GeneratedRegex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringQuoteOdbcValueRegex();
#else
private static Regex CreateConnectionStringValidKeyRegex() => new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled);
private static Regex CreateConnectionStringQuoteValueRegex() => new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled);
private static Regex CreateConnectionStringQuoteOdbcValueRegex() => new Regex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled);
#endif

// connection string common keywords
private static class KEY
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<!-- Ignore analyzers that recommend APIs introduced in .NET Core when targeting frameworks that lack those APIs
to avoid issues with multitargeting.
-->
<NoWarn Condition="$(TargetFrameworks.Contains('net4')) or $(TargetFrameworks.Contains('netstandard'))">$(NoWarn);CA1510;CA1511;CA1512;CA1513</NoWarn>
<NoWarn Condition="$(TargetFrameworks.Contains('net4')) or $(TargetFrameworks.Contains('netstandard'))">$(NoWarn);CA1510;CA1511;CA1512;CA1513;CA1845;CA1846;CA1847</NoWarn>

<!-- Microsoft.NET.Sdk enables some warnings as errors out of the box.
We want to remove some items from this list so they don't fail the build.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<NoWarn>$(NoWarn);nullable</NoWarn>
<!-- opt-out of trimming until it works https://github.com/dotnet/runtime/issues/49062 -->
<IsTrimmable>false</IsTrimmable>
<NoWarn>$(NoWarn);CA1847</NoWarn>
<IsPackable>true</IsPackable>
<PackageDescription>Provides types that support using XML configuration files (app.config). This package exists only to support migrating existing .NET Framework code that already uses System.Configuration. When writing new code, use another configuration system instead, such as Microsoft.Extensions.Configuration.</PackageDescription>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
Expand Down Expand Up @@ -327,5 +327,9 @@
<Reference Include="System.Transactions.Local" />
<Reference Include="System.Xml.ReaderWriter" />
<Reference Include="System.Xml.XmlSerializer" />
<ProjectReference Include="..\..\System.Text.RegularExpressions\gen\System.Text.RegularExpressions.Generator.csproj"
SetTargetFramework="TargetFramework=netstandard2.0"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
8 changes: 5 additions & 3 deletions src/libraries/System.Data.Odbc/src/System.Data.Odbc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-solaris;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;$(NetCoreAppCurrent);$(NetCoreAppPrevious)-windows;$(NetCoreAppPrevious)-freebsd;$(NetCoreAppPrevious)-illumos;$(NetCoreAppPrevious)-solaris;$(NetCoreAppPrevious)-linux;$(NetCoreAppPrevious)-osx;$(NetCoreAppPrevious)-ios;$(NetCoreAppPrevious)-tvos;$(NetCoreAppPrevious);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum)-freebsd;$(NetCoreAppMinimum)-linux;$(NetCoreAppMinimum)-osx;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);CA2249;CA1838;CA1846</NoWarn>
<!-- Suppress CA1845: Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring' to avoid ifdefs. -->
<NoWarn>$(NoWarn);CA1845</NoWarn>
<NoWarn>$(NoWarn);CA2249;CA1838</NoWarn>
<IsPackable>true</IsPackable>
<PackageDescription>Provides a collection of classes used to access an ODBC data source in the managed space

Expand Down Expand Up @@ -135,6 +133,10 @@ System.Data.Odbc.OdbcTransaction</PackageDescription>
Link="Common\DisableRuntimeMarshalling.cs" />
<Compile Include="$(CommonPath)System\Runtime\InteropServices\HandleRefMarshaller.cs"
Link="Common\System\Runtime\InteropServices\HandleRefMarshaller.cs" />
<ProjectReference Include="..\..\System.Text.RegularExpressions\gen\System.Text.RegularExpressions.Generator.csproj"
SetTargetFramework="TargetFramework=netstandard2.0"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'linux' or '$(TargetPlatformIdentifier)' == 'freebsd' or '$(TargetPlatformIdentifier)' == 'illumos' or '$(TargetPlatformIdentifier)' == 'solaris'">
Expand Down
52 changes: 31 additions & 21 deletions src/libraries/System.Data.OleDb/src/DbConnectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,18 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.Versioning;
using System.Text;
using System.Text.RegularExpressions;

namespace System.Data.Common
{
internal class DbConnectionOptions
internal partial class DbConnectionOptions
{
// instances of this class are intended to be immutable, i.e readonly
// used by pooling classes so it is much easier to verify correctness
// when not worried about the class being modified during execution

#if DEBUG
/*private const string ConnectionStringPatternV1 =
"[\\s;]*"
+"(?<key>([^=\\s]|\\s+[^=\\s]|\\s+==|==)+)"
+ "\\s*=(?!=)\\s*"
+"(?<value>("
+ "(" + "\"" + "([^\"]|\"\")*" + "\"" + ")"
+ "|"
+ "(" + "'" + "([^']|'')*" + "'" + ")"
+ "|"
+ "(" + "(?![\"'])" + "([^\\s;]|\\s+[^\\s;])*" + "(?<![\"'])" + ")"
+ "))"
+ "[\\s;]*"
;*/
private const string ConnectionStringPattern = // may not contain embedded null except trailing last value
"([\\s;]*" // leading whitespace and extra semicolons
+ "(?![\\s;])" // key does not start with space or semicolon
Expand Down Expand Up @@ -69,15 +55,39 @@ internal class DbConnectionOptions
+ "[\\s;]*[\u0000\\s]*" // trailing whitespace/semicolons (DataSourceLocator), embedded nulls are allowed only in the end
;

private static readonly Regex ConnectionStringRegex = new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static readonly Regex ConnectionStringRegexOdbc = new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
#endif
private static readonly Regex ConnectionStringValidKeyRegex = new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled); // key not allowed to start with semi-colon or space or contain non-visible characters or end with space

private static readonly Regex ConnectionStringRegex = CreateConnectionStringRegex();
private static readonly Regex ConnectionStringRegexOdbc = CreateConnectionStringRegexOdbc();

#if NET7_0_OR_GREATER
[GeneratedRegex(ConnectionStringPattern, RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringRegex();

[GeneratedRegex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringRegexOdbc();
#else
private static Regex CreateConnectionStringRegex() => new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
private static Regex CreateConnectionStringRegexOdbc() => new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
#endif
#endif
internal const string DataDirectory = "|datadirectory|";

private static readonly Regex ConnectionStringQuoteValueRegex = new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled); // generally do not quote the value if it matches the pattern
private static readonly Regex ConnectionStringQuoteOdbcValueRegex = new Regex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled); // do not quote odbc value if it matches this pattern
private static readonly Regex ConnectionStringValidKeyRegex = CreateConnectionStringValidKeyRegex(); // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
private static readonly Regex ConnectionStringQuoteValueRegex = CreateConnectionStringQuoteValueRegex(); // generally do not quote the value if it matches the pattern
private static readonly Regex ConnectionStringQuoteOdbcValueRegex = CreateConnectionStringQuoteOdbcValueRegex(); // do not quote odbc value if it matches this pattern

#if NET7_0_OR_GREATER
[GeneratedRegex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$")]
private static partial Regex CreateConnectionStringValidKeyRegex();
[GeneratedRegex("^[^\"'=;\\s\\p{Cc}]*$")]
private static partial Regex CreateConnectionStringQuoteValueRegex();
[GeneratedRegex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture)]
private static partial Regex CreateConnectionStringQuoteOdbcValueRegex();
#else
private static Regex CreateConnectionStringValidKeyRegex() => new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled);
private static Regex CreateConnectionStringQuoteValueRegex() => new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled);
private static Regex CreateConnectionStringQuoteOdbcValueRegex() => new Regex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled);
#endif

// connection string common keywords
private static class KEY
Expand Down
11 changes: 8 additions & 3 deletions src/libraries/System.Data.OleDb/src/System.Data.OleDb.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppPrevious)-windows;$(NetCoreAppPrevious);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- Suppress CA2249: Consider using String.Contains instead of String.IndexOf to avoid ifdefs. -->
<NoWarn>$(NoWarn);CA2249</NoWarn>
<!-- Suppress SYSLIB0004: 'RuntimeHelpers.PrepareConstrainedRegions()' is obsolete to avoid ifdefs. -->
<NoWarn>$(NoWarn);SYSLIB0004</NoWarn>
<!-- Suppress CA1845: Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring' to avoid ifdefs. -->
<NoWarn>$(NoWarn);CA1845</NoWarn>
<IsPackable>true</IsPackable>
<PackageDescription>Provides a collection of classes for OLEDB.

Expand Down Expand Up @@ -149,4 +147,11 @@ System.Data.OleDb.OleDbTransaction</PackageDescription>
<ProjectReference Include="$(LibrariesProjectRoot)System.Configuration.ConfigurationManager\src\System.Configuration.ConfigurationManager.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Diagnostics.PerformanceCounter\src\System.Diagnostics.PerformanceCounter.csproj" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
<ProjectReference Include="..\..\System.Text.RegularExpressions\gen\System.Text.RegularExpressions.Generator.csproj"
SetTargetFramework="TargetFramework=netstandard2.0"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
Loading

0 comments on commit fb7d8b1

Please sign in to comment.