private void Analyzer(SymbolAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllHotfix)) { return; } if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol)) { return; } if (namedTypeSymbol.IsStatic) { return; } if (!this.CheckIsTypeOrBaseTypeHasBaseAttributeInherit(namedTypeSymbol)) { foreach (SyntaxReference?declaringSyntaxReference in namedTypeSymbol.DeclaringSyntaxReferences) { Diagnostic diagnostic = Diagnostic.Create(Rule, declaringSyntaxReference.GetSyntax()?.GetLocation(), namedTypeSymbol.Name); context.ReportDiagnostic(diagnostic); } } }
private void Analyzer(SymbolAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllHotfix)) { return; } if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol)) { return; } foreach (ISymbol?memberSymbol in namedTypeSymbol.GetMembers()) { // 筛选出字段成员 if (!(memberSymbol is IFieldSymbol fieldSymbol)) { return; } // 允许声明Const字段 if (fieldSymbol.IsConst) { return; } foreach (SyntaxReference?declaringSyntaxReference in fieldSymbol.DeclaringSyntaxReferences) { Diagnostic diagnostic = Diagnostic.Create(Rule, declaringSyntaxReference.GetSyntax()?.GetLocation(), fieldSymbol.Name); context.ReportDiagnostic(diagnostic); } } }
private void Analyzer(SymbolAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllModel)) { return; } if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol)) { return; } // 筛选出实体类 if (namedTypeSymbol.BaseType?.ToString() != Definition.EntityType) { return; } foreach (var member in namedTypeSymbol.GetMembers()) { if (member is IFieldSymbol fieldSymbol && fieldSymbol.Type.BaseType?.ToString() == typeof(MulticastDelegate).FullName) { ReportDiagnostic(fieldSymbol, fieldSymbol.Name); continue; } if (member is IPropertySymbol propertySymbol && propertySymbol.Type.BaseType?.ToString() == typeof(MulticastDelegate).FullName) { ReportDiagnostic(propertySymbol, propertySymbol.Name); continue; } } void ReportDiagnostic(ISymbol symbol, string delegateName) { foreach (var syntaxReference in symbol.DeclaringSyntaxReferences) { var syntax = syntaxReference.GetSyntax(); Diagnostic diagnostic = Diagnostic.Create(Rule, syntax.GetLocation(), namedTypeSymbol.Name, delegateName); context.ReportDiagnostic(diagnostic); } } }
private void Analyzer(SymbolAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllModel)) { return; } if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol)) { return; } // 筛选出实体类 if (namedTypeSymbol.BaseType?.ToString() != EntityType) { return; } // 忽略含有EnableMethod标签的实体类 if (namedTypeSymbol.HasAttribute(EnableMethodAttribute)) { return; } foreach (var syntaxReference in namedTypeSymbol.DeclaringSyntaxReferences) { var classSyntax = syntaxReference.GetSyntax(); if (!(classSyntax is ClassDeclarationSyntax classDeclarationSyntax)) { return; } foreach (var memberDeclarationSyntax in classDeclarationSyntax.Members) { // 筛选出类声明语法节点下的所有方法声明语法节点 if (memberDeclarationSyntax is MethodDeclarationSyntax methodDeclarationSyntax) { Diagnostic diagnostic = Diagnostic.Create(Rule, methodDeclarationSyntax.GetLocation(), namedTypeSymbol.Name, methodDeclarationSyntax.Identifier.Text); context.ReportDiagnostic(diagnostic); } } } }
private void CompilationStartAnalysis(CompilationStartAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllHotfix)) { return; } if (context.Compilation.AssemblyName == null) { return; } var dependencyMap = new ConcurrentDictionary <string, HashSet <string> >(); var staticClassSet = new HashSet <string>(); context.RegisterSyntaxNodeAction( analysisContext => { this.StaticClassDependencyAnalyze(analysisContext, dependencyMap, staticClassSet); }, SyntaxKind.InvocationExpression); context.RegisterCompilationEndAction(analysisContext => { this.CircularDependencyAnalyze(analysisContext, dependencyMap, staticClassSet); }); }
private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllHotfix)) { return; } if (!(context.Node is MemberAccessExpressionSyntax memberAccessExpressionSyntax)) { return; } // 筛选出 AddChild函数syntax string methodName = memberAccessExpressionSyntax.Name.Identifier.Text; if (!AddChildMethods.Contains(methodName)) { return; } if (!(memberAccessExpressionSyntax?.Parent is InvocationExpressionSyntax invocationExpressionSyntax) || !(context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol is IMethodSymbol addChildMethodSymbol)) { return; } // 获取AddChild函数的调用者类型 ITypeSymbol?parentTypeSymbol = memberAccessExpressionSyntax.GetMemberAccessSyntaxParentType(context.SemanticModel); if (parentTypeSymbol == null) { return; } // 只检查Entity的子类 if (parentTypeSymbol.BaseType?.ToString() != EntityType) { return; } // 获取实体类 ChildType标签的约束类型 INamedTypeSymbol?availableChildTypeSymbol = null; bool hasChildTypeAttribute = false; foreach (AttributeData?attributeData in parentTypeSymbol.GetAttributes()) { if (attributeData.AttributeClass?.Name == "ChildTypeAttribute") { hasChildTypeAttribute = true; if (!(attributeData.ConstructorArguments[0].Value is INamedTypeSymbol s)) { continue; } availableChildTypeSymbol = s; } } if (hasChildTypeAttribute && (availableChildTypeSymbol == null)) { return; } // 获取 child实体类型 ISymbol?childTypeSymbol = null; // addChild为泛型调用 if (addChildMethodSymbol.IsGenericMethod) { GenericNameSyntax?genericNameSyntax = memberAccessExpressionSyntax?.GetFirstChild <GenericNameSyntax>(); TypeArgumentListSyntax?typeArgumentList = genericNameSyntax?.GetFirstChild <TypeArgumentListSyntax>(); IdentifierNameSyntax?childTypeSyntax = typeArgumentList?.GetFirstChild <IdentifierNameSyntax>(); if (childTypeSyntax == null) { Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation()); context.ReportDiagnostic(diagnostic); throw new Exception("childTypeSyntax==null"); } childTypeSymbol = context.SemanticModel.GetSymbolInfo(childTypeSyntax).Symbol; } // addChild为非泛型调用 else { SyntaxNode?firstArgumentSyntax = invocationExpressionSyntax.GetFirstChild <ArgumentListSyntax>()?.GetFirstChild <ArgumentSyntax>() ?.ChildNodes().First(); if (firstArgumentSyntax == null) { Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation()); context.ReportDiagnostic(diagnostic); return; } ISymbol?firstArgumentSymbol = context.SemanticModel.GetSymbolInfo(firstArgumentSyntax).Symbol; if (firstArgumentSymbol is ILocalSymbol childLocalSymbol) { childTypeSymbol = childLocalSymbol.Type; } else if (firstArgumentSymbol is IParameterSymbol childParamaterSymbol) { childTypeSymbol = childParamaterSymbol.Type; } else if (firstArgumentSymbol is IMethodSymbol methodSymbol) { childTypeSymbol = methodSymbol.ReturnType; } else if (firstArgumentSymbol is IFieldSymbol fieldSymbol) { childTypeSymbol = fieldSymbol.Type; } else if (firstArgumentSymbol is IPropertySymbol propertySymbol) { childTypeSymbol = propertySymbol.Type; } else if (firstArgumentSymbol != null) { Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), firstArgumentSymbol.Name, parentTypeSymbol.Name); context.ReportDiagnostic(diagnostic); return; } else { Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), firstArgumentSyntax.GetText(), parentTypeSymbol.Name); context.ReportDiagnostic(diagnostic); return; } } if (childTypeSymbol == null) { return; } // 判断child类型是否属于约束类型 if (availableChildTypeSymbol?.ToString() == childTypeSymbol.ToString()) { return; } { Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), childTypeSymbol?.Name, parentTypeSymbol?.Name); context.ReportDiagnostic(diagnostic); } }
private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.All)) { return; } if (!(context.Node is MemberAccessExpressionSyntax memberAccessExpressionSyntax)) { return; } // 筛选出 Component函数syntax string methodName = memberAccessExpressionSyntax.Name.Identifier.Text; if (!Definition.ComponentMethod.Contains(methodName)) { return; } if (!(memberAccessExpressionSyntax?.Parent is InvocationExpressionSyntax invocationExpressionSyntax) || !(context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol is IMethodSymbol addComponentMethodSymbol)) { return; } // 获取AComponent函数的调用者类型 ITypeSymbol?parentTypeSymbol = memberAccessExpressionSyntax.GetMemberAccessSyntaxParentType(context.SemanticModel); if (parentTypeSymbol == null) { return; } // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild if (parentTypeSymbol.ToString() == Definition.EntityType) { HandleAcessEntityChild(context); return; } // 非Entity的子类 跳过 if (parentTypeSymbol.BaseType?.ToString() != Definition.EntityType) { return; } // 获取 component实体类型 ISymbol?componentTypeSymbol = null; // Component为泛型调用 if (addComponentMethodSymbol.IsGenericMethod) { GenericNameSyntax?genericNameSyntax = memberAccessExpressionSyntax?.GetFirstChild <GenericNameSyntax>(); TypeArgumentListSyntax?typeArgumentList = genericNameSyntax?.GetFirstChild <TypeArgumentListSyntax>(); var componentTypeSyntax = typeArgumentList?.Arguments.First(); if (componentTypeSyntax == null) { Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation()); context.ReportDiagnostic(diagnostic); throw new Exception("componentTypeSyntax==null"); } componentTypeSymbol = context.SemanticModel.GetSymbolInfo(componentTypeSyntax).Symbol; } //Component为非泛型调用 else { SyntaxNode?firstArgumentSyntax = invocationExpressionSyntax.GetFirstChild <ArgumentListSyntax>()?.GetFirstChild <ArgumentSyntax>() ?.ChildNodes().First(); if (firstArgumentSyntax == null) { Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation()); context.ReportDiagnostic(diagnostic); return; } // 参数为typeOf时 提取Type类型 if (firstArgumentSyntax is TypeOfExpressionSyntax typeOfExpressionSyntax) { firstArgumentSyntax = typeOfExpressionSyntax.Type; } ISymbol?firstArgumentSymbol = context.SemanticModel.GetSymbolInfo(firstArgumentSyntax).Symbol; if (firstArgumentSymbol is ILocalSymbol childLocalSymbol) { componentTypeSymbol = childLocalSymbol.Type; } else if (firstArgumentSymbol is IParameterSymbol childParamaterSymbol) { componentTypeSymbol = childParamaterSymbol.Type; } else if (firstArgumentSymbol is IMethodSymbol methodSymbol) { componentTypeSymbol = methodSymbol.ReturnType; } else if (firstArgumentSymbol is IFieldSymbol fieldSymbol) { componentTypeSymbol = fieldSymbol.Type; } else if (firstArgumentSymbol is IPropertySymbol propertySymbol) { componentTypeSymbol = propertySymbol.Type; } else if (firstArgumentSymbol is INamedTypeSymbol namedTypeSymbol) { componentTypeSymbol = namedTypeSymbol; } else if (firstArgumentSymbol is ITypeParameterSymbol) { // 忽略typeof(T)参数类型 return; } else if (firstArgumentSymbol != null) { Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), firstArgumentSymbol.Name, parentTypeSymbol.Name); context.ReportDiagnostic(diagnostic); return; } else { Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), firstArgumentSyntax.GetText(), parentTypeSymbol.Name); context.ReportDiagnostic(diagnostic); return; } } if (componentTypeSymbol == null) { return; } // 忽略 component类型为泛型类型 if (componentTypeSymbol is ITypeParameterSymbol typeParameterSymbol) { return; } // 忽略 Type参数 if (componentTypeSymbol.ToString() == "System.Type") { return; } // 组件类型为Entity时 忽略检查 if (componentTypeSymbol.ToString() == Definition.EntityType) { return; } // 判断component类型是否属于约束类型 //获取component类的parentType标记数据 INamedTypeSymbol?availableParentTypeSymbol = null; bool hasParentTypeAttribute = false; foreach (AttributeData?attributeData in componentTypeSymbol.GetAttributes()) { if (attributeData.AttributeClass?.ToString() == Definition.ComponentOfAttribute) { hasParentTypeAttribute = true; if (attributeData.ConstructorArguments[0].Value is INamedTypeSymbol typeSymbol) { availableParentTypeSymbol = typeSymbol; break; } } } if (hasParentTypeAttribute && availableParentTypeSymbol == null) { return; } // 符合约束条件 通过检查 if (availableParentTypeSymbol != null && availableParentTypeSymbol.ToString() == parentTypeSymbol.ToString()) { return; } { Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), componentTypeSymbol?.Name, parentTypeSymbol?.Name); context.ReportDiagnostic(diagnostic); } }
private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllHotfix)) { return; } if (!(context.Node is MemberAccessExpressionSyntax memberAccessExpressionSyntax)) { return; } // -----筛选出实体类的字段symbol----- ISymbol?filedSymbol = context.SemanticModel.GetSymbolInfo(memberAccessExpressionSyntax).Symbol; if (filedSymbol == null || !(filedSymbol is IFieldSymbol)) { return; } if (filedSymbol.IsStatic) { return; } if (filedSymbol.ContainingType.BaseType?.ToString() != EntityType) { return; } // -----筛选出在实体类和实体System外部字段访问----- // 实体System包括awakeSystem updateSystem等生命周期类和 componentSystem静态方法类 ClassDeclarationSyntax?accessFieldClassDeclaretion = memberAccessExpressionSyntax.GetParentClassDeclaration(); if (accessFieldClassDeclaretion == null) { return; } INamedTypeSymbol?accessFieldClassSymbol = context.SemanticModel.GetDeclaredSymbol(accessFieldClassDeclaretion); if (accessFieldClassSymbol == null) { return; } // 实体基类忽略处理 if (accessFieldClassSymbol.ToString() == EntityType) { return; } //判断是否在实体类生命周期System中 if (this.CheckIsEntityLifecycleSystem(accessFieldClassSymbol, filedSymbol.ContainingType)) { return; } //判断是否在实体类的友元类中 if (this.CheckIsEntityFriendClass(accessFieldClassSymbol, filedSymbol.ContainingType)) { return; } Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax.GetLocation(), filedSymbol.ContainingType.Name, filedSymbol.Name); context.ReportDiagnostic(diagnostic); }
private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.All)) { return; } if (!(context.Node is MemberAccessExpressionSyntax memberAccessExpressionSyntax)) { return; } // 筛选出 AddChild函数syntax string methodName = memberAccessExpressionSyntax.Name.Identifier.Text; if (!Definition.AddChildMethods.Contains(methodName)) { return; } if (!(memberAccessExpressionSyntax?.Parent is InvocationExpressionSyntax invocationExpressionSyntax) || !(context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol is IMethodSymbol addChildMethodSymbol)) { return; } // 获取AddChild函数的调用者类型 ITypeSymbol?parentTypeSymbol = memberAccessExpressionSyntax.GetMemberAccessSyntaxParentType(context.SemanticModel); if (parentTypeSymbol == null) { return; } // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild if (parentTypeSymbol.ToString() == Definition.EntityType) { HandleAcessEntityChild(context); return; } // 非Entity的子类 跳过 if (parentTypeSymbol.BaseType?.ToString() != Definition.EntityType) { return; } // 获取 child实体类型 ISymbol?childTypeSymbol = null; // addChild为泛型调用 if (addChildMethodSymbol.IsGenericMethod) { GenericNameSyntax?genericNameSyntax = memberAccessExpressionSyntax?.GetFirstChild <GenericNameSyntax>(); TypeArgumentListSyntax?typeArgumentList = genericNameSyntax?.GetFirstChild <TypeArgumentListSyntax>(); var childTypeSyntax = typeArgumentList?.Arguments.First(); if (childTypeSyntax == null) { Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation()); context.ReportDiagnostic(diagnostic); throw new Exception("childTypeSyntax==null"); } childTypeSymbol = context.SemanticModel.GetSymbolInfo(childTypeSyntax).Symbol; } // addChild为非泛型调用 else { SyntaxNode?firstArgumentSyntax = invocationExpressionSyntax.GetFirstChild <ArgumentListSyntax>()?.GetFirstChild <ArgumentSyntax>() ?.ChildNodes().First(); if (firstArgumentSyntax == null) { Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation()); context.ReportDiagnostic(diagnostic); return; } ISymbol?firstArgumentSymbol = context.SemanticModel.GetSymbolInfo(firstArgumentSyntax).Symbol; if (firstArgumentSymbol is ILocalSymbol childLocalSymbol) { childTypeSymbol = childLocalSymbol.Type; } else if (firstArgumentSymbol is IParameterSymbol childParamaterSymbol) { childTypeSymbol = childParamaterSymbol.Type; } else if (firstArgumentSymbol is IMethodSymbol methodSymbol) { childTypeSymbol = methodSymbol.ReturnType; } else if (firstArgumentSymbol is IFieldSymbol fieldSymbol) { childTypeSymbol = fieldSymbol.Type; } else if (firstArgumentSymbol is IPropertySymbol propertySymbol) { childTypeSymbol = propertySymbol.Type; } else if (firstArgumentSymbol != null) { Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), firstArgumentSymbol.Name, parentTypeSymbol.Name); context.ReportDiagnostic(diagnostic); return; } else { Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), firstArgumentSyntax.GetText(), parentTypeSymbol.Name); context.ReportDiagnostic(diagnostic); return; } } if (childTypeSymbol == null) { return; } // 忽略 child类型为泛型类型 if (childTypeSymbol is ITypeParameterSymbol typeParameterSymbol) { return; } // 获取ChildOf标签的约束类型 if (!(childTypeSymbol is ITypeSymbol childType)) { throw new Exception($"{childTypeSymbol} 不是typeSymbol"); } INamedTypeSymbol?availableParentType = null; bool hasAttribute = false; foreach (AttributeData?attributeData in childType.GetAttributes()) { if (attributeData.AttributeClass?.ToString() == Definition.ChildOfAttribute) { hasAttribute = true; availableParentType = attributeData.ConstructorArguments[0].Value as INamedTypeSymbol; break; } } if (hasAttribute && availableParentType == null) { return; } // 判断父级类型是否属于child约束的父级类型 if (availableParentType?.ToString() == parentTypeSymbol.ToString()) { return; } { Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), childTypeSymbol?.Name, parentTypeSymbol?.Name); context.ReportDiagnostic(diagnostic); } }
private void Analyzer(SymbolAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllModel)) { return; } if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol)) { return; } // 筛选出含有UniqueId标签的类 var attr = namedTypeSymbol.GetFirstAttribute(UniqueIdAttribute); if (attr == null) { return; } // 获取id 最小值最大值 var minIdValue = attr.ConstructorArguments[0].Value; var maxIdValue = attr.ConstructorArguments[1].Value; if (minIdValue == null || maxIdValue == null) { return; } int minId = (int)minIdValue; int maxId = (int)maxIdValue; HashSet <int> IdSet = new HashSet <int>(); foreach (var member in namedTypeSymbol.GetMembers()) { if (member is IFieldSymbol { IsConst: true, ConstantValue: int id } fieldSymbol) { if (id < minId || id > maxId) { ReportDiagnostic(fieldSymbol, id, UniqueIdRangeAnaluzerRule.Rule); } else if (IdSet.Contains(id)) { ReportDiagnostic(fieldSymbol, id, UniqueIdDuplicateAnalyzerRule.Rule); } else { IdSet.Add(id); } } } void ReportDiagnostic(IFieldSymbol fieldSymbol, int idValue, DiagnosticDescriptor rule) { foreach (var syntaxReference in fieldSymbol.DeclaringSyntaxReferences) { var syntax = syntaxReference.GetSyntax(); Diagnostic diagnostic = Diagnostic.Create(rule, syntax.GetLocation(), namedTypeSymbol.Name, fieldSymbol.Name, idValue.ToString()); context.ReportDiagnostic(diagnostic); } } }
private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context) { if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.All)) { return; } if (!(context.Node is MemberAccessExpressionSyntax memberAccessExpressionSyntax)) { return; } // 获取方法调用Syntax 对应的methodSymbol if (!(memberAccessExpressionSyntax?.Parent is InvocationExpressionSyntax invocationExpressionSyntax) || !(context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol is IMethodSymbol methodSymbol)) { return; } //忽略void返回值函数 if (methodSymbol.ReturnsVoid) { return; } if (!(methodSymbol.ReturnType is INamedTypeSymbol namedTypeSymbol)) { return; } // 筛选出返回值为ETTask 和ETTask<T>的函数 if (namedTypeSymbol.Name != Definition.ETTask) { return; } // 获取ETTask函数调用处所在的函数体 var containingMethodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor <MethodDeclarationSyntax>(); if (containingMethodDeclarationSyntax == null) { return; } IMethodSymbol?containingMethodSymbol = context.SemanticModel.GetDeclaredSymbol(containingMethodDeclarationSyntax); if (containingMethodSymbol == null) { return; } // ETTask函数在 ()=>Function(); 形式的lanmda表达式中时 if (invocationExpressionSyntax.Parent is ParenthesizedLambdaExpressionSyntax) { Diagnostic diagnostic = Diagnostic.Create(ETTaskInSyncMethodAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), memberAccessExpressionSyntax?.Name); context.ReportDiagnostic(diagnostic); return; } // 方法体内ETTask单独调用时 if (invocationExpressionSyntax.Parent is ExpressionStatementSyntax) { if (containingMethodSymbol.IsAsync) { Diagnostic diagnostic = Diagnostic.Create(ETTaskInAsyncMethodAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), memberAccessExpressionSyntax?.Name); context.ReportDiagnostic(diagnostic); } else { Diagnostic diagnostic = Diagnostic.Create(ETTaskInSyncMethodAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), memberAccessExpressionSyntax?.Name); context.ReportDiagnostic(diagnostic); } } }