public override void Initialize(AnalysisContext context) { context.EnableConcurrentExecution(); context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); context.RegisterCompilationStartAction(ctxt => { if (ReferenceSymbols.TryCreate(ctxt.Compilation, out ReferenceSymbols? typeReferences)) { ctxt.RegisterSyntaxNodeAction(c => Analyze(c, typeReferences), SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.InterfaceDeclaration); } }); }
public static bool TryCreate(Compilation compilation, [NotNullWhen(true)] out ReferenceSymbols?instance) { instance = null; var messagePackObjectAttribute = compilation.GetTypeByMetadataName("MessagePack.MessagePackObjectAttribute"); if (messagePackObjectAttribute is null) { return(false); } var unionAttribute = compilation.GetTypeByMetadataName("MessagePack.UnionAttribute"); if (unionAttribute is null) { return(false); } var keyAttribute = compilation.GetTypeByMetadataName("MessagePack.KeyAttribute"); if (keyAttribute is null) { return(false); } var ignoreAttribute = compilation.GetTypeByMetadataName("MessagePack.IgnoreMemberAttribute"); if (ignoreAttribute is null) { return(false); } var ignoreDataMemberAttribute = compilation.GetTypeByMetadataName("System.Runtime.Serialization.IgnoreDataMemberAttribute"); if (ignoreDataMemberAttribute is null) { return(false); } instance = new ReferenceSymbols( messagePackObjectAttribute, unionAttribute, keyAttribute, ignoreAttribute, ignoreDataMemberAttribute); return(true); }
private static void Analyze(SyntaxNodeAnalysisContext context, ReferenceSymbols typeReferences) { TypeDeclarationSyntax typeDeclaration = (TypeDeclarationSyntax)context.Node; INamedTypeSymbol? declaredSymbol = context.SemanticModel.GetDeclaredSymbol(typeDeclaration); if (declaredSymbol is null) { return; } if ( ((declaredSymbol.TypeKind == TypeKind.Interface) && declaredSymbol.GetAttributes().Any(x2 => Equals(x2.AttributeClass, typeReferences.UnionAttribute))) || ((declaredSymbol.TypeKind == TypeKind.Class) && declaredSymbol.GetAttributes().Any(x2 => Equals(x2.AttributeClass, typeReferences.MessagePackObjectAttribute))) || ((declaredSymbol.TypeKind == TypeKind.Struct) && declaredSymbol.GetAttributes().Any(x2 => Equals(x2.AttributeClass, typeReferences.MessagePackObjectAttribute)))) { var reportContext = new DiagnosticsReportContext(context); var collector = new TypeCollector(reportContext, typeReferences); collector.CollectCore(declaredSymbol); reportContext.ReportAll(); } }
public TypeCollector(DiagnosticsReportContext reportContext, Compilation compilation) { this.typeReferences = new ReferenceSymbols(compilation); this.ReportContext = reportContext; }
public TypeCollector(DiagnosticsReportContext reportContext, ReferenceSymbols typeReferences) { this.typeReferences = typeReferences; this.ReportContext = reportContext; }