-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
sourcegeneration/StereologueSourceGenerator/DiagnosticInfo.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,54 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Stereologue.SourceGenerator; | ||
|
||
/// <summary> | ||
/// Descriptor for diagnostic instances using structural equality comparison. | ||
/// Provides a work-around for https://github.com/dotnet/roslyn/issues/68291. | ||
/// </summary> | ||
internal readonly struct DiagnosticInfo : IEquatable<DiagnosticInfo> | ||
{ | ||
public DiagnosticDescriptor Descriptor { get; private init; } | ||
public object?[] MessageArgs { get; private init; } | ||
public Location? Location { get; private init; } | ||
|
||
public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, Location? location, object?[]? messageArgs) | ||
{ | ||
Location? trimmedLocation = location is null ? null : GetTrimmedLocation(location); | ||
|
||
return new DiagnosticInfo | ||
{ | ||
Descriptor = descriptor, | ||
Location = trimmedLocation, | ||
MessageArgs = messageArgs ?? [] | ||
}; | ||
|
||
// 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); | ||
} | ||
|
||
public Diagnostic CreateDiagnostic() | ||
=> Diagnostic.Create(Descriptor, Location, MessageArgs); | ||
|
||
public override readonly bool Equals(object? obj) => obj is DiagnosticInfo info && Equals(info); | ||
|
||
public readonly bool Equals(DiagnosticInfo other) | ||
{ | ||
return Descriptor.Equals(other.Descriptor) && | ||
MessageArgs.SequenceEqual(other.MessageArgs) && | ||
Location == other.Location; | ||
} | ||
|
||
public override readonly int GetHashCode() | ||
{ | ||
int hashCode = Descriptor.GetHashCode(); | ||
foreach (object? messageArg in MessageArgs) | ||
{ | ||
hashCode = HashCode.Combine(hashCode, messageArg?.GetHashCode() ?? 0); | ||
} | ||
|
||
hashCode = HashCode.Combine(hashCode, Location?.GetHashCode() ?? 0); | ||
return hashCode; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
sourcegeneration/StereologueSourceGenerator/GeneratorDiagnostics.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,50 @@ | ||
namespace Stereologue.SourceGenerator; | ||
|
||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#pragma warning disable RS2008 // Enable analyzer release tracking | ||
|
||
using System; | ||
using Microsoft.CodeAnalysis; | ||
|
||
public static class GeneratorDiagnostics | ||
{ | ||
public class Ids | ||
{ | ||
public const string Prefix = "WPILIB"; | ||
public const string GeneratedTypeNotPartial = Prefix + "1000"; | ||
public const string GeneratedTypeImplementsILogged = Prefix + "1001"; | ||
public const string LoggableTypeNotSupported = Prefix + "1002"; | ||
public const string GeneratedTypeIsInterface = Prefix + "1003"; | ||
public const string GeneratedTypeIsRefStruct = Prefix + "1004"; | ||
public const string LoggedMethodDoesntReturnVoid = Prefix + "1005"; | ||
public const string LoggedMethodTakesArguments = Prefix + "1006"; | ||
public const string LoggedMemberTypeNotSupported = Prefix + "1007"; | ||
} | ||
|
||
private const string Category = "StereologueSourceGenerator"; | ||
|
||
public static readonly DiagnosticDescriptor GeneratedTypeNotPartial = new( | ||
Ids.GeneratedTypeNotPartial, "", "", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, ""); | ||
|
||
public static readonly DiagnosticDescriptor GeneratedTypeImplementsILogged = new( | ||
Ids.GeneratedTypeImplementsILogged, "", "", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, ""); | ||
|
||
public static readonly DiagnosticDescriptor LoggableTypeNotSupported = new( | ||
Ids.LoggableTypeNotSupported, "", "", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, ""); | ||
|
||
public static readonly DiagnosticDescriptor GeneratedTypeIsInterface = new( | ||
Ids.LoggableTypeNotSupported, "", "", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, ""); | ||
|
||
public static readonly DiagnosticDescriptor GeneratedTypeIsRefStruct = new( | ||
Ids.GeneratedTypeIsRefStruct, "", "", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, ""); | ||
|
||
public static readonly DiagnosticDescriptor LoggedMethodDoesntReturnVoid = new( | ||
Ids.LoggedMethodDoesntReturnVoid, "", "", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, ""); | ||
|
||
public static readonly DiagnosticDescriptor LoggedMethodTakesArguments = new( | ||
Ids.LoggedMethodTakesArguments, "", "", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, ""); | ||
public static readonly DiagnosticDescriptor LoggedMemberTypeNotSupported = new( | ||
Ids.LoggedMemberTypeNotSupported, "", "", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, ""); | ||
} |
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