示例#1
0
def test_EncodeNodes_inlined_struct(encoder: node_encoder.GraphNodeEncoder):
  """Test that struct definition is inlined in pre-processed text.

  NOTE(github.com/ChrisCummins/ProGraML/issues/57): Regression test.
  """
  ir = """
%struct.foo = type { i32, i8* }

define i32 @A(%struct.foo*) #0 {
  %2 = alloca %struct.foo*, align 8
  store %struct.foo* %0, %struct.foo** %2, align 8
  %3 = load %struct.foo*, %struct.foo** %2, align 8
  %4 = getelementptr inbounds %struct.foo, %struct.foo* %3, i32 0, i32 0
  %5 = load i32, i32* %4, align 8
  ret i32 %5
}
"""
  builder = programl.GraphBuilder()
  a = builder.AddNode(text="%2 = alloca %struct.foo*, align 8")
  g = builder.g

  encoder.EncodeNodes(g, ir=ir)

  assert (
    g.nodes[a]["preprocessed_text"] == "<%ID> = alloca { i32, i8* }*, align 8"
  )
示例#2
0
def test_MakeAnnotated_no_root_nodes():
    """Test that a graph with no root nodes produces no annotations."""
    builder = programl.GraphBuilder()
    annotator = MockNetworkXDataFlowGraphAnnotator(builder.proto)
    annotated = annotator.MakeAnnotated()
    assert len(annotated.graphs) == 0
    assert len(annotated.protos) == 0
示例#3
0
def test_GetStatementsForNode_node():
    """Test the nodes returned when root is a statementt."""
    builder = programl.GraphBuilder()
    foo = builder.AddNode()

    nodes = list(nx_utils.GetStatementsForNode(builder.g, foo))
    assert nodes == [foo]
示例#4
0
def g1() -> programl_pb2.ProgramGraph:
    """A four statement graph with one data element."""
    #            +---+
    #   +--------+ a +--------+       +---+
    #   |        +---+        |       | v1|
    #   V                     v       +---+
    # +---+                 +-+-+       |
    # | b |                 | c +<------+
    # +-+-+                 +-+-+
    #   |        +---+        |
    #   +------->+ d +<-------+
    #            +---+
    builder = programl.GraphBuilder()
    fn = builder.AddFunction()
    a = builder.AddNode(x=[-1], function=fn)
    b = builder.AddNode(x=[-1], function=fn)
    c = builder.AddNode(x=[-1], function=fn)
    d = builder.AddNode(x=[-1], function=fn)
    v1 = builder.AddNode(x=[-1],
                         type=programl_pb2.Node.IDENTIFIER,
                         function=fn)
    builder.AddEdge(a, b)
    builder.AddEdge(a, c)
    builder.AddEdge(b, d)
    builder.AddEdge(c, d)
    builder.AddEdge(v1, c, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(a, c, flow=programl_pb2.Edge.DATA)
    return builder.proto
示例#5
0
def test_IsValidRootNode():
    """Test that root nodes are correctly selected."""
    builder = programl.GraphBuilder()
    builder.AddNode(type=programl_pb2.Node.STATEMENT)
    builder.AddNode(type=programl_pb2.Node.IDENTIFIER)
    annotator = MockNetworkXDataFlowGraphAnnotator(builder.proto)
    assert annotator.root_nodes == [0]
示例#6
0
def g2() -> programl_pb2.ProgramGraph:
    """A five statement graph with one data element.

  This is the same as g1, but the extra statement "e" means that 'a' no longer
  dominates all of the other nodes.
  """
    #                       +---+
    # +---+        +--------+ a +--------+       +---+
    # | e |        |        +---+        |       | v1|
    # +-+-+        V                     v       +---+
    #   |        +---+                 +-+-+       ||
    #   +------->+ b |                 | c +<======++
    #            +-+-+                 +-+-+
    #              |        +---+        |
    #              +------->+ d +<-------+
    #                       +---+
    #
    builder = programl.GraphBuilder()
    fn = builder.AddFunction()
    a = builder.AddNode(x=[-1], function=fn)
    b = builder.AddNode(x=[-1], function=fn)
    c = builder.AddNode(x=[-1], function=fn)
    d = builder.AddNode(x=[-1], function=fn)
    e = builder.AddNode(x=[-1], function=fn)
    v1 = builder.AddNode(x=[-1],
                         type=programl_pb2.Node.IDENTIFIER,
                         function=fn)
    builder.AddEdge(a, b)
    builder.AddEdge(a, c)
    builder.AddEdge(b, d)
    builder.AddEdge(c, d)
    builder.AddEdge(v1, c, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(a, c, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(e, b)
    return builder.proto
示例#7
0
def wiki_without_subexpressions() -> programl_pb2.ProgramGraph:
    """Same as the wiki graph, but the order of the operands for the two
  multiplications has been reversed so that they are no longer common, and
  the operands is not commutative.
  """
    # a = b / c + g;
    # d = c / b * e;
    builder = programl.GraphBuilder()
    a1 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    a2 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    b = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    c = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    d1 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    d2 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    e = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    g = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])

    s0 = builder.AddNode(
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = sdiv <ID> <ID>",
        text="%a1 = sdiv %b %c",
        x=[-1],
    )
    s1 = builder.AddNode(
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = sdiv <ID> <ID>",
        text="%d1 = sdiv %c %b",
        x=[-1],
    )
    s2 = builder.AddNode(
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = add <ID> <ID>",
        text="%a2 = add %a1 %g",
        x=[-1],
    )
    s3 = builder.AddNode(
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = mul <ID> <ID>",
        text="%d2 = mul %d1 %e",
        x=[-1],
    )

    builder.AddEdge(s0, a1, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(b, s0, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(c, s0, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(s1, d1, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(b, s1, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(c, s1, flow=programl_pb2.Edge.DATA, position=0)

    builder.AddEdge(a1, s2, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(g, s2, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(s2, a2, flow=programl_pb2.Edge.DATA, position=0)

    builder.AddEdge(d1, s3, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(e, s3, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(s3, d2, flow=programl_pb2.Edge.DATA, position=0)
    return builder.proto
示例#8
0
def test_EncodeNodes_encoded_values(encoder: node_encoder.GraphNodeEncoder):
  """Test that "x" attribute of a node matches dictionary value."""
  builder = programl.GraphBuilder()
  a = builder.AddNode(text="br label %4")
  g = builder.g

  encoder.EncodeNodes(g)

  assert g.nodes[a]["x"][0] == encoder.dictionary["br label <%ID>"]
示例#9
0
def test_EncodeNodes_identifier(encoder: node_encoder.GraphNodeEncoder):
  """Test the encoding of identifier nodes."""
  builder = programl.GraphBuilder()
  a = builder.AddNode(text="abcd", type=programl_pb2.Node.IDENTIFIER)
  g = builder.g

  encoder.EncodeNodes(g)
  assert g.nodes[a]["text"] == "abcd"
  assert g.nodes[a]["preprocessed_text"] == "!IDENTIFIER"
示例#10
0
def test_root_node_is_not_in_a_function():
    """Test that if root node is not in a function, then nothing is dominated."""
    builder = programl.GraphBuilder()
    a = builder.AddNode(type=programl_pb2.Node.STATEMENT)

    annotator = dominator_tree.DominatorTreeAnnotator(builder.proto)
    g = builder.g
    annotator.Annotate(g, root_node=a)
    assert g.graph["data_flow_steps"] == 0
示例#11
0
def test_EncodeNodes_immediate(encoder: node_encoder.GraphNodeEncoder):
  """Test the encoding of immediate nodes."""
  builder = programl.GraphBuilder()
  a = builder.AddNode(text="abcd", type=programl_pb2.Node.IMMEDIATE)
  g = builder.g

  encoder.EncodeNodes(g)
  assert g.nodes[a]["text"] == "abcd"
  assert g.nodes[a]["preprocessed_text"] == "!IMMEDIATE"
示例#12
0
def wiki_with_commutativity() -> programl_pb2.ProgramGraph:
    """Same as the wiki graph, but the order of the operands has been reversed
  and the statement is commutative.
  """
    # a = b * c + g;
    # d = c * b * e;
    builder = programl.GraphBuilder()
    a1 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    a2 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    b = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    c = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    d1 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    d2 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    e = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    g = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])

    s0 = builder.AddNode(
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = mul <ID> <ID>",
        text="%a1 = mul %b %c",
        x=[-1],
    )
    s1 = builder.AddNode(
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = mul <ID> <ID>",
        text="%d1 = mul %c %b",
        x=[-1],
    )
    s2 = builder.AddNode(
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = add <ID> <ID>",
        text="%a2 = add %a1 %g",
        x=[-1],
    )
    s3 = builder.AddNode(
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = mul <ID> <ID>",
        text="%d2 = mul %d1 %e",
        x=[-1],
    )

    builder.AddEdge(s0, a1, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(b, s0, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(c, s0, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(s1, d1, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(b, s1, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(c, s1, flow=programl_pb2.Edge.DATA, position=0)

    builder.AddEdge(a1, s2, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(g, s2, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(s2, a2, flow=programl_pb2.Edge.DATA, position=0)

    builder.AddEdge(d1, s3, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(e, s3, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(s3, d2, flow=programl_pb2.Edge.DATA, position=0)
    return builder.proto
示例#13
0
def test_MakeAnnotated_graph_limit(n: int):
    """Test annotator with node labels."""
    builder = programl.GraphBuilder()
    for _ in range(100):
        builder.AddNode()
    annotator = MockNetworkXDataFlowGraphAnnotator(builder.proto,
                                                   node_y=[1, 2])
    annotated = annotator.MakeAnnotated(n)
    assert len(annotated.graphs) == n
    assert len(annotated.protos) == n
示例#14
0
def test_MakeAnnotated_no_node_y():
    """Test annotator with no node labels."""
    builder = programl.GraphBuilder()
    builder.AddNode()
    annotator = MockNetworkXDataFlowGraphAnnotator(builder.proto)
    annotated = annotator.MakeAnnotated()
    assert len(annotated.graphs) == 1
    assert len(annotated.protos) == 1
    assert annotated.graphs[0].nodes[0]["y"] == []
    assert annotated.protos[0].node[0].y == []
示例#15
0
def test_GetStatementsForNode_identifier():
    """Test the nodes returned when root is an identifier."""
    builder = programl.GraphBuilder()
    foo = builder.AddNode()
    bar = builder.AddNode()
    v1 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER)
    builder.AddEdge(foo, v1, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(v1, bar, flow=programl_pb2.Edge.DATA)

    nodes = list(nx_utils.GetStatementsForNode(builder.g, v1))
    assert nodes == [foo, bar]
示例#16
0
def graph() -> programl_pb2.ProgramGraph:
    """A program graph with linear control flow."""
    builder = programl.GraphBuilder()
    a = builder.AddNode(x=[0])
    b = builder.AddNode(x=[0])
    c = builder.AddNode(x=[0])
    d = builder.AddNode(x=[0])
    builder.AddEdge(a, b)
    builder.AddEdge(b, c)
    builder.AddEdge(c, d)
    return builder.proto
示例#17
0
def test_MakeAnnotated_no_graph_limit():
    """Test annotator with node labels."""
    builder = programl.GraphBuilder()
    builder.AddNode()
    builder.AddNode()
    builder.AddNode()
    annotator = MockNetworkXDataFlowGraphAnnotator(builder.proto,
                                                   node_y=[1, 2])
    annotated = annotator.MakeAnnotated()
    assert len(annotated.graphs) == 3
    assert len(annotated.protos) == 3
示例#18
0
def test_FindCallSites_multiple_call_sites():
    builder = programl.GraphBuilder()
    fn1 = builder.AddFunction()
    call = builder.AddNode(function=fn1, text="%2 = call i32 @B()")
    foo = builder.AddNode(function=fn1)
    call2 = builder.AddNode(function=fn1, text="%call = call i32 @B()")
    g = builder.g

    call_sites = llvm_statements.FindCallSites(g, fn1, "B")
    assert len(call_sites) == 2
    assert set(call_sites) == {call, call2}
示例#19
0
def test_EncodeNodes_encoded_values_differ_between_statements(
  encoder: node_encoder.GraphNodeEncoder,
):
  """Test that "x" attribute of nodes differ between different texts."""
  builder = programl.GraphBuilder()
  a = builder.AddNode(text="%7 = add nsw i32 %5, -1")
  b = builder.AddNode(text="br label %4")
  g = builder.g

  encoder.EncodeNodes(g)

  assert g.nodes[a]["x"][0] != g.nodes[b]["x"][0]
示例#20
0
def test_fuzz_GraphBuilder():
    """Test that graph construction doesn't set on fire."""
    builder = programl.GraphBuilder()
    random_node_count = random.randint(3, 100)
    random_edge_count = random.randint(3, 100)
    nodes = []
    for _ in range(random_node_count):
        nodes.append(builder.AddNode())
    for _ in range(random_edge_count):
        builder.AddEdge(random.choice(nodes), random.choice(nodes))
    assert builder.g
    assert builder.proto
示例#21
0
def wiki() -> programl_pb2.ProgramGraph:
    # a = b * c + g;
    # d = b * c * e;
    builder = programl.GraphBuilder()
    a1 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])  # 0
    a2 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])  # 1
    b = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])  # 2
    c = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])  # 3
    d1 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])  # 4
    d2 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])  # 5
    e = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])  # 6
    g = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])  # 7

    s0 = builder.AddNode(  # 8
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = mul <ID> <ID>",
        text="%a1 = div %b %c",
        x=[-1],
    )
    s1 = builder.AddNode(  # 9
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = mul <ID> <ID>",
        text="%d1 = div %b %c",
        x=[-1],
    )
    s2 = builder.AddNode(  # 10
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = add <ID> <ID>",
        text="%a2 = add %a1 %g",
        x=[-1],
    )
    s3 = builder.AddNode(  # 11
        type=programl_pb2.Node.STATEMENT,
        preprocessed_text="<ID> = mul <ID> <ID>",
        text="%d2 = mul %d1 %e",
        x=[-1],
    )

    builder.AddEdge(s0, a1, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(b, s0, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(c, s0, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(s1, d1, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(b, s1, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(c, s1, flow=programl_pb2.Edge.DATA, position=1)

    builder.AddEdge(a1, s2, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(g, s2, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(s2, a2, flow=programl_pb2.Edge.DATA, position=0)

    builder.AddEdge(d1, s3, flow=programl_pb2.Edge.DATA, position=0)
    builder.AddEdge(e, s3, flow=programl_pb2.Edge.DATA, position=1)
    builder.AddEdge(s3, d2, flow=programl_pb2.Edge.DATA, position=0)
    return builder.proto
示例#22
0
def test_EncodeNodes_equivalent_preprocessed_text(
  encoder: node_encoder.GraphNodeEncoder,
):
  """Test equivalence of nodes that pre-process to the same text."""
  builder = programl.GraphBuilder()
  a = builder.AddNode(text="%7 = add nsw i32 %5, -1")
  b = builder.AddNode(text="%9 = add nsw i32 %5, -2")
  g = builder.g

  encoder.EncodeNodes(g)

  assert g.nodes[a]["preprocessed_text"] == "<%ID> = add nsw i32 <%ID>, <INT>"
  assert g.nodes[b]["preprocessed_text"] == "<%ID> = add nsw i32 <%ID>, <INT>"
示例#23
0
def test_MakeAnnotated_graph_subset_of_root_nodes():
    """Test the number of graphs when only a subset of nodes are roots."""
    builder = programl.GraphBuilder()
    for i in range(50):
        if i % 2 == 0:
            builder.AddNode(type=programl_pb2.Node.STATEMENT)
        else:
            builder.AddNode(type=programl_pb2.Node.IDENTIFIER)
    annotator = MockNetworkXDataFlowGraphAnnotator(builder.proto,
                                                   node_y=[1, 2])
    annotated = annotator.MakeAnnotated()
    # Only the 25 statement nodes are used as roots.
    assert len(annotated.graphs) == 25
    assert len(annotated.protos) == 25
示例#24
0
def while_loop() -> programl_pb2.ProgramGraph:
  """Test fixture which returns a simple "while loop" graph."""
  #          (v1)
  #            |
  #            V
  #    +------[A]<--------+
  #    |       |          |
  #    |       V          |
  #    |      [Ba]----+   |
  #    |       |      |   |
  #    |       |      V   |
  #    |       |     (v3) |
  #    |       V      |   |
  #    |    +-[Bb]<---+   |
  #    |    |  |          |
  #    |    |  +----------+
  #    |  (v2)
  #    V    |
  #   [C]<--+
  builder = programl.GraphBuilder()
  a = builder.AddNode(type=programl_pb2.Node.STATEMENT, x=[-1])  # Loop header
  ba = builder.AddNode(type=programl_pb2.Node.STATEMENT, x=[-1])  # Loop body
  bb = builder.AddNode(type=programl_pb2.Node.STATEMENT, x=[-1])  # Loop body
  c = builder.AddNode(type=programl_pb2.Node.STATEMENT, x=[-1])  # Loop exit

  # Control flow:
  builder.AddEdge(a, ba, flow=programl_pb2.Edge.CONTROL)
  builder.AddEdge(ba, bb, flow=programl_pb2.Edge.CONTROL)
  builder.AddEdge(bb, c, flow=programl_pb2.Edge.CONTROL)
  builder.AddEdge(a, c, flow=programl_pb2.Edge.CONTROL)

  # Data flow:
  v1 = builder.AddNode(
    type=programl_pb2.Node.IDENTIFIER, x=[-1]
  )  # Loop induction variable
  v2 = builder.AddNode(
    type=programl_pb2.Node.IDENTIFIER, x=[-1]
  )  # Computed result
  v3 = builder.AddNode(
    type=programl_pb2.Node.IDENTIFIER, x=[-1]
  )  # Intermediate value

  builder.AddEdge(v1, a, flow=programl_pb2.Edge.DATA)
  builder.AddEdge(ba, v3, flow=programl_pb2.Edge.DATA)
  builder.AddEdge(v3, bb, flow=programl_pb2.Edge.DATA)
  builder.AddEdge(bb, v2, flow=programl_pb2.Edge.DATA)
  builder.AddEdge(v2, c, flow=programl_pb2.Edge.DATA)

  return builder.proto
示例#25
0
def graph():
    builder = programl.GraphBuilder()
    fn1 = builder.AddFunction()
    root = builder.AddNode()
    A = builder.AddNode(function=fn1)
    B = builder.AddNode(function=fn1)
    C = builder.AddNode(function=fn1)
    D = builder.AddNode(function=fn1)
    v1 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, function=fn1)
    builder.AddEdge(A, B, flow=programl_pb2.Edge.CONTROL)
    builder.AddEdge(B, C, flow=programl_pb2.Edge.CONTROL)
    builder.AddEdge(C, D, flow=programl_pb2.Edge.CONTROL)
    builder.AddEdge(root, A, flow=programl_pb2.Edge.CALL)
    builder.AddEdge(A, v1, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(v1, D, flow=programl_pb2.Edge.DATA)
    return builder.g
示例#26
0
def test_MakeAnnotated_node_x():
    """Test that node X values get appended."""
    builder = programl.GraphBuilder()
    for _ in range(10):
        builder.AddNode()
    annotator = MockNetworkXDataFlowGraphAnnotator(builder.proto)
    annotated = annotator.MakeAnnotated()
    assert len(annotated.graphs) == 10
    assert len(annotated.protos) == 10
    # Check each graph to ensure that each graph gets a fresh set of node x
    # arrays to append to, else these lists will graph in length.
    for graph, proto in zip(annotated.graphs, annotated.protos):
        assert graph.nodes[0]["x"] == [0]
        assert graph.nodes[1]["x"] == [1]
        assert graph.nodes[2]["x"] == [2]

        assert proto.node[0].x == [0]
        assert proto.node[1].x == [1]
        assert proto.node[2].x == [2]
示例#27
0
def test_StatementIsSuccessor_linear_control_path():
    # A -> B -> C
    builder = programl.GraphBuilder()
    a = builder.AddNode()
    b = builder.AddNode()
    c = builder.AddNode()
    builder.AddEdge(a, b)
    builder.AddEdge(b, c)
    g = builder.g
    assert nx_utils.StatementIsSuccessor(g, a, a)
    assert nx_utils.StatementIsSuccessor(g, a, b)
    assert nx_utils.StatementIsSuccessor(g, a, c)
    assert nx_utils.StatementIsSuccessor(g, b, c)
    assert not nx_utils.StatementIsSuccessor(g, c, a)
    assert not nx_utils.StatementIsSuccessor(g, b, a)
    assert not nx_utils.StatementIsSuccessor(g, a,
                                             -1)  # Destination not in graph
    with test.Raises(Exception):
        nx_utils.StatementIsSuccessor(g, -1, a)  # Source not in graph
示例#28
0
def test_StatementIsSuccessor_branched_control_path():
    # A -> B -> D
    # A -> C -> D
    builder = programl.GraphBuilder()
    a = builder.AddNode()
    b = builder.AddNode()
    c = builder.AddNode()
    d = builder.AddNode()
    builder.AddEdge(a, b)
    builder.AddEdge(a, c)
    builder.AddEdge(b, d)
    builder.AddEdge(c, d)
    g = builder.g
    assert nx_utils.StatementIsSuccessor(g, a, b)
    assert nx_utils.StatementIsSuccessor(g, a, c)
    assert nx_utils.StatementIsSuccessor(g, a, b)
    assert not nx_utils.StatementIsSuccessor(g, b, a)
    assert not nx_utils.StatementIsSuccessor(g, b, c)
    assert nx_utils.StatementIsSuccessor(g, b, d)
示例#29
0
def graph() -> programl_pb2.ProgramGraph:
    """A test fixture which yields a program graph."""
    builder = programl.GraphBuilder()
    a = builder.AddNode(type=programl_pb2.Node.STATEMENT, x=[-1])
    b = builder.AddNode(type=programl_pb2.Node.STATEMENT, x=[-1])
    c = builder.AddNode(type=programl_pb2.Node.STATEMENT, x=[-1])
    d = builder.AddNode(type=programl_pb2.Node.STATEMENT, x=[-1])

    v1 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])
    v2 = builder.AddNode(type=programl_pb2.Node.IDENTIFIER, x=[-1])

    builder.AddEdge(a, b, flow=programl_pb2.Edge.CONTROL)
    builder.AddEdge(a, c, flow=programl_pb2.Edge.CONTROL)
    builder.AddEdge(b, d, flow=programl_pb2.Edge.CONTROL)
    builder.AddEdge(c, d, flow=programl_pb2.Edge.CONTROL)

    builder.AddEdge(a, v1, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(v1, b, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(b, v2, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(v2, d, flow=programl_pb2.Edge.DATA)

    return builder.proto
示例#30
0
def test_Annotate():
    builder = programl.GraphBuilder()
    A = builder.AddNode(x=[-1])
    B = builder.AddNode(x=[-1])
    C = builder.AddNode(x=[-1])
    D = builder.AddNode(x=[-1])
    E = builder.AddNode(x=[-1])

    builder.AddEdge(A, B)
    builder.AddEdge(A, C)
    builder.AddEdge(B, D)
    builder.AddEdge(C, D)
    builder.AddEdge(A, E)

    builder.AddEdge(A, B, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(A, C, flow=programl_pb2.Edge.DATA)
    builder.AddEdge(C, D, flow=programl_pb2.Edge.DATA)

    g = builder.g

    annotator = data_dependence.DataDependencyAnnotator(builder.proto)

    annotator.Annotate(g, root_node=D)
    assert g.graph["data_flow_positive_node_count"] == 3
    assert g.graph["data_flow_steps"] == 3

    # Features
    assert g.nodes[A]["x"] == [-1, data_flow_graphs.ROOT_NODE_NO]
    assert g.nodes[B]["x"] == [-1, data_flow_graphs.ROOT_NODE_NO]
    assert g.nodes[C]["x"] == [-1, data_flow_graphs.ROOT_NODE_NO]
    assert g.nodes[D]["x"] == [-1, data_flow_graphs.ROOT_NODE_YES]
    assert g.nodes[E]["x"] == [-1, data_flow_graphs.ROOT_NODE_NO]

    # Labels
    assert g.nodes[A]["y"] == data_dependence.DEPENDENCY
    assert g.nodes[B]["y"] == data_dependence.NOT_DEPENDENCY
    assert g.nodes[C]["y"] == data_dependence.DEPENDENCY
    assert g.nodes[D]["y"] == data_dependence.DEPENDENCY
    assert g.nodes[E]["y"] == data_dependence.NOT_DEPENDENCY