File tree Expand file tree Collapse file tree 6 files changed +87
-1
lines changed Expand file tree Collapse file tree 6 files changed +87
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ Author: Daniel Kroening, kroening@kroening.com
12
12
13
13
#include " source_location.h"
14
14
#include " std_expr.h"
15
+ #include " suffix.h"
15
16
16
17
// / Dump the state of a symbol object to a given output stream.
17
18
// / \param out: The stream to output object state to.
@@ -121,3 +122,25 @@ symbol_exprt symbolt::symbol_expr() const
121
122
{
122
123
return symbol_exprt (name, type);
123
124
}
125
+
126
+ // / Check that the instance object is well formed.
127
+ // / \return: true if well-formed; false otherwise.
128
+ bool symbolt::is_well_formed () const
129
+ {
130
+ // Well-formedness criterion number 1 is for a symbol
131
+ // to have a non-empty mode (see #1880)
132
+ if (mode.empty ())
133
+ {
134
+ return false ;
135
+ }
136
+
137
+ // Well-formedness criterion number 2 is for a symbol
138
+ // to have it's base name as a suffix to it's more
139
+ // general name.
140
+ if (!has_suffix (id2string (name), id2string (base_name)))
141
+ {
142
+ return false ;
143
+ }
144
+
145
+ return true ;
146
+ }
Original file line number Diff line number Diff line change @@ -121,6 +121,9 @@ class symbolt
121
121
PRECONDITION (type.id () == ID_code);
122
122
value = exprt (ID_compiled);
123
123
}
124
+
125
+ // / Check that a symbol is well formed.
126
+ bool is_well_formed () const ;
124
127
};
125
128
126
129
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
vm,
Original file line number Diff line number Diff line change @@ -63,6 +63,7 @@ SRC += analyses/ai/ai.cpp \
63
63
util/string_utils/split_string.cpp \
64
64
util/string_utils/strip_string.cpp \
65
65
util/symbol_table.cpp \
66
+ util/symbol.cpp \
66
67
util/unicode.cpp \
67
68
# Empty last line
68
69
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
+ symbol.mode = " C" ;
47
+
48
+ THEN (" Symbol well-formedness check should fail" )
49
+ {
50
+ REQUIRE_FALSE (symbol.is_well_formed ());
51
+ }
52
+ }
53
+ }
54
+ }
Original file line number Diff line number Diff line change @@ -35,10 +35,11 @@ SCENARIO(
35
35
symbol_tablet symbol_table;
36
36
37
37
symbolt symbol;
38
- irep_idt symbol_name = " Test " ;
38
+ irep_idt symbol_name = " Test_TestBase " ;
39
39
symbol.name = symbol_name;
40
40
symbol.base_name = " TestBase" ;
41
41
symbol.module = " TestModule" ;
42
+ symbol.mode = " C" ;
42
43
43
44
symbol_table.insert (symbol);
44
45
You can’t perform that action at this time.
0 commit comments