Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions src/dotnetCampus.Ipc.Analyzers/CodeAnalysis/Core/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static class Diagnostics
Categories.Compiler,
DiagnosticSeverity.Error,
true,
customTags: new[] { AnalyzerException, NotConfigurable });
customTags: [AnalyzerException, NotConfigurable]);

/// <summary>
/// 生成代码时出现已知错误。本生成器不会报告此错误,因为后续编译器会准确报告之。
Expand All @@ -32,7 +32,7 @@ internal static class Diagnostics
Categories.Compiler,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

/// <summary>
/// 生成代码时出现已知错误。本生成器不会报告此错误,因为分析器会准确报告之。
Expand All @@ -44,7 +44,7 @@ internal static class Diagnostics
Categories.Compiler,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPC101_IpcType_TimeoutCannotBeNegative { get; } = new(
nameof(IPC101),
Expand All @@ -61,7 +61,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC131_IpcMembers_IgnoresIpcExceptionIsRecommended { get; } = new(
nameof(IPC131),
Expand All @@ -78,7 +78,7 @@ internal static class Diagnostics
Categories.Mechanism,
DiagnosticSeverity.Error,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPC161_IpcShape_ContractTypeDismatchWithInterface { get; } = new(
nameof(IPC161),
Expand All @@ -87,7 +87,7 @@ internal static class Diagnostics
Categories.Mechanism,
DiagnosticSeverity.Error,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPC162_IpcShape_AllMembersShouldBeMarkedAsIpcMembers { get; } = new(
nameof(IPC162),
Expand All @@ -104,7 +104,7 @@ internal static class Diagnostics
Categories.Mechanism,
DiagnosticSeverity.Error,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPC201_IpcMember_EmptyIpcMemberAttributeIsUnnecessary { get; } = new(
nameof(IPC201),
Expand All @@ -113,7 +113,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC202_IpcMember_AllMembersShouldBeMarkedAsIpcMembers { get; } = new(
nameof(IPC202),
Expand All @@ -130,7 +130,7 @@ internal static class Diagnostics
Categories.AvoidBugs,
DiagnosticSeverity.Info,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPC241_IpcProperty_SetOnlyPropertyIsNotSupported { get; } = new(
nameof(IPC241),
Expand All @@ -139,7 +139,7 @@ internal static class Diagnostics
Categories.Mechanism,
DiagnosticSeverity.Error,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPC242_IpcProperty_DefaultReturnDependsOnIgnoresIpcException { get; } = new(
nameof(IPC242),
Expand All @@ -148,7 +148,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC243_IpcProperty_IsReadonlyFalseIsUnnecessary { get; } = new(
nameof(IPC243),
Expand All @@ -157,7 +157,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC244_IpcProperty_DefaultReturnDismatchWithPropertyType { get; } = new(
nameof(IPC244),
Expand Down Expand Up @@ -206,7 +206,7 @@ internal static class Diagnostics
Categories.AvoidBugs,
DiagnosticSeverity.Info,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPC261_IpcMethod_DefaultReturnDependsOnIgnoresIpcException { get; } = new(
nameof(IPC261),
Expand All @@ -215,7 +215,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC262_IpcMethod_WaitsVoidIsRecommended { get; } = new(
nameof(IPC262),
Expand All @@ -232,7 +232,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC264_IpcMethod_DefaultReturnDismatchWithMethodReturnType { get; } = new(
nameof(IPC264),
Expand All @@ -249,7 +249,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC266_IpcMethod_DefaultReturnIsUselessForATaskMethod { get; } = new(
nameof(IPC266),
Expand All @@ -258,7 +258,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC267_IpcMethod_DefaultReturnsStringForAnObjectType { get; } = new(
nameof(IPC267),
Expand Down Expand Up @@ -291,7 +291,7 @@ internal static class Diagnostics
Categories.Mechanism,
DiagnosticSeverity.Error,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPC271_IpcMethod_MethodParameterTypeIsNotSupportedForIpc { get; } = new(
nameof(IPC271),
Expand Down Expand Up @@ -340,7 +340,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC305_CreateIpcProxy_IgnoresIpcExceptionIsRecommended { get; } = new(
nameof(IPC305),
Expand All @@ -357,7 +357,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC307_CreateIpcProxy_TimeoutIsRecommended { get; } = new(
nameof(IPC307),
Expand All @@ -374,7 +374,7 @@ internal static class Diagnostics
Categories.Useless,
DiagnosticSeverity.Hidden,
true,
customTags: new[] { Unnecessary });
customTags: [Unnecessary]);

public static DiagnosticDescriptor IPC309_CreateIpcProxy_IpcShapeIsNotValid { get; } = new(
nameof(IPC309),
Expand All @@ -383,7 +383,7 @@ internal static class Diagnostics
Categories.Mechanism,
DiagnosticSeverity.Error,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPC310_CreateIpcProxy_IpcShapeDismatchWithContractType { get; } = new(
nameof(IPC310),
Expand All @@ -392,7 +392,7 @@ internal static class Diagnostics
Categories.Mechanism,
DiagnosticSeverity.Error,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

public static DiagnosticDescriptor IPCTMP1_IpcMembers_EventIsNotSupported { get; } = new(
"IPCTMP1",
Expand All @@ -401,7 +401,7 @@ internal static class Diagnostics
Categories.Mechanism,
DiagnosticSeverity.Error,
true,
customTags: new[] { NotConfigurable });
customTags: [NotConfigurable]);

private static class Categories
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public IpcPublicAttributeNamedValues(INamedTypeSymbol? ipcType, ISymbol? member,

public ITypeSymbol? MemberReturnType { get; }

public Assignable<object?>? DefaultReturn { get; init; }
public Assignable<string?>? DefaultReturn { get; init; }

public Assignable<bool>? IgnoresIpcException { get; init; }

Expand All @@ -40,7 +40,7 @@ public string ToIndentString(string indent, int baseIndentLevel = 1)
var baseIndent = string.Concat(Enumerable.Repeat(indent, baseIndentLevel));
List<string> assignments =
[
Format(nameof(DefaultReturn), DefaultReturn, x => Format(x, MemberReturnType)),
Format(nameof(DefaultReturn), DefaultReturn, x => x ?? "null"),
Format(nameof(Timeout), Timeout),
Format(nameof(IgnoresIpcException), IgnoresIpcException),
Format(nameof(IsReadonly), IsReadonly),
Expand Down Expand Up @@ -107,34 +107,4 @@ protected static string Format(object? value, Type valueType)
return value.ToString();
}
}

/// <summary>
/// 将 Attribute 里设置的值转为字符串。
/// </summary>
/// <param name="value">值。</param>
/// <param name="valueType">值的类型。</param>
/// <returns></returns>
protected static string Format(object? value, ITypeSymbol? valueType)
{
if (value is null)
{
return "null";
}
else if (valueType?.ToString() == "string")
{
return $@"""{value}""";
}
if (valueType?.ToString() == "bool" && value is bool booleanValue)
{
return booleanValue ? "true" : "false";
}
if (value is int int32Value)
{
return int32Value.ToString(CultureInfo.InvariantCulture);
}
else
{
return value.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override string ToString()
{
return $@"new()
{{
{Format(nameof(DefaultReturn), DefaultReturn, x => Format(x, MemberReturnType))}
{Format(nameof(DefaultReturn), DefaultReturn, x => x ?? "null")}
{Format(nameof(Timeout), Timeout)}
{Format(nameof(IgnoresIpcException), IgnoresIpcException)}
{Format(nameof(IsReadonly), IsReadonly)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal static IpcPublicAttributeNamedValues GetIpcNamedValues(this IMethodSymb
var waitsVoid = method.GetAttributeValue<IpcMethodAttribute, bool>(nameof(IpcMethodAttribute.WaitsVoid));
var ignoresIpcException = method.GetAttributeValue<IpcMethodAttribute, bool>(nameof(IpcMethodAttribute.IgnoresIpcException))
?? type.GetAttributeValue<IpcPublicAttribute, bool>(nameof(IpcPublicAttribute.IgnoresIpcException));
var defaultReturn = method.GetAttributeValue<IpcMethodAttribute, object?>(nameof(IpcMethodAttribute.DefaultReturn));
var defaultReturn = method.GetAttributeValue<IpcMethodAttribute, string?>(nameof(IpcMethodAttribute.DefaultReturn));
var timeout = method.GetAttributeValue<IpcMethodAttribute, int>(nameof(IpcMethodAttribute.Timeout))
?? type.GetAttributeValue<IpcPublicAttribute, int>(nameof(IpcPublicAttribute.Timeout));
return new IpcPublicAttributeNamedValues(type, method, returnTypeSymbol)
Expand All @@ -88,7 +88,7 @@ public static IpcPublicAttributeNamedValues GetIpcNamedValues(this IPropertySymb
var isReadonly = property.GetAttributeValue<IpcPropertyAttribute, bool>(nameof(IpcPropertyAttribute.IsReadonly));
var ignoresIpcException = property.GetAttributeValue<IpcPropertyAttribute, bool>(nameof(IpcPropertyAttribute.IgnoresIpcException))
?? type.GetAttributeValue<IpcPublicAttribute, bool>(nameof(IpcPublicAttribute.IgnoresIpcException));
var defaultReturn = property.GetAttributeValue<IpcPropertyAttribute, object?>(nameof(IpcPropertyAttribute.DefaultReturn));
var defaultReturn = property.GetAttributeValue<IpcPropertyAttribute, string?>(nameof(IpcPropertyAttribute.DefaultReturn));
var timeout = property.GetAttributeValue<IpcPropertyAttribute, int>(nameof(IpcPropertyAttribute.Timeout))
?? type.GetAttributeValue<IpcPublicAttribute, int>(nameof(IpcPublicAttribute.Timeout));
return new IpcPublicAttributeNamedValues(type, property, property.Type)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics;

namespace dotnetCampus.Ipc.CodeAnalysis.Utils;
namespace dotnetCampus.Ipc.CodeAnalysis.Utils;

/// <summary>
/// 包含语义分析的辅助扩展方法。
Expand All @@ -15,10 +13,9 @@ internal static class SemanticAttributeHelper
/// <param name="symbol">要查找特性的语义符号。</param>
/// <param name="namedArgumentName">参数名称。</param>
/// <returns>参数的值。</returns>
[return: MaybeNull]
public static Assignable<T>? GetAttributeValue<TAttribute, T>(this ISymbol symbol, string namedArgumentName)
{
var assignable = GetAttributeValue(symbol, typeof(TAttribute).FullName, namedArgumentName);
var assignable = GetAttributeValue(symbol, typeof(TAttribute).FullName!, namedArgumentName);

// 未赋值。
if (assignable is null)
Expand All @@ -31,6 +28,10 @@ internal static class SemanticAttributeHelper
{
return new((T?) assignable.Value);
}
else if (typeof(T) == typeof(string))
{
return new((T?) assignable.Value);
}

// null 已赋值。
var value = assignable.Value.Value;
Expand Down Expand Up @@ -64,7 +65,7 @@ internal static bool GetIsDefined<TAttribute>(this ISymbol symbol)
return symbol.GetAttributes().FirstOrDefault(x => string.Equals(
x.AttributeClass?.ToString(),
typeof(TAttribute).FullName,
StringComparison.Ordinal)) is { } ipcMethodAttribute;
StringComparison.Ordinal)) is not null;
}

/// <summary>
Expand Down
Loading
Loading