Skip to content

Commit dd2faa0

Browse files
Daniel Kroeningtautschnig
Daniel Kroening
authored andcommitted
Fix sorting in symbol_table_baset::show
This is a fix for 25ffad4; std::string::compare returns an integer <0, ==0 or >0, but std::sort wants a boolean when <0. Now use a shared sorting implementation, which also makes sure we test this in various regression tests.
1 parent 230e525 commit dd2faa0

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/goto-programs/show_symbol_table.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,10 @@ void show_symbol_table_plain(
7070
{
7171
out << '\n' << "Symbols:" << '\n' << '\n';
7272

73-
// we want to sort alphabetically
74-
std::vector<std::string> symbols;
75-
symbols.reserve(symbol_table.symbols.size());
76-
77-
for(const auto &symbol_pair : symbol_table.symbols)
78-
symbols.push_back(id2string(symbol_pair.first));
79-
std::sort(symbols.begin(), symbols.end());
80-
8173
const namespacet ns(symbol_table);
8274

83-
for(const irep_idt &id : symbols)
75+
// we want to sort alphabetically
76+
for(const irep_idt &id : symbol_table.sorted_symbol_names())
8477
{
8578
const symbolt &symbol=ns.lookup(id);
8679

src/util/symbol_table_base.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,30 @@ bool symbol_table_baset::remove(const irep_idt &name)
3333
return false;
3434
}
3535

36-
/// Print the contents of the symbol table.
37-
/// \param out: The ostream to direct output to.
38-
void symbol_table_baset::show(std::ostream &out) const
36+
std::vector<irep_idt> symbol_table_baset::sorted_symbol_names() const
3937
{
4038
std::vector<irep_idt> sorted_names;
4139
sorted_names.reserve(symbols.size());
40+
4241
for(const auto &elem : symbols)
4342
sorted_names.push_back(elem.first);
43+
4444
std::sort(
4545
sorted_names.begin(),
4646
sorted_names.end(),
47-
[](const irep_idt &a, const irep_idt &b) { return a.compare(b); });
47+
[](const irep_idt &a, const irep_idt &b) { return a.compare(b) < 0; });
48+
49+
return sorted_names;
50+
}
51+
52+
/// Print the contents of the symbol table.
53+
/// \param out: The ostream to direct output to.
54+
void symbol_table_baset::show(std::ostream &out) const
55+
{
4856
out << "\n"
4957
<< "Symbols:"
5058
<< "\n";
51-
for(const auto &name : sorted_names)
59+
for(const auto &name : sorted_symbol_names())
5260
out << symbols.at(name);
5361
}
5462

src/util/symbol_table_base.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class symbol_table_baset
136136

137137
void show(std::ostream &out) const;
138138

139+
/// Build and return a lexicographically sorted vector of symbol names from
140+
/// all symbols stored in this symbol table.
141+
std::vector<irep_idt> sorted_symbol_names() const;
142+
139143
class iteratort
140144
{
141145
private:

0 commit comments

Comments
 (0)