我正在使用 Roslyn 创建一个分析器,如果特定类以不同步的方式公开其字段,该分析器会向用户发出警告,以帮助防止竞争条件。
问题:
我目前有工作代码可以检查以确保字段是私有的。我在解决最后一个难题时遇到了麻烦:找出一种方法来确保所有字段只能在锁块内访问,因此它们(表面上)是同步的。
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.FindSymbols;
namespace RaceConditions
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class UnsynchronizedMemberAccess : DiagnosticAnalyzer
{
public const string DiagnosticId = "UnsynchronizedMemberAccess";
internal static readonly LocalizableString Title = "UnsynchronizedMemberAccess Title";
private static readonly LocalizableString MessageFormat = "Unsychronized fields are not thread-safe";
private static readonly LocalizableString Description = "Accessing fields without a get/set methods synchronized with each other and the constructor may lead to race conditions";
internal const string Category = "Race Conditions";
private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
//meant to stop other classes and itself from accessing members in an unsychronized fashion.
public override void Initialize(AnalysisContext analysisContext)
{
analysisContext.RegisterSemanticModelAction((context) =>
{
var model = context.SemanticModel;
var root = model.SyntaxTree.GetRoot();
var nodes = model.SyntaxTree.GetRoot().DescendantNodes();
var fields = nodes.OfType<VariableDeclaratorSyntax>()
.Where(v => v.Ancestors().OfType<FieldDeclarationSyntax>().Any());
//since (it appears) that you can't read/write to a an initialized field,
//I think it means you can only read/write inside a block
foreach (BlockSyntax b in nodes.OfType<BlockSyntax>())
{
//where I plan to put code to check references to the fields
}
});
}
}
}
更具体地说,我希望能够确保引用荧光笔突出显示的所有内容(至少微软似乎是这么称呼它的)都位于锁定块内,而重载参数则不必如此。
using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
using System.Data.SqlClient;
namespace Sandbox
{
partial class Program
{
private int xe = 0, y = 0;
public Program(int xe)
{
this.xe = xe;
}
void bleh()
{
if (xe == 0)
{
xe = xe + 1;
}
}
static void Main(string[] args)
{
Program p0 = new Program(5),
p1 = new Program(p0),
p2 = new Program(p0.xe);
Console.WriteLine(p1.xe);
Console.Read();
}
}
partial class Program
{
public Program(Program p) : this(p.xe) { }
}
}
这个调查:
在这里,Josh Varty [1] 建议我使用SymbolFinder.FindReferencesAsync
,这需要一个Solution
目的。 Jason Malinowski [2] 说我不应该在分析器中使用 do this,因为制作MSBuildWorkspace
得到一个Solution
对象太慢,而这个人 [3] 为缓慢问题提供了不完整/缺失的解决方法(链接到ReferenceResolver
好像坏了)。
我也研究过DataFlowAnalysis
(SemanticModel.AnalyzeDataFlow()
),但我在那里找不到任何特定的方法,显然可以让我保证我正在引用该字段xe
,而不是局部变量xe
.
问题:
我确实觉得我错过了一些非常明显的东西。有没有一些我忽略的优雅方法来实现这一点?如果答案使用语义模型,那就更好了,因为我希望我必须在其他分析器中使用它来找出数据/引用的来源,但我意识到存在局限性,因此任何没有语义模型的答案也是如此美好的。
Notes:
- 显然,这个问题在Github上也遇到了[4],但显然它仍然在那里被跟踪,他们不知道分析器是否应该在项目级别进行分析。事情还没有解决。出于此分析器的目的,我将假设整个类包含在一个
.cs
文件。首先是小步骤,是吗?
- 我还搜索了 John Koerner 的网站 [5] 和 Josh Varty 的网站 [6],但找不到与分析器和 DataFlowAnalysis 相关的任何内容。