Skip to content

Commit d8b4679

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 b3cc3e9 commit d8b4679

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/write_goto_binary.h>
3940

@@ -359,9 +360,9 @@ bool compilet::compile()
359360
if(echo_file_name)
360361
std::cout << get_base_name(file_name, false) << '\n' << std::flush;
361362

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

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

383384
// "compile" functions
384-
convert_symbols(goto_model.goto_functions);
385+
convert_symbols(file_goto_model->goto_functions);
385386

386387
std::string cfn;
387388

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

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

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

@@ -579,30 +589,31 @@ bool compilet::write_bin_object_file(
579589
return false;
580590
}
581591

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

589599
if(parse(file_name, language_files))
590-
return true;
600+
return {};
591601

592602
// we just typecheck one file here
593-
if(language_files.typecheck(goto_model.symbol_table))
603+
goto_modelt file_goto_model;
604+
if(language_files.typecheck(file_goto_model.symbol_table))
594605
{
595606
error() << "CONVERSION ERROR" << eom;
596-
return true;
607+
return {};
597608
}
598609

599-
if(language_files.final(goto_model.symbol_table))
610+
if(language_files.final(file_goto_model.symbol_table))
600611
{
601612
error() << "CONVERSION ERROR" << eom;
602-
return true;
613+
return {};
603614
}
604615

605-
return false;
616+
return std::move(file_goto_model);
606617
}
607618

608619
/// 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)