RegionDataFlowAnalysis.VariablesDeclared bug or incomplete?
-
2012年1月24日 15: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日 19: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日 19: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日 0: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日 0:18
- 編集済み Shyam NamboodiripadMicrosoft Employee, Owner 2012年1月26日 0:19
- 編集済み Shyam NamboodiripadMicrosoft Employee, Owner 2012年1月26日 0:20
- 編集済み Shyam NamboodiripadMicrosoft Employee, Owner 2012年1月26日 0:20

