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)?
dynamic analysis
. But even if so, in this case you deal with only a couple of parameters, what if you deal with a function, ike, say,IEnumerable<int>GetValuesForx(...)
? – Gomulka