|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.Collections.Immutable; |
| 21 | +using System.Linq; |
| 22 | +using Lucene.Net.CodeAnalysis.Dev.Utility; |
| 23 | +using Microsoft.CodeAnalysis; |
| 24 | +using Microsoft.CodeAnalysis.CSharp; |
| 25 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 26 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 27 | + |
| 28 | +namespace Lucene.Net.CodeAnalysis.Dev.LuceneDev4xxx |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// LuceneDev4002: Reports methods referenced by the 2-argument |
| 32 | + /// StackTraceHelper.DoesStackTraceContainMethod(className, methodName) overload |
| 33 | + /// that lack [MethodImpl(MethodImplOptions.NoInlining)]. Without it the JIT may |
| 34 | + /// inline the method out of the stack trace, silently breaking the check. |
| 35 | + /// |
| 36 | + /// This analyzer has no code fix: the diagnostic is reported on the referenced |
| 37 | + /// method declaration but is triggered by a separate invocation, which Roslyn |
| 38 | + /// treats as a non-local diagnostic and does not allow code fixes for. |
| 39 | + /// </summary> |
| 40 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 41 | + public sealed class LuceneDev4002_StackTraceHelperNoInliningAnalyzer : DiagnosticAnalyzer |
| 42 | + { |
| 43 | + private const string StackTraceHelperFullName = "Lucene.Net.Support.ExceptionHandling.StackTraceHelper"; |
| 44 | + private const string DoesStackTraceContainMethodName = "DoesStackTraceContainMethod"; |
| 45 | + |
| 46 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| 47 | + => ImmutableArray.Create(Descriptors.LuceneDev4002_MissingNoInlining); |
| 48 | + |
| 49 | + public override void Initialize(AnalysisContext context) |
| 50 | + { |
| 51 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze); |
| 52 | + context.EnableConcurrentExecution(); |
| 53 | + |
| 54 | + context.RegisterCompilationStartAction(compilationCtx => |
| 55 | + { |
| 56 | + var methodImplAttrSymbol = compilationCtx.Compilation.GetTypeByMetadataName( |
| 57 | + "System.Runtime.CompilerServices.MethodImplAttribute"); |
| 58 | + if (methodImplAttrSymbol is null) |
| 59 | + return; |
| 60 | + |
| 61 | + compilationCtx.RegisterSyntaxNodeAction( |
| 62 | + ctx => AnalyzeInvocation(ctx, methodImplAttrSymbol), |
| 63 | + SyntaxKind.InvocationExpression); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + private static void AnalyzeInvocation(SyntaxNodeAnalysisContext ctx, INamedTypeSymbol methodImplAttrSymbol) |
| 68 | + { |
| 69 | + var invocation = (InvocationExpressionSyntax)ctx.Node; |
| 70 | + |
| 71 | + if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess) |
| 72 | + return; |
| 73 | + if (memberAccess.Name.Identifier.ValueText != DoesStackTraceContainMethodName) |
| 74 | + return; |
| 75 | + |
| 76 | + // Only the 2-argument overload (className, methodName) is in scope. |
| 77 | + if (invocation.ArgumentList.Arguments.Count != 2) |
| 78 | + return; |
| 79 | + |
| 80 | + var symbol = ctx.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol; |
| 81 | + if (symbol is null) |
| 82 | + return; |
| 83 | + if (symbol.ContainingType?.ToDisplayString() != StackTraceHelperFullName) |
| 84 | + return; |
| 85 | + |
| 86 | + var classArg = invocation.ArgumentList.Arguments[0].Expression; |
| 87 | + var methodArg = invocation.ArgumentList.Arguments[1].Expression; |
| 88 | + |
| 89 | + var (classNameValue, classTypeFromNameof) = ResolveClassReference(classArg, ctx.SemanticModel); |
| 90 | + if (classNameValue is null) |
| 91 | + return; |
| 92 | + |
| 93 | + var methodNameValue = ResolveMethodNameValue(methodArg, ctx.SemanticModel); |
| 94 | + if (methodNameValue is null) |
| 95 | + return; |
| 96 | + |
| 97 | + var targetType = classTypeFromNameof |
| 98 | + ?? FindSourceTypeByName(ctx.SemanticModel.Compilation, classNameValue); |
| 99 | + if (targetType is null) |
| 100 | + return; |
| 101 | + |
| 102 | + foreach (var member in targetType.GetMembers(methodNameValue).OfType<IMethodSymbol>()) |
| 103 | + { |
| 104 | + if (member.MethodKind != MethodKind.Ordinary) |
| 105 | + continue; |
| 106 | + |
| 107 | + foreach (var declRef in member.DeclaringSyntaxReferences) |
| 108 | + { |
| 109 | + if (declRef.GetSyntax(ctx.CancellationToken) is not MethodDeclarationSyntax methodDecl) |
| 110 | + continue; |
| 111 | + |
| 112 | + if (NoInliningAttributeHelper.FindNoInliningAttribute(methodDecl, ctx.SemanticModel, methodImplAttrSymbol) is not null) |
| 113 | + continue; |
| 114 | + |
| 115 | + if (NoInliningAttributeHelper.HasEmptyBody(methodDecl)) |
| 116 | + continue; |
| 117 | + |
| 118 | + if (NoInliningAttributeHelper.IsInterfaceOrAbstractMethod(methodDecl)) |
| 119 | + continue; |
| 120 | + |
| 121 | + ctx.ReportDiagnostic(Diagnostic.Create( |
| 122 | + Descriptors.LuceneDev4002_MissingNoInlining, |
| 123 | + methodDecl.GetLocation(), |
| 124 | + methodDecl.Identifier.ValueText)); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static (string? Name, INamedTypeSymbol? TypeFromNameof) ResolveClassReference( |
| 130 | + ExpressionSyntax expr, |
| 131 | + SemanticModel semantic) |
| 132 | + { |
| 133 | + if (expr is InvocationExpressionSyntax inv |
| 134 | + && inv.Expression is IdentifierNameSyntax id |
| 135 | + && id.Identifier.ValueText == "nameof" |
| 136 | + && inv.ArgumentList.Arguments.Count == 1) |
| 137 | + { |
| 138 | + var inner = inv.ArgumentList.Arguments[0].Expression; |
| 139 | + var typeSymbol = semantic.GetTypeInfo(inner).Type as INamedTypeSymbol |
| 140 | + ?? semantic.GetSymbolInfo(inner).Symbol as INamedTypeSymbol; |
| 141 | + if (typeSymbol is not null) |
| 142 | + return (typeSymbol.Name, typeSymbol); |
| 143 | + } |
| 144 | + |
| 145 | + if (expr is LiteralExpressionSyntax literal && literal.IsKind(SyntaxKind.StringLiteralExpression)) |
| 146 | + return (literal.Token.ValueText, null); |
| 147 | + |
| 148 | + var constant = semantic.GetConstantValue(expr); |
| 149 | + if (constant.HasValue && constant.Value is string s) |
| 150 | + return (s, null); |
| 151 | + |
| 152 | + return (null, null); |
| 153 | + } |
| 154 | + |
| 155 | + private static string? ResolveMethodNameValue(ExpressionSyntax expr, SemanticModel semantic) |
| 156 | + { |
| 157 | + if (expr is InvocationExpressionSyntax inv |
| 158 | + && inv.Expression is IdentifierNameSyntax id |
| 159 | + && id.Identifier.ValueText == "nameof" |
| 160 | + && inv.ArgumentList.Arguments.Count == 1) |
| 161 | + { |
| 162 | + var inner = inv.ArgumentList.Arguments[0].Expression; |
| 163 | + return ExtractRightmostIdentifier(inner); |
| 164 | + } |
| 165 | + |
| 166 | + if (expr is LiteralExpressionSyntax literal && literal.IsKind(SyntaxKind.StringLiteralExpression)) |
| 167 | + return literal.Token.ValueText; |
| 168 | + |
| 169 | + var constant = semantic.GetConstantValue(expr); |
| 170 | + if (constant.HasValue && constant.Value is string s) |
| 171 | + return s; |
| 172 | + |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + private static string? ExtractRightmostIdentifier(ExpressionSyntax expr) |
| 177 | + { |
| 178 | + return expr switch |
| 179 | + { |
| 180 | + IdentifierNameSyntax id => id.Identifier.ValueText, |
| 181 | + MemberAccessExpressionSyntax ma => ma.Name.Identifier.ValueText, |
| 182 | + _ => null, |
| 183 | + }; |
| 184 | + } |
| 185 | + |
| 186 | + private static INamedTypeSymbol? FindSourceTypeByName(Compilation compilation, string typeName) |
| 187 | + { |
| 188 | + foreach (var type in EnumerateAllTypes(compilation.Assembly.GlobalNamespace)) |
| 189 | + { |
| 190 | + if (type.Name == typeName) |
| 191 | + return type; |
| 192 | + } |
| 193 | + return null; |
| 194 | + } |
| 195 | + |
| 196 | + private static IEnumerable<INamedTypeSymbol> EnumerateAllTypes(INamespaceSymbol ns) |
| 197 | + { |
| 198 | + foreach (var member in ns.GetMembers()) |
| 199 | + { |
| 200 | + if (member is INamedTypeSymbol type) |
| 201 | + { |
| 202 | + yield return type; |
| 203 | + foreach (var nested in EnumerateNestedTypes(type)) |
| 204 | + yield return nested; |
| 205 | + } |
| 206 | + else if (member is INamespaceSymbol child) |
| 207 | + { |
| 208 | + foreach (var t in EnumerateAllTypes(child)) |
| 209 | + yield return t; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + private static IEnumerable<INamedTypeSymbol> EnumerateNestedTypes(INamedTypeSymbol type) |
| 215 | + { |
| 216 | + foreach (var nested in type.GetTypeMembers()) |
| 217 | + { |
| 218 | + yield return nested; |
| 219 | + foreach (var deeper in EnumerateNestedTypes(nested)) |
| 220 | + yield return deeper; |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments