Skip to content

Commit

Permalink
Merge branch 'bugfix/ikvmc-nowarnerror' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AliveDevil committed Aug 23, 2024
2 parents b0eebd8 + 9fda594 commit 3675931
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 279 deletions.
61 changes: 56 additions & 5 deletions src/IKVM.MSBuild.Tasks/IkvmToolTaskDiagnosticWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using IKVM.Tools.Runner;

using Microsoft.Build.Framework;

namespace IKVM.MSBuild.Tasks
{

Expand Down Expand Up @@ -38,24 +40,73 @@ public Task ReceiveAsync(IkvmToolDiagnosticEvent @event)
if (@event == null)
return Task.CompletedTask;

(
IkvmToolDiagnosticEventLevel Level,
string Code,
string Message
) structuredLog;

#pragma warning disable IDE0079 // Unused suppression (net472 doesn't produce IDE0057)
#pragma warning disable IDE0057 // Suppress net6 & net8 analyzer for range-operations
// unspecified format: "Level: Message"
// MSBuild Format: "Level IKVMC0000: Message"
// Some write """
// warning IKVMC0000: Message
// (additional information)
// """
// Skip these:
// - StdErr is mapped to Information
// - StdOut is mapped to Debug.
if (@event.Message.Length > 0 && @event.Message[0] is char first
&& !char.IsWhiteSpace(first)
&& @event.Message.IndexOf(": ", 0, 19) is { } firstColon and not -1)
{
structuredLog.Message = @event.Message.Substring(firstColon + 2);

int levelLength;
if (@event.Message.IndexOf("IKVMC", 0, firstColon, StringComparison.OrdinalIgnoreCase) is { } codeIndex and not -1)
{
levelLength = codeIndex - 1;
structuredLog.Code = @event.Message.Substring(codeIndex, 9 /* IKVMC0000 */);
}
else
{
levelLength = firstColon;
structuredLog.Code = "";
}

structuredLog.Level = @event.Message.Substring(0, levelLength).ToUpperInvariant() switch
{
"ERROR" => IkvmToolDiagnosticEventLevel.Error,
"WARNING" => IkvmToolDiagnosticEventLevel.Warning,
_ => IkvmToolDiagnosticEventLevel.Information
};
}
else
{
// Can't figure out level.
structuredLog = (@event.Level, null, @event.Message);
}
#pragma warning restore

try
{
switch (@event.Level)
switch (structuredLog.Level)
{
case IkvmToolDiagnosticEventLevel.Debug:
logger.LogMessage(Microsoft.Build.Framework.MessageImportance.Low, @event.Message, @event.MessageArgs);
logger.LogMessage(null, structuredLog.Code, null, null, 0, 0, 0, 0, MessageImportance.Low, structuredLog.Message, @event.MessageArgs);
writer?.WriteLine("DEBUG: " + @event.Message, @event.MessageArgs);
break;
case IkvmToolDiagnosticEventLevel.Information:
logger.LogMessage(Microsoft.Build.Framework.MessageImportance.Normal, @event.Message, @event.MessageArgs);
logger.LogMessage(null, structuredLog.Code, null, null, 0, 0, 0, 0, MessageImportance.Normal, structuredLog.Message, @event.MessageArgs);
writer?.WriteLine("INFO: " + @event.Message, @event.MessageArgs);
break;
case IkvmToolDiagnosticEventLevel.Warning:
logger.LogWarning(@event.Message, @event.MessageArgs);
logger.LogWarning(null, structuredLog.Code, null, null, 0, 0, 0, 0, structuredLog.Message, @event.MessageArgs);
writer?.WriteLine("WARN: " + @event.Message, @event.MessageArgs);
break;
case IkvmToolDiagnosticEventLevel.Error:
logger.LogWarning(@event.Message, @event.MessageArgs);
logger.LogError(null, structuredLog.Code, null, null, 0, 0, 0, 0, structuredLog.Message, @event.MessageArgs);
writer?.WriteLine("ERROR: " + @event.Message, @event.MessageArgs);
break;
}
Expand Down
3 changes: 3 additions & 0 deletions src/IKVM.NET.Sdk/targets/IKVM.Java.Core.NoTasks.targets
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ Value = b.ToString();
<_IkvmCompilerArgs Include="-nologo" />
<_IkvmCompilerArgs Include="-bootstrap" Condition=" '$(Bootstrap)' == 'true' " />
<_IkvmCompilerArgs Include="-debug:$(DebugType)" Condition=" '$(DebugType)' != 'none' " />
<_IkvmCompilerArgs Include="-nowarn:$(NoWarn.Replace(';', ','))" Condition=" '$(NoWarn)' != '' " />
<_IkvmCompilerArgs Include="-warnaserror" Condition=" '$(TreatWarningsAsErrors)' == 'true' " />
<_IkvmCompilerArgs Include="-warnaserror:$(WarningsAsErrors.Replace(';', ','))" Condition=" '$(TreatWarningsAsErrors)' != 'true' And '$(WarningsAsErrors)' != '' " />
<_IkvmCompilerArgs Include="-assembly:$(AssemblyName)" />
<_IkvmCompilerArgs Include="-version:$(AssemblyVersion)" />
<_IkvmCompilerArgs Include="-runtime:$(IkvmRuntimeAssembly)" />
Expand Down
3 changes: 3 additions & 0 deletions src/IKVM.NET.Sdk/targets/IKVM.Java.Core.Tasks.targets
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
Platform="$(PlatformTarget.ToLowerInvariant())"
Main="$(StartupObject)"
Debug="$(DebugType)"
NoWarn="$(NoWarn.Replace(',', ';'))"
WarnAsError="$(TreatWarningsAsErrors)"
WarnAsErrorWarnings="$(WarningsAsErrors.Replace(',', ';'))"
KeyFile="$(KeyOriginatorFile)"
CompressResources="$(CompressResources)"
ClassLoader="$(ClassLoader)"
Expand Down
8 changes: 4 additions & 4 deletions src/IKVM.Tools.Importer/CompilerClassLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3415,8 +3415,8 @@ sealed class CompilerOptions
internal uint fileAlignment;
internal bool highentropyva;
internal List<CompilerClassLoader> sharedclassloader; // should *not* be deep copied in Copy(), because we want the list of all compilers that share a class loader
internal Dictionary<string, string> suppressWarnings = new Dictionary<string, string>();
internal Dictionary<string, string> errorWarnings = new Dictionary<string, string>(); // treat specific warnings as errors
internal HashSet<string> suppressWarnings = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
internal HashSet<string> errorWarnings = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
internal bool warnaserror; // treat all warnings as errors
internal FileInfo writeSuppressWarningsFile;
internal List<string> proxies = new List<string>();
Expand All @@ -3438,8 +3438,8 @@ internal CompilerOptions Copy()
{
copy.externalResources = new Dictionary<string, string>(externalResources);
}
copy.suppressWarnings = new Dictionary<string, string>(suppressWarnings);
copy.errorWarnings = new Dictionary<string, string>(errorWarnings);
copy.suppressWarnings = new(suppressWarnings, StringComparer.OrdinalIgnoreCase);
copy.errorWarnings = new(errorWarnings, StringComparer.OrdinalIgnoreCase);
return copy;
}

Expand Down
68 changes: 44 additions & 24 deletions src/IKVM.Tools.Importer/IkvmImporterInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,31 +748,15 @@ void ContinueParseCommandLine(RuntimeContext context, StaticCompiler compiler, I
}
else if (s.StartsWith("-nowarn:"))
{
foreach (var w in s.Substring(8).Split(','))
{
// lame way to chop off the leading zeroes
string ws = w;
while (ws.StartsWith("0"))
ws = ws.Substring(1);

options.suppressWarnings[ws] = ws;
}
HandleWarnArg(options.suppressWarnings, s.Substring(8));
}
else if (s == "-warnaserror")
{
options.warnaserror = true;
}
else if (s.StartsWith("-warnaserror:"))
{
foreach (string w in s.Substring(13).Split(','))
{
// lame way to chop off the leading zeroes
string ws = w;
while (ws.StartsWith("0"))
ws = ws.Substring(1);

options.errorWarnings[ws] = ws;
}
HandleWarnArg(options.errorWarnings, s.Substring(13));
}
else if (s.StartsWith("-runtime:"))
{
Expand Down Expand Up @@ -1446,20 +1430,21 @@ internal static void IssueMessage(StaticCompiler compiler, CompilerOptions optio
return;
}

string key = ((int)msgId).ToString();
var msgIdKey = $"{(int)msgId}";
string key = msgIdKey;
for (int i = 0; ; i++)
{
if (options.suppressWarnings.ContainsKey(key))
if (options.suppressWarnings.Contains(key))
{
return;
}
if (i == values.Length)
{
break;
}
key += ":" + values[i];
key = $"{key}:{values[i]}";
}
options.suppressWarnings.Add(key, key);
options.suppressWarnings.Add(key);
if (options.writeSuppressWarningsFile != null)
{
File.AppendAllText(options.writeSuppressWarningsFile.FullName, "-nowarn:" + key + Environment.NewLine);
Expand Down Expand Up @@ -1665,8 +1650,8 @@ internal static void IssueMessage(StaticCompiler compiler, CompilerOptions optio
}
bool error = msgId >= Message.StartErrors
|| (options.warnaserror && msgId >= Message.StartWarnings)
|| options.errorWarnings.ContainsKey(key)
|| options.errorWarnings.ContainsKey(((int)msgId).ToString());
|| options.errorWarnings.Contains(key)
|| options.errorWarnings.Contains(msgIdKey);
Console.Error.Write("{0} IKVMC{1:D4}: ", error ? "error" : msgId < Message.StartWarnings ? "note" : "warning", (int)msgId);
if (error && Message.StartWarnings <= msgId && msgId < Message.StartErrors)
{
Expand All @@ -1686,6 +1671,41 @@ internal static void IssueMessage(StaticCompiler compiler, CompilerOptions optio
}
}

internal static void HandleWarnArg(ICollection<string> target, string arg)
{
foreach (var w in arg.Split(','))
{
// Strip IKVMC prefix
int prefixStart = w.StartsWith("IKVMC", StringComparison.OrdinalIgnoreCase) ? 5 : 0;
int contextIndex = w.IndexOf(':', prefixStart);
string context = string.Empty;
string parse;
if(contextIndex != -1)
{
// context includes ':' separator
context = w.Substring(contextIndex);
parse = w.Substring(prefixStart, contextIndex - prefixStart);
}
else
{
parse = w.Substring(prefixStart);
}

if (!int.TryParse(parse, out var intResult))
{
if (!Enum.TryParse<Message>(parse, out var namedResult))
{
continue; // silently continue
}

// Warnings are handled as int.
intResult = (int)namedResult;
}

// Check IssueMessage
target.Add($"{intResult}{context}");
}
}
}

}
Loading

0 comments on commit 3675931

Please sign in to comment.