Skip to content

Commit 8f1c0d5

Browse files
committed
goto-cc: also use the linker when processing multiple source files
The linker is able to do some type sanitisation across translation units, and there is no reason behaviour should be different whether we link previously compiled object files or a set of source files.
1 parent 507523c commit 8f1c0d5

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

regression/ansi-c/linking_conflicts1/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ main.c
33
other.c
44
^EXIT=(64|1)$
55
^SIGNAL=0$
6-
^CONVERSION ERROR$
76
error: conflicting function declarations `bar'
87
error: conflicting function declarations `bar2'
98
--

src/goto-cc/compile.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Date: June 2006
3434

3535
#include <goto-programs/goto_convert.h>
3636
#include <goto-programs/goto_convert_functions.h>
37+
#include <goto-programs/link_goto_model.h>
3738
#include <goto-programs/read_goto_binary.h>
3839
#include <goto-programs/validate_goto_model.h>
3940
#include <goto-programs/write_goto_binary.h>
@@ -360,9 +361,9 @@ bool compilet::compile()
360361
if(echo_file_name)
361362
std::cout << get_base_name(file_name, false) << '\n' << std::flush;
362363

363-
bool r=parse_source(file_name); // don't break the program!
364+
auto file_goto_model = parse_source(file_name);
364365

365-
if(r)
366+
if(!file_goto_model.has_value())
366367
{
367368
const std::string &debug_outfile=
368369
cmdline.get_value("print-rejected-preprocessed-source");
@@ -382,7 +383,7 @@ bool compilet::compile()
382383
// output an object file for every source file
383384

384385
// "compile" functions
385-
convert_symbols(goto_model.goto_functions);
386+
convert_symbols(file_goto_model->goto_functions);
386387

387388
std::string cfn;
388389

@@ -399,13 +400,22 @@ bool compilet::compile()
399400
else
400401
cfn = output_file_object;
401402

402-
if(write_bin_object_file(cfn, goto_model))
403+
if(write_bin_object_file(cfn, *file_goto_model))
403404
return true;
404405

405-
if(add_written_cprover_symbols(goto_model.symbol_table))
406+
if(add_written_cprover_symbols(file_goto_model->symbol_table))
406407
return true;
407-
408-
goto_model.clear(); // clean symbol table for next source file.
408+
}
409+
else
410+
{
411+
try
412+
{
413+
link_goto_model(goto_model, *file_goto_model, get_message_handler());
414+
}
415+
catch(...)
416+
{
417+
return true;
418+
}
409419
}
410420
}
411421

@@ -580,30 +590,31 @@ bool compilet::write_bin_object_file(
580590
return false;
581591
}
582592

583-
/// parses a source file
584-
/// \return true on error, false otherwise
585-
bool compilet::parse_source(const std::string &file_name)
593+
/// Parses and type checks a source file located at \p file_name.
594+
/// \return A goto model if, and only if, parsing and type checking succeeded.
595+
optionalt<goto_modelt> compilet::parse_source(const std::string &file_name)
586596
{
587597
language_filest language_files;
588598
language_files.set_message_handler(get_message_handler());
589599

590600
if(parse(file_name, language_files))
591-
return true;
601+
return {};
592602

593603
// we just typecheck one file here
594-
if(language_files.typecheck(goto_model.symbol_table))
604+
goto_modelt file_goto_model;
605+
if(language_files.typecheck(file_goto_model.symbol_table))
595606
{
596607
error() << "CONVERSION ERROR" << eom;
597-
return true;
608+
return {};
598609
}
599610

600-
if(language_files.final(goto_model.symbol_table))
611+
if(language_files.final(file_goto_model.symbol_table))
601612
{
602613
error() << "CONVERSION ERROR" << eom;
603-
return true;
614+
return {};
604615
}
605616

606-
return false;
617+
return std::move(file_goto_model);
607618
}
608619

609620
/// constructor

src/goto-cc/compile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class compilet : public messaget
7171
bool compile();
7272
bool link();
7373

74-
bool parse_source(const std::string &);
74+
optionalt<goto_modelt> parse_source(const std::string &);
7575

7676
bool write_bin_object_file(const std::string &, const goto_modelt &);
7777

0 commit comments

Comments
 (0)