Skip to content

Commit c57902b

Browse files
author
Daniel Kroening
committed
use run() in ansi-c/c_preprocess
1 parent 0777715 commit c57902b

File tree

1 file changed

+86
-111
lines changed

1 file changed

+86
-111
lines changed

src/ansi-c/c_preprocess.cpp

Lines changed: 86 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010

1111
#include <util/c_types.h>
1212
#include <util/config.h>
13+
#include <util/run.h>
1314
#include <util/suffix.h>
1415
#include <util/tempfile.h>
1516
#include <util/unicode.h>
@@ -379,25 +380,12 @@ bool c_preprocess_visual_studio(
379380
temporary_filet tmpi("tmp.cl", "");
380381

381382
std::string command = "CL @\"" + command_file_name() + "\"";
382-
command += " > \"" + tmpi() + "\"";
383383
command += " 2> \"" + stderr_file() + "\"";
384384

385385
// _popen isn't very reliable on WIN32
386-
// that's why we use system()
387-
int result=system(command.c_str());
388-
389-
std::ifstream instream(tmpi());
390-
391-
if(!instream)
392-
{
393-
message.error() << "CL Preprocessing failed (open failed)"
394-
<< messaget::eom;
395-
return true;
396-
}
397-
398-
outstream << instream.rdbuf(); // copy
399-
400-
instream.close();
386+
// that's why we use run()
387+
int result =
388+
run("cl", {"cl", "@" + command_file_name()}, "", outstream, stderr_file());
401389

402390
// errors/warnings
403391
std::ifstream stderr_stream(stderr_file());
@@ -463,30 +451,30 @@ bool c_preprocess_codewarrior(
463451

464452
temporary_filet stderr_file("tmp.stderr", "");
465453

466-
std::string command;
467-
468-
command="mwcceppc -E -P -D__CPROVER__ -ppopt line -ppopt full";
454+
std::vector<std::string> command = {
455+
"mwcceppc", "-E", "-P", "-D__CPROVER__", "-ppopt", "line", "-ppopt full"};
469456

470457
for(const auto &define : config.ansi_c.defines)
471-
command+=" -D"+shell_quote(define);
458+
command.push_back(" -D" + define);
472459

473460
for(const auto &include_path : config.ansi_c.include_paths)
474-
command+=" -I"+shell_quote(include_path);
461+
command.push_back(" -I" + include_path);
475462

476463
for(const auto &include_file : config.ansi_c.include_files)
477-
command+=" -include "+shell_quote(include_file);
464+
{
465+
command.push_back(" -include");
466+
command.push_back(include_file);
467+
}
478468

479469
for(const auto &opt : config.ansi_c.preprocessor_options)
480-
command+=" "+opt;
481-
482-
int result;
470+
command.push_back(opt);
483471

484472
temporary_filet tmpi("tmp.cl", "");
485-
command+=" \""+file+"\"";
486-
command += " -o \"" + tmpi() + "\"";
487-
command += " 2> \"" + stderr_file() + "\"";
473+
command.push_back(file);
474+
command.push_back("-o");
475+
command.push_back(tmpi());
488476

489-
result=system(command.c_str());
477+
int result = run(command[0], command, "", "", stderr_file());
490478

491479
std::ifstream stream_i(tmpi());
492480

@@ -532,62 +520,63 @@ bool c_preprocess_gcc_clang(
532520

533521
temporary_filet stderr_file("tmp.stderr", "");
534522

535-
std::string command;
523+
std::vector<std::string> argv;
536524

537525
if(preprocessor==configt::ansi_ct::preprocessort::CLANG)
538-
command="clang";
526+
argv.push_back("clang");
539527
else
540-
command="gcc";
528+
argv.push_back("gcc");
541529

542-
command += " -E -D__CPROVER__";
530+
argv.push_back("-E");
531+
argv.push_back("-D__CPROVER__");
543532

544533
const irep_idt &arch = config.ansi_c.arch;
545534

546535
if(config.ansi_c.pointer_width == 16)
547536
{
548537
if(arch == "i386" || arch == "x86_64" || arch == "x32")
549-
command += " -m16";
538+
argv.push_back("-m16");
550539
else if(has_prefix(id2string(arch), "mips"))
551-
command += " -mips16";
540+
argv.push_back("-mips16");
552541
}
553542
else if(config.ansi_c.pointer_width == 32)
554543
{
555544
if(arch == "i386" || arch == "x86_64")
556-
command += " -m32";
545+
argv.push_back("-m32");
557546
else if(arch == "x32")
558-
command += " -mx32";
547+
argv.push_back("-mx32");
559548
else if(has_prefix(id2string(arch), "mips"))
560-
command += " -mabi=32";
549+
argv.push_back("-mabi=32");
561550
else if(arch == "powerpc" || arch == "ppc64" || arch == "ppc64le")
562-
command += " -m32";
551+
argv.push_back("-m32");
563552
else if(arch == "s390" || arch == "s390x")
564-
command += " -m31"; // yes, 31, not 32!
553+
argv.push_back("-m31"); // yes, 31, not 32!
565554
else if(arch == "sparc" || arch == "sparc64")
566-
command += " -m32";
555+
argv.push_back("-m32");
567556
}
568557
else if(config.ansi_c.pointer_width == 64)
569558
{
570559
if(arch == "i386" || arch == "x86_64" || arch == "x32")
571-
command += " -m64";
560+
argv.push_back("-m64");
572561
else if(has_prefix(id2string(arch), "mips"))
573-
command += " -mabi=64";
562+
argv.push_back("-mabi=64");
574563
else if(arch == "powerpc" || arch == "ppc64" || arch == "ppc64le")
575-
command += " -m64";
564+
argv.push_back("-m64");
576565
else if(arch == "s390" || arch == "s390x")
577-
command += " -m64";
566+
argv.push_back("-m64");
578567
else if(arch == "sparc" || arch == "sparc64")
579-
command += " -m64";
568+
argv.push_back("-m64");
580569
}
581570

582571
// The width of wchar_t depends on the OS!
583572
if(config.ansi_c.wchar_t_width == config.ansi_c.short_int_width)
584-
command += " -fshort-wchar";
573+
argv.push_back("-fshort-wchar");
585574

586575
if(config.ansi_c.char_is_unsigned)
587-
command += " -funsigned-char";
576+
argv.push_back("-funsigned-char");
588577

589578
if(config.ansi_c.os == configt::ansi_ct::ost::NO_OS)
590-
command += " -nostdinc";
579+
argv.push_back("-nostdinc");
591580

592581
// Set the standard
593582
if(has_suffix(file, ".cpp") || has_suffix(file, ".CPP") ||
@@ -600,19 +589,19 @@ bool c_preprocess_gcc_clang(
600589
switch(config.cpp.cpp_standard)
601590
{
602591
case configt::cppt::cpp_standardt::CPP98:
603-
command += " -std=gnu++98";
592+
argv.push_back("-std=gnu++98");
604593
break;
605594

606595
case configt::cppt::cpp_standardt::CPP03:
607-
command += " -std=gnu++03";
596+
argv.push_back("-std=gnu++03");
608597
break;
609598

610599
case configt::cppt::cpp_standardt::CPP11:
611-
command += " -std=gnu++11";
600+
argv.push_back("-std=gnu++11");
612601
break;
613602

614603
case configt::cppt::cpp_standardt::CPP14:
615-
command += " -std=gnu++14";
604+
argv.push_back("-std=gnu++14");
616605
break;
617606
}
618607
}
@@ -621,30 +610,33 @@ bool c_preprocess_gcc_clang(
621610
switch(config.ansi_c.c_standard)
622611
{
623612
case configt::ansi_ct::c_standardt::C89:
624-
command += " -std=gnu++89";
613+
argv.push_back("-std=gnu++89");
625614
break;
626615

627616
case configt::ansi_ct::c_standardt::C99:
628-
command += " -std=gnu99";
617+
argv.push_back("-std=gnu99");
629618
break;
630619

631620
case configt::ansi_ct::c_standardt::C11:
632-
command += " -std=gnu11";
621+
argv.push_back("-std=gnu11");
633622
break;
634623
}
635624
}
636625

637626
for(const auto &define : config.ansi_c.defines)
638-
command+=" -D"+shell_quote(define);
627+
argv.push_back("-D" + define);
639628

640629
for(const auto &include_path : config.ansi_c.include_paths)
641-
command+=" -I"+shell_quote(include_path);
630+
argv.push_back("-I" + include_path);
642631

643632
for(const auto &include_file : config.ansi_c.include_files)
644-
command+=" -include "+shell_quote(include_file);
633+
{
634+
argv.push_back("-include");
635+
argv.push_back(include_file);
636+
}
645637

646638
for(const auto &opt : config.ansi_c.preprocessor_options)
647-
command+=" "+opt;
639+
argv.push_back(opt);
648640

649641
int result;
650642

@@ -660,35 +652,28 @@ bool c_preprocess_gcc_clang(
660652
}
661653
#endif
662654

663-
#ifdef _WIN32
655+
// the file that is to be preprocessed
656+
argv.push_back(file);
657+
658+
#ifdef _WIN32
664659
temporary_filet tmpi("tmp.gcc", "");
665-
command+=" \""+file+"\"";
666-
command += " -o \"" + tmpi() + "\"";
667-
command += " 2> \"" + stderr_file() + "\"";
660+
argv.push_back("-o");
661+
argv.push_back(tmpi());
668662

669663
// _popen isn't very reliable on WIN32
670-
// that's why we use system() and a temporary file
671-
result=system(command.c_str());
672-
673-
std::ifstream instream(tmpi());
664+
// that's why we use run() and a temporary file
665+
result = run(argv[0], argv, "", outstream, stderr_file());
674666

675667
// errors/warnings
676668
std::ifstream stderr_stream(stderr_file());
677669
error_parse(stderr_stream, result==0, message);
678670

679-
if(instream)
680-
{
681-
outstream << instream.rdbuf();
682-
instream.close();
683-
}
684-
else
685-
{
686-
message.error() << "GCC preprocessing failed (open failed)"
687-
<< messaget::eom;
688-
result=1;
689-
}
690671
#else
691-
command+=" \""+file+"\"";
672+
673+
std::string command;
674+
for(const auto &arg : argv)
675+
command += " " + shell_quote(arg);
676+
692677
command += " 2> \"" + stderr_file() + "\"";
693678

694679
FILE *stream=popen(command.c_str(), "r");
@@ -738,66 +723,56 @@ bool c_preprocess_arm(
738723

739724
temporary_filet stderr_file("tmp.stderr", "");
740725

741-
std::string command;
726+
std::vector<std::string> argv;
742727

743-
command="armcc -E -D__CPROVER__";
728+
argv.push_back("armcc");
729+
argv.push_back("-E");
730+
argv.push_back("-D__CPROVER__");
744731

745732
if(config.ansi_c.endianness == configt::ansi_ct::endiannesst::IS_BIG_ENDIAN)
746-
command += " --bigend";
733+
argv.push_back("--bigend");
747734
else
748-
command += " --littleend";
735+
argv.push_back("--littleend");
749736

750737
if(config.ansi_c.char_is_unsigned)
751-
command += " --unsigned_chars";
738+
argv.push_back("--unsigned_chars");
752739
else
753-
command += " --signed_chars";
740+
argv.push_back("--signed_chars");
754741

755742
// Set the standard
756743
switch(config.ansi_c.c_standard)
757744
{
758745
case configt::ansi_ct::c_standardt::C89:
759-
command += " --c90";
746+
argv.push_back("--c90");
760747
break;
761748

762749
case configt::ansi_ct::c_standardt::C99:
763750
case configt::ansi_ct::c_standardt::C11:
764-
command += " --c99";
751+
argv.push_back("--c99");
765752
break;
766753
}
767754

768755
for(const auto &define : config.ansi_c.defines)
769-
command+=" "+shell_quote("-D"+define);
756+
argv.push_back("-D" + define);
770757

771758
for(const auto &include_path : config.ansi_c.include_paths)
772-
command+=" "+shell_quote("-I"+include_path);
759+
argv.push_back("-I" + include_path);
760+
761+
// the file that is to be preprocessed
762+
argv.push_back(file);
773763

774764
int result;
775765

776766
#ifdef _WIN32
777-
temporary_filet tmpi("tmp.cl", "");
778-
command+=" \""+file+"\"";
779-
command += " > \"" + tmpi() + "\"";
780-
command += " 2> \"" + stderr_file() + "\"";
781-
782767
// _popen isn't very reliable on WIN32
783-
// that's why we use system() and a temporary file
784-
result=system(command.c_str());
768+
// that's why we use run() and a temporary file
769+
result = run(argv[0], argv, "", outstream, stderr_file());
785770

786-
std::ifstream instream(tmpi());
771+
#else
772+
std::string command;
773+
for(const auto &arg : argv)
774+
command += " " + shell_quote(arg);
787775

788-
if(!instream)
789-
{
790-
outstream << instream.rdbuf(); // copy
791-
instream.close();
792-
}
793-
else
794-
{
795-
message.error() << "ARMCC preprocessing failed (fopen failed)"
796-
<< messaget::eom;
797-
return true;
798-
}
799-
#else
800-
command+=" \""+file+"\"";
801776
command += " 2> \"" + stderr_file() + "\"";
802777

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

0 commit comments

Comments
 (0)