Browse Source

Merge pull request #3484 from ds5678/issue3483

Add configuration option to check for overflow and underflow
pull/3487/head
Siegfried Pammer 1 week ago committed by GitHub
parent
commit
a18d0d44d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      ICSharpCode.Decompiler.Tests/Helpers/Tester.cs
  2. 3
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  3. 6
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  4. 44
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Issue3483.cs
  5. 5
      ICSharpCode.Decompiler/CSharp/ProjectDecompiler/IProjectInfoProvider.cs
  6. 1
      ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterDefault.cs
  7. 1
      ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterSdkStyle.cs
  8. 2
      ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs
  9. 10
      ICSharpCode.Decompiler/CSharp/Transforms/AddCheckedBlocks.cs
  10. 18
      ICSharpCode.Decompiler/DecompilerSettings.cs
  11. 19
      ILSpy/Properties/Resources.Designer.cs
  12. 3
      ILSpy/Properties/Resources.resx
  13. 3
      ILSpy/Properties/Resources.zh-Hans.resx

11
ICSharpCode.Decompiler.Tests/Helpers/Tester.cs

@ -69,6 +69,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -69,6 +69,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
UseTestRunner = 0x4000,
NullableEnable = 0x8000,
ReferenceUnsafe = 0x10000,
CheckForOverflowUnderflow = 0x20000,
UseMcsMask = UseMcs2_6_4 | UseMcs5_23,
UseRoslynMask = UseRoslyn1_3_2 | UseRoslyn2_10_0 | UseRoslyn3_11_0 | UseRoslynLatest
}
@ -543,6 +544,16 @@ namespace System.Runtime.CompilerServices @@ -543,6 +544,16 @@ namespace System.Runtime.CompilerServices
{
otherOptions += "-platform:anycpu ";
}
if (flags.HasFlag(CompilerOptions.CheckForOverflowUnderflow))
{
otherOptions += "-checked+ ";
}
else
{
otherOptions += "-checked- ";
}
if (preprocessorSymbols.Count > 0)
{
otherOptions += " \"-d:" + string.Join(";", preprocessorSymbols) + "\" ";

3
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -141,9 +141,10 @@ @@ -141,9 +141,10 @@
<Compile Include="TestCases\Pretty\Comparisons.cs" />
<Compile Include="TestCases\Pretty\GloballyQualifiedTypeInStringInterpolation.cs" />
<Compile Include="TestCases\Pretty\Issue3406.cs" />
<Compile Include="TestCases\Pretty\PointerArithmetic.cs" />
<Compile Include="TestCases\Pretty\Issue3439.cs" />
<Compile Include="TestCases\Pretty\Issue3442.cs" />
<Compile Include="TestCases\Pretty\Issue3483.cs" />
<Compile Include="TestCases\Pretty\PointerArithmetic.cs" />
<None Include="TestCases\VBPretty\VBAutomaticEvents.vb" />
<Compile Include="TestCases\VBPretty\VBAutomaticEvents.cs" />
<Compile Include="TestCases\VBPretty\VBNonGenericForEach.cs" />

6
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -635,6 +635,12 @@ namespace ICSharpCode.Decompiler.Tests @@ -635,6 +635,12 @@ namespace ICSharpCode.Decompiler.Tests
await RunForLibrary(cscOptions: cscOptions);
}
[Test]
public async Task Issue3483([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions | CompilerOptions.CheckForOverflowUnderflow, configureDecompiler: settings => settings.CheckForOverflowUnderflow = true);
}
[Test]
public async Task AssemblyCustomAttributes([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{

44
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Issue3483.cs

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
internal static class Issue3483
{
public static int Add_Checked(int x, int y)
{
return x + y;
}
public static int Add_Unchecked(int x, int y)
{
return unchecked(x + y);
}
public static int Add_CheckedAndUnchecked_1(int x, int y, int z)
{
return x + unchecked(y + z);
}
public static int Add_CheckedAndUnchecked_2(int x, int y, int z)
{
unchecked
{
return x + checked(y + z);
}
}
public static uint Cast_Checked(int x)
{
return (uint)x;
}
public static uint Cast_Unchecked(int x)
{
return unchecked((uint)x);
}
public static int Cast_CheckedAndUnchecked_1(int x)
{
return (int)unchecked((uint)x);
}
public static int Cast_CheckedAndUnchecked_2(int x)
{
unchecked
{
return (int)checked((uint)x);
}
}
}
}

5
ICSharpCode.Decompiler/CSharp/ProjectDecompiler/IProjectInfoProvider.cs

@ -39,6 +39,11 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler @@ -39,6 +39,11 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
/// </summary>
LanguageVersion LanguageVersion { get; }
/// <summary>
/// Check for overflow and underflow in operators.
/// </summary>
bool CheckForOverflowUnderflow { get; }
/// <summary>
/// Gets the unique ID of the project.
/// </summary>

1
ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterDefault.cs

@ -98,6 +98,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler @@ -98,6 +98,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
w.WriteElementString("OutputType", outputType);
w.WriteElementString("LangVersion", project.LanguageVersion.ToString().Replace("CSharp", "").Replace('_', '.'));
w.WriteElementString("CheckForOverflowUnderflow", project.CheckForOverflowUnderflow ? "true" : "false");
w.WriteElementString("AssemblyName", module.Name);
if (targetFramework.Identifier != null)

1
ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterSdkStyle.cs

@ -192,6 +192,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler @@ -192,6 +192,7 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
{
xml.WriteElementString("LangVersion", project.LanguageVersion.ToString().Replace("CSharp", "").Replace('_', '.'));
xml.WriteElementString("AllowUnsafeBlocks", TrueString);
xml.WriteElementString("CheckForOverflowUnderflow", project.CheckForOverflowUnderflow ? TrueString : FalseString);
if (project.StrongNameKeyFile != null)
{

2
ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs

@ -68,6 +68,8 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler @@ -68,6 +68,8 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler
}
}
bool IProjectInfoProvider.CheckForOverflowUnderflow => Settings.CheckForOverflowUnderflow;
public IAssemblyResolver AssemblyResolver { get; }
public AssemblyReferenceClassifier AssemblyReferenceClassifier { get; }

10
ICSharpCode.Decompiler/CSharp/Transforms/AddCheckedBlocks.cs

@ -252,8 +252,14 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -252,8 +252,14 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
else
{
Result r = GetResultFromBlock(block);
if (r.NodesToInsertInUncheckedContext != null)
r.NodesToInsertInUncheckedContext.Insert();
if (context.DecompileRun.Settings.CheckForOverflowUnderflow)
{
r.NodesToInsertInCheckedContext?.Insert();
}
else
{
r.NodesToInsertInUncheckedContext?.Insert();
}
}
}

18
ICSharpCode.Decompiler/DecompilerSettings.cs

@ -2185,6 +2185,24 @@ namespace ICSharpCode.Decompiler @@ -2185,6 +2185,24 @@ namespace ICSharpCode.Decompiler
}
}
bool checkForOverflowUnderflow = false;
/// <summary>
/// Check for overflow and underflow in operators.
/// </summary>
[Category("DecompilerSettings.Other")]
[Description("DecompilerSettings.CheckForOverflowUnderflow")]
public bool CheckForOverflowUnderflow {
get { return checkForOverflowUnderflow; }
set {
if (checkForOverflowUnderflow != value)
{
checkForOverflowUnderflow = value;
OnPropertyChanged();
}
}
}
CSharpFormattingOptions csharpFormattingOptions;
[Browsable(false)]

19
ILSpy/Properties/Resources.Designer.cs generated

@ -810,6 +810,15 @@ namespace ICSharpCode.ILSpy.Properties { @@ -810,6 +810,15 @@ namespace ICSharpCode.ILSpy.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Check for overflow and underflow in operators.
/// </summary>
public static string DecompilerSettings_CheckForOverflowUnderflow {
get {
return ResourceManager.GetString("DecompilerSettings.CheckForOverflowUnderflow", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Decompile anonymous methods/lambdas.
/// </summary>
@ -1127,16 +1136,6 @@ namespace ICSharpCode.ILSpy.Properties { @@ -1127,16 +1136,6 @@ namespace ICSharpCode.ILSpy.Properties {
}
}
/// <summary>
/// Looks up a localized resource of type System.Object.
/// </summary>
public static object DecompilerSettings_LifetimeAnnotations {
get {
object obj = ResourceManager.GetObject("DecompilerSettings.LifetimeAnnotations", resourceCulture);
return ((object)(obj));
}
}
/// <summary>
/// Looks up a localized string similar to Use nint/nuint types.
/// </summary>

3
ILSpy/Properties/Resources.resx

@ -288,6 +288,9 @@ Are you sure you want to continue?</value> @@ -288,6 +288,9 @@ Are you sure you want to continue?</value>
<data name="DecompilerSettings.AutoLoadAssemblyReferences" xml:space="preserve">
<value>Automatically load assembly references</value>
</data>
<data name="DecompilerSettings.CheckForOverflowUnderflow" xml:space="preserve">
<value>Check for overflow and underflow in operators</value>
</data>
<data name="DecompilerSettings.CheckedOperators" xml:space="preserve">
<value>User-defined checked operators</value>
</data>

3
ILSpy/Properties/Resources.zh-Hans.resx

@ -279,6 +279,9 @@ @@ -279,6 +279,9 @@
<data name="DecompilerSettings.AsyncEnumerator" xml:space="preserve">
<value>反编译异步 IAsyncEnumerator 方法</value>
</data>
<data name="DecompilerSettings.CheckForOverflowUnderflow" xml:space="preserve">
<value />
</data>
<data name="DecompilerSettings.DecompileAnonymousMethodsLambdas" xml:space="preserve">
<value>反编译匿名方法或 lambda</value>
</data>

Loading…
Cancel
Save