Skip to content

Commit 13dac86

Browse files
committed
Add equality operators to symbolt and symbol_tablet
This adds equality member operators to symbolt and symbol_tablet. They will be useful in unit tests to check that when outputting a symbol table in JSON and reading it back in again the same symbol table is obtained.
1 parent 3614bdc commit 13dac86

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

src/util/symbol.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,34 @@ bool symbolt::is_well_formed() const
144144

145145
return true;
146146
}
147+
148+
bool symbolt::operator==(const symbolt &other) const
149+
{
150+
// clang-format off
151+
return
152+
type == other.type &&
153+
value == other.value &&
154+
location == other.location &&
155+
name == other.name &&
156+
module == other.module &&
157+
base_name == other.base_name &&
158+
mode == other.mode &&
159+
pretty_name == other.pretty_name &&
160+
is_type == other.is_type &&
161+
is_macro == other.is_macro &&
162+
is_exported == other.is_exported &&
163+
is_input == other.is_input &&
164+
is_output == other.is_output &&
165+
is_state_var == other.is_state_var &&
166+
is_property == other.is_property &&
167+
is_parameter == other.is_parameter &&
168+
is_auxiliary == other.is_auxiliary &&
169+
is_weak == other.is_weak &&
170+
is_lvalue == other.is_lvalue &&
171+
is_static_lifetime == other.is_static_lifetime &&
172+
is_thread_local == other.is_thread_local &&
173+
is_file_local == other.is_file_local &&
174+
is_extern == other.is_extern &&
175+
is_volatile == other.is_volatile;
176+
// clang-format on
177+
}

src/util/symbol.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class symbolt
124124

125125
/// Check that a symbol is well formed.
126126
bool is_well_formed() const;
127+
128+
bool operator==(const symbolt &other) const;
127129
};
128130

129131
std::ostream &operator<<(std::ostream &out, const symbolt &symbol);

src/util/symbol_table.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <util/invariant.h>
77
#include <util/validate.h>
88

9+
#include <set>
10+
911
/// Move or copy a new symbol to the symbol table.
1012
/// \remarks This is a nicer interface than move and achieves the same
1113
/// result as both move and add.
@@ -219,3 +221,42 @@ void symbol_tablet::validate(const validation_modet vm) const
219221
"'");
220222
}
221223
}
224+
225+
bool symbol_tablet::operator==(const symbol_tablet &other) const
226+
{
227+
// we cannot use == for comparing the multimaps as it compares the items
228+
// sequentially, but the order of items with equal keys depends on the
229+
// insertion order
230+
231+
{
232+
std::vector<std::pair<irep_idt, irep_idt>> v1(
233+
internal_symbol_base_map.begin(), internal_symbol_base_map.end());
234+
235+
std::vector<std::pair<irep_idt, irep_idt>> v2(
236+
other.internal_symbol_base_map.begin(),
237+
other.internal_symbol_base_map.end());
238+
239+
std::sort(v1.begin(), v1.end());
240+
std::sort(v2.begin(), v2.end());
241+
242+
if(v1 != v2)
243+
return false;
244+
}
245+
246+
{
247+
std::vector<std::pair<irep_idt, irep_idt>> v1(
248+
internal_symbol_module_map.begin(), internal_symbol_module_map.end());
249+
250+
std::vector<std::pair<irep_idt, irep_idt>> v2(
251+
other.internal_symbol_module_map.begin(),
252+
other.internal_symbol_module_map.end());
253+
254+
std::sort(v1.begin(), v1.end());
255+
std::sort(v2.begin(), v2.end());
256+
257+
if(v1 != v2)
258+
return false;
259+
}
260+
261+
return internal_symbols == other.internal_symbols;
262+
}

src/util/symbol_table.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class symbol_tablet : public symbol_table_baset
119119

120120
/// Check that the symbol table is well-formed
121121
void validate(const validation_modet vm = validation_modet::INVARIANT) const;
122+
123+
bool operator==(const symbol_tablet &other) const;
122124
};
123125

124126
#endif // CPROVER_UTIL_SYMBOL_TABLE_H

0 commit comments

Comments
 (0)