gir.core
gir.core copied to clipboard
Idea: Analyzer to verify api versions
Probably related to #612.
Any GObject API could have an attribute since when it was introduced.
There could be an analyzer project which checks another attribute in the user code that defines per GirCore assembly which api version is allowed to be used.
If a newer api call is found a warning is raised.
This could benefit projects which depend on an older version of for example GTK and can't use newly introduced API. In this way they can use the newest GirCore version and be sure no exception is raised during runtime.
This could probably be a project independent of GirCore.
Example code from Copilot:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ApiUsageAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
id: "MY001",
title: "Restricted API Usage",
messageFormat: "The API '{0}' has the restricted attribute and should not be used.",
category: "Usage",
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
// Register an action to analyze method invocations
context.RegisterSymbolAction(AnalyzeMethodSymbol, SymbolKind.Method);
}
private static void AnalyzeMethodSymbol(SymbolAnalysisContext context)
{
var methodSymbol = (IMethodSymbol)context.Symbol;
// Check if the method has the specific attribute
foreach (var attribute in methodSymbol.GetAttributes())
{
if (attribute.AttributeClass?.Name == "SomeCustomAttribute")
{
// Report a diagnostic
var diagnostic = Diagnostic.Create(Rule, methodSymbol.Locations[0], methodSymbol.Name);
context.ReportDiagnostic(diagnostic);
}
}
}
}
Perhaps this can be done with a generic attribute and enums to define versions?