From 0dbc34e156af848ef94521ab2e52a03988885791 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Tue, 24 Nov 2026 12:08:44 +0000 Subject: [PATCH 1/2] goto-cc: compile files named *.cxx as C++ GCC (manual, "Options Controlling the Kind of Output"), Clang and Visual Studio compile a file named *.cxx as C++ source code. goto-cc's gcc mode did not preprocess such a file, and compilet did not consider it a source file, so `goto-gcc -c x.cxx -o x.o` silently produced a native object file without a goto-cc section, and `goto-cc x.cxx` no goto program. The C++ front end already registers the cxx extension. Add .cxx to the files that gcc mode preprocesses and to the source files that compilet compiles; it was the only C++ source suffix of GCC's list missing (.cc, .cp, .cxx, .cpp, .CPP, .c++, .C, and .ii for preprocessed C++). C++ header files (.hh, .H, .hp, .hxx, .hpp, .HPP, .h++, .tcc) remain ignored, as GCC turns them into precompiled headers, as do GCC 15's C++20 module unit suffixes (.cppm, .ixx, .ccm, .cxxm, .c++m), as the C++ front end does not support modules. Co-authored-by: Kiro --- regression/goto-cc-cbmc/cxx-suffix/main.cxx | 22 ++++++++++++++++++++ regression/goto-cc-cbmc/cxx-suffix/test.desc | 12 +++++++++++ regression/goto-gcc/cxx_suffix/main.cxx | 5 +++++ regression/goto-gcc/cxx_suffix/test.desc | 13 ++++++++++++ src/goto-cc/compile.cpp | 6 +++--- src/goto-cc/gcc_mode.cpp | 16 ++++++-------- 6 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 regression/goto-cc-cbmc/cxx-suffix/main.cxx create mode 100644 regression/goto-cc-cbmc/cxx-suffix/test.desc create mode 100644 regression/goto-gcc/cxx_suffix/main.cxx create mode 100644 regression/goto-gcc/cxx_suffix/test.desc 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/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..84e37bc930ab 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; } diff --git a/src/goto-cc/gcc_mode.cpp b/src/goto-cc/gcc_mode.cpp index aa3f261f7833..90d9bca52f87 100644 --- a/src/goto-cc/gcc_mode.cpp +++ b/src/goto-cc/gcc_mode.cpp @@ -293,16 +293,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. From e6b3a08aa202a7c02e15ad112be3bc3726ffa4fd Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Tue, 24 Nov 2026 12:08:52 +0000 Subject: [PATCH 2/2] goto-gcc: keep the goto binary of every source file compiled with -c Without -o, `gcc -c` writes .o for each source file. In hybrid mode, goto-gcc moves the goto binaries that it wrote aside, runs the native compiler and then adds them to the native object files. It predicted the names of the goto binaries from those input files that need preprocessing (.c and C++ source suffixes) instead of those it compiled, so: - `goto-gcc -c x.i` (or x.ii) and `goto-gcc -c -x c x.src` compiled the file, but the native object file then replaced the goto binary: x.o had no goto-cc section; - `goto-gcc -c p/x.c q/x.c` failed with "Rename failed" when moving x.o aside for the second time, while gcc writes x.o twice and keeps the second one. compilet now records the object files it writes in mode COMPILE_ONLY (and ASSEMBLE_ONLY), and gcc mode uses that list, without duplicates. The new tests compile without -o and thus write x.o next to their sources, so the clean target of regression/goto-gcc now removes all object files. Co-authored-by: Kiro --- regression/goto-gcc/Makefile | 3 ++- .../compile_without_o_preprocessed/cxx.desc | 10 +++++++++ .../compile_without_o_preprocessed/main.i | 5 +++++ .../compile_without_o_preprocessed/other.ii | 5 +++++ .../compile_without_o_preprocessed/test.desc | 12 +++++++++++ .../compile_without_o_same_base_name/p/x.c | 4 ++++ .../compile_without_o_same_base_name/q/x.c | 4 ++++ .../test.desc | 12 +++++++++++ .../compile_without_o_x_language/main.src | 5 +++++ .../compile_without_o_x_language/test.desc | 12 +++++++++++ src/goto-cc/compile.cpp | 2 ++ src/goto-cc/compile.h | 10 +++++++++ src/goto-cc/gcc_mode.cpp | 21 ++++++++++++------- 13 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 regression/goto-gcc/compile_without_o_preprocessed/cxx.desc create mode 100644 regression/goto-gcc/compile_without_o_preprocessed/main.i create mode 100644 regression/goto-gcc/compile_without_o_preprocessed/other.ii create mode 100644 regression/goto-gcc/compile_without_o_preprocessed/test.desc create mode 100644 regression/goto-gcc/compile_without_o_same_base_name/p/x.c create mode 100644 regression/goto-gcc/compile_without_o_same_base_name/q/x.c create mode 100644 regression/goto-gcc/compile_without_o_same_base_name/test.desc create mode 100644 regression/goto-gcc/compile_without_o_x_language/main.src create mode 100644 regression/goto-gcc/compile_without_o_x_language/test.desc 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/src/goto-cc/compile.cpp b/src/goto-cc/compile.cpp index 84e37bc930ab..42887e4b0709 100644 --- a/src/goto-cc/compile.cpp +++ b/src/goto-cc/compile.cpp @@ -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 90d9bca52f87..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 @@ -1007,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