Skip to content

Commit 6754dbe

Browse files
committed
Add well-formedness checks for symbols.
Add a method that checks a symbol for structural validity according to some predetermined rule, and add it as an extra rule for symbol-table validity that each symbol is well-formed.
1 parent a622be9 commit 6754dbe

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

src/util/symbol.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include "source_location.h"
1414
#include "std_expr.h"
15+
#include "suffix.h"
1516

1617
void symbolt::show(std::ostream &out) const
1718
{
@@ -112,3 +113,25 @@ symbol_exprt symbolt::symbol_expr() const
112113
{
113114
return symbol_exprt(name, type);
114115
}
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+
}

src/util/symbol.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class symbolt
123123
PRECONDITION(type.id() == ID_code);
124124
value = exprt(ID_compiled);
125125
}
126+
127+
/// Check that a symbol is well formed.
128+
bool is_well_formed() const;
126129
};
127130

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

src/util/symbol_table.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ void symbol_tablet::validate(const validation_modet vm) const
128128
const auto symbol_key = elem.first;
129129
const auto &symbol = elem.second;
130130

131+
// First of all, ensure symbol well-formedness
132+
DATA_CHECK_WITH_DIAGNOSTICS(
133+
symbol.is_well_formed(), "Symbol is malformed: ", symbol_key);
134+
131135
// Check that symbols[id].name == id
132136
DATA_CHECK_WITH_DIAGNOSTICS(
133137
symbol.name == symbol_key,

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ SRC += analyses/ai/ai.cpp \
5050
util/string_utils/split_string.cpp \
5151
util/string_utils/strip_string.cpp \
5252
util/symbol_table.cpp \
53+
util/symbol.cpp \
5354
util/unicode.cpp \
5455
# Empty last line
5556

unit/util/symbol.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

0 commit comments

Comments
 (0)