diff --git a/regression/goto-cc-cbmc/cxx-suffix/main.cxx b/regression/goto-cc-cbmc/cxx-suffix/main.cxx new file mode 100644 index 000000000000..27bd7628443a --- /dev/null +++ b/regression/goto-cc-cbmc/cxx-suffix/main.cxx @@ -0,0 +1,22 @@ +// GCC, Clang and Visual Studio compile a file named *.cxx as C++ +class counter +{ +public: + int value; + counter() : value(0) + { + } + void increment(int &by) + { + value += by; + } +}; + +int main() +{ + counter c; + int one = 1; + c.increment(one); + __CPROVER_assert(c.value == 1, "C++ code was compiled"); + return 0; +} diff --git a/regression/goto-cc-cbmc/cxx-suffix/test.desc b/regression/goto-cc-cbmc/cxx-suffix/test.desc new file mode 100644 index 000000000000..6f5a4c9e07c7 --- /dev/null +++ b/regression/goto-cc-cbmc/cxx-suffix/test.desc @@ -0,0 +1,12 @@ +CORE +main.cxx + +^EXIT=0$ +^SIGNAL=0$ +^\[main\.assertion\.1\] .* C\+\+ code was compiled: SUCCESS$ +^VERIFICATION SUCCESSFUL$ +-- +^warning: ignoring +-- +A file named *.cxx is a C++ source file (GCC manual, "Options Controlling the +Kind of Output"), which goto-cc used to ignore. diff --git a/regression/goto-gcc/Makefile b/regression/goto-gcc/Makefile index abb1742548ab..46d38a4fcf98 100644 --- a/regression/goto-gcc/Makefile +++ b/regression/goto-gcc/Makefile @@ -38,5 +38,6 @@ endif clean: find . -name '*.out' -execdir $(RM) '{}' \; find . -name '*.gb' -execdir $(RM) '{}' \; + find . -name '*.o' -execdir $(RM) '{}' \; find . -name '*.goto-cc-saved' -execdir $(RM) '{}' \; - $(RM) tests.log archives/libour_archive.a archives/foo.o + $(RM) tests.log archives/libour_archive.a diff --git a/regression/goto-gcc/compile_without_o_preprocessed/cxx.desc b/regression/goto-gcc/compile_without_o_preprocessed/cxx.desc new file mode 100644 index 000000000000..0c808828bc4e --- /dev/null +++ b/regression/goto-gcc/compile_without_o_preprocessed/cxx.desc @@ -0,0 +1,10 @@ +CORE +other.ii +--verbosity 10 -c +^EXIT=0$ +^SIGNAL=0$ +^merging other\.o and other\.o\.goto-cc-saved +-- +^warning: ignoring +-- +As test.desc, for preprocessed C++. diff --git a/regression/goto-gcc/compile_without_o_preprocessed/main.i b/regression/goto-gcc/compile_without_o_preprocessed/main.i new file mode 100644 index 000000000000..7ed2d8666c31 --- /dev/null +++ b/regression/goto-gcc/compile_without_o_preprocessed/main.i @@ -0,0 +1,5 @@ +// preprocessed C +int f(int x) +{ + return x + 1; +} diff --git a/regression/goto-gcc/compile_without_o_preprocessed/other.ii b/regression/goto-gcc/compile_without_o_preprocessed/other.ii new file mode 100644 index 000000000000..796dd73edf33 --- /dev/null +++ b/regression/goto-gcc/compile_without_o_preprocessed/other.ii @@ -0,0 +1,5 @@ +// preprocessed C++ +int g(int &x) +{ + return x + 1; +} diff --git a/regression/goto-gcc/compile_without_o_preprocessed/test.desc b/regression/goto-gcc/compile_without_o_preprocessed/test.desc new file mode 100644 index 000000000000..3c63aa076ae1 --- /dev/null +++ b/regression/goto-gcc/compile_without_o_preprocessed/test.desc @@ -0,0 +1,12 @@ +CORE +main.i +--verbosity 10 -c +^EXIT=0$ +^SIGNAL=0$ +^merging main\.o and main\.o\.goto-cc-saved +-- +^warning: ignoring +-- +Without -o, gcc -c writes main.o for main.i. goto-gcc compiled main.i, but +expected an output file only for source files that need preprocessing, so the +native object file replaced the goto binary. diff --git a/regression/goto-gcc/compile_without_o_same_base_name/p/x.c b/regression/goto-gcc/compile_without_o_same_base_name/p/x.c new file mode 100644 index 000000000000..383f78155f9a --- /dev/null +++ b/regression/goto-gcc/compile_without_o_same_base_name/p/x.c @@ -0,0 +1,4 @@ +int p(void) +{ + return 1; +} diff --git a/regression/goto-gcc/compile_without_o_same_base_name/q/x.c b/regression/goto-gcc/compile_without_o_same_base_name/q/x.c new file mode 100644 index 000000000000..fac17b649aa6 --- /dev/null +++ b/regression/goto-gcc/compile_without_o_same_base_name/q/x.c @@ -0,0 +1,4 @@ +int q(void) +{ + return 2; +} diff --git a/regression/goto-gcc/compile_without_o_same_base_name/test.desc b/regression/goto-gcc/compile_without_o_same_base_name/test.desc new file mode 100644 index 000000000000..10dd4e67bd83 --- /dev/null +++ b/regression/goto-gcc/compile_without_o_same_base_name/test.desc @@ -0,0 +1,12 @@ +CORE +q/x.c +--verbosity 10 -c p/x.c +^EXIT=0$ +^SIGNAL=0$ +^merging x\.o and x\.o\.goto-cc-saved +-- +^warning: ignoring +^Rename failed +-- +gcc -c p/x.c q/x.c writes x.o twice; the second one is kept. goto-gcc expected +x.o twice, and failed when it tried to move it aside for the second time. diff --git a/regression/goto-gcc/compile_without_o_x_language/main.src b/regression/goto-gcc/compile_without_o_x_language/main.src new file mode 100644 index 000000000000..5e7f640696d4 --- /dev/null +++ b/regression/goto-gcc/compile_without_o_x_language/main.src @@ -0,0 +1,5 @@ +// C source in a file without a C suffix, compiled with -x c +int f(int x) +{ + return x + 1; +} diff --git a/regression/goto-gcc/compile_without_o_x_language/test.desc b/regression/goto-gcc/compile_without_o_x_language/test.desc new file mode 100644 index 000000000000..0e98e85cb210 --- /dev/null +++ b/regression/goto-gcc/compile_without_o_x_language/test.desc @@ -0,0 +1,12 @@ +CORE +main.src +--verbosity 10 -c -x c +^EXIT=0$ +^SIGNAL=0$ +^merging main\.o and main\.o\.goto-cc-saved +-- +^warning: ignoring +-- +With -x c, gcc -c compiles main.src as C and writes main.o. goto-gcc compiled +main.src, but expected an output file only for files whose suffix implies a +language, so the native object file replaced the goto binary. diff --git a/regression/goto-gcc/cxx_suffix/main.cxx b/regression/goto-gcc/cxx_suffix/main.cxx new file mode 100644 index 000000000000..ca74c685b440 --- /dev/null +++ b/regression/goto-gcc/cxx_suffix/main.cxx @@ -0,0 +1,5 @@ +// gcc compiles a file named *.cxx as C++ +int f(int &x) +{ + return x + 1; +} diff --git a/regression/goto-gcc/cxx_suffix/test.desc b/regression/goto-gcc/cxx_suffix/test.desc new file mode 100644 index 000000000000..ecc89235bf57 --- /dev/null +++ b/regression/goto-gcc/cxx_suffix/test.desc @@ -0,0 +1,13 @@ +CORE +main.cxx +--verbosity 10 -c -o main.gb +^EXIT=0$ +^SIGNAL=0$ +^merging main\.gb and main\.gb\.goto-cc-saved +-- +^warning: ignoring +^unknown file type +-- +A file named *.cxx is a C++ source file (GCC manual, "Options Controlling the +Kind of Output"). goto-gcc used to ignore it, so that main.gb was a native +object file without a goto-cc section. diff --git a/src/goto-cc/compile.cpp b/src/goto-cc/compile.cpp index 0a49ec521ab9..42887e4b0709 100644 --- a/src/goto-cc/compile.cpp +++ b/src/goto-cc/compile.cpp @@ -138,9 +138,9 @@ static file_typet detect_file_type( r == std::string::npos ? "" : file_name.substr(r + 1, file_name.length()); if( - ext == "c" || ext == "cc" || ext == "cp" || ext == "cpp" || ext == "CPP" || - ext == "c++" || ext == "C" || ext == "i" || ext == "ii" || ext == "class" || - ext == "jar") + ext == "c" || ext == "cc" || ext == "cp" || ext == "cxx" || ext == "cpp" || + ext == "CPP" || ext == "c++" || ext == "C" || ext == "i" || ext == "ii" || + ext == "class" || ext == "jar") { return file_typet::SOURCE_FILE; } @@ -437,6 +437,8 @@ std::optional compilet::compile() if(write_bin_object_file(cfn, file_goto_model)) return {}; + written_object_files.push_back(cfn); + if(add_written_cprover_symbols(file_goto_model.symbol_table)) return {}; } diff --git a/src/goto-cc/compile.h b/src/goto-cc/compile.h index 1e933d016067..e0e1f2144004 100644 --- a/src/goto-cc/compile.h +++ b/src/goto-cc/compile.h @@ -86,6 +86,13 @@ class compilet /// \brief Has this compiler written any object files? bool wrote_object_files() const { return wrote_object; } + /// \brief Object files written for the source files in mode COMPILE_ONLY or + /// ASSEMBLE_ONLY, in the order of the source files + const std::list &compiled_object_files() const + { + return written_object_files; + } + /// \brief `__CPROVER_...` macros written to object files and their arities /// /// \param [out] cprover_macros: A mapping from every `__CPROVER` macro that @@ -150,6 +157,9 @@ class compilet // from the case where the user forgot to write an object file before // calling add_written_cprover_symbols(). bool wrote_object; + + /// \brief See \ref compiled_object_files + std::list written_object_files; }; #endif // CPROVER_GOTO_CC_COMPILE_H diff --git a/src/goto-cc/gcc_mode.cpp b/src/goto-cc/gcc_mode.cpp index aa3f261f7833..28e4443c5fb9 100644 --- a/src/goto-cc/gcc_mode.cpp +++ b/src/goto-cc/gcc_mode.cpp @@ -34,6 +34,7 @@ Author: CM Wintersteiger, 2006 #include "hybrid_binary.h" #include "linker_script_merge.h" +#include #include #include // IWYU pragma: keep #include @@ -293,16 +294,12 @@ gcc_modet::gcc_modet( bool gcc_modet::needs_preprocessing(const std::string &file) { - if(has_suffix(file, ".c") || - has_suffix(file, ".cc") || - has_suffix(file, ".cp") || - has_suffix(file, ".cpp") || - has_suffix(file, ".CPP") || - has_suffix(file, ".c++") || - has_suffix(file, ".C")) - return true; - else - return false; + // the C and C++ source files that GCC preprocesses (GCC manual, "Options + // Controlling the Kind of Output") + return has_suffix(file, ".c") || has_suffix(file, ".cc") || + has_suffix(file, ".cp") || has_suffix(file, ".cxx") || + has_suffix(file, ".cpp") || has_suffix(file, ".CPP") || + has_suffix(file, ".c++") || has_suffix(file, ".C"); } /// does it. @@ -1011,13 +1008,19 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler) } else { - for(goto_cc_cmdlinet::parsed_argvt::const_iterator - i_it=cmdline.parsed_argv.begin(); - i_it!=cmdline.parsed_argv.end(); - i_it++) - if(i_it->is_infile_name && - needs_preprocessing(i_it->arg)) - output_files.push_back(get_base_name(i_it->arg, true)+".o"); + // gcc writes .o for each source file, and so did compilet + // for each source file it compiled: C or C++, to be preprocessed or not + // (.i, .ii), named by suffix or given a language with -x + for(const auto &object_file : compiler.compiled_object_files()) + { + // gcc -c a/x.c b/x.c writes x.o twice + if( + std::find(output_files.begin(), output_files.end(), object_file) == + output_files.end()) + { + output_files.push_back(object_file); + } + } } } else