File: symbol_table.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (452 lines) | stat: -rw-r--r-- 13,865 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/// Author: Diffblue Ltd.

/// \file Tests for symbol_tablet

#include <util/exception_utils.h> // IWYU pragma: keep
#include <util/journalling_symbol_table.h>
#include <util/symbol_table.h>

#include <testing-utils/invariant.h>
#include <testing-utils/use_catch.h>

TEST_CASE("Iterating through a symbol table", "[core][utils][symbol_tablet]")
{
  symbol_tablet symbol_table;

  symbolt symbol;
  irep_idt symbol_name = "Test";
  symbol.name = symbol_name;

  symbol_table.insert(symbol);

  SECTION("Non const iterator")
  {
    int counter = 0;
    for(auto &entry : symbol_table)
    {
      (void)entry; // we are just testing iteration here
      ++counter;
    }
    REQUIRE(counter == 1);
  }
  SECTION("Const iterator")
  {
    int counter = 0;
    for(const auto &entry : symbol_table)
    {
      (void)entry; // we are just testing iteration here
      ++counter;
    }
    REQUIRE(counter == 1);
  }
  SECTION("Polymorphic access")
  {
    SECTION("Non-const iterator")
    {
      symbol_table_baset &base_st = symbol_table;
      REQUIRE(std::distance(base_st.begin(), base_st.end()) == 1);
      int counter = 0;
      for(auto &entry : base_st)
      {
        (void)entry; // we are just testing iteration here
        ++counter;
      }
      REQUIRE(counter == 1);
    }
    SECTION("Const iterator")
    {
      const symbol_table_baset &base_st = symbol_table;
      REQUIRE(std::distance(base_st.begin(), base_st.end()) == 1);
      int counter = 0;
      for(const auto &entry : base_st)
      {
        (void)entry; // we are just testing iteration here
        ++counter;
      }
      REQUIRE(counter == 1);
    }
  }
}

SCENARIO(
  "symbol_table_validity_checks",
  "[core][utils][symbol_table_validity_checks]")
{
  GIVEN("A valid symbol table")
  {
    symbol_tablet symbol_table;

    irep_idt symbol_name = "Test_TestBase";
    symbolt symbol{symbol_name, typet{}, ID_C};
    symbol.base_name = "TestBase";
    symbol.module = "TestModule";

    symbol_table.insert(symbol);

    THEN("validate() should return")
    {
      symbol_table.validate(validation_modet::EXCEPTION);
    }
    WHEN("A symbol name is transformed without updating the symbol table")
    {
      symbolt &transformed_symbol = symbol_table.get_writeable_ref(symbol_name);
      transformed_symbol.name = "TransformTest";

      THEN("validate() should throw an exception")
      {
        REQUIRE_THROWS_AS(
          symbol_table.validate(validation_modet::EXCEPTION),
          incorrect_goto_program_exceptiont);
      }
    }
    WHEN(
      "A symbol base_name is transformed without updating the base_name "
      "mapping")
    {
      symbolt &transformed_symbol = symbol_table.get_writeable_ref(symbol_name);
      // Transform the symbol
      transformed_symbol.base_name = "TransformTestBase";

      THEN("validate() should throw an exception")
      {
        REQUIRE_THROWS_AS(
          symbol_table.validate(validation_modet::EXCEPTION),
          incorrect_goto_program_exceptiont);
      }
    }
    WHEN(
      "A symbol module identifier is transformed without updating the module "
      "mapping")
    {
      symbolt &transformed_symbol = symbol_table.get_writeable_ref(symbol_name);
      transformed_symbol.module = "TransformTestModule";

      THEN("validate() should throw an exception")
      {
        REQUIRE_THROWS_AS(
          symbol_table.validate(validation_modet::EXCEPTION),
          incorrect_goto_program_exceptiont);
      }
    }
  }
}

SCENARIO(
  "symbol_table_symbol_module_map",
  "[core][utils][symbol_table_symbol_module_map]")
{
  GIVEN("A valid symbol table")
  {
    symbol_tablet symbol_table;
    WHEN("Inserting a symbol with non-empty module")
    {
      symbolt symbol;
      symbol.name = "TestName";
      symbol.module = "TestModule";
      symbol_table.insert(std::move(symbol));
      THEN("The symbol module map contains an entry for the symbol")
      {
        REQUIRE(symbol_table.symbol_module_map.size() == 1);
        const auto entry = symbol_table.symbol_module_map.begin();
        REQUIRE(id2string(entry->first) == "TestModule");
        REQUIRE(id2string(entry->second) == "TestName");
      }
      WHEN("Removing the symbol again")
      {
        symbol_table.remove("TestName");
        THEN("The symbol module map no longer contains an entry for the symbol")
        {
          REQUIRE(symbol_table.symbol_module_map.size() == 0);
        }
      }
    }
    WHEN("Inserting a symbol with empty module")
    {
      symbolt symbol;
      symbol.name = "TestName";
      symbol_table.insert(std::move(symbol));
      THEN("The symbol module map does not contain an entry for the symbol")
      {
        REQUIRE(symbol_table.symbol_module_map.size() == 0);
      }
      WHEN("Removing the symbol again")
      {
        symbol_table.remove("TestName");
        THEN(
          "The symbol module map still does not contain an entry for the "
          "symbol")
        {
          REQUIRE(symbol_table.symbol_module_map.size() == 0);
        }
      }
    }
  }
}

SCENARIO("journalling_symbol_table_writer",
  "[core][utils][journalling_symbol_table_writer]")
{
  GIVEN("A journalling_symbol_table_writert wrapping an empty symbol_tablet")
  {
    symbol_tablet base_symbol_table;
    journalling_symbol_tablet symbol_table =
      journalling_symbol_tablet::wrap(base_symbol_table);
    const symbol_tablet &read_symbol_table=symbol_table;

    irep_idt symbol_name="Test";

    WHEN("A symbol is inserted into the symbol table")
    {
      symbolt symbol;
      symbol.name = symbol_name;
      auto result=symbol_table.insert(symbol);
      THEN("The insert should succeed")
      {
        REQUIRE(result.second);
      }
      THEN("The inserted symbol should have the same name")
      {
        REQUIRE(result.first.name==symbol_name);
      }
      THEN(
        "The symbol table should return a symbol from a lookup of that name")
      {
        REQUIRE(read_symbol_table.lookup(symbol_name)!=nullptr);
      }
      THEN("The symbol table should return the same symbol from a lookup")
      {
        REQUIRE(&result.first==read_symbol_table.lookup(symbol_name));
      }
      THEN("The added symbol should appear in the updated collection")
      {
        REQUIRE(symbol_table.get_updated().count(symbol_name)==1);
      }
      THEN("The added symbol should not appear in the removed collection")
      {
        REQUIRE(symbol_table.get_removed().count(symbol_name)==0);
      }
      WHEN("Adding the same symbol again")
      {
        const auto result_re_insert = symbol_table.insert(symbol);
        THEN("The insert should fail")
        {
          REQUIRE(!result_re_insert.second);
        }
      }
    }
    WHEN("Moving a symbol into the symbol table")
    {
      symbolt symbol;
      symbol.name = symbol_name;
      symbolt *symbol_in_table;
      auto result=symbol_table.move(symbol, symbol_in_table);
      THEN("The move should succeed")
      {
        REQUIRE(!result);
      }
      THEN(
        "The symbol table should return a symbol from a lookup of that name")
      {
        REQUIRE(read_symbol_table.lookup(symbol_name)!=nullptr);
      }
      THEN("The symbol table should return the same symbol from a lookup")
      {
        REQUIRE(symbol_in_table==read_symbol_table.lookup(symbol_name));
      }
      THEN("The added symbol should appear in the updated collection")
      {
        REQUIRE(symbol_table.get_updated().count(symbol_name)==1);
      }
      THEN("The added symbol should not appear in the removed collection")
      {
        REQUIRE(symbol_table.get_removed().count(symbol_name)==0);
      }
      WHEN("Moving the same symbol again")
      {
        symbolt *symbol_in_table2;
        const auto result_move = symbol_table.move(symbol, symbol_in_table2);
        THEN("The move should fail")
        {
          REQUIRE(result_move);
        }
        THEN("The returned pointer should match the previous move result")
        {
          REQUIRE(symbol_in_table==symbol_in_table2);
        }
      }
    }
    WHEN("Adding a symbol to the symbol table")
    {
      symbolt symbol;
      symbol.name = symbol_name;
      auto result=symbol_table.add(symbol);
      THEN("The add should succeed")
      {
        REQUIRE(!result);
      }
      THEN(
        "The symbol table should return a symbol from a lookup of that name")
      {
        REQUIRE(read_symbol_table.lookup(symbol_name)!=nullptr);
      }
      THEN("The symbol table should return the same symbol from a lookup")
      {
        REQUIRE(symbol.name==read_symbol_table.lookup(symbol_name)->name);
      }
      THEN("The added symbol should appear in the updated collection")
      {
        REQUIRE(symbol_table.get_updated().count(symbol_name)==1);
      }
      THEN("The added symbol should not appear in the removed collection")
      {
        REQUIRE(symbol_table.get_removed().count(symbol_name)==0);
      }
      WHEN("Adding the same symbol again")
      {
        auto result_add_2 = symbol_table.add(symbol);
        THEN("The insert should fail")
        {
          REQUIRE(result_add_2);
        }
      }
    }
    WHEN("Updating an existing symbol")
    {
      symbolt symbol;
      symbol.name = symbol_name;
      base_symbol_table.add(symbol);
      symbolt *writeable=symbol_table.get_writeable(symbol_name);

      THEN("get_writeable should succeed")
      {
        REQUIRE(writeable!=nullptr);
      }
      THEN("get_writeable should return the same pointer "
           "as the underlying table")
      {
        symbolt *writeable2=base_symbol_table.get_writeable(symbol_name);
        REQUIRE(writeable==writeable2);
      }
      THEN("get_writeable_ref should not throw")
      {
        symbol_table.get_writeable_ref(symbol_name);
      }
      THEN("The updated symbol should appear in the updated collection")
      {
        REQUIRE(symbol_table.get_updated().count(symbol_name)==1);
      }
      THEN("The updated symbol should not appear in the removed collection")
      {
        REQUIRE(symbol_table.get_removed().count(symbol_name)==0);
      }
    }
    WHEN("Removing a non-existent symbol")
    {
      irep_idt no_such_symbol_name = "DoesNotExist";
      bool ret = symbol_table.remove(no_such_symbol_name);
      THEN("The remove operation should fail")
      {
        REQUIRE(ret);
      }
      THEN("The symbol we failed to remove should appear in neither journal")
      {
        REQUIRE(symbol_table.get_updated().count(no_such_symbol_name) == 0);
        REQUIRE(symbol_table.get_removed().count(no_such_symbol_name) == 0);
      }
    }
    WHEN("Removing an existing symbol added via the journalling writer")
    {
      symbolt symbol;
      symbol.name = symbol_name;
      symbol_table.add(symbol);
      bool ret=symbol_table.remove(symbol_name);
      THEN("The remove operation should succeed")
      {
        REQUIRE(!ret);
      }
      THEN("The symbol should appear in neither journal")
      {
        REQUIRE(symbol_table.get_updated().count(symbol_name)==0);
        REQUIRE(symbol_table.get_removed().count(symbol_name)==0);
      }
    }
    WHEN("Removing an existing symbol added outside the journalling writer")
    {
      symbolt symbol;
      symbol.name = symbol_name;
      base_symbol_table.add(symbol);
      bool ret=symbol_table.remove(symbol_name);
      THEN("The remove operation should succeed")
      {
        REQUIRE(!ret);
      }
      THEN("The symbol we removed should be journalled as removed "
           "but not updated")
      {
        REQUIRE(symbol_table.get_updated().count(symbol_name)==0);
        REQUIRE(symbol_table.get_removed().count(symbol_name)==1);
      }
    }
    WHEN("Removing an existing symbol using an iterator (added via writer)")
    {
      symbolt symbol;
      symbol.name = symbol_name;
      symbol_table.add(symbol);
      auto erase_iterator=read_symbol_table.symbols.find(symbol_name);
      symbol_table.erase(erase_iterator);
      THEN("The symbol should appear in neither journal")
      {
        REQUIRE(symbol_table.get_updated().count(symbol_name)==0);
        REQUIRE(symbol_table.get_removed().count(symbol_name)==0);
      }
    }
    WHEN("Removing an existing symbol using an iterator (added via base)")
    {
      symbolt symbol;
      symbol.name = symbol_name;
      base_symbol_table.add(symbol);
      auto erase_iterator=read_symbol_table.symbols.find(symbol_name);
      symbol_table.erase(erase_iterator);
      THEN("The symbol should be journalled as removed")
      {
        REQUIRE(symbol_table.get_updated().count(symbol_name)==0);
        REQUIRE(symbol_table.get_removed().count(symbol_name)==1);
      }
    }
    WHEN("Re-adding a symbol previously removed")
    {
      symbolt symbol;
      symbol.name = symbol_name;
      const auto result_add_1 = symbol_table.add(symbol);
      symbol_table.remove(symbol.name);
      const auto result_add_2 = symbol_table.add(symbol);
      THEN("The first add should succeed")
      {
        REQUIRE(!result_add_1);
      }
      THEN("The second add should succeed")
      {
        REQUIRE(!result_add_2);
      }
      THEN("The symbol should be journalled as updated but not removed")
      {
        REQUIRE(symbol_table.get_updated().count(symbol_name)==1);
        REQUIRE(symbol_table.get_removed().count(symbol_name)==0);
      }
    }
  }
}

TEST_CASE("symbol_tablet::lookup_ref invariant", "[core][utils][symbol_tablet]")
{
  symbolt foo_symbol;
  foo_symbol.name = "foo";
  symbol_tablet symbol_table;
  symbol_table.insert(foo_symbol);
  const cbmc_invariants_should_throwt invariants_throw;
  REQUIRE_NOTHROW(symbol_table.lookup_ref("foo"));
  REQUIRE_THROWS_MATCHES(
    symbol_table.lookup_ref("bar"),
    invariant_failedt,
    invariant_failure_containing("`bar' must exist in the symbol table."));
}