Skip to content

Commit

Permalink
优化分析器检查逻辑 (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
susices authored Nov 28, 2023
1 parent 359a7aa commit c39c506
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 182 deletions.
87 changes: 45 additions & 42 deletions Share/Analyzer/Analyzer/AddChildTypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace ET.Analyzer
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AddChildTypeAnalyzer: DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(AddChildTypeAnalyzerRule.Rule,DisableAccessEntityChildAnalyzerRule.Rule);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(AddChildTypeAnalyzerRule.Rule, DisableAccessEntityChildAnalyzerRule.Rule);

public override void Initialize(AnalysisContext context)
{
Expand All @@ -22,17 +23,13 @@ public override void Initialize(AnalysisContext context)

context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
//context.RegisterSyntaxNodeAction(this.AnalyzeMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
context.RegisterCompilationStartAction((analysisContext =>
{
if (analysisContext.Compilation.AssemblyName==AnalyzeAssembly.UnityCodes)
{
analysisContext.RegisterSyntaxNodeAction(this.AnalyzeMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
}else if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName,AnalyzeAssembly.AllModelHotfix))
{
analysisContext.RegisterSyntaxNodeAction(this.AnalyzeMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
}
}));
context.RegisterCompilationStartAction(analysisContext =>
{
if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName, AnalyzeAssembly.AllModelHotfix))
{
analysisContext.RegisterSyntaxNodeAction(this.AnalyzeMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
}
});
}

private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
Expand All @@ -58,12 +55,11 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)

// 获取AddChild函数的调用者类型
ITypeSymbol? parentTypeSymbol = memberAccessExpressionSyntax.GetMemberAccessSyntaxParentType(context.SemanticModel);
if (parentTypeSymbol==null)
if (parentTypeSymbol == null)
{
return;
}



// 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
if (parentTypeSymbol.ToString() is Definition.EntityType or Definition.LSEntityType)
{
Expand All @@ -72,12 +68,11 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
}

// 非Entity的子类 跳过
if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType && parentTypeSymbol.BaseType?.ToString()!=Definition.LSEntityType)
if (parentTypeSymbol.BaseType?.ToString() != Definition.EntityType && parentTypeSymbol.BaseType?.ToString() != Definition.LSEntityType)
{
return;
}



// 获取 child实体类型
ISymbol? childTypeSymbol = null;
// addChild为泛型调用
Expand All @@ -87,11 +82,12 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)

TypeArgumentListSyntax? typeArgumentList = genericNameSyntax?.GetFirstChild<TypeArgumentListSyntax>();

var childTypeSyntax = typeArgumentList?.Arguments.First();
TypeSyntax? childTypeSyntax = typeArgumentList?.Arguments.First();

if (childTypeSyntax == null)
{
Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule,
memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
context.ReportDiagnostic(diagnostic);
throw new Exception("childTypeSyntax==null");
}
Expand All @@ -105,7 +101,8 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
?.ChildNodes().First();
if (firstArgumentSyntax == null)
{
Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule,
memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
context.ReportDiagnostic(diagnostic);
return;
}
Expand Down Expand Up @@ -134,14 +131,16 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
}
else if (firstArgumentSymbol != null)
{
Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
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(),
Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule,
memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
firstArgumentSyntax.GetText(), parentTypeSymbol.Name);
context.ReportDiagnostic(diagnostic);
return;
Expand All @@ -158,7 +157,7 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
{
return;
}

// 获取ChildOf标签的约束类型

if (!(childTypeSymbol is ITypeSymbol childType))
Expand All @@ -179,56 +178,60 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
}
}

if (hasAttribute && availableParentType==null)
if (hasAttribute && availableParentType == null)
{
return;
}

// 判断父级类型是否属于child约束的父级类型
if (availableParentType?.ToString() == parentTypeSymbol.ToString())
{
return;
}

{
Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), childTypeSymbol?.Name,
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;
MemberAccessExpressionSyntax? memberAccessExpressionSyntax = context.Node as MemberAccessExpressionSyntax;
//在方法体内
var methodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<MethodDeclarationSyntax>();
if (methodDeclarationSyntax!=null)
MethodDeclarationSyntax? methodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<MethodDeclarationSyntax>();
if (methodDeclarationSyntax != null)
{
var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);
IMethodSymbol? methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);

bool? enableAccessEntiyChild = methodSymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
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());
Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule,
memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
context.ReportDiagnostic(diagnostic);
}

return;
}

//在属性内
var propertyDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<PropertyDeclarationSyntax>();
if (propertyDeclarationSyntax!=null)
PropertyDeclarationSyntax? propertyDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<PropertyDeclarationSyntax>();
if (propertyDeclarationSyntax != null)
{
var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);

bool? enableAccessEntiyChild = propertySymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
IPropertySymbol? 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());
Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule,
memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
context.ReportDiagnostic(diagnostic);
}

return;
}
}
}
Expand Down
13 changes: 0 additions & 13 deletions Share/Analyzer/Analyzer/ClassDeclarationInHotfixAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ public override void Initialize(AnalysisContext context)
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction((analysisContext =>
{
if (analysisContext.Compilation.AssemblyName==AnalyzeAssembly.UnityCodes)
{
analysisContext.RegisterSemanticModelAction((modelAnalysisContext =>
{
if (AnalyzerHelper.IsSemanticModelNeedAnalyze(modelAnalysisContext.SemanticModel,UnityCodesPath.AllHotfix))
{
AnalyzeSemanticModel(modelAnalysisContext);
}
} ));
return;
}
if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName,AnalyzeAssembly.AllHotfix))
{
analysisContext.RegisterSemanticModelAction((this.AnalyzeSemanticModel));
Expand Down
13 changes: 0 additions & 13 deletions Share/Analyzer/Analyzer/ETTaskAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ public override void Initialize(AnalysisContext context)
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction((analysisContext =>
{
if (analysisContext.Compilation.AssemblyName==AnalyzeAssembly.UnityCodes)
{
analysisContext.RegisterSemanticModelAction((modelAnalysisContext =>
{
if (AnalyzerHelper.IsSemanticModelNeedAnalyze(modelAnalysisContext.SemanticModel,UnityCodesPath.AllModelHotfix))
{
AnalyzeSemanticModel(modelAnalysisContext);
}
} ));
return;
}
if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName, AnalyzeAssembly.AllModelHotfix))
{
analysisContext.RegisterSemanticModelAction((this.AnalyzeSemanticModel));
Expand Down
14 changes: 0 additions & 14 deletions Share/Analyzer/Analyzer/EntityComponentAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,8 @@ public override void Initialize(AnalysisContext context)
}
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();

context.RegisterCompilationStartAction((analysisContext =>
{
if (analysisContext.Compilation.AssemblyName==AnalyzeAssembly.UnityCodes)
{
analysisContext.RegisterSemanticModelAction((modelAnalysisContext =>
{
if (AnalyzerHelper.IsSemanticModelNeedAnalyze(modelAnalysisContext.SemanticModel,UnityCodesPath.AllModelHotfix))
{
AnalyzeSemanticModel(modelAnalysisContext);
}
} ));
return;
}
if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName, AnalyzeAssembly.AllModelHotfix))
{
analysisContext.RegisterSemanticModelAction((this.AnalyzeSemanticModel));
Expand Down
13 changes: 0 additions & 13 deletions Share/Analyzer/Analyzer/EntityFiledAccessAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ public override void Initialize(AnalysisContext context)
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction((analysisContext =>
{
if (analysisContext.Compilation.AssemblyName==AnalyzeAssembly.UnityCodes)
{
analysisContext.RegisterSemanticModelAction((modelAnalysisContext =>
{
if (AnalyzerHelper.IsSemanticModelNeedAnalyze(modelAnalysisContext.SemanticModel,UnityCodesPath.AllModelHotfix))
{
AnalyzeSemanticModel(modelAnalysisContext);
}
} ));
return;
}
if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName, AnalyzeAssembly.AllModelHotfix))
{
analysisContext.RegisterSemanticModelAction((this.AnalyzeSemanticModel));
Expand Down
13 changes: 9 additions & 4 deletions Share/Analyzer/Analyzer/EntityHashCodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ public override void Initialize(AnalysisContext context)

context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction((this.CompilationStartAnalysis));
var entityHashCodeMap = new ConcurrentDictionary<long, string>();

context.RegisterCompilationStartAction((analysisContext =>
{
CompilationStartAnalysis(analysisContext, entityHashCodeMap);
} ));
}

private void CompilationStartAnalysis(CompilationStartAnalysisContext context)
private void CompilationStartAnalysis(CompilationStartAnalysisContext context,ConcurrentDictionary<long, string> entityHashCodeMap)
{
var entityHashCodeMap = new ConcurrentDictionary<long, string>();

context.RegisterSemanticModelAction((analysisContext =>
{
if (AnalyzerHelper.IsSemanticModelNeedAnalyze(analysisContext.SemanticModel,UnityCodesPath.UnityModel))
if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.SemanticModel.Compilation.AssemblyName,AnalyzeAssembly.AllModel))
{
AnalyzeSemanticModel(analysisContext, entityHashCodeMap);
}
Expand Down
13 changes: 0 additions & 13 deletions Share/Analyzer/Analyzer/EntityMemberDeclarationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ public override void Initialize(AnalysisContext context)
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction((analysisContext =>
{
if (analysisContext.Compilation.AssemblyName==AnalyzeAssembly.UnityCodes)
{
analysisContext.RegisterSemanticModelAction((modelAnalysisContext =>
{
if (AnalyzerHelper.IsSemanticModelNeedAnalyze(modelAnalysisContext.SemanticModel,UnityCodesPath.AllModel))
{
AnalyzeSemanticModel(modelAnalysisContext);
}
} ));
return;
}
if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName,AnalyzeAssembly.AllModel))
{
analysisContext.RegisterSemanticModelAction((this.AnalyzeSemanticModel));
Expand Down
13 changes: 0 additions & 13 deletions Share/Analyzer/Analyzer/EntityMethodDeclarationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction((analysisContext =>
{
if (analysisContext.Compilation.AssemblyName==AnalyzeAssembly.UnityCodes)
{
analysisContext.RegisterSemanticModelAction((modelAnalysisContext =>
{
if (AnalyzerHelper.IsSemanticModelNeedAnalyze(modelAnalysisContext.SemanticModel,UnityCodesPath.AllModel))
{
AnalyzeSemanticModel(modelAnalysisContext);
}
} ));
return;
}
if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName,AnalyzeAssembly.AllModel))
{
analysisContext.RegisterSemanticModelAction((this.AnalyzeSemanticModel));
Expand Down
13 changes: 0 additions & 13 deletions Share/Analyzer/Analyzer/HotfixProjectFieldDeclarationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ public override void Initialize(AnalysisContext context)
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction((analysisContext =>
{
if (analysisContext.Compilation.AssemblyName==AnalyzeAssembly.UnityCodes)
{
analysisContext.RegisterSemanticModelAction((modelAnalysisContext =>
{
if (AnalyzerHelper.IsSemanticModelNeedAnalyze(modelAnalysisContext.SemanticModel,UnityCodesPath.AllHotfix))
{
AnalyzeSemanticModel(modelAnalysisContext);
}
} ));
return;
}
if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName,AnalyzeAssembly.AllHotfix))
{
analysisContext.RegisterSemanticModelAction((this.AnalyzeSemanticModel));
Expand Down
Loading

0 comments on commit c39c506

Please sign in to comment.