Common.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Microsoft.CodeAnalysis;
  2. using Microsoft.CodeAnalysis.CSharp.Syntax;
  3. namespace Godot.SourceGenerators.Internal;
  4. internal static class Common
  5. {
  6. public static void ReportNonPartialUnmanagedCallbacksClass(
  7. GeneratorExecutionContext context,
  8. ClassDeclarationSyntax cds, INamedTypeSymbol symbol
  9. )
  10. {
  11. string message =
  12. "Missing partial modifier on declaration of type '" +
  13. $"{symbol.FullQualifiedNameOmitGlobal()}' which has attribute '{GeneratorClasses.GenerateUnmanagedCallbacksAttr}'";
  14. string description = $"{message}. Classes with attribute '{GeneratorClasses.GenerateUnmanagedCallbacksAttr}' " +
  15. "must be declared with the partial modifier.";
  16. context.ReportDiagnostic(Diagnostic.Create(
  17. new DiagnosticDescriptor(id: "GODOT-INTERNAL-G0001",
  18. title: message,
  19. messageFormat: message,
  20. category: "Usage",
  21. DiagnosticSeverity.Error,
  22. isEnabledByDefault: true,
  23. description),
  24. cds.GetLocation(),
  25. cds.SyntaxTree.FilePath));
  26. }
  27. public static void ReportNonPartialUnmanagedCallbacksOuterClass(
  28. GeneratorExecutionContext context,
  29. TypeDeclarationSyntax outerTypeDeclSyntax
  30. )
  31. {
  32. var outerSymbol = context.Compilation
  33. .GetSemanticModel(outerTypeDeclSyntax.SyntaxTree)
  34. .GetDeclaredSymbol(outerTypeDeclSyntax);
  35. string fullQualifiedName = outerSymbol is INamedTypeSymbol namedTypeSymbol ?
  36. namedTypeSymbol.FullQualifiedNameOmitGlobal() :
  37. "type not found";
  38. string message =
  39. $"Missing partial modifier on declaration of type '{fullQualifiedName}', " +
  40. $"which contains one or more subclasses with attribute " +
  41. $"'{GeneratorClasses.GenerateUnmanagedCallbacksAttr}'";
  42. string description = $"{message}. Classes with attribute " +
  43. $"'{GeneratorClasses.GenerateUnmanagedCallbacksAttr}' and their " +
  44. "containing types must be declared with the partial modifier.";
  45. context.ReportDiagnostic(Diagnostic.Create(
  46. new DiagnosticDescriptor(id: "GODOT-INTERNAL-G0002",
  47. title: message,
  48. messageFormat: message,
  49. category: "Usage",
  50. DiagnosticSeverity.Error,
  51. isEnabledByDefault: true,
  52. description),
  53. outerTypeDeclSyntax.GetLocation(),
  54. outerTypeDeclSyntax.SyntaxTree.FilePath));
  55. }
  56. }