Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roslyn Data flow analysis - ambiguous values for WrittenInside and Locations fields

Tags:

c#

roslyn

I recently started working with Data Flow Analysis APIs provided by Roslyn and find values presented in WrittenInside field and Locations field a bit ambiguous.

Consider below code snippet in Main method

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. }
  1. If I perform DFA on for loop node, resulting Data Flow Analysis object never shows lcolSample[] in WrittenInside field as symbol that is written inside for loop. Reason being it is declared outside the node on which Dataflow analysis is performed. But, ReadInside field shows this symbol. Is there any way to know all the symbols that are modified/written inside a given node even though they are declared outside the node on which DFA is performed?

  2. Variable lintCount1 is written twice (statement 2 and 7) and read twice. Locations property on lintCount1 shows only the place where it is declared (statement 2). Is there a way to find all the locations in which lintCount1 is written? Find all references of that symbol would give all the locations where the symbol is used, but I require the locations where it is written but not read.

This is my first question on this forum. Please ask for any other details if information provided above is not sufficient. Thanks in advance..

like image 645
Sreenath Avatar asked Sep 02 '14 17:09

Sreenath


1 Answers

Data Flow Analysis object never shows lcolSample[] in WrittenInside field as symbol that is written inside for loop

Yes, because that symbol is not written inside the loop (i.e. there is no lcolSample = whatever there). An element of the array represented by the lcolSample symbol is written in the loop, which is very different. I don't know how to find such writes using Roslyn's data flow analysis.

Locations property on lintCount1 shows only the place where it is declared (statement 2). Is there a way to find all the locations in which lintCount1 is written?

The DataFlowAnalysis object only gives you the symbols, accessing their Locations doesn't make much sense (because that location has nothing to do with the data flow analysis).

To me, both your questions sound like reasonable feature requests, you might want to make them on the Roslyn repo.

like image 147
svick Avatar answered Oct 31 '22 20:10

svick