Skip to content

Commit 4fcde9a

Browse files
author
Daniel Kroening
committed
show_goto_functions: sort alphabetically
1 parent 2a76328 commit 4fcde9a

5 files changed

+65
-11
lines changed

src/goto-programs/goto_functions.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Date: June 2003
1313

1414
#include "goto_functions.h"
1515

16+
#include <algorithm>
17+
1618
void goto_functionst::compute_location_numbers()
1719
{
1820
unused_location_number = 0;
@@ -54,3 +56,44 @@ void goto_functionst::compute_loop_numbers()
5456
func.second.body.compute_loop_numbers();
5557
}
5658
}
59+
60+
/// returns a vector of the iterators in alphabetical order
61+
std::vector<goto_functionst::function_mapt::const_iterator>
62+
goto_functionst::sorted() const
63+
{
64+
std::vector<function_mapt::const_iterator> result;
65+
66+
result.reserve(function_map.size());
67+
68+
for(auto it = function_map.begin(); it != function_map.end(); it++)
69+
result.push_back(it);
70+
71+
std::sort(
72+
result.begin(),
73+
result.end(),
74+
[](function_mapt::const_iterator a, function_mapt::const_iterator b) {
75+
return id2string(a->first) < id2string(b->first);
76+
});
77+
78+
return result;
79+
}
80+
81+
/// returns a vector of the iterators in alphabetical order
82+
std::vector<goto_functionst::function_mapt::iterator> goto_functionst::sorted()
83+
{
84+
std::vector<function_mapt::iterator> result;
85+
86+
result.reserve(function_map.size());
87+
88+
for(auto it = function_map.begin(); it != function_map.end(); it++)
89+
result.push_back(it);
90+
91+
std::sort(
92+
result.begin(),
93+
result.end(),
94+
[](function_mapt::iterator a, function_mapt::iterator b) {
95+
return id2string(a->first) < id2string(b->first);
96+
});
97+
98+
return result;
99+
}

src/goto-programs/goto_functions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class goto_functionst
114114
for(const auto &fun : other.function_map)
115115
function_map[fun.first].copy_from(fun.second);
116116
}
117+
118+
std::vector<function_mapt::const_iterator> sorted() const;
119+
std::vector<function_mapt::iterator> sorted();
117120
};
118121

119122
#define Forall_goto_functions(it, functions) \

src/goto-programs/show_goto_functions.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ void show_goto_functions(
5151

5252
case ui_message_handlert::uit::PLAIN:
5353
{
54-
for(const auto &fun : goto_functions.function_map)
54+
// sort alphabetically
55+
const auto sorted = goto_functions.sorted();
56+
57+
for(const auto &fun : sorted)
5558
{
56-
const symbolt &symbol = ns.lookup(fun.first);
57-
const bool has_body = fun.second.body_available();
59+
const symbolt &symbol = ns.lookup(fun->first);
60+
const bool has_body = fun->second.body_available();
5861

5962
if(list_only)
6063
{
@@ -67,10 +70,9 @@ void show_goto_functions(
6770
{
6871
msg.status() << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n";
6972

70-
const symbolt &symbol = ns.lookup(fun.first);
7173
msg.status() << messaget::bold << symbol.display_name()
7274
<< messaget::reset << " /* " << symbol.name << " */\n";
73-
fun.second.body.output(ns, symbol.name, msg.status());
75+
fun->second.body.output(ns, symbol.name, msg.status());
7476
msg.status() << messaget::eom;
7577
}
7678
}

src/goto-programs/show_goto_functions_json.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ json_objectt show_goto_functions_jsont::convert(
4141
{
4242
json_arrayt json_functions;
4343
const json_irept no_comments_irep_converter(false);
44-
for(const auto &function_entry : goto_functions.function_map)
44+
45+
const auto sorted = goto_functions.sorted();
46+
47+
for(const auto &function_entry : sorted)
4548
{
46-
const irep_idt &function_name=function_entry.first;
47-
const goto_functionst::goto_functiont &function=function_entry.second;
49+
const irep_idt &function_name = function_entry->first;
50+
const goto_functionst::goto_functiont &function = function_entry->second;
4851

4952
json_objectt &json_function=
5053
json_functions.push_back(jsont()).make_object();

src/goto-programs/show_goto_functions_xml.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ xmlt show_goto_functions_xmlt::convert(
4343
const goto_functionst &goto_functions)
4444
{
4545
xmlt xml_functions=xmlt("functions");
46-
for(const auto &function_entry : goto_functions.function_map)
46+
47+
const auto sorted = goto_functions.sorted();
48+
49+
for(const auto &function_entry : sorted)
4750
{
48-
const irep_idt &function_name=function_entry.first;
49-
const goto_functionst::goto_functiont &function=function_entry.second;
51+
const irep_idt &function_name = function_entry->first;
52+
const goto_functionst::goto_functiont &function = function_entry->second;
5053

5154
xmlt &xml_function=xml_functions.new_element("function");
5255
xml_function.set_attribute("name", id2string(function_name));

0 commit comments

Comments
 (0)