Skip to content

Commit

Permalink
Add NoWarns flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoituron committed Nov 28, 2024
1 parent 5435ba7 commit e734fb1
Showing 1 changed file with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
publicResource = false;
}

var noWarns = Array.Empty<string>();
if (options.TryGetValue("build_metadata.AdditionalFiles.NoWarns", out var noWarnsText))
{
noWarns = noWarnsText.Split([',', ';'], StringSplitOptions.RemoveEmptyEntries) ?? [];
}

return new[]
{
new ResourceInformation(
Expand All @@ -127,7 +133,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
AsConstants: asConstants,
IncludeDefaultValues: includeDefaultValues,
EmitFormatMethods: emitFormatMethods,
Public: publicResource)
Public: publicResource,
NoWarns: noWarns)
};
});
var renameMapping = resourceFilesToGenerateSource
Expand Down Expand Up @@ -252,6 +259,7 @@ private sealed record CompilationInformation(
/// <param name="IncludeDefaultValues">If set to <see langword="true"/>, calls to <c>GetResourceString</c> receive a default resource string value.</param>
/// <param name="EmitFormatMethods">If set to <see langword="true"/>, the generated code will include <c>.FormatXYZ(...)</c> methods.</param>
/// <param name="Public">If set to <see langword="true"/>, the generated class will be declared <see langword="public"/>; otherwise, it will be declared <see langword="internal"/>.</param>
/// <param name="NoWarns">List of of disabled warnings, adding a <c>#pragma warning disable</c>.</param>
private sealed record ResourceInformation(
CompilationInformation CompilationInformation,
AdditionalText ResourceFile,
Expand All @@ -262,7 +270,8 @@ private sealed record ResourceInformation(
bool AsConstants,
bool IncludeDefaultValues,
bool EmitFormatMethods,
bool Public);
bool Public,
string[] NoWarns);

private sealed class ImmutableDictionaryEqualityComparer<TKey, TValue> : IEqualityComparer<ImmutableDictionary<TKey, TValue>?>
where TKey : notnull
Expand Down Expand Up @@ -609,14 +618,39 @@ public bool Execute(CancellationToken cancellationToken)
}
}

// List of NoWarns
string? noWarnsDisabled = null;
string? noWarnsRestored = null;
if (ResourceInformation.NoWarns.Length > 0)
{
var noWarnsList = string.Join(", ", ResourceInformation.NoWarns);
var crLf = Environment.NewLine;

switch (language)
{
case Lang.CSharp:
noWarnsDisabled = $"{crLf}{crLf}#pragma warning disable {noWarnsList}";
noWarnsRestored = $"{crLf}{crLf}#pragma warning restore {noWarnsList}";
break;

case Lang.VisualBasic:
noWarnsDisabled = $"{crLf}{crLf}#Disable Warning {noWarnsList}";
noWarnsRestored = $"{crLf}{crLf}#Enable Warning {noWarnsList}";
break;

default:
throw new InvalidOperationException();
}
}

// The ResourceManager property being initialized lazily is an important optimization that lets .NETNative
// completely remove the ResourceManager class if the disk space saving optimization to strip resources
// (/DisableExceptionMessages) is turned on in the compiler.
string result;
switch (language)
{
case Lang.CSharp:
result = $@"// <auto-generated/>
result = $@"// <auto-generated/>{noWarnsDisabled}
{(CompilationInformation.SupportsNullable ? "#nullable enable" : "")}
using System.Reflection;
Expand All @@ -630,12 +664,12 @@ public bool Execute(CancellationToken cancellationToken)
{getStringMethod}
{strings}
{classIndent}}}
{namespaceEnd}
{namespaceEnd}{noWarnsRestored}
";
break;

case Lang.VisualBasic:
result = $@"' <auto-generated/>
result = $@"' <auto-generated/>{noWarnsDisabled}
Imports System.Reflection
Expand All @@ -657,7 +691,7 @@ Imports System.Reflection
{getStringMethod}
{strings}
{classIndent}End Class
{namespaceEnd}
{namespaceEnd}{noWarnsRestored}
";
break;

Expand Down

0 comments on commit e734fb1

Please sign in to comment.