Skip to content

Commit 7d55b72

Browse files
committed
Use temporary_filet for automatic resource management
1 parent efb31e5 commit 7d55b72

File tree

1 file changed

+30
-63
lines changed

1 file changed

+30
-63
lines changed

src/ansi-c/c_preprocess.cpp

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ Author: Daniel Kroening, [email protected]
1616

1717
#include <fstream>
1818

19-
#if defined(__linux__) || \
20-
defined(__FreeBSD_kernel__) || \
21-
defined(__GNU__) || \
22-
defined(__unix__) || \
23-
defined(__CYGWIN__) || \
24-
defined(__MACH__)
25-
#include <unistd.h>
26-
#endif
27-
2819
/// quote a string for bash and CMD
2920
static std::string shell_quote(const std::string &src)
3021
{
@@ -325,11 +316,11 @@ bool c_preprocess_visual_studio(
325316

326317
// use Visual Studio's CL
327318

328-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
329-
std::string command_file_name=get_temporary_file("tmp.cl-cmd", "");
319+
temporary_filet stderr_file("tmp.stderr", "");
320+
temporary_filet command_file_name("tmp.cl-cmd", "");
330321

331322
{
332-
std::ofstream command_file(command_file_name);
323+
std::ofstream command_file(command_file_name());
333324

334325
// This marks the command file as UTF-8, which Visual Studio
335326
// understands.
@@ -379,23 +370,20 @@ bool c_preprocess_visual_studio(
379370
command_file << shell_quote(file) << "\n";
380371
}
381372

382-
std::string tmpi=get_temporary_file("tmp.cl", "");
373+
temporary_filet tmpi("tmp.cl", "");
383374

384-
std::string command="CL @\""+command_file_name+"\"";
385-
command+=" > \""+tmpi+"\"";
386-
command+=" 2> \""+stderr_file+"\"";
375+
std::string command = "CL @\"" + command_file_name() + "\"";
376+
command += " > \"" + tmpi() + "\"";
377+
command += " 2> \"" + stderr_file() + "\"";
387378

388379
// _popen isn't very reliable on WIN32
389380
// that's why we use system()
390381
int result=system(command.c_str());
391382

392-
std::ifstream instream(tmpi);
383+
std::ifstream instream(tmpi());
393384

394385
if(!instream)
395386
{
396-
unlink(tmpi.c_str());
397-
unlink(stderr_file.c_str());
398-
unlink(command_file_name.c_str());
399387
message.error() << "CL Preprocessing failed (open failed)"
400388
<< messaget::eom;
401389
return true;
@@ -404,15 +392,11 @@ bool c_preprocess_visual_studio(
404392
outstream << instream.rdbuf(); // copy
405393

406394
instream.close();
407-
unlink(tmpi.c_str());
408-
unlink(command_file_name.c_str());
409395

410396
// errors/warnings
411-
std::ifstream stderr_stream(stderr_file);
397+
std::ifstream stderr_stream(stderr_file());
412398
error_parse(stderr_stream, result==0, message);
413399

414-
unlink(stderr_file.c_str());
415-
416400
if(result!=0)
417401
{
418402
message.error() << "CL Preprocessing failed" << messaget::eom;
@@ -471,7 +455,7 @@ bool c_preprocess_codewarrior(
471455
// preprocessing
472456
messaget message(message_handler);
473457

474-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
458+
temporary_filet stderr_file("tmp.stderr", "");
475459

476460
std::string command;
477461

@@ -491,37 +475,32 @@ bool c_preprocess_codewarrior(
491475

492476
int result;
493477

494-
std::string tmpi=get_temporary_file("tmp.cl", "");
478+
temporary_filet tmpi("tmp.cl", "");
495479
command+=" \""+file+"\"";
496-
command+=" -o \""+tmpi+"\"";
497-
command+=" 2> \""+stderr_file+"\"";
480+
command += " -o \"" + tmpi() + "\"";
481+
command += " 2> \"" + stderr_file() + "\"";
498482

499483
result=system(command.c_str());
500484

501-
std::ifstream stream_i(tmpi);
485+
std::ifstream stream_i(tmpi());
502486

503487
if(stream_i)
504488
{
505489
postprocess_codewarrior(stream_i, outstream);
506490

507491
stream_i.close();
508-
unlink(tmpi.c_str());
509492
}
510493
else
511494
{
512-
unlink(tmpi.c_str());
513-
unlink(stderr_file.c_str());
514495
message.error() << "Preprocessing failed (fopen failed)"
515496
<< messaget::eom;
516497
return true;
517498
}
518499

519500
// errors/warnings
520-
std::ifstream stderr_stream(stderr_file);
501+
std::ifstream stderr_stream(stderr_file());
521502
error_parse(stderr_stream, result==0, message);
522503

523-
unlink(stderr_file.c_str());
524-
525504
if(result!=0)
526505
{
527506
message.error() << "Preprocessing failed" << messaget::eom;
@@ -545,7 +524,7 @@ bool c_preprocess_gcc_clang(
545524
// preprocessing
546525
messaget message(message_handler);
547526

548-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
527+
temporary_filet stderr_file("tmp.stderr", "");
549528

550529
std::string command;
551530

@@ -645,39 +624,35 @@ bool c_preprocess_gcc_clang(
645624
#endif
646625

647626
#ifdef _WIN32
648-
std::string tmpi=get_temporary_file("tmp.gcc", "");
627+
temporary_filet tmpi("tmp.gcc", "");
649628
command+=" \""+file+"\"";
650-
command+=" -o \""+tmpi+"\"";
651-
command+=" 2> \""+stderr_file+"\"";
629+
command += " -o \"" + tmpi() + "\"";
630+
command += " 2> \"" + stderr_file() + "\"";
652631

653632
// _popen isn't very reliable on WIN32
654633
// that's why we use system() and a temporary file
655634
result=system(command.c_str());
656635

657-
std::ifstream instream(tmpi);
636+
std::ifstream instream(tmpi());
658637

659638
// errors/warnings
660-
std::ifstream stderr_stream(stderr_file);
639+
std::ifstream stderr_stream(stderr_file());
661640
error_parse(stderr_stream, result==0, message);
662641

663-
unlink(stderr_file.c_str());
664-
665642
if(instream)
666643
{
667644
outstream << instream.rdbuf();
668645
instream.close();
669-
unlink(tmpi.c_str());
670646
}
671647
else
672648
{
673-
unlink(tmpi.c_str());
674649
message.error() << "GCC preprocessing failed (open failed)"
675650
<< messaget::eom;
676651
result=1;
677652
}
678653
#else
679654
command+=" \""+file+"\"";
680-
command+=" 2> \""+stderr_file+"\"";
655+
command += " 2> \"" + stderr_file() + "\"";
681656

682657
FILE *stream=popen(command.c_str(), "r");
683658

@@ -697,11 +672,9 @@ bool c_preprocess_gcc_clang(
697672
}
698673

699674
// errors/warnings
700-
std::ifstream stderr_stream(stderr_file);
675+
std::ifstream stderr_stream(stderr_file());
701676
error_parse(stderr_stream, result==0, message);
702677

703-
unlink(stderr_file.c_str());
704-
705678
#endif
706679

707680
if(result!=0)
@@ -726,7 +699,7 @@ bool c_preprocess_arm(
726699
// preprocessing using armcc
727700
messaget message(message_handler);
728701

729-
std::string stderr_file=get_temporary_file("tmp.stderr", "");
702+
temporary_filet stderr_file("tmp.stderr", "");
730703

731704
std::string command;
732705

@@ -764,34 +737,31 @@ bool c_preprocess_arm(
764737
int result;
765738

766739
#ifdef _WIN32
767-
std::string tmpi=get_temporary_file("tmp.cl", "");
740+
temporary_filet tmpi("tmp.cl", "");
768741
command+=" \""+file+"\"";
769-
command+=" > \""+tmpi+"\"";
770-
command+=" 2> \""+stderr_file+"\"";
742+
command += " > \"" + tmpi() + "\"";
743+
command += " 2> \"" + stderr_file() + "\"";
771744

772745
// _popen isn't very reliable on WIN32
773746
// that's why we use system() and a temporary file
774747
result=system(command.c_str());
775748

776-
std::ifstream instream(tmpi);
749+
std::ifstream instream(tmpi());
777750

778751
if(!instream)
779752
{
780753
outstream << instream.rdbuf(); // copy
781754
instream.close();
782-
unlink(tmpi.c_str());
783755
}
784756
else
785757
{
786-
unlink(tmpi.c_str());
787-
unlink(stderr_file.c_str());
788758
message.error() << "ARMCC preprocessing failed (fopen failed)"
789759
<< messaget::eom;
790760
return true;
791761
}
792762
#else
793763
command+=" \""+file+"\"";
794-
command+=" 2> \""+stderr_file+"\"";
764+
command += " 2> \"" + stderr_file() + "\"";
795765

796766
FILE *stream=popen(command.c_str(), "r");
797767

@@ -805,19 +775,16 @@ bool c_preprocess_arm(
805775
}
806776
else
807777
{
808-
unlink(stderr_file.c_str());
809778
message.error() << "ARMCC preprocessing failed (popen failed)"
810779
<< messaget::eom;
811780
return true;
812781
}
813782
#endif
814783

815784
// errors/warnings
816-
std::ifstream stderr_stream(stderr_file);
785+
std::ifstream stderr_stream(stderr_file());
817786
error_parse(stderr_stream, result==0, message);
818787

819-
unlink(stderr_file.c_str());
820-
821788
if(result!=0)
822789
{
823790
message.error() << "ARMCC preprocessing failed" << messaget::eom;

0 commit comments

Comments
 (0)