Roslyn Data Flow Analysis - Ambiguous WrittenInside and Locations Field Values

I recently started working with the data flow analysis APIs provided by Roslyn, and finding the values ​​presented in the WrittenInside field and the Locations field is a bit ambiguous.

Consider the code snippet in the main method below

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. } 
  • If I execute DFA on for a node loop, the result of the data flow analysis object will never display lcolSample [] in the WrittenInside field as a character that is written inside the loop. The reason is that it is declared outside the node on which the data flow analysis is performed. But in the ReadInside field this symbol is displayed. Is there a way to find out all the characters that change / write inside the given node, even if they are declared outside the node on which DFA is running?

  • The variable lintCount1 is written twice (statement 2 and 7) and read twice. The Locations property on lintCount1 shows only the place where it is declared (statement 2). Is there a way to find all the places where lintCount1 is written? Find all the links to this symbol will give all the places where the symbol is used, but I need the places where they are written, but not read.

This is my first question on this forum. Please request any other information if the information above is insufficient. Thanks in advance.

+6
source share
1 answer

The data flow analysis object never shows lcolSample [] in the WrittenInside field as a character that is written inside the loop

Yes, because this character is not written inside the loop (i.e. there is no lcolSample = whatever ). The array element represented by the lcolSample symbol is written in a loop, which is very different. I do not know how to find such records using Roslyn data flow analysis.

The location property on lintCount1 shows only the place where it is declared (statement 2). Is there a way to find all the places where lintCount1 is written?

The DataFlowAnalysis object provides only characters; access to their Location does not make much sense (since this location has nothing to do with data flow analysis).

For me, both of your questions sound like reasonable feature requests, you can do them in the Roslyn repository .

+4
source

Source: https://habr.com/ru/post/974705/


All Articles