summaryrefslogtreecommitdiff
path: root/modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/Common.cs
blob: 16e96c725aa0ad3ed9a727ccb08d028614c72517 (plain)
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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Godot.SourceGenerators.Internal;

internal static class Common
{
    public static void ReportNonPartialUnmanagedCallbacksClass(
        GeneratorExecutionContext context,
        ClassDeclarationSyntax cds, INamedTypeSymbol symbol
    )
    {
        string message =
            "Missing partial modifier on declaration of type '" +
            $"{symbol.FullQualifiedName()}' which has attribute '{GeneratorClasses.GenerateUnmanagedCallbacksAttr}'";

        string description = $"{message}. Classes with attribute '{GeneratorClasses.GenerateUnmanagedCallbacksAttr}' " +
                             "must be declared with the partial modifier.";

        context.ReportDiagnostic(Diagnostic.Create(
            new DiagnosticDescriptor(id: "GODOT-INTERNAL-G0001",
                title: message,
                messageFormat: message,
                category: "Usage",
                DiagnosticSeverity.Error,
                isEnabledByDefault: true,
                description),
            cds.GetLocation(),
            cds.SyntaxTree.FilePath));
    }

    public static void ReportNonPartialUnmanagedCallbacksOuterClass(
        GeneratorExecutionContext context,
        TypeDeclarationSyntax outerTypeDeclSyntax
    )
    {
        var outerSymbol = context.Compilation
            .GetSemanticModel(outerTypeDeclSyntax.SyntaxTree)
            .GetDeclaredSymbol(outerTypeDeclSyntax);

        string fullQualifiedName = outerSymbol is INamedTypeSymbol namedTypeSymbol ?
            namedTypeSymbol.FullQualifiedName() :
            "type not found";

        string message =
            $"Missing partial modifier on declaration of type '{fullQualifiedName}', " +
            $"which contains one or more subclasses with attribute " +
            $"'{GeneratorClasses.GenerateUnmanagedCallbacksAttr}'";

        string description = $"{message}. Classes with attribute " +
                             $"'{GeneratorClasses.GenerateUnmanagedCallbacksAttr}' and their " +
                             "containing types must be declared with the partial modifier.";

        context.ReportDiagnostic(Diagnostic.Create(
            new DiagnosticDescriptor(id: "GODOT-INTERNAL-G0002",
                title: message,
                messageFormat: message,
                category: "Usage",
                DiagnosticSeverity.Error,
                isEnabledByDefault: true,
                description),
            outerTypeDeclSyntax.GetLocation(),
            outerTypeDeclSyntax.SyntaxTree.FilePath));
    }
}