C#静态分析,一个变量/参数的可能值 [英] C# Static Analysis, possible values for a variable/parameter

查看:210
本文介绍了C#静态分析,一个变量/参数的可能值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类似于下面每一个实施例的代码,我想能够静态分析代码,以确定被传递到SpecialFunction()的可能值的列表。



  SpecialFunction(5); //一个

INT X = 5;
SpecialFunction(X); // B

INT X = 5;
X =条件? 3:19;
SpecialFunction(X); // C



我已经可以解析C#为一个抽象语法树,我已经可以处理类似案件A,我想我可以跟踪值的初始分配的猜测情况下b,但案件容易为C似乎得到迅速复杂化了。



我几乎可以肯定,我们将不能够在所有情况下,解决对于x静态,这就是确定。我想知道的战略企图是,如何认识它的时候不能这样做。如果我们需要包括类级别的领域和多线程?闭包?它是否有助于如果我们知道,对于集 X 所有可能值 X | X | < 50



从@Vladimir Perevalov的建议,怎么可能在Pex的概念被应用到了有针对性的代码点找到可能的值(而不是什么PEX似乎做这是发现导致的选中(?)的特殊情况下)?


解决方案

有一个项目,你想要做(至少是非常接近)的东西。这是 Pex的
尝试寻找他们的文档,也可以反编译源代码,看看他们做什么。


In code similar to each of the following examples, I would like to be able to statically analyze code to determine the list of possible values that are passed into SpecialFunction().

SpecialFunction(5); // A

int x = 5;
SpecialFunction(x); // B

int x = 5;
x = condition ? 3 : 19;
SpecialFunction(x); // C

I can already parse the C# into an abstract syntax tree and I can already handle cases like A, and I guess I could keep track of initial assignments of values to guess case B, but cases as easy as C seem to get complicated quickly.

I'm almost certain that we won't be able to solve for x in all cases statically and that's OK. I would like to know strategies for attempting it, ways to recognize when it can't be done. What if we need to include class-level fields and multi-threading? Closures? Would it help if we know that for the set X of all possible values for x, |X| < 50?

From @Vladimir Perevalov's suggestion, how could the concepts in Pex be applied to finding possible values for targeted code points (rather than what Pex seems to do which is to discover code paths and values that lead to unchecked(?) exceptional cases)?

解决方案

There is a project that does what you want (at least very near). It is Pex. Try looking at their docs, also you could decompile the sources and see what they do.

这篇关于C#静态分析,一个变量/参数的可能值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆