-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Options Source Gen Trimming Issues
- Loading branch information
Showing
41 changed files
with
3,819 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
445 changes: 417 additions & 28 deletions
445
src/libraries/Microsoft.Extensions.Options/gen/Emitter.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/libraries/Microsoft.Extensions.Options/gen/OptionsSourceGenContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Runtime.Versioning; | ||
|
||
namespace Microsoft.Extensions.Options.Generators | ||
{ | ||
internal sealed class OptionsSourceGenContext | ||
{ | ||
public OptionsSourceGenContext(Compilation compilation) | ||
{ | ||
IsLangVersion11AndAbove = ((CSharpCompilation)compilation).LanguageVersion >= Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp11; | ||
ClassModifier = IsLangVersion11AndAbove ? "file" : "internal"; | ||
Suffix = IsLangVersion11AndAbove ? "" : $"_{GetNonRandomizedHashCode(compilation.SourceModule.Name):X8}"; | ||
} | ||
|
||
internal string Suffix { get; } | ||
internal string ClassModifier { get; } | ||
internal bool IsLangVersion11AndAbove { get; } | ||
internal Dictionary<string, HashSet<object>?> AttributesToGenerate { get; set; } = new Dictionary<string, HashSet<object>?>(); | ||
|
||
internal void EnsureTrackingAttribute(string attributeName, bool createValue, out HashSet<object>? value) | ||
{ | ||
bool exist = AttributesToGenerate.TryGetValue(attributeName, out value); | ||
if (value is null) | ||
{ | ||
if (createValue) | ||
{ | ||
value = new HashSet<object>(); | ||
} | ||
|
||
if (!exist || createValue) | ||
{ | ||
AttributesToGenerate[attributeName] = value; | ||
} | ||
} | ||
} | ||
|
||
internal static bool IsConvertibleBasicType(ITypeSymbol typeSymbol) | ||
{ | ||
return typeSymbol.SpecialType switch | ||
{ | ||
SpecialType.System_Boolean => true, | ||
SpecialType.System_Byte => true, | ||
SpecialType.System_Char => true, | ||
SpecialType.System_DateTime => true, | ||
SpecialType.System_Decimal => true, | ||
SpecialType.System_Double => true, | ||
SpecialType.System_Int16 => true, | ||
SpecialType.System_Int32 => true, | ||
SpecialType.System_Int64 => true, | ||
SpecialType.System_SByte => true, | ||
SpecialType.System_Single => true, | ||
SpecialType.System_UInt16 => true, | ||
SpecialType.System_UInt32 => true, | ||
SpecialType.System_UInt64 => true, | ||
SpecialType.System_String => true, | ||
_ => false, | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Returns a non-randomized hash code for the given string. | ||
/// We always return a positive value. | ||
/// </summary> | ||
internal static int GetNonRandomizedHashCode(string s) | ||
{ | ||
uint result = 2166136261u; | ||
foreach (char c in s) | ||
{ | ||
result = (c ^ result) * 16777619; | ||
} | ||
|
||
return Math.Abs((int)result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.