Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions regression/goto-cc-cbmc/cxx-suffix/main.cxx
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions regression/goto-cc-cbmc/cxx-suffix/test.desc
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion regression/goto-gcc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions regression/goto-gcc/compile_without_o_preprocessed/cxx.desc
Original file line number Diff line number Diff line change
@@ -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++.
5 changes: 5 additions & 0 deletions regression/goto-gcc/compile_without_o_preprocessed/main.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// preprocessed C
int f(int x)
{
return x + 1;
}
5 changes: 5 additions & 0 deletions regression/goto-gcc/compile_without_o_preprocessed/other.ii
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// preprocessed C++
int g(int &x)
{
return x + 1;
}
12 changes: 12 additions & 0 deletions regression/goto-gcc/compile_without_o_preprocessed/test.desc
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions regression/goto-gcc/compile_without_o_same_base_name/p/x.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int p(void)
{
return 1;
}
4 changes: 4 additions & 0 deletions regression/goto-gcc/compile_without_o_same_base_name/q/x.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int q(void)
{
return 2;
}
12 changes: 12 additions & 0 deletions regression/goto-gcc/compile_without_o_same_base_name/test.desc
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions regression/goto-gcc/compile_without_o_x_language/main.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// C source in a file without a C suffix, compiled with -x c
int f(int x)
{
return x + 1;
}
12 changes: 12 additions & 0 deletions regression/goto-gcc/compile_without_o_x_language/test.desc
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions regression/goto-gcc/cxx_suffix/main.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// gcc compiles a file named *.cxx as C++
int f(int &x)
{
return x + 1;
}
13 changes: 13 additions & 0 deletions regression/goto-gcc/cxx_suffix/test.desc
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 5 additions & 3 deletions src/goto-cc/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -437,6 +437,8 @@ std::optional<symbol_tablet> 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 {};
}
Expand Down
10 changes: 10 additions & 0 deletions src/goto-cc/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> &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
Expand Down Expand Up @@ -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<std::string> written_object_files;
};

#endif // CPROVER_GOTO_CC_COMPILE_H
37 changes: 20 additions & 17 deletions src/goto-cc/gcc_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Author: CM Wintersteiger, 2006
#include "hybrid_binary.h"
#include "linker_script_merge.h"

#include <algorithm>
#include <filesystem>
#include <fstream> // IWYU pragma: keep
#include <iostream>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 <base name>.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
Expand Down
Loading