forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request diffblue#2321 from tautschnig/vs-temporary-filet
Use temporary_filet for automatic resource management
- Loading branch information
Showing
2 changed files
with
49 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,15 +16,6 @@ Author: Daniel Kroening, [email protected] | |
|
||
#include <fstream> | ||
|
||
#if defined(__linux__) || \ | ||
defined(__FreeBSD_kernel__) || \ | ||
defined(__GNU__) || \ | ||
defined(__unix__) || \ | ||
defined(__CYGWIN__) || \ | ||
defined(__MACH__) | ||
#include <unistd.h> | ||
#endif | ||
|
||
/// quote a string for bash and CMD | ||
static std::string shell_quote(const std::string &src) | ||
{ | ||
|
@@ -325,11 +316,11 @@ bool c_preprocess_visual_studio( | |
|
||
// use Visual Studio's CL | ||
|
||
std::string stderr_file=get_temporary_file("tmp.stderr", ""); | ||
std::string command_file_name=get_temporary_file("tmp.cl-cmd", ""); | ||
temporary_filet stderr_file("tmp.stderr", ""); | ||
temporary_filet command_file_name("tmp.cl-cmd", ""); | ||
|
||
{ | ||
std::ofstream command_file(command_file_name); | ||
std::ofstream command_file(command_file_name()); | ||
|
||
// This marks the command file as UTF-8, which Visual Studio | ||
// understands. | ||
|
@@ -385,23 +376,20 @@ bool c_preprocess_visual_studio( | |
command_file << shell_quote(file) << "\n"; | ||
} | ||
|
||
std::string tmpi=get_temporary_file("tmp.cl", ""); | ||
temporary_filet tmpi("tmp.cl", ""); | ||
|
||
std::string command="CL @\""+command_file_name+"\""; | ||
command+=" > \""+tmpi+"\""; | ||
command+=" 2> \""+stderr_file+"\""; | ||
std::string command = "CL @\"" + command_file_name() + "\""; | ||
command += " > \"" + tmpi() + "\""; | ||
command += " 2> \"" + stderr_file() + "\""; | ||
|
||
// _popen isn't very reliable on WIN32 | ||
// that's why we use system() | ||
int result=system(command.c_str()); | ||
|
||
std::ifstream instream(tmpi); | ||
std::ifstream instream(tmpi()); | ||
|
||
if(!instream) | ||
{ | ||
unlink(tmpi.c_str()); | ||
unlink(stderr_file.c_str()); | ||
unlink(command_file_name.c_str()); | ||
message.error() << "CL Preprocessing failed (open failed)" | ||
<< messaget::eom; | ||
return true; | ||
|
@@ -410,15 +398,11 @@ bool c_preprocess_visual_studio( | |
outstream << instream.rdbuf(); // copy | ||
|
||
instream.close(); | ||
unlink(tmpi.c_str()); | ||
unlink(command_file_name.c_str()); | ||
|
||
// errors/warnings | ||
std::ifstream stderr_stream(stderr_file); | ||
std::ifstream stderr_stream(stderr_file()); | ||
error_parse(stderr_stream, result==0, message); | ||
|
||
unlink(stderr_file.c_str()); | ||
|
||
if(result!=0) | ||
{ | ||
message.error() << "CL Preprocessing failed" << messaget::eom; | ||
|
@@ -477,7 +461,7 @@ bool c_preprocess_codewarrior( | |
// preprocessing | ||
messaget message(message_handler); | ||
|
||
std::string stderr_file=get_temporary_file("tmp.stderr", ""); | ||
temporary_filet stderr_file("tmp.stderr", ""); | ||
|
||
std::string command; | ||
|
||
|
@@ -497,37 +481,32 @@ bool c_preprocess_codewarrior( | |
|
||
int result; | ||
|
||
std::string tmpi=get_temporary_file("tmp.cl", ""); | ||
temporary_filet tmpi("tmp.cl", ""); | ||
command+=" \""+file+"\""; | ||
command+=" -o \""+tmpi+"\""; | ||
command+=" 2> \""+stderr_file+"\""; | ||
command += " -o \"" + tmpi() + "\""; | ||
command += " 2> \"" + stderr_file() + "\""; | ||
|
||
result=system(command.c_str()); | ||
|
||
std::ifstream stream_i(tmpi); | ||
std::ifstream stream_i(tmpi()); | ||
|
||
if(stream_i) | ||
{ | ||
postprocess_codewarrior(stream_i, outstream); | ||
|
||
stream_i.close(); | ||
unlink(tmpi.c_str()); | ||
} | ||
else | ||
{ | ||
unlink(tmpi.c_str()); | ||
unlink(stderr_file.c_str()); | ||
message.error() << "Preprocessing failed (fopen failed)" | ||
<< messaget::eom; | ||
return true; | ||
} | ||
|
||
// errors/warnings | ||
std::ifstream stderr_stream(stderr_file); | ||
std::ifstream stderr_stream(stderr_file()); | ||
error_parse(stderr_stream, result==0, message); | ||
|
||
unlink(stderr_file.c_str()); | ||
|
||
if(result!=0) | ||
{ | ||
message.error() << "Preprocessing failed" << messaget::eom; | ||
|
@@ -551,7 +530,7 @@ bool c_preprocess_gcc_clang( | |
// preprocessing | ||
messaget message(message_handler); | ||
|
||
std::string stderr_file=get_temporary_file("tmp.stderr", ""); | ||
temporary_filet stderr_file("tmp.stderr", ""); | ||
|
||
std::string command; | ||
|
||
|
@@ -651,39 +630,35 @@ bool c_preprocess_gcc_clang( | |
#endif | ||
|
||
#ifdef _WIN32 | ||
std::string tmpi=get_temporary_file("tmp.gcc", ""); | ||
temporary_filet tmpi("tmp.gcc", ""); | ||
command+=" \""+file+"\""; | ||
command+=" -o \""+tmpi+"\""; | ||
command+=" 2> \""+stderr_file+"\""; | ||
command += " -o \"" + tmpi() + "\""; | ||
command += " 2> \"" + stderr_file() + "\""; | ||
|
||
// _popen isn't very reliable on WIN32 | ||
// that's why we use system() and a temporary file | ||
result=system(command.c_str()); | ||
|
||
std::ifstream instream(tmpi); | ||
std::ifstream instream(tmpi()); | ||
|
||
// errors/warnings | ||
std::ifstream stderr_stream(stderr_file); | ||
std::ifstream stderr_stream(stderr_file()); | ||
error_parse(stderr_stream, result==0, message); | ||
|
||
unlink(stderr_file.c_str()); | ||
|
||
if(instream) | ||
{ | ||
outstream << instream.rdbuf(); | ||
instream.close(); | ||
unlink(tmpi.c_str()); | ||
} | ||
else | ||
{ | ||
unlink(tmpi.c_str()); | ||
message.error() << "GCC preprocessing failed (open failed)" | ||
<< messaget::eom; | ||
result=1; | ||
} | ||
#else | ||
command+=" \""+file+"\""; | ||
command+=" 2> \""+stderr_file+"\""; | ||
command += " 2> \"" + stderr_file() + "\""; | ||
|
||
FILE *stream=popen(command.c_str(), "r"); | ||
|
||
|
@@ -703,11 +678,9 @@ bool c_preprocess_gcc_clang( | |
} | ||
|
||
// errors/warnings | ||
std::ifstream stderr_stream(stderr_file); | ||
std::ifstream stderr_stream(stderr_file()); | ||
error_parse(stderr_stream, result==0, message); | ||
|
||
unlink(stderr_file.c_str()); | ||
|
||
#endif | ||
|
||
if(result!=0) | ||
|
@@ -732,7 +705,7 @@ bool c_preprocess_arm( | |
// preprocessing using armcc | ||
messaget message(message_handler); | ||
|
||
std::string stderr_file=get_temporary_file("tmp.stderr", ""); | ||
temporary_filet stderr_file("tmp.stderr", ""); | ||
|
||
std::string command; | ||
|
||
|
@@ -770,34 +743,31 @@ bool c_preprocess_arm( | |
int result; | ||
|
||
#ifdef _WIN32 | ||
std::string tmpi=get_temporary_file("tmp.cl", ""); | ||
temporary_filet tmpi("tmp.cl", ""); | ||
command+=" \""+file+"\""; | ||
command+=" > \""+tmpi+"\""; | ||
command+=" 2> \""+stderr_file+"\""; | ||
command += " > \"" + tmpi() + "\""; | ||
command += " 2> \"" + stderr_file() + "\""; | ||
|
||
// _popen isn't very reliable on WIN32 | ||
// that's why we use system() and a temporary file | ||
result=system(command.c_str()); | ||
|
||
std::ifstream instream(tmpi); | ||
std::ifstream instream(tmpi()); | ||
|
||
if(!instream) | ||
{ | ||
outstream << instream.rdbuf(); // copy | ||
instream.close(); | ||
unlink(tmpi.c_str()); | ||
} | ||
else | ||
{ | ||
unlink(tmpi.c_str()); | ||
unlink(stderr_file.c_str()); | ||
message.error() << "ARMCC preprocessing failed (fopen failed)" | ||
<< messaget::eom; | ||
return true; | ||
} | ||
#else | ||
command+=" \""+file+"\""; | ||
command+=" 2> \""+stderr_file+"\""; | ||
command += " 2> \"" + stderr_file() + "\""; | ||
|
||
FILE *stream=popen(command.c_str(), "r"); | ||
|
||
|
@@ -811,19 +781,16 @@ bool c_preprocess_arm( | |
} | ||
else | ||
{ | ||
unlink(stderr_file.c_str()); | ||
message.error() << "ARMCC preprocessing failed (popen failed)" | ||
<< messaget::eom; | ||
return true; | ||
} | ||
#endif | ||
|
||
// errors/warnings | ||
std::ifstream stderr_stream(stderr_file); | ||
std::ifstream stderr_stream(stderr_file()); | ||
error_parse(stderr_stream, result==0, message); | ||
|
||
unlink(stderr_file.c_str()); | ||
|
||
if(result!=0) | ||
{ | ||
message.error() << "ARMCC preprocessing failed" << messaget::eom; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters