-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathErrorReporter.cs
More file actions
112 lines (102 loc) · 5.37 KB
/
ErrorReporter.cs
File metadata and controls
112 lines (102 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using static GDScriptInterface.SourceGenerator.Analyzer.Diagnostics;
namespace GDScriptInterface.SourceGenerator;
/// <summary>
/// Provides methods for reporting diagnostics (errors) during source generation.
/// This class is responsible for creating and reporting diagnostic messages
/// based on invalid usage of attributes, types, or unsupported features.
/// </summary>
internal static class ErrorReporter
{
/// <summary>
/// Reports an error when an invalid type is used in the context of an interface member.
/// </summary>
/// <param name="context">The source production context used to report the diagnostic.</param>
/// <param name="iface">The named type symbol representing the interface.</param>
/// <param name="invalidType">The type symbol that is considered invalid.</param>
/// <param name="member">The symbol representing the member where the invalid type was used.</param>
public static void ReportInvalidType(
SourceProductionContext context,
INamedTypeSymbol iface,
ITypeSymbol invalidType,
ISymbol member)
{
var diagnostic = Diagnostic.Create(
InvalidVariantTypeDescriptor, // Descriptor for invalid variant type diagnostics.
member.Locations.FirstOrDefault(), // Location of the member in the source code.
new object[]
{
iface.Name, // Name of the interface.
invalidType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat), // Display string of the invalid type.
member.Name // Name of the member.
}
);
context.ReportDiagnostic(diagnostic);
}
/// <summary>
/// Reports an error when an attribute is used incorrectly.
/// </summary>
/// <param name="context">The source production context used to report the diagnostic.</param>
/// <param name="target">The symbol where the invalid attribute usage occurred.</param>
/// <param name="message">A custom message describing the error.</param>
public static void ReportInvalidAttributeUsage(SourceProductionContext context, ISymbol target, string message)
{
var diagnostic = Diagnostic.Create(
descriptor: InvalidAttributeUsageDescriptor, // Descriptor for invalid attribute usage diagnostics.
location: target.Locations.FirstOrDefault(), // Location of the target symbol in the source code.
messageArgs: new object[] { message } // Custom error message.
);
context.ReportDiagnostic(diagnostic);
}
/// <summary>
/// Reports an error when an indexer is used in a context where it is not supported.
/// </summary>
/// <param name="context">The source production context used to report the diagnostic.</param>
/// <param name="iface">The named type symbol representing the interface.</param>
/// <param name="prop">The property symbol representing the indexer.</param>
public static void ReportIndexerNotSupported(SourceProductionContext context, INamedTypeSymbol iface, IPropertySymbol prop)
{
var diagnostic = Diagnostic.Create(
descriptor: IndexerNotSupportedDescriptor, // Descriptor for indexer not supported diagnostics.
location: prop.Locations.FirstOrDefault(), // Location of the property in the source code.
messageArgs: new object[]
{
iface.Name, // Name of the interface.
prop.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat) // Display string of the property.
}
);
context.ReportDiagnostic(diagnostic);
}
/// <summary>
/// Reports an error when a generic interface is used in a context where it is not supported.
/// </summary>
/// <param name="context">The source production context used to report the diagnostic.</param>
/// <param name="iface">The named type symbol representing the generic interface.</param>
public static void ReportGenericInterfaceNotSupported(SourceProductionContext context, INamedTypeSymbol iface)
{
var diagnostic = Diagnostic.Create(
GenericInterfaceNotSupported, // Descriptor for generic interface not supported diagnostics.
iface.Locations.FirstOrDefault(), // Location of the interface in the source code.
iface.Name // Name of the interface.
);
context.ReportDiagnostic(diagnostic);
}
/// <summary>
/// Reports an error when a generic method is used in a context where it is not supported.
/// </summary>
/// <param name="context">The source production context used to report the diagnostic.</param>
/// <param name="iface">The named type symbol representing the interface containing the method.</param>
/// <param name="method">The method symbol representing the generic method.</param>
public static void ReportGenericMethodNotSupported(SourceProductionContext context, INamedTypeSymbol iface, IMethodSymbol method)
{
var diagnostic = Diagnostic.Create(
GenericMethodNotSupported, // Descriptor for generic method not supported diagnostics.
method.Locations.FirstOrDefault(), // Location of the method in the source code.
method.Name, // Name of the method.
iface.Name // Name of the interface.
);
context.ReportDiagnostic(diagnostic);
}
}