EntityComponentAnalyzer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections.Immutable;
  3. using System.Linq;
  4. using Microsoft.CodeAnalysis;
  5. using Microsoft.CodeAnalysis.CSharp;
  6. using Microsoft.CodeAnalysis.CSharp.Syntax;
  7. using Microsoft.CodeAnalysis.Diagnostics;
  8. namespace ET.Analyzer
  9. {
  10. [DiagnosticAnalyzer(LanguageNames.CSharp)]
  11. public class EntityComponentAnalyzer:DiagnosticAnalyzer
  12. {
  13. public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(EntityComponentAnalyzerRule.Rule,DisableAccessEntityChildAnalyzerRule.Rule);
  14. public override void Initialize(AnalysisContext context)
  15. {
  16. if (!AnalyzerGlobalSetting.EnableAnalyzer)
  17. {
  18. return;
  19. }
  20. context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
  21. context.EnableConcurrentExecution();
  22. context.RegisterCompilationStartAction((analysisContext =>
  23. {
  24. if (analysisContext.Compilation.AssemblyName==AnalyzeAssembly.UnityCodes)
  25. {
  26. analysisContext.RegisterSemanticModelAction((modelAnalysisContext =>
  27. {
  28. if (AnalyzerHelper.IsSemanticModelNeedAnalyze(modelAnalysisContext.SemanticModel,UnityCodesPath.AllModelHotfix))
  29. {
  30. AnalyzeSemanticModel(modelAnalysisContext);
  31. }
  32. } ));
  33. return;
  34. }
  35. if (AnalyzerHelper.IsAssemblyNeedAnalyze(analysisContext.Compilation.AssemblyName, AnalyzeAssembly.AllModelHotfix))
  36. {
  37. analysisContext.RegisterSemanticModelAction((this.AnalyzeSemanticModel));
  38. }
  39. } ));
  40. }
  41. private void AnalyzeSemanticModel(SemanticModelAnalysisContext analysisContext)
  42. {
  43. foreach (var memberAccessExpressionSyntax in analysisContext.SemanticModel.SyntaxTree.GetRoot().DescendantNodes<MemberAccessExpressionSyntax>())
  44. {
  45. AnalyzeMemberAccessExpression(analysisContext, memberAccessExpressionSyntax);
  46. }
  47. }
  48. private void AnalyzeMemberAccessExpression(SemanticModelAnalysisContext context, MemberAccessExpressionSyntax memberAccessExpressionSyntax)
  49. {
  50. // 筛选出 Component函数syntax
  51. string methodName = memberAccessExpressionSyntax.Name.Identifier.Text;
  52. if (!Definition.ComponentMethod.Contains(methodName))
  53. {
  54. return;
  55. }
  56. if (!(memberAccessExpressionSyntax?.Parent is InvocationExpressionSyntax invocationExpressionSyntax) ||
  57. !(context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol is IMethodSymbol addComponentMethodSymbol))
  58. {
  59. return;
  60. }
  61. // 获取AComponent函数的调用者类型
  62. ITypeSymbol? parentTypeSymbol = memberAccessExpressionSyntax.GetMemberAccessSyntaxParentType(context.SemanticModel);
  63. if (parentTypeSymbol==null)
  64. {
  65. return;
  66. }
  67. // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
  68. if (parentTypeSymbol.ToString() is Definition.EntityType or Definition.LSEntityType)
  69. {
  70. HandleAcessEntityChild(context,memberAccessExpressionSyntax);
  71. return;
  72. }
  73. // 非Entity的子类 跳过
  74. if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType && parentTypeSymbol.BaseType?.ToString()!= Definition.LSEntityType)
  75. {
  76. return;
  77. }
  78. // 获取 component实体类型
  79. ISymbol? componentTypeSymbol = null;
  80. // Component为泛型调用
  81. if (addComponentMethodSymbol.IsGenericMethod)
  82. {
  83. GenericNameSyntax? genericNameSyntax = memberAccessExpressionSyntax?.GetFirstChild<GenericNameSyntax>();
  84. TypeArgumentListSyntax? typeArgumentList = genericNameSyntax?.GetFirstChild<TypeArgumentListSyntax>();
  85. var componentTypeSyntax = typeArgumentList?.Arguments.First();
  86. if (componentTypeSyntax == null)
  87. {
  88. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  89. context.ReportDiagnostic(diagnostic);
  90. throw new Exception("componentTypeSyntax==null");
  91. }
  92. componentTypeSymbol = context.SemanticModel.GetSymbolInfo(componentTypeSyntax).Symbol;
  93. }
  94. //Component为非泛型调用
  95. else
  96. {
  97. SyntaxNode? firstArgumentSyntax = invocationExpressionSyntax.GetFirstChild<ArgumentListSyntax>()?.GetFirstChild<ArgumentSyntax>()
  98. ?.ChildNodes().First();
  99. if (firstArgumentSyntax == null)
  100. {
  101. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  102. context.ReportDiagnostic(diagnostic);
  103. return;
  104. }
  105. // 参数为typeOf时 提取Type类型
  106. if (firstArgumentSyntax is TypeOfExpressionSyntax typeOfExpressionSyntax)
  107. {
  108. firstArgumentSyntax = typeOfExpressionSyntax.Type;
  109. }
  110. ISymbol? firstArgumentSymbol = context.SemanticModel.GetSymbolInfo(firstArgumentSyntax).Symbol;
  111. if (firstArgumentSymbol is ILocalSymbol childLocalSymbol)
  112. {
  113. componentTypeSymbol = childLocalSymbol.Type;
  114. }
  115. else if (firstArgumentSymbol is IParameterSymbol childParamaterSymbol)
  116. {
  117. componentTypeSymbol = childParamaterSymbol.Type;
  118. }
  119. else if (firstArgumentSymbol is IMethodSymbol methodSymbol)
  120. {
  121. componentTypeSymbol = methodSymbol.ReturnType;
  122. }
  123. else if (firstArgumentSymbol is IFieldSymbol fieldSymbol)
  124. {
  125. componentTypeSymbol = fieldSymbol.Type;
  126. }
  127. else if (firstArgumentSymbol is IPropertySymbol propertySymbol)
  128. {
  129. componentTypeSymbol = propertySymbol.Type;
  130. }else if (firstArgumentSymbol is INamedTypeSymbol namedTypeSymbol)
  131. {
  132. componentTypeSymbol = namedTypeSymbol;
  133. }else if (firstArgumentSymbol is ITypeParameterSymbol)
  134. {
  135. // 忽略typeof(T)参数类型
  136. return;
  137. }
  138. else if (firstArgumentSymbol != null)
  139. {
  140. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
  141. firstArgumentSymbol.Name, parentTypeSymbol.Name);
  142. context.ReportDiagnostic(diagnostic);
  143. return;
  144. }
  145. else
  146. {
  147. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
  148. firstArgumentSyntax.GetText(), parentTypeSymbol.Name);
  149. context.ReportDiagnostic(diagnostic);
  150. return;
  151. }
  152. }
  153. if (componentTypeSymbol==null)
  154. {
  155. return;
  156. }
  157. // 忽略 component类型为泛型类型
  158. if (componentTypeSymbol is ITypeParameterSymbol typeParameterSymbol)
  159. {
  160. return;
  161. }
  162. // 忽略 Type参数
  163. if (componentTypeSymbol.ToString()=="System.Type")
  164. {
  165. return;
  166. }
  167. // 组件类型为Entity时 忽略检查
  168. if (componentTypeSymbol.ToString() is Definition.EntityType or Definition.LSEntityType)
  169. {
  170. return;
  171. }
  172. // 判断component类型是否属于约束类型
  173. //获取component类的parentType标记数据
  174. INamedTypeSymbol? availableParentTypeSymbol = null;
  175. bool hasParentTypeAttribute = false;
  176. foreach (AttributeData? attributeData in componentTypeSymbol.GetAttributes())
  177. {
  178. if (attributeData.AttributeClass?.ToString() == Definition.ComponentOfAttribute)
  179. {
  180. hasParentTypeAttribute = true;
  181. if (attributeData.ConstructorArguments[0].Value is INamedTypeSymbol typeSymbol)
  182. {
  183. availableParentTypeSymbol = typeSymbol;
  184. break;
  185. }
  186. }
  187. }
  188. if (hasParentTypeAttribute&&availableParentTypeSymbol==null)
  189. {
  190. return;
  191. }
  192. // 符合约束条件 通过检查
  193. if (availableParentTypeSymbol!=null && availableParentTypeSymbol.ToString()==parentTypeSymbol.ToString())
  194. {
  195. return;
  196. }
  197. {
  198. Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), componentTypeSymbol?.Name,
  199. parentTypeSymbol?.Name);
  200. context.ReportDiagnostic(diagnostic);
  201. }
  202. }
  203. private void HandleAcessEntityChild(SemanticModelAnalysisContext context, MemberAccessExpressionSyntax memberAccessExpressionSyntax)
  204. {
  205. //在方法体内
  206. var methodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<MethodDeclarationSyntax>();
  207. if (methodDeclarationSyntax!=null)
  208. {
  209. var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);
  210. bool? enableAccessEntiyChild = methodSymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
  211. if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
  212. {
  213. Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  214. context.ReportDiagnostic(diagnostic);
  215. }
  216. return;
  217. }
  218. //在属性内
  219. var propertyDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<PropertyDeclarationSyntax>();
  220. if (propertyDeclarationSyntax!=null)
  221. {
  222. var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);
  223. bool? enableAccessEntiyChild = propertySymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
  224. if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
  225. {
  226. Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
  227. context.ReportDiagnostic(diagnostic);
  228. }
  229. return;
  230. }
  231. }
  232. }
  233. }