-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Reenable pragma suppressions for config binder gen (+ JSON) #92740
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,9 @@ | |
| using System; | ||
| using System.Linq; | ||
| using System.Numerics.Hashing; | ||
| using System.Reflection; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
|
|
||
| namespace SourceGenerators; | ||
|
|
||
|
|
@@ -14,6 +16,9 @@ namespace SourceGenerators; | |
| /// </summary> | ||
| internal readonly struct DiagnosticInfo : IEquatable<DiagnosticInfo> | ||
| { | ||
| private static readonly Lazy<CSharpSyntaxTree?> s_dummySyntaxTree = new Lazy<CSharpSyntaxTree?>(GetDummySyntaxTree); | ||
| private static readonly Lazy<FieldInfo?> s_sourceTreeBackingFieldInfo = new Lazy<FieldInfo?>(GetSourceTreeBackingFieldInfo); | ||
|
|
||
| public DiagnosticDescriptor Descriptor { get; private init; } | ||
| public object?[] MessageArgs { get; private init; } | ||
| public Location? Location { get; private init; } | ||
|
|
@@ -30,8 +35,35 @@ public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, Location? l | |
| }; | ||
|
|
||
| // Creates a copy of the Location instance that does not capture a reference to Compilation. | ||
| static Location GetTrimmedLocation(Location location) | ||
| => Location.Create(location.SourceTree?.FilePath ?? "", location.SourceSpan, location.GetLineSpan().Span); | ||
| static Location? GetTrimmedLocation(Location? sourceLocation) | ||
| { | ||
| if (sourceLocation is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| Location trimmedLocation = Location.Create(sourceLocation.SourceTree?.FilePath ?? "", sourceLocation.SourceSpan, sourceLocation.GetLineSpan().Span); | ||
|
|
||
| if (sourceLocation.IsInSource && | ||
| !trimmedLocation.IsInSource && | ||
| s_sourceTreeBackingFieldInfo.Value is FieldInfo sourceTreeField && | ||
| s_dummySyntaxTree.Value is CSharpSyntaxTree syntaxTree) | ||
eiriktsarpalis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| // Attempt to mark this as a source location, so that it is suppressible with #pragma. | ||
| try | ||
| { | ||
| sourceTreeField.SetValue(trimmedLocation, syntaxTree); | ||
|
|
||
| if (!trimmedLocation.IsInSource) | ||
| { | ||
| sourceTreeField.SetValue(trimmedLocation, null); | ||
| } | ||
| } | ||
| catch { } | ||
| } | ||
|
|
||
| return trimmedLocation; | ||
| } | ||
| } | ||
|
|
||
| public Diagnostic CreateDiagnostic() | ||
|
|
@@ -57,4 +89,34 @@ public override readonly int GetHashCode() | |
| hashCode = HashHelpers.Combine(hashCode, Location?.GetHashCode() ?? 0); | ||
| return hashCode; | ||
| } | ||
|
|
||
| private static FieldInfo? GetSourceTreeBackingFieldInfo() | ||
| { | ||
| FieldInfo? info; | ||
|
|
||
| try | ||
| { | ||
| FieldInfo[] fields = typeof(Location).GetFields(BindingFlags.NonPublic); | ||
| info = typeof(Location).GetField("<SourceTree>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not comfortable taking a dependency on this type of implementation detail, particularly in product code. We should instead work with @dotnet/roslyn to come up with a better solution for storing cc @stephentoub |
||
| } | ||
| catch | ||
| { | ||
| info = null; | ||
| } | ||
|
|
||
| return info; | ||
| } | ||
|
|
||
| private static CSharpSyntaxTree? GetDummySyntaxTree() | ||
| { | ||
| try | ||
| { | ||
| Type? dummySyntaxTree = typeof(CSharpSyntaxTree).Assembly.GetType("Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.DummySyntaxTree", throwOnError: false); | ||
| return dummySyntaxTree is null ? null : (CSharpSyntaxTree?)Activator.CreateInstance(dummySyntaxTree); | ||
| } | ||
| catch | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What determines whether
trimmedLocation.IsInSourceis true?