File tree 5 files changed +84
-0
lines changed 5 files changed +84
-0
lines changed Original file line number Diff line number Diff line change 12
12
13
13
#include " source_location.h"
14
14
#include " std_expr.h"
15
+ #include " suffix.h"
15
16
16
17
void symbolt::show (std::ostream &out) const
17
18
{
@@ -112,3 +113,25 @@ symbol_exprt symbolt::symbol_expr() const
112
113
{
113
114
return symbol_exprt (name, type);
114
115
}
116
+
117
+ // / Check that the instance object is well formed.
118
+ // / \return: true if well-formed; false otherwise.
119
+ bool symbolt::is_well_formed () const
120
+ {
121
+ // Well-formedness criterion number 1 is for a symbol
122
+ // to have a non-empty mode (see #1880)
123
+ if (mode.empty ())
124
+ {
125
+ return false ;
126
+ }
127
+
128
+ // Well-formedness criterion number 2 is for a symbol
129
+ // to have it's base name as a suffix to it's more
130
+ // general name.
131
+ if (!has_suffix (id2string (name), id2string (base_name)))
132
+ {
133
+ return false ;
134
+ }
135
+
136
+ return true ;
137
+ }
Original file line number Diff line number Diff line change @@ -123,6 +123,9 @@ class symbolt
123
123
PRECONDITION (type.id () == ID_code);
124
124
value = exprt (ID_compiled);
125
125
}
126
+
127
+ // / Check that a symbol is well formed.
128
+ bool is_well_formed () const ;
126
129
};
127
130
128
131
std::ostream &operator <<(std::ostream &out, const symbolt &symbol);
Original file line number Diff line number Diff line change @@ -128,6 +128,10 @@ void symbol_tablet::validate(const validation_modet vm) const
128
128
const auto symbol_key = elem.first ;
129
129
const auto &symbol = elem.second ;
130
130
131
+ // First of all, ensure symbol well-formedness
132
+ DATA_CHECK_WITH_DIAGNOSTICS (
133
+ symbol.is_well_formed (), " Symbol is malformed: " , symbol_key);
134
+
131
135
// Check that symbols[id].name == id
132
136
DATA_CHECK_WITH_DIAGNOSTICS (
133
137
symbol.name == symbol_key,
Original file line number Diff line number Diff line change @@ -50,6 +50,7 @@ SRC += analyses/ai/ai.cpp \
50
50
util/string_utils/split_string.cpp \
51
51
util/string_utils/strip_string.cpp \
52
52
util/symbol_table.cpp \
53
+ util/symbol.cpp \
53
54
util/unicode.cpp \
54
55
# Empty last line
55
56
Original file line number Diff line number Diff line change
1
+ // / Author: Diffblue Ltd.
2
+
3
+ // / \file Tests for symbol_tablet
4
+
5
+ #include < testing-utils/catch.hpp>
6
+ #include < util/exception_utils.h>
7
+ #include < util/symbol.h>
8
+
9
+ SCENARIO (
10
+ " Constructed symbol validity checks" ,
11
+ " [core][utils][symbol__validity_checks]" )
12
+ {
13
+ GIVEN (" A valid symbol" )
14
+ {
15
+ symbolt symbol;
16
+ irep_idt symbol_name = " Test_TestBase" ;
17
+ symbol.name = symbol_name;
18
+ symbol.base_name = " TestBase" ;
19
+ symbol.module = " TestModule" ;
20
+ symbol.mode = " C" ;
21
+
22
+ THEN (" Symbol should be well formed" )
23
+ {
24
+ REQUIRE (symbol.is_well_formed ());
25
+ }
26
+ }
27
+
28
+ GIVEN (" An improperly initialised symbol" )
29
+ {
30
+ symbolt symbol;
31
+
32
+ WHEN (" The symbol doesn't have a valid mode" )
33
+ {
34
+ symbol.mode = " " ;
35
+
36
+ THEN (" Symbol well-formedness check should fail" )
37
+ {
38
+ REQUIRE_FALSE (symbol.is_well_formed ());
39
+ }
40
+ }
41
+
42
+ WHEN (" The symbol doesn't have base name as a suffix to name" )
43
+ {
44
+ symbol.name = " TEST" ;
45
+ symbol.base_name = " TestBase" ;
46
+
47
+ THEN (" Symbol well-formedness check should fail" )
48
+ {
49
+ REQUIRE_FALSE (symbol.is_well_formed ());
50
+ }
51
+ }
52
+ }
53
+ }
You can’t perform that action at this time.
0 commit comments