-
Notifications
You must be signed in to change notification settings - Fork 147
/
VSTHRD103UseAsyncOptionAnalyzer.cs
237 lines (213 loc) · 11.9 KB
/
VSTHRD103UseAsyncOptionAnalyzer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Microsoft.VisualStudio.Threading.Analyzers;
/// <summary>
/// This analyzer recognizes invocations of JoinableTaskFactory.Run(Func{Task}), JoinableTask.Join(), and variants
/// that occur within an async method, thus defeating a perfect opportunity to be asynchronous.
/// </summary>
/// <remarks>
/// <![CDATA[
/// async Task MyMethod()
/// {
/// JoinableTaskFactory jtf;
/// jtf.Run(async delegate { /* This analyzer will report warning on this JoinableTaskFactory.Run invocation. */
/// await Stuff();
/// });
/// }
/// ]]>
/// </remarks>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class VSTHRD103UseAsyncOptionAnalyzer : DiagnosticAnalyzer
{
public const string Id = "VSTHRD103";
internal const string AsyncMethodKeyName = "AsyncMethodName";
internal const string ExtensionMethodNamespaceKeyName = "ExtensionMethodNamespace";
internal static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor(
id: Id,
title: new LocalizableResourceString(nameof(Strings.VSTHRD103_Title), Strings.ResourceManager, typeof(Strings)),
messageFormat: new LocalizableResourceString(nameof(Strings.VSTHRD103_MessageFormat), Strings.ResourceManager, typeof(Strings)),
helpLinkUri: Utils.GetHelpLink(Id),
category: "Usage",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
internal static readonly DiagnosticDescriptor DescriptorNoAlternativeMethod = new DiagnosticDescriptor(
id: Id,
title: new LocalizableResourceString(nameof(Strings.VSTHRD103_Title), Strings.ResourceManager, typeof(Strings)),
messageFormat: new LocalizableResourceString(nameof(Strings.VSTHRD103_MessageFormat_UseAwaitInstead), Strings.ResourceManager, typeof(Strings)),
helpLinkUri: Utils.GetHelpLink(Id),
category: "Usage",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
/// <inheritdoc />
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(
Descriptor,
DescriptorNoAlternativeMethod);
/// <inheritdoc />
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze);
context.RegisterCodeBlockStartAction<SyntaxKind>(ctxt =>
{
ctxt.RegisterSyntaxNodeAction(Utils.DebuggableWrapper(MethodAnalyzer.AnalyzeInvocation), SyntaxKind.InvocationExpression);
ctxt.RegisterSyntaxNodeAction(Utils.DebuggableWrapper(MethodAnalyzer.AnalyzePropertyGetter), SyntaxKind.SimpleMemberAccessExpression);
ctxt.RegisterSyntaxNodeAction(Utils.DebuggableWrapper(MethodAnalyzer.AnalyzeConditionalAccessExpression), SyntaxKind.ConditionalAccessExpression);
});
}
private class MethodAnalyzer
{
internal static void AnalyzePropertyGetter(SyntaxNodeAnalysisContext context)
{
var memberAccessSyntax = (MemberAccessExpressionSyntax)context.Node;
if (IsInTaskReturningMethodOrDelegate(context))
{
InspectMemberAccess(context, memberAccessSyntax.Name, CommonInterest.SyncBlockingProperties);
}
}
internal static void AnalyzeConditionalAccessExpression(SyntaxNodeAnalysisContext context)
{
var conditionalAccessSyntax = (ConditionalAccessExpressionSyntax)context.Node;
if (IsInTaskReturningMethodOrDelegate(context))
{
ExpressionSyntax rightSide = conditionalAccessSyntax.WhenNotNull switch
{
MemberBindingExpressionSyntax bindingExpr => bindingExpr.Name,
_ => conditionalAccessSyntax.WhenNotNull,
};
InspectMemberAccess(context, rightSide, CommonInterest.SyncBlockingProperties);
}
}
internal static void AnalyzeInvocation(SyntaxNodeAnalysisContext context)
{
if (IsInTaskReturningMethodOrDelegate(context))
{
var invocationExpressionSyntax = (InvocationExpressionSyntax)context.Node;
var memberAccessSyntax = invocationExpressionSyntax.Expression as MemberAccessExpressionSyntax;
if (memberAccessSyntax is not null && InspectMemberAccess(context, memberAccessSyntax.Name, CommonInterest.SyncBlockingMethods))
{
// Don't return double-diagnostics.
return;
}
// Also consider all method calls to check for Async-suffixed alternatives.
SymbolInfo symbolInfo = context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax, context.CancellationToken);
if (symbolInfo.Symbol is IMethodSymbol methodSymbol && !methodSymbol.Name.EndsWith(VSTHRD200UseAsyncNamingConventionAnalyzer.MandatoryAsyncSuffix, StringComparison.CurrentCulture) &&
!methodSymbol.HasAsyncCompatibleReturnType())
{
string asyncMethodName = methodSymbol.Name + VSTHRD200UseAsyncNamingConventionAnalyzer.MandatoryAsyncSuffix;
ImmutableArray<ISymbol> symbols = context.SemanticModel.LookupSymbols(
invocationExpressionSyntax.Expression.GetLocation().SourceSpan.Start,
methodSymbol.ContainingType,
asyncMethodName,
includeReducedExtensionMethods: true);
MethodDeclarationSyntax? invocationDeclaringMethod = invocationExpressionSyntax.FirstAncestorOrSelf<MethodDeclarationSyntax>();
ExpressionSyntax invokedMethodName = CSharpUtils.IsolateMethodName(invocationExpressionSyntax);
foreach (IMethodSymbol m in symbols.OfType<IMethodSymbol>())
{
if (!m.IsObsolete()
&& HasSupersetOfParameterTypes(m, methodSymbol)
&& m.Name != invocationDeclaringMethod?.Identifier.Text
&& m.HasAsyncCompatibleReturnType())
{
// An async alternative exists.
ImmutableDictionary<string, string?>? properties = ImmutableDictionary<string, string?>.Empty
.Add(AsyncMethodKeyName, asyncMethodName);
Diagnostic diagnostic = Diagnostic.Create(
Descriptor,
invokedMethodName.GetLocation(),
properties,
invokedMethodName.ToString(),
asyncMethodName);
context.ReportDiagnostic(diagnostic);
return;
}
}
}
}
}
/// <summary>
/// Determines whether the given method has parameters to cover all the parameter types in another method.
/// </summary>
/// <param name="candidateMethod">The candidate method.</param>
/// <param name="baselineMethod">The baseline method.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="candidateMethod"/> has a superset of parameter types found in <paramref name="baselineMethod"/>; otherwise <see langword="false" />.
/// </returns>
private static bool HasSupersetOfParameterTypes(IMethodSymbol candidateMethod, IMethodSymbol baselineMethod)
{
return candidateMethod.Parameters.All(candidateParameter => baselineMethod.Parameters.Any(baselineParameter => baselineParameter.Type?.Equals(candidateParameter.Type, SymbolEqualityComparer.Default) ?? false));
}
private static bool IsInTaskReturningMethodOrDelegate(SyntaxNodeAnalysisContext context)
{
// We want to scan invocations that occur inside Task and Task<T>-returning delegates or methods.
// That is: methods that either are or could be made async.
IMethodSymbol? methodSymbol = null;
for (SyntaxNode? focusedNode = context.Node; focusedNode is not null; focusedNode = focusedNode.Parent)
{
switch (focusedNode)
{
case AnonymousFunctionExpressionSyntax anonFunc:
SymbolInfo symbolInfo = context.SemanticModel.GetSymbolInfo(anonFunc, context.CancellationToken);
methodSymbol = symbolInfo.Symbol as IMethodSymbol;
break;
case LocalFunctionStatementSyntax localFunc:
methodSymbol = context.SemanticModel.GetDeclaredSymbol(localFunc, context.CancellationToken) as IMethodSymbol;
break;
case MethodDeclarationSyntax methodDecl:
methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDecl, context.CancellationToken);
break;
default:
// We want to continue iteration of the for loop.
continue;
}
// We encountered one of our case statements, so whether or not we have a methodSymbol, we shouldn't look further.
break;
}
return methodSymbol?.HasAsyncCompatibleReturnType() is true;
}
private static bool InspectMemberAccess(SyntaxNodeAnalysisContext context, ExpressionSyntax memberName, IEnumerable<CommonInterest.SyncBlockingMethod> problematicMethods)
{
ISymbol? memberSymbol = context.SemanticModel.GetSymbolInfo(memberName, context.CancellationToken).Symbol;
if (memberSymbol is object)
{
foreach (CommonInterest.SyncBlockingMethod item in problematicMethods)
{
if (item.Method.IsMatch(memberSymbol))
{
Location? location = memberName.GetLocation();
ImmutableDictionary<string, string?>? properties = ImmutableDictionary<string, string?>.Empty
.Add(ExtensionMethodNamespaceKeyName, item.ExtensionMethodNamespace is object ? string.Join(".", item.ExtensionMethodNamespace) : string.Empty);
DiagnosticDescriptor descriptor;
var messageArgs = new List<object>(2);
messageArgs.Add(item.Method.Name);
if (item.AsyncAlternativeMethodName is object)
{
properties = properties.Add(AsyncMethodKeyName, item.AsyncAlternativeMethodName);
descriptor = Descriptor;
messageArgs.Add(item.AsyncAlternativeMethodName);
}
else
{
properties = properties.Add(AsyncMethodKeyName, string.Empty);
descriptor = DescriptorNoAlternativeMethod;
}
Diagnostic diagnostic = Diagnostic.Create(descriptor, location, properties, messageArgs.ToArray());
context.ReportDiagnostic(diagnostic);
return true;
}
}
}
return false;
}
}
}