Roslyn数据流分析 - WrittenInside和Locations字段的模糊值

Sre*_*ath 7 c# roslyn

我最近开始使用Roslyn提供的数据流分析API,并发现WrittenInside字段和Locations字段中显示的值有点模棱两可.

请考虑Main方法中的以下代码段

1. int[] lcolSample = new int[10] { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4};
2. for (int lintCount1 = 0; lintCount1 < 10; lintCount1++)
3. {
4.     Prog1(lintCount1);
5.     int[] lcolSample1 = new int[10] { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 };
6.     lintCount3 = lintCount3 + 100;
7.     lintCount1 = lintCount1 + 2;
8.     lcolSample[lintCount1-1] = lcolSample1[lintCount1] + 100;
9. }
Run Code Online (Sandbox Code Playgroud)
  1. 如果我在for循环节点上执行DFA,则生成的数据流分析对象永远不会在WrittenInside字段中将lcolSample []显示为在for循环中写入的符号.原因是它在执行数据流分析的节点之外声明.但是,ReadInside字段显示此符号.有没有办法知道在给定节点内修改/写入的所有符号,即使它们是在执行DFA的节点之外声明的?

  2. 变量lintCount1写入两次(语句2和7)并读取两次.lintCount1上的Locations属性仅显示声明它的位置(语句2).有没有办法找到lintCount1写入的所有位置?查找该符号的所有引用将给出使用符号的所有位置,但是我需要写入但不读取的位置.

这是我在这个论坛上的第一个问题.如果上面提供的信息不充分,请询问任何其他详细信息.提前致谢..

svi*_*ick 5

数据流分析对象永远不会将WrittenInside字段中的lcolSample []显示为for循环中写入的符号

是的,因为该符号不是写在循环内(即没有lcolSample = whatever).由lcolSample符号表示的数组元素写在循环中,这是非常不同的.我不知道如何使用Roslyn的数据流分析找到这样的写法.

lintCount1上的Locations属性仅显示声明它的位置(语句2).有没有办法找到lintCount1写入的所有位置?

DataFlowAnalysis对象仅为您提供符号,访问它们的Locations没有多大意义(因为该位置与数据流分析无关).

对我来说,你的问题听起来像是合理的功能请求,你可能想在Roslyn回购中制作它们.