RegionDataFlowAnalysis.VariablesDeclared bug or incomplete?
-
2012년 1월 24일 화요일 오후 3:20
I am trying to use RegionDataFlowAnalysis.VariablesDeclared on this foreach loop (I deleted unnecessary code here):
foreach (String s in args) { int[] b = new int[args.Length]; }
I created the SyntaxTree (tree) and the Compilation (compilation). compilation.GetDiagnostics() resolves with no errors. Then I search for the ForEachStatementSyntax and try to run the RegionDataFlowAnalysis as follows:SemanticModel semanticModel = compilation.GetSemanticModel(tree); ForEachStatementSyntax forEachNode = tree.Root.DescendentNodes().OfType<ForEachStatementSyntax>().First(); RegionDataFlowAnalysis dataFlow = semanticModel .AnalyzeRegionDataFlow(forEachNode .Span); var variablesDeclared = dataFlow.VariablesDeclared;
In my opinion variablesDeclares should contain the Symbol bIs this a bug, an unimplemented feature or am I simply wrong?
모든 응답
-
2012년 1월 24일 화요일 오후 7:33
As I understand it RegionDataFlowAnalysis analyses a piece of code as if you were looking at it from outside. So, for declared variables, it gives you only variables that are declared on the top level of that code, not within blocks inside it. For example, when doing extract method refactoring, you don't care about variables inside blocks, but you do care about variables declared at the (relative) top level of the piece of code.
At least that's what I though at first. But then I tried other constructs that use blocks of code, and the results were inconsistent. If you use for, it shows with two variables declared (iteration variable and the variable declared inside), but foreach shows with zero variables.
I think this is a bug, unless I'm missing something.
- 편집됨 svick 2012년 1월 24일 화요일 오후 7:34
- 답변으로 표시됨 Jochen Huck 2012년 1월 25일 수요일 오전 11:19
-
2012년 1월 25일 수요일 오전 11:10Thanks for your suggestions. I thought that this is a bug too.
-
2012년 1월 26일 목요일 오전 12:18소유자
I believe svick is correct - this looks like a bug. The VariablesDeclared property should list all variables declared within the specified span (including the loop control variable and the int[] b in the above example). Looks like the the flow analysis APIs aren't handling foreach loops correctly in the CTP.
However, I checked that this bug doesn't repro in our latest internal implementation (i.e. both variables from the above example are being correctly reported in the VariablesDeclared property). So this should be fixed for future releases.
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team- 편집됨 Shyam NamboodiripadMicrosoft Employee, Owner 2012년 1월 26일 목요일 오전 12:20

