Skip to content
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

Add WarningsAsErrors and WarningsNotAsErrors #1388

Merged
merged 6 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 29 additions & 0 deletions src/ILLink.Tasks/LinkTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public class ILLink : ToolTask
/// </summary>
public string Warn { get; set; }

/// <summary>
/// Treat all warnings as errors.
/// Maps to '--warnaserror' if true, '--warnaserror-' if false.
/// </summary>
public bool TreatWarningsAsErrors { get; set; }
mateoatr marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The list of warnings to report as errors.
/// Maps to '--warnaserror LIST-OF-WARNINGS'.
/// </summary>
public string WarningsAsErrors { get; set; }

/// <summary>
/// The list of warnings to report as usual.
/// Maps to '--warnaserror- LIST-OF-WARNINGS'.
/// </summary>
public string WarningsNotAsErrors { get; set; }

mateoatr marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// A list of XML root descriptor files specifying linker
/// roots at a granular level. See the mono/linker
Expand Down Expand Up @@ -315,6 +333,17 @@ protected override string GenerateResponseFileCommands ()
if (Warn != null)
args.Append ("--warn ").AppendLine (Quote (Warn));

if (TreatWarningsAsErrors)
args.AppendLine ("--warnaserror");
mateoatr marked this conversation as resolved.
Show resolved Hide resolved
else
args.AppendLine ("--warnaserror-");

if (WarningsAsErrors != null)
args.Append ("--warnaserror ").AppendLine (Quote (WarningsAsErrors));

if (WarningsNotAsErrors != null)
args.Append ("--warnaserror- ").AppendLine (Quote (WarningsNotAsErrors));

// Add global optimization arguments
if (_beforeFieldInit is bool beforeFieldInit)
SetOpt (args, "beforefieldinit", beforeFieldInit);
Expand Down
6 changes: 3 additions & 3 deletions src/linker/Linker/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -777,18 +777,18 @@ string Unquote (string arg)

value = Unquote (value);
string[] values = value.Split (new char[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
HashSet<uint> noWarnCodes = new HashSet<uint> ();
HashSet<uint> warningCodes = new HashSet<uint> ();
foreach (string id in values) {
if (!id.StartsWith ("IL", StringComparison.Ordinal))
continue;

var warningCode = id.Substring (2);
if (ushort.TryParse (warningCode, out ushort code)
&& code > 2000 && code <= 6000)
noWarnCodes.Add (code);
warningCodes.Add (code);
}

return noWarnCodes;
return warningCodes;
}

private static Assembly GetCustomAssembly (string arg)
Expand Down
41 changes: 37 additions & 4 deletions test/ILLink.Tasks.Tests/ILLink.Tasks.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
using System.Linq;
using System.Reflection;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using ILLink.Tasks;
using Mono.Linker;
using Mono.Linker.Steps;

namespace ILLink.Tasks.Tests
{
Expand Down Expand Up @@ -306,6 +302,43 @@ public void TestWarn (string warnArg, WarnVersion expectedVersion)
}
}

#nullable enable
[Theory]
[InlineData (true, null, null, new uint[] { }, new uint[] { })]
[InlineData (false, "IL1001,IL####,IL2000,IL2021,IL2022", null,
new uint[] { 2021, 2022 }, new uint[] { })]
[InlineData (false, "IL2023,IL6000;IL5042 IL2040", "IL4000,IL4001;IL4002 IL4003",
new uint[] { 2023, 2040, 5042, 6000 }, new uint[] { 4000, 4001, 4002, 4003 })]
[InlineData (false, "IL3000;IL3000;ABCD", "IL2005 il3000 IL2005",
new uint[] { 3000 }, new uint[] { 2005 })]
[InlineData (true, null, "IL2006", new uint[] { }, new uint[] { 2006 })]
[InlineData (true, "IL2001", "IL2001", new uint[] { }, new uint[] { 2001 })]
public void TestWarningsAsErrors (bool treatWarningsAsErrors, string? warningsAsErrors, string? warningsNotAsErrors, uint[] warnAsError, uint[] warnNotAsError)
{
var task = new MockTask () {
TreatWarningsAsErrors = treatWarningsAsErrors,
WarningsAsErrors = warningsAsErrors,
WarningsNotAsErrors = warningsNotAsErrors
};

using (var driver = task.CreateDriver ()) {
var actualWarnAsError = driver.Context.WarnAsError;
var actualGeneralWarnAsError = driver.Context.GeneralWarnAsError;
Assert.Equal (actualWarnAsError.Count, warnAsError.Distinct ().Count () + warnNotAsError.Distinct ().Count ());
Assert.Equal (actualGeneralWarnAsError, treatWarningsAsErrors);
if (warnAsError.Length > 0) {
foreach (var warningCode in warnAsError)
Assert.True (actualWarnAsError.ContainsKey (warningCode) && actualWarnAsError[warningCode] == true);
}

if (warnNotAsError.Length > 0) {
foreach (var warningCode in warnNotAsError)
Assert.True (actualWarnAsError.ContainsKey (warningCode) && actualWarnAsError[warningCode] == false);
}
}
}
#nullable disable

public static IEnumerable<object[]> CustomDataCases => new List<object[]> {
new object [] {
new ITaskItem [] {
Expand Down