Skip to content

Class hierarchy: add DOT output, unit tests #1687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions regression/goto-instrument/class-hierarchy/dot.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CORE
main.c
--class-hierarchy --dot
digraph class_hierarchy
^EXIT=0$
^SIGNAL=0$
4 changes: 4 additions & 0 deletions regression/goto-instrument/class-hierarchy/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

void main()
{
}
5 changes: 5 additions & 0 deletions regression/goto-instrument/class-hierarchy/plain.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CORE
main.c
--class-hierarchy
^EXIT=0$
^SIGNAL=0$
14 changes: 14 additions & 0 deletions src/goto-instrument/goto_instrument_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Author: Daniel Kroening, [email protected]
#include <util/json.h>
#include <util/exit_codes.h>

#include <goto-programs/class_hierarchy.h>
#include <goto-programs/goto_convert_functions.h>
#include <goto-programs/remove_function_pointers.h>
#include <goto-programs/remove_virtual_functions.h>
Expand Down Expand Up @@ -675,6 +676,18 @@ int goto_instrument_parse_optionst::doit()
return 0;
}

if(cmdline.isset("class-hierarchy"))
{
class_hierarchyt hierarchy;
hierarchy(goto_model.symbol_table);
if(cmdline.isset("dot"))
hierarchy.output_dot(std::cout);
else
hierarchy.output(std::cout);

return 0;
}

if(cmdline.isset("dot"))
{
namespacet ns(goto_model.symbol_table);
Expand Down Expand Up @@ -1474,6 +1487,7 @@ void goto_instrument_parse_optionst::help()
" --call-graph show graph of function calls\n"
// NOLINTNEXTLINE(whitespace/line_length)
" --reachable-call-graph show graph of function calls potentially reachable from main function\n"
" --class-hierarchy show class hierarchy\n"
"\n"
"Safety checks:\n"
" --no-assertions ignore user assertions\n"
Expand Down
1 change: 1 addition & 0 deletions src/goto-instrument/goto_instrument_parse_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Author: Daniel Kroening, [email protected]
"(max-var):(max-po-trans):(ignore-arrays)" \
"(cfg-kill)(no-dependencies)(force-loop-duplication)" \
"(call-graph)(reachable-call-graph)" \
"(class-hierarchy)" \
"(no-po-rendering)(render-cluster-file)(render-cluster-function)" \
"(nondet-volatile)(isr):" \
"(stack-depth):(nondet-static)" \
Expand Down
20 changes: 20 additions & 0 deletions src/goto-programs/class_hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,23 @@ void class_hierarchyt::output(std::ostream &out) const
<< ch << '\n';
}
}

/// Output class hierarchy in Graphviz DOT format
/// \param ostr: stream to write DOT to
void class_hierarchyt::output_dot(std::ostream &ostr) const
{
ostr << "digraph class_hierarchy {\n"
<< " rankdir=BT;\n"
<< " node [fontsize=12 shape=box];\n";
for(const auto &c : class_map)
{
for(const auto &ch : c.second.parents)
{
ostr << " \"" << c.first << "\" -> "
<< "\"" << ch << "\" "
<< " [arrowhead=\"vee\"];"
<< "\n";
}
}
ostr << "}\n";
}
1 change: 1 addition & 0 deletions src/goto-programs/class_hierarchy.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class class_hierarchyt
}

void output(std::ostream &) const;
void output_dot(std::ostream &) const;

protected:
void get_children_trans_rec(const irep_idt &, idst &) const;
Expand Down
1 change: 1 addition & 0 deletions unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SRC += unit_tests.cpp \
analyses/does_remove_const/does_type_preserve_const_correctness.cpp \
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
goto-programs/goto_trace_output.cpp \
goto-programs/class_hierarchy_output.cpp \
java_bytecode/java_bytecode_convert_class/convert_abstract_class.cpp \
java_bytecode/java_bytecode_parse_generics/parse_generic_class.cpp \
java_bytecode/java_object_factory/gen_nondet_string_init.cpp \
Expand Down
Binary file added unit/goto-programs/HierarchyTest.class
Binary file not shown.
12 changes: 12 additions & 0 deletions unit/goto-programs/HierarchyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class HierarchyTest {
// These fields exist only so the classloader will load all test classes:
HierarchyTestGrandchild field1;
HierarchyTestChild2 field2;
}

class HierarchyTestChild1 extends HierarchyTest {}

class HierarchyTestChild2 extends HierarchyTest {}

class HierarchyTestGrandchild extends HierarchyTestChild1
implements HierarchyTestInterface1, HierarchyTestInterface2 {}
Binary file added unit/goto-programs/HierarchyTestChild1.class
Binary file not shown.
Binary file added unit/goto-programs/HierarchyTestChild2.class
Binary file not shown.
Binary file added unit/goto-programs/HierarchyTestGrandchild.class
Binary file not shown.
Binary file added unit/goto-programs/HierarchyTestInterface1.class
Binary file not shown.
1 change: 1 addition & 0 deletions unit/goto-programs/HierarchyTestInterface1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
interface HierarchyTestInterface1 {}
Binary file added unit/goto-programs/HierarchyTestInterface2.class
Binary file not shown.
1 change: 1 addition & 0 deletions unit/goto-programs/HierarchyTestInterface2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
interface HierarchyTestInterface2 {}
66 changes: 66 additions & 0 deletions unit/goto-programs/class_hierarchy_output.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************\

Module: Unit tests for class_hierarchyt output functions

Author: Diffblue Limited. All rights reserved.

\*******************************************************************/

#include <testing-utils/catch.hpp>
#include <testing-utils/load_java_class.h>

#include <goto-programs/class_hierarchy.h>

#include <iostream>
#include <sstream>

void require_parent_child_relationship(
const std::string &parent_raw,
const std::string &child_raw,
const std::string &output,
const std::string &output_dot)
{
std::string parent = "java::" + parent_raw;
std::string child = "java::" + child_raw;

std::stringstream
plain_child_expectation, plain_parent_expectation, dot_expectation;

plain_child_expectation << "Child of " << parent << ": " << child;
plain_parent_expectation << "Parent of " << child << ": " << parent;
dot_expectation << "\"" << child << "\" -> \"" << parent << "\"";

REQUIRE(output.find(plain_child_expectation.str()) != std::string::npos);
REQUIRE(output.find(plain_parent_expectation.str()) != std::string::npos);
REQUIRE(output_dot.find(dot_expectation.str()) != std::string::npos);
}

SCENARIO(
"Output a simple class hierarchy"
"[core][goto-programs][class_hierarchy]")
{
symbol_tablet symbol_table =
load_java_class("HierarchyTest", "goto-programs/");
class_hierarchyt hierarchy;

std::stringstream output_stream;
std::stringstream output_dot_stream;

hierarchy(symbol_table);
hierarchy.output(output_stream);
hierarchy.output_dot(output_dot_stream);

std::string output = output_stream.str();
std::string output_dot = output_dot_stream.str();

require_parent_child_relationship(
"HierarchyTest", "HierarchyTestChild1", output, output_dot);
require_parent_child_relationship(
"HierarchyTest", "HierarchyTestChild2", output, output_dot);
require_parent_child_relationship(
"HierarchyTestChild1", "HierarchyTestGrandchild", output, output_dot);
require_parent_child_relationship(
"HierarchyTestInterface1", "HierarchyTestGrandchild", output, output_dot);
require_parent_child_relationship(
"HierarchyTestInterface2", "HierarchyTestGrandchild", output, output_dot);
}