Skip to content

Commit 9cbb841

Browse files
committed
C front-end: use new symbolt constructors
To the extent possible, apply resource-acquisition-is-initialisation. The constructors ensure that at least the most essential fields (name, type, mode) are set.
1 parent 6a6aff1 commit 9cbb841

File tree

7 files changed

+33
-90
lines changed

7 files changed

+33
-90
lines changed

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,10 @@ bool generate_ansi_c_start_function(
255255
namespacet ns(symbol_table);
256256

257257
{
258-
symbolt argc_symbol;
259-
258+
symbolt argc_symbol{"argc'", signed_int_type(), ID_C};
260259
argc_symbol.base_name = "argc'";
261-
argc_symbol.name = "argc'";
262-
argc_symbol.type = signed_int_type();
263260
argc_symbol.is_static_lifetime = true;
264261
argc_symbol.is_lvalue = true;
265-
argc_symbol.mode = ID_C;
266262

267263
auto r = symbol_table.insert(argc_symbol);
268264
if(!r.second && r.first != argc_symbol)
@@ -284,14 +280,10 @@ bool generate_ansi_c_start_function(
284280
const plus_exprt size_expr(argc_symbol.symbol_expr(), one_expr);
285281
const array_typet argv_type(pointer_type(char_type()), size_expr);
286282

287-
symbolt argv_symbol;
288-
283+
symbolt argv_symbol{"argv'", argv_type, ID_C};
289284
argv_symbol.base_name = "argv'";
290-
argv_symbol.name = "argv'";
291-
argv_symbol.type = argv_type;
292285
argv_symbol.is_static_lifetime = true;
293286
argv_symbol.is_lvalue = true;
294-
argv_symbol.mode = ID_C;
295287

296288
auto r = symbol_table.insert(argv_symbol);
297289
if(!r.second && r.first != argv_symbol)
@@ -337,12 +329,9 @@ bool generate_ansi_c_start_function(
337329
if(parameters.size()==3)
338330
{
339331
{
340-
symbolt envp_size_symbol;
332+
symbolt envp_size_symbol{"envp_size'", size_type(), ID_C};
341333
envp_size_symbol.base_name = "envp_size'";
342-
envp_size_symbol.name = "envp_size'";
343-
envp_size_symbol.type = size_type();
344334
envp_size_symbol.is_static_lifetime = true;
345-
envp_size_symbol.mode = ID_C;
346335

347336
if(!symbol_table.insert(std::move(envp_size_symbol)).second)
348337
{
@@ -356,13 +345,13 @@ bool generate_ansi_c_start_function(
356345
const symbolt &envp_size_symbol=ns.lookup("envp_size'");
357346

358347
{
359-
symbolt envp_symbol;
348+
symbolt envp_symbol{
349+
"envp'",
350+
array_typet(
351+
pointer_type(char_type()), envp_size_symbol.symbol_expr()),
352+
ID_C};
360353
envp_symbol.base_name = "envp'";
361-
envp_symbol.name = "envp'";
362-
envp_symbol.type = array_typet(
363-
pointer_type(char_type()), envp_size_symbol.symbol_expr());
364354
envp_symbol.is_static_lifetime = true;
365-
envp_symbol.mode = ID_C;
366355

367356
if(!symbol_table.insert(std::move(envp_symbol)).second)
368357
{
@@ -543,13 +532,10 @@ bool generate_ansi_c_start_function(
543532
}
544533

545534
// add the entry point symbol
546-
symbolt new_symbol;
547-
548-
new_symbol.name=goto_functionst::entry_point();
535+
symbolt new_symbol{
536+
goto_functionst::entry_point(), code_typet{{}, void_type()}, symbol.mode};
549537
new_symbol.base_name = goto_functionst::entry_point();
550-
new_symbol.type = code_typet({}, void_type());
551538
new_symbol.value.swap(init_code);
552-
new_symbol.mode=symbol.mode;
553539

554540
if(!symbol_table.insert(std::move(new_symbol)).second)
555541
{

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,8 @@ void c_typecheck_baset::typecheck_expr_builtin_va_arg(exprt &expr)
547547
code_typet symbol_type=new_type;
548548
symbol_type.return_type()=void_type();
549549

550-
symbolt symbol;
550+
symbolt symbol{ID_gcc_builtin_va_arg, symbol_type, ID_C};
551551
symbol.base_name=ID_gcc_builtin_va_arg;
552-
symbol.name=ID_gcc_builtin_va_arg;
553-
symbol.type=symbol_type;
554-
symbol.mode = ID_C;
555552

556553
symbol_table.insert(std::move(symbol));
557554
}
@@ -2175,13 +2172,10 @@ void c_typecheck_baset::typecheck_side_effect_function_call(
21752172
symbol_table.add(new_symbol);
21762173
}
21772174

2178-
symbolt new_symbol;
2179-
2180-
new_symbol.name = identifier_with_type;
2175+
symbolt new_symbol{
2176+
identifier_with_type, gcc_polymorphic->type(), ID_C};
21812177
new_symbol.base_name = identifier_with_type;
21822178
new_symbol.location = f_op.source_location();
2183-
new_symbol.type = gcc_polymorphic->type();
2184-
new_symbol.mode = ID_C;
21852179
code_blockt implementation =
21862180
instantiate_gcc_polymorphic_builtin(identifier, *gcc_polymorphic);
21872181
typet parent_return_type = return_type;
@@ -2213,12 +2207,10 @@ void c_typecheck_baset::typecheck_side_effect_function_call(
22132207
guessed_return_type = pointer_type(void_type()); // void *
22142208
}
22152209

2216-
symbolt new_symbol;
2217-
2218-
new_symbol.name=identifier;
2210+
symbolt new_symbol{
2211+
identifier, code_typet({}, guessed_return_type), mode};
22192212
new_symbol.base_name=identifier;
22202213
new_symbol.location=expr.source_location();
2221-
new_symbol.type = code_typet({}, guessed_return_type);
22222214
new_symbol.type.set(ID_C_incomplete, true);
22232215

22242216
// TODO: should also guess some argument types

src/ansi-c/c_typecheck_gcc_polymorphic_builtins.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,8 @@ static symbolt result_symbol(
612612
const source_locationt &source_location,
613613
symbol_table_baset &symbol_table)
614614
{
615-
symbolt symbol;
616-
symbol.name = id2string(identifier) + "::1::result";
615+
symbolt symbol{id2string(identifier) + "::1::result", type, ID_C};
617616
symbol.base_name = "result";
618-
symbol.type = type;
619-
symbol.mode = ID_C;
620617
symbol.location = source_location;
621618
symbol.is_file_local = true;
622619
symbol.is_lvalue = true;

src/ansi-c/c_typecheck_initializer.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,13 +1039,11 @@ exprt c_typecheck_baset::do_initializer_list(
10391039

10401040
// mimic bits of typecheck_compound_type to produce a new struct tag
10411041
actual_struct_type.remove(ID_tag);
1042-
type_symbolt compound_symbol{actual_struct_type};
1043-
compound_symbol.mode = mode;
1044-
compound_symbol.location = value.source_location();
1045-
std::string typestr = type2name(compound_symbol.type, *this);
1042+
std::string typestr = type2name(actual_struct_type, *this);
1043+
irep_idt tag_identifier = "tag-#anon#" + typestr;
1044+
type_symbolt compound_symbol{tag_identifier, actual_struct_type, mode};
10461045
compound_symbol.base_name = "#anon#" + typestr;
1047-
compound_symbol.name = "tag-#anon#" + typestr;
1048-
irep_idt tag_identifier = compound_symbol.name;
1046+
compound_symbol.location = value.source_location();
10491047

10501048
// We might already have the same anonymous struct, which is fine as it
10511049
// will be exactly the same type.

src/ansi-c/c_typecheck_type.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,7 @@ void c_typecheck_baset::typecheck_compound_type(struct_union_typet &type)
785785
assert(have_body);
786786

787787
// produce symbol
788-
symbolt compound_symbol;
789-
compound_symbol.is_type=true;
790-
compound_symbol.type=type;
788+
type_symbolt compound_symbol{irep_idt{}, type, mode};
791789
compound_symbol.location=type.source_location();
792790

793791
typecheck_compound_body(to_struct_union_type(compound_symbol.type));
@@ -821,11 +819,8 @@ void c_typecheck_baset::typecheck_compound_type(struct_union_typet &type)
821819
type.remove(ID_tag);
822820
type.set(ID_tag, base_name);
823821

824-
symbolt compound_symbol;
825-
compound_symbol.is_type=true;
826-
compound_symbol.name=identifier;
822+
type_symbolt compound_symbol{identifier, type, mode};
827823
compound_symbol.base_name=base_name;
828-
compound_symbol.type=type;
829824
compound_symbol.location=type.source_location();
830825
compound_symbol.pretty_name=id2string(type.id())+" "+id2string(base_name);
831826

@@ -1368,14 +1363,10 @@ void c_typecheck_baset::typecheck_c_enum_type(typet &type)
13681363
irep_idt identifier=tag.get(ID_identifier);
13691364

13701365
// Put into symbol table
1371-
symbolt enum_tag_symbol;
1372-
1373-
enum_tag_symbol.is_type=true;
1374-
enum_tag_symbol.type=type;
1366+
type_symbolt enum_tag_symbol{identifier, type, mode};
13751367
enum_tag_symbol.location=source_location;
13761368
enum_tag_symbol.is_file_local=true;
13771369
enum_tag_symbol.base_name=base_name;
1378-
enum_tag_symbol.name=identifier;
13791370

13801371
// throw in the enum members as 'body'
13811372
irept::subt &body=enum_tag_symbol.type.add(ID_body).get_sub();
@@ -1478,14 +1469,10 @@ void c_typecheck_baset::typecheck_c_enum_tag_type(c_enum_tag_typet &type)
14781469
new_type.add(ID_tag)=tag;
14791470
new_type.make_incomplete();
14801471

1481-
symbolt enum_tag_symbol;
1482-
1483-
enum_tag_symbol.is_type=true;
1484-
enum_tag_symbol.type=new_type;
1472+
type_symbolt enum_tag_symbol{identifier, new_type, mode};
14851473
enum_tag_symbol.location=source_location;
14861474
enum_tag_symbol.is_file_local=true;
14871475
enum_tag_symbol.base_name=base_name;
1488-
enum_tag_symbol.name=identifier;
14891476

14901477
symbolt *new_symbol;
14911478
move_symbol(enum_tag_symbol, new_symbol);

src/assembler/remove_asm.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,8 @@ void remove_asmt::gcc_asm_function_call(
124124
// do we have it?
125125
if(!symbol_table.has_symbol(function_identifier))
126126
{
127-
symbolt symbol;
128-
129-
symbol.name = function_identifier;
130-
symbol.type = fkt_type;
127+
symbolt symbol{function_identifier, fkt_type, ID_C};
131128
symbol.base_name = function_base_name;
132-
symbol.value = nil_exprt();
133-
symbol.mode = ID_C;
134129

135130
symbol_table.add(symbol);
136131

@@ -182,13 +177,8 @@ void remove_asmt::msc_asm_function_call(
182177
// do we have it?
183178
if(!symbol_table.has_symbol(function_identifier))
184179
{
185-
symbolt symbol;
186-
187-
symbol.name = function_identifier;
188-
symbol.type = fkt_type;
180+
symbolt symbol{function_identifier, fkt_type, ID_C};
189181
symbol.base_name = function_base_name;
190-
symbol.value = nil_exprt();
191-
symbol.mode = ID_C;
192182

193183
symbol_table.add(symbol);
194184

src/goto-cc/linker_script_merge.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,10 @@ int linker_script_merget::ls_data2instructions(
461461
CHECK_RETURN(zi.has_value());
462462

463463
// Add the array to the symbol table.
464-
symbolt array_sym;
464+
symbolt array_sym{array_name.str(), array_type, ID_C};
465465
array_sym.is_static_lifetime = true;
466466
array_sym.is_lvalue = true;
467467
array_sym.is_state_var = true;
468-
array_sym.name = array_name.str();
469-
array_sym.type = array_type;
470468
array_sym.value = *zi;
471469
array_sym.location = array_loc;
472470

@@ -478,12 +476,10 @@ int linker_script_merget::ls_data2instructions(
478476
address_of_exprt array_start(zero_idx);
479477

480478
// Linker-defined symbol_exprt pointing to start address
481-
symbolt start_sym;
479+
symbolt start_sym{d["start-symbol"].value, pointer_type(char_type()), ID_C};
482480
start_sym.is_static_lifetime = true;
483481
start_sym.is_lvalue = true;
484482
start_sym.is_state_var = true;
485-
start_sym.name = d["start-symbol"].value;
486-
start_sym.type = pointer_type(char_type());
487483
start_sym.value = array_start;
488484
linker_values.emplace(
489485
d["start-symbol"].value,
@@ -521,12 +517,10 @@ int linker_script_merget::ls_data2instructions(
521517
{
522518
plus_exprt array_end(array_start, array_size_expr);
523519

524-
symbolt end_sym;
520+
symbolt end_sym{d["end-symbol"].value, pointer_type(char_type()), ID_C};
525521
end_sym.is_static_lifetime = true;
526522
end_sym.is_lvalue = true;
527523
end_sym.is_state_var = true;
528-
end_sym.name = d["end-symbol"].value;
529-
end_sym.type = pointer_type(char_type());
530524
end_sym.value = array_end;
531525
linker_values.emplace(
532526
d["end-symbol"].value,
@@ -639,12 +633,11 @@ int linker_script_merget::ls_data2instructions(
639633
640634
if(!symbol_table.has_symbol(CPROVER_PREFIX "allocated_memory"))
641635
{
642-
symbolt sym;
643-
sym.name=CPROVER_PREFIX "allocated_memory";
644-
sym.pretty_name=CPROVER_PREFIX "allocated_memory";
636+
symbolt sym{
637+
CPROVER_PREFIX "allocated_memory",
638+
code_typet({}, empty_typet()),
639+
ID_C} sym.pretty_name = CPROVER_PREFIX "allocated_memory";
645640
sym.is_lvalue=sym.is_static_lifetime=true;
646-
const code_typet void_t({}, empty_typet());
647-
sym.type=void_t;
648641
symbol_table.add(sym);
649642
}
650643

0 commit comments

Comments
 (0)