123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using System;
- using System.Collections.Immutable;
- using System.Linq;
- using Microsoft.CodeAnalysis;
- using Microsoft.CodeAnalysis.CSharp;
- using Microsoft.CodeAnalysis.CSharp.Syntax;
- using Microsoft.CodeAnalysis.Diagnostics;
- namespace ET.Analyzer
- {
- [DiagnosticAnalyzer(LanguageNames.CSharp)]
- public class AddChildTypeAnalyzer: DiagnosticAnalyzer
- {
- public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(AddChildTypeAnalyzerRule.Rule,DisableAccessEntityChildAnalyzerRule.Rule);
- public override void Initialize(AnalysisContext context)
- {
- if (!AnalyzerGlobalSetting.EnableAnalyzer)
- {
- return;
- }
- context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
- context.EnableConcurrentExecution();
- context.RegisterSyntaxNodeAction(this.AnalyzeMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
- }
- private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
- {
- if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllModelHotfix))
- {
- 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 HandleAcessEntityChild(SyntaxNodeAnalysisContext context)
- {
- var memberAccessExpressionSyntax = context.Node as MemberAccessExpressionSyntax;
- //在方法体内
- var methodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<MethodDeclarationSyntax>();
- if (methodDeclarationSyntax!=null)
- {
- var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);
- bool? enableAccessEntiyChild = methodSymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
- if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
- {
- Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
- context.ReportDiagnostic(diagnostic);
- }
- return;
- }
-
- //在属性内
- var propertyDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<PropertyDeclarationSyntax>();
- if (propertyDeclarationSyntax!=null)
- {
- var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);
-
- bool? enableAccessEntiyChild = propertySymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
- if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
- {
- Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
- context.ReportDiagnostic(diagnostic);
- }
- return;
- }
- }
- }
- }
|