| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | using System.Linq; |
| | | 6 | | using Microsoft.CodeAnalysis; |
| | | 7 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 8 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 9 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 10 | | |
| | | 11 | | namespace Lattice.Analyzers |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// 检测热路径方法是否缺少 AggressiveInlining 的 Analyzer |
| | | 15 | | /// </summary> |
| | | 16 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 17 | | public class InlineHintAnalyzer : DiagnosticAnalyzer |
| | | 18 | | { |
| | | 19 | | // P0: 必须内联(简单属性、运算符) |
| | | 20 | | public const string RequiredId = "LATTICE001"; |
| | 0 | 21 | | private static readonly DiagnosticDescriptor RequiredRule = new( |
| | 0 | 22 | | RequiredId, |
| | 0 | 23 | | "热路径方法必须添加 AggressiveInlining", |
| | 0 | 24 | | "方法 '{0}' 是简单的 FP 操作,必须添加 [MethodImpl(MethodImplOptions.AggressiveInlining)] 以确保性能", |
| | 0 | 25 | | "Performance", |
| | 0 | 26 | | DiagnosticSeverity.Error, |
| | 0 | 27 | | isEnabledByDefault: true, |
| | 0 | 28 | | description: "简单的 FP/FPVector 属性访问器和运算符应该使用 AggressiveInlining 来避免方法调用开销。", |
| | 0 | 29 | | helpLinkUri: "https://docs.lattice.dev/analyzers/LATTICE001"); |
| | | 30 | | |
| | | 31 | | // P1: 建议内联(小型数学方法) |
| | | 32 | | public const string RecommendedId = "LATTICE002"; |
| | 0 | 33 | | private static readonly DiagnosticDescriptor RecommendedRule = new( |
| | 0 | 34 | | RecommendedId, |
| | 0 | 35 | | "建议添加 AggressiveInlining", |
| | 0 | 36 | | "方法 '{0}' 可能是热路径,建议添加 [MethodImpl(MethodImplOptions.AggressiveInlining)]", |
| | 0 | 37 | | "Performance", |
| | 0 | 38 | | DiagnosticSeverity.Warning, |
| | 0 | 39 | | isEnabledByDefault: true, |
| | 0 | 40 | | description: "小型数学方法(≤3 行)建议内联以减少调用开销。", |
| | 0 | 41 | | helpLinkUri: "https://docs.lattice.dev/analyzers/LATTICE002"); |
| | | 42 | | |
| | | 43 | | // P2: 考虑内联 |
| | | 44 | | public const string ConsiderId = "LATTICE003"; |
| | 0 | 45 | | private static readonly DiagnosticDescriptor ConsiderRule = new( |
| | 0 | 46 | | ConsiderId, |
| | 0 | 47 | | "考虑添加 AggressiveInlining", |
| | 0 | 48 | | "方法 '{0}' 返回 FP/FPVector,考虑添加 [MethodImpl(MethodImplOptions.AggressiveInlining)]", |
| | 0 | 49 | | "Performance", |
| | 0 | 50 | | DiagnosticSeverity.Info, |
| | 0 | 51 | | isEnabledByDefault: true, |
| | 0 | 52 | | description: "如果此方法是热路径,考虑添加 AggressiveInlining。", |
| | 0 | 53 | | helpLinkUri: "https://docs.lattice.dev/analyzers/LATTICE003"); |
| | | 54 | | |
| | | 55 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 0 | 56 | | ImmutableArray.Create(RequiredRule, RecommendedRule, ConsiderRule); |
| | | 57 | | |
| | | 58 | | public override void Initialize(AnalysisContext context) |
| | | 59 | | { |
| | 0 | 60 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 0 | 61 | | context.EnableConcurrentExecution(); |
| | 0 | 62 | | context.RegisterSyntaxNodeAction(AnalyzeMethod, SyntaxKind.MethodDeclaration); |
| | 0 | 63 | | context.RegisterSyntaxNodeAction(AnalyzeProperty, SyntaxKind.PropertyDeclaration); |
| | 0 | 64 | | context.RegisterSyntaxNodeAction(AnalyzeOperator, SyntaxKind.OperatorDeclaration); |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | private void AnalyzeMethod(SyntaxNodeAnalysisContext context) |
| | | 68 | | { |
| | 0 | 69 | | var method = (MethodDeclarationSyntax)context.Node; |
| | | 70 | | |
| | | 71 | | // 跳过已有 AggressiveInlining 的方法 |
| | 0 | 72 | | if (HasAggressiveInlining(method)) |
| | | 73 | | { |
| | 0 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | // 检查返回类型 |
| | 0 | 78 | | var returnType = method.ReturnType.ToString(); |
| | 0 | 79 | | bool returnsFP = IsFPOrVectorType(returnType); |
| | 0 | 80 | | bool returnsBoolOrInt = returnType == "bool" || returnType == "int" || returnType == "long"; |
| | | 81 | | |
| | | 82 | | // 计算方法体大小 |
| | 0 | 83 | | int statementCount = GetStatementCount(method); |
| | 0 | 84 | | bool isSimple = statementCount <= 1; |
| | 0 | 85 | | bool isSmall = statementCount <= 3; |
| | | 86 | | |
| | | 87 | | // 运算符由 OperatorDeclarationSyntax 单独处理 |
| | | 88 | | |
| | | 89 | | // P0: 简单方法(如 FP.Abs, FP.Min)必须内联 |
| | 0 | 90 | | if (returnsFP && isSimple && method.ParameterList.Parameters.Count <= 2) |
| | | 91 | | { |
| | 0 | 92 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 93 | | RequiredRule, |
| | 0 | 94 | | method.Identifier.GetLocation(), |
| | 0 | 95 | | method.Identifier.Text)); |
| | 0 | 96 | | return; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | // P1: 小型数学方法建议内联 |
| | 0 | 100 | | if (returnsFP && isSmall) |
| | | 101 | | { |
| | 0 | 102 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 103 | | RecommendedRule, |
| | 0 | 104 | | method.Identifier.GetLocation(), |
| | 0 | 105 | | method.Identifier.Text)); |
| | 0 | 106 | | return; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | // P1: 比较方法建议内联 |
| | 0 | 110 | | if (returnsBoolOrInt && isSimple && method.Identifier.Text.Contains("Compare")) |
| | | 111 | | { |
| | 0 | 112 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 113 | | RecommendedRule, |
| | 0 | 114 | | method.Identifier.GetLocation(), |
| | 0 | 115 | | method.Identifier.Text)); |
| | 0 | 116 | | return; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | // P2: 其他返回 FP 的方法考虑内联 |
| | 0 | 120 | | if (returnsFP) |
| | | 121 | | { |
| | 0 | 122 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 123 | | ConsiderRule, |
| | 0 | 124 | | method.Identifier.GetLocation(), |
| | 0 | 125 | | method.Identifier.Text)); |
| | | 126 | | } |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | private void AnalyzeProperty(SyntaxNodeAnalysisContext context) |
| | | 130 | | { |
| | 0 | 131 | | var property = (PropertyDeclarationSyntax)context.Node; |
| | | 132 | | |
| | | 133 | | // 跳过已有 AggressiveInlining 的属性 |
| | 0 | 134 | | if (HasAggressiveInlining(property)) |
| | | 135 | | { |
| | 0 | 136 | | return; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | // 检查返回类型 |
| | 0 | 140 | | var returnType = property.Type.ToString(); |
| | 0 | 141 | | if (!IsFPOrVectorType(returnType)) |
| | | 142 | | { |
| | 0 | 143 | | return; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | // 检查是否是简单的 getter-only 属性 |
| | 0 | 147 | | if (property.AccessorList == null) |
| | | 148 | | { |
| | 0 | 149 | | return; |
| | | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | var getter = property.AccessorList.Accessors |
| | 0 | 153 | | .FirstOrDefault(a => a.Keyword.IsKind(SyntaxKind.GetKeyword)); |
| | | 154 | | |
| | 0 | 155 | | if (getter == null) |
| | | 156 | | { |
| | 0 | 157 | | return; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | // 简单的表达式体或单行 getter |
| | 0 | 161 | | bool isSimple = getter.ExpressionBody != null || |
| | 0 | 162 | | (getter.Body?.Statements.Count ?? 0) <= 1; |
| | | 163 | | |
| | 0 | 164 | | if (isSimple) |
| | | 165 | | { |
| | 0 | 166 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 167 | | RequiredRule, |
| | 0 | 168 | | property.Identifier.GetLocation(), |
| | 0 | 169 | | property.Identifier.Text)); |
| | | 170 | | } |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | private void AnalyzeOperator(SyntaxNodeAnalysisContext context) |
| | | 174 | | { |
| | 0 | 175 | | var op = (OperatorDeclarationSyntax)context.Node; |
| | | 176 | | |
| | | 177 | | // 跳过已有 AggressiveInlining 的运算符 |
| | 0 | 178 | | if (HasAggressiveInlining(op)) |
| | | 179 | | { |
| | 0 | 180 | | return; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | // 检查返回类型 |
| | 0 | 184 | | var returnType = op.ReturnType.ToString(); |
| | 0 | 185 | | if (!IsFPOrVectorType(returnType)) |
| | | 186 | | { |
| | 0 | 187 | | return; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | // 所有 FP/FPVector 运算符必须内联 |
| | 0 | 191 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 192 | | RequiredRule, |
| | 0 | 193 | | op.OperatorToken.GetLocation(), |
| | 0 | 194 | | "operator " + op.OperatorToken.Text)); |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | private static bool HasAggressiveInlining(MethodDeclarationSyntax method) |
| | | 198 | | { |
| | 0 | 199 | | return method.AttributeLists |
| | 0 | 200 | | .SelectMany(a => a.Attributes) |
| | 0 | 201 | | .Any(IsAggressiveInliningAttribute); |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | private static bool HasAggressiveInlining(PropertyDeclarationSyntax property) |
| | | 205 | | { |
| | 0 | 206 | | if (property.AttributeLists |
| | 0 | 207 | | .SelectMany(a => a.Attributes) |
| | 0 | 208 | | .Any(IsAggressiveInliningAttribute)) |
| | | 209 | | { |
| | 0 | 210 | | return true; |
| | | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | if (property.AccessorList == null) |
| | | 214 | | { |
| | 0 | 215 | | return false; |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | return property.AccessorList.Accessors |
| | 0 | 219 | | .SelectMany(accessor => accessor.AttributeLists) |
| | 0 | 220 | | .SelectMany(attributeList => attributeList.Attributes) |
| | 0 | 221 | | .Any(IsAggressiveInliningAttribute); |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | private static bool HasAggressiveInlining(OperatorDeclarationSyntax op) |
| | | 225 | | { |
| | 0 | 226 | | return op.AttributeLists |
| | 0 | 227 | | .SelectMany(a => a.Attributes) |
| | 0 | 228 | | .Any(IsAggressiveInliningAttribute); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | private static bool IsAggressiveInliningAttribute(AttributeSyntax attribute) |
| | | 232 | | { |
| | 0 | 233 | | var name = attribute.Name.ToString(); |
| | 0 | 234 | | return name.Contains("MethodImpl") && |
| | 0 | 235 | | attribute.ArgumentList?.ToString().Contains("AggressiveInlining") == true; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | private static bool IsFPOrVectorType(string typeName) |
| | | 239 | | { |
| | 0 | 240 | | return typeName == "FP" || |
| | 0 | 241 | | typeName == "FPVector2" || |
| | 0 | 242 | | typeName == "FPVector3" || |
| | 0 | 243 | | typeName.Contains("FP"); |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | private static int GetStatementCount(MethodDeclarationSyntax method) |
| | | 247 | | { |
| | | 248 | | // 表达式体 |
| | 0 | 249 | | if (method.ExpressionBody != null) |
| | | 250 | | { |
| | 0 | 251 | | return 1; |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | // 语句体 |
| | 0 | 255 | | if (method.Body == null) |
| | | 256 | | { |
| | 0 | 257 | | return 0; |
| | | 258 | | } |
| | | 259 | | |
| | 0 | 260 | | int count = method.Body.Statements.Count; |
| | | 261 | | |
| | | 262 | | // 忽略 #if DEBUG 块 |
| | 0 | 263 | | foreach (var stmt in method.Body.Statements) |
| | | 264 | | { |
| | 0 | 265 | | if (stmt is IfStatementSyntax ifStmt && |
| | 0 | 266 | | ifStmt.Condition.ToString().Contains("DEBUG")) |
| | | 267 | | { |
| | 0 | 268 | | count--; |
| | | 269 | | } |
| | | 270 | | } |
| | | 271 | | |
| | 0 | 272 | | return count; |
| | | 273 | | } |
| | | 274 | | } |
| | | 275 | | } |