コード例 #1
0
        static void Main(string[] args)
        {
            /*{
             *  var n = new NumericalValue<int>();
             *  n.UnionWith(new NumericalValue<int>(0, Inclusivity.INCLUSIVE, 3, Inclusivity.INCLUSIVE));
             *  AssertEquals(n.ToString(), "[0, 3]");
             *  n.UnionWith(new NumericalValue<int>(0, Inclusivity.INCLUSIVE, 3, Inclusivity.INCLUSIVE));
             *  AssertEquals(n.ToString(), "[0, 3]");
             *  n.UnionWith(new NumericalValue<int>(1, Inclusivity.INCLUSIVE, 4, Inclusivity.EXCLUSIVE));
             *  AssertEquals(n.ToString(), "[0, 4)");
             *  n.UnionWith(new NumericalValue<int>(7, Inclusivity.INCLUSIVE, 10, Inclusivity.INCLUSIVE));
             *  AssertEquals(n.ToString(), "[0, 4) U [7, 10]");
             *  n.UnionWith(new NumericalValue<int>(6, Inclusivity.INCLUSIVE, 7, Inclusivity.EXCLUSIVE));
             *  AssertEquals(n.ToString(), "[0, 4) U [6, 10]");
             *  n.UnionWith(new NumericalValue<int>(3, Inclusivity.INCLUSIVE, 7, Inclusivity.EXCLUSIVE));
             *  AssertEquals(n.ToString(), "[0, 10]");
             * }
             * {
             *  var n = new NumericalValue<int>();
             *  n.UnionWith(new NumericalValue<int>(0, Inclusivity.INCLUSIVE, 4, Inclusivity.EXCLUSIVE));
             *  n.UnionWith(new NumericalValue<int>(6, Inclusivity.INCLUSIVE, 10, Inclusivity.INCLUSIVE));
             *  n.UnionWith(new NumericalValue<int>(-2, Inclusivity.INCLUSIVE, -2, Inclusivity.INCLUSIVE));
             *  n.UnionWith(new NumericalValue<int>(14, Inclusivity.EXCLUSIVE, 15, Inclusivity.EXCLUSIVE));
             *  AssertEquals(n.ToString(), "[-2, -2] U [0, 4) U [6, 10] U (14, 15)");
             *  n.IntersectWith(3, Inclusivity.INCLUSIVE, 6, Inclusivity.INCLUSIVE);
             *  AssertEquals(n.ToString(), "[3, 4) U [6, 6]");
             * }*/

            SyntaxTree tree = CSharpSyntaxTree.ParseText(
                @"using System;
            using System.Collections;
            using System.Linq;
            using System.Text;
 
            namespace HelloWorld
            {
                class ProgramUnderTest
                {

static void Main(string[] args) {}
int f() { return 4; }

private void Test() {
    int j = 3;
    for (int i = 0; i < 10; i = i + 1) {
        if (i > j) { j = i + 1; }
    }
    return;
/*
    int i = 0;
    i = 3;
    int j = f();
    if (j + 3 > 5) {
        i = j;
    } else {
        i = -1;
    }
    return;*/
}
                }
            }");

            var diagnostics = new List <NumericValueDiagnostic>();
            var compilation = CompileCode(tree);
            var model       = compilation.GetSemanticModel(tree);

            foreach (var c in tree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>())
            {
                foreach (var m in c.DescendantNodes().OfType <MethodDeclarationSyntax>())
                {
                    if (m.Identifier.ToString().Equals("Test"))
                    {
                        ControlFlowGraph controlFlowGraph = new ControlFlowGraph(m, model);
                        Console.WriteLine("digraph {0} {{", "Test");
                        HashSet <SyntaxNode> visitedNodes = new HashSet <SyntaxNode>();
                        printGraphViz(controlFlowGraph, controlFlowGraph.GetEntryPoint(), visitedNodes);
                        Console.WriteLine();
                        foreach (var node in visitedNodes)
                        {
                            if (node.GetLocation().ToString().Contains("[0..7)"))
                            {
                                Console.WriteLine("\"{0}\" [color=\"#55AAFF\"];", node.GetLocation().ToString());
                            }
                            Console.WriteLine("\"{0}\" [label=\"{1}\"];", node.GetLocation().ToString(), interpolateLabel(node.ToFullString()));
                        }
                        Console.WriteLine("}");

                        Console.Write("\n\n\n");

                        /*NumericalValueAnalysis numericalValueAnalysis = new NumericalValueAnalysis(controlFlowGraph);*/
                        StringBuilder builder = new StringBuilder();
                        builder.Append("{ \"nodes\": [\n");
                        outputNumericalValueOutput(controlFlowGraph, controlFlowGraph.GetEntryPoint(), new HashSet <SyntaxNode>(), builder);
                        builder.Append("\n] }");

                        Console.WriteLine("Builder: '\n{0}\n'", builder);
                        System.IO.File.WriteAllText("input_code.json", builder.ToString());

                        var process = new System.Diagnostics.Process();
                        process.StartInfo.FileName       = "numerical_value.exe";
                        process.StartInfo.Arguments      = "input_code.json diagnostics.json";
                        process.StartInfo.CreateNoWindow = true;
                        process.Start();
                        process.WaitForExit();

                        diagnostics.AddRange(Newtonsoft.Json.JsonConvert.DeserializeObject <List <NumericValueDiagnostic> >(System.IO.File.ReadAllText("diagnostics.json")));
                        foreach (var diagnostic in diagnostics)
                        {
                            Console.WriteLine("{0}: This is always {1}", diagnostic.location, diagnostic.always_true);
                        }
                    }
                }
            }


            Console.WriteLine();
            Console.Write("Press enter to continue...");
            Console.ReadLine();
        }
コード例 #2
0
 public NumericalValueAnalysis(ControlFlowGraph graph)
 {
     analyze(graph, graph.GetEntryPoint(), new Dictionary <string, NumericalValue <int> >(), new Dictionary <SyntaxNode, Dictionary <string, NumericalValue <int> > >());
 }