Skip to content

Commit 487b122

Browse files
author
Daniel Kroening
committed
use run() in ansi-c/c_preprocess
1 parent 0e31f73 commit 487b122

File tree

1 file changed

+86
-69
lines changed

1 file changed

+86
-69
lines changed

src/ansi-c/c_preprocess.cpp

Lines changed: 86 additions & 69 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>
@@ -383,8 +384,9 @@ bool c_preprocess_visual_studio(
383384
command += " 2> \"" + stderr_file() + "\"";
384385

385386
// _popen isn't very reliable on WIN32
386-
// that's why we use system()
387-
int result=system(command.c_str());
387+
// that's why we use run()
388+
int result =
389+
run("cl", {"cl", "@" + command_file_name()}, "", tmpi(), stderr_file());
388390

389391
std::ifstream instream(tmpi());
390392

@@ -463,30 +465,30 @@ bool c_preprocess_codewarrior(
463465

464466
temporary_filet stderr_file("tmp.stderr", "");
465467

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

470471
for(const auto &define : config.ansi_c.defines)
471-
command+=" -D"+shell_quote(define);
472+
command.push_back(" -D"+define);
472473

473474
for(const auto &include_path : config.ansi_c.include_paths)
474-
command+=" -I"+shell_quote(include_path);
475+
command.push_back(" -I"+include_path);
475476

476477
for(const auto &include_file : config.ansi_c.include_files)
477-
command+=" -include "+shell_quote(include_file);
478+
{
479+
command.push_back(" -include");
480+
command.push_back(include_file);
481+
}
478482

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

484486
temporary_filet tmpi("tmp.cl", "");
485-
command+=" \""+file+"\"";
486-
command += " -o \"" + tmpi() + "\"";
487-
command += " 2> \"" + stderr_file() + "\"";
487+
command.push_back(file);
488+
command.push_back("-o");
489+
command.push_back(tmpi());
488490

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

491493
std::ifstream stream_i(tmpi());
492494

@@ -532,62 +534,63 @@ bool c_preprocess_gcc_clang(
532534

533535
temporary_filet stderr_file("tmp.stderr", "");
534536

535-
std::string command;
537+
std::vector<std::string> argv;
536538

537539
if(preprocessor==configt::ansi_ct::preprocessort::CLANG)
538-
command="clang";
540+
argv.push_back("clang");
539541
else
540-
command="gcc";
542+
argv.push_back("gcc");
541543

542-
command += " -E -D__CPROVER__";
544+
argv.push_back("-E");
545+
argv.push_back("-D__CPROVER__");
543546

544547
const irep_idt &arch = config.ansi_c.arch;
545548

546549
if(config.ansi_c.pointer_width == 16)
547550
{
548551
if(arch == "i386" || arch == "x86_64" || arch == "x32")
549-
command += " -m16";
552+
argv.push_back("-m16");
550553
else if(has_prefix(id2string(arch), "mips"))
551-
command += " -mips16";
554+
argv.push_back("-mips16");
552555
}
553556
else if(config.ansi_c.pointer_width == 32)
554557
{
555558
if(arch == "i386" || arch == "x86_64")
556-
command += " -m32";
559+
argv.push_back("-m32");
557560
else if(arch == "x32")
558-
command += " -mx32";
561+
argv.push_back("-mx32");
559562
else if(has_prefix(id2string(arch), "mips"))
560-
command += " -mabi=32";
563+
argv.push_back("-mabi=32");
561564
else if(arch == "powerpc" || arch == "ppc64" || arch == "ppc64le")
562-
command += " -m32";
565+
argv.push_back("-m32");
563566
else if(arch == "s390" || arch == "s390x")
564-
command += " -m31"; // yes, 31, not 32!
567+
argv.push_back("-m31"); // yes, 31, not 32!
565568
else if(arch == "sparc" || arch == "sparc64")
566-
command += " -m32";
569+
argv.push_back("-m32");
567570
}
568571
else if(config.ansi_c.pointer_width == 64)
569572
{
570573
if(arch == "i386" || arch == "x86_64" || arch == "x32")
571-
command += " -m64";
574+
argv.push_back("-m64");
572575
else if(has_prefix(id2string(arch), "mips"))
573-
command += " -mabi=64";
576+
argv.push_back("-mabi=64");
574577
else if(arch == "powerpc" || arch == "ppc64" || arch == "ppc64le")
575-
command += " -m64";
578+
argv.push_back("-m64");
576579
else if(arch == "s390" || arch == "s390x")
577-
command += " -m64";
580+
argv.push_back("-m64");
578581
else if(arch == "sparc" || arch == "sparc64")
579-
command += " -m64";
582+
argv.push_back("-m64");
580583
}
581584

582585
// The width of wchar_t depends on the OS!
583586
if(config.ansi_c.wchar_t_width == config.ansi_c.short_int_width)
584-
command += " -fshort-wchar";
587+
argv.push_back("-fshort-wchar");
585588

586589
if(config.ansi_c.char_is_unsigned)
587-
command += " -funsigned-char";
590+
argv.push_back("-funsigned-char");
588591

589592
if(config.ansi_c.os == configt::ansi_ct::ost::NO_OS)
590-
command += " -nostdinc";
593+
argv.push_back("-nostdinc");
591594

592595
// Set the standard
593596
if(has_suffix(file, ".cpp") || has_suffix(file, ".CPP") ||
@@ -600,19 +603,19 @@ bool c_preprocess_gcc_clang(
600603
switch(config.cpp.cpp_standard)
601604
{
602605
case configt::cppt::cpp_standardt::CPP98:
603-
command += " -std=gnu++98";
606+
argv.push_back("-std=gnu++98");
604607
break;
605608

606609
case configt::cppt::cpp_standardt::CPP03:
607-
command += " -std=gnu++03";
610+
argv.push_back("-std=gnu++03");
608611
break;
609612

610613
case configt::cppt::cpp_standardt::CPP11:
611-
command += " -std=gnu++11";
614+
argv.push_back("-std=gnu++11");
612615
break;
613616

614617
case configt::cppt::cpp_standardt::CPP14:
615-
command += " -std=gnu++14";
618+
argv.push_back("-std=gnu++14");
616619
break;
617620
}
618621
}
@@ -621,30 +624,33 @@ bool c_preprocess_gcc_clang(
621624
switch(config.ansi_c.c_standard)
622625
{
623626
case configt::ansi_ct::c_standardt::C89:
624-
command += " -std=gnu++89";
627+
argv.push_back("-std=gnu++89");
625628
break;
626629

627630
case configt::ansi_ct::c_standardt::C99:
628-
command += " -std=gnu99";
631+
argv.push_back("-std=gnu99");
629632
break;
630633

631634
case configt::ansi_ct::c_standardt::C11:
632-
command += " -std=gnu11";
635+
argv.push_back("-std=gnu11");
633636
break;
634637
}
635638
}
636639

637640
for(const auto &define : config.ansi_c.defines)
638-
command+=" -D"+shell_quote(define);
641+
argv.push_back("-D"+define);
639642

640643
for(const auto &include_path : config.ansi_c.include_paths)
641-
command+=" -I"+shell_quote(include_path);
644+
argv.push_back("-I"+include_path);
642645

643646
for(const auto &include_file : config.ansi_c.include_files)
644-
command+=" -include "+shell_quote(include_file);
647+
{
648+
argv.push_back("-include");
649+
argv.push_back(include_file);
650+
}
645651

646652
for(const auto &opt : config.ansi_c.preprocessor_options)
647-
command+=" "+opt;
653+
argv.push_back(opt);
648654

649655
int result;
650656

@@ -660,15 +666,17 @@ bool c_preprocess_gcc_clang(
660666
}
661667
#endif
662668

669+
// the file that is to be preprocessed
670+
argv.push_back(file);
671+
663672
#ifdef _WIN32
664673
temporary_filet tmpi("tmp.gcc", "");
665-
command+=" \""+file+"\"";
666-
command += " -o \"" + tmpi() + "\"";
667-
command += " 2> \"" + stderr_file() + "\"";
674+
argv.push_back("-o");
675+
argv.push_back(tmpi());
668676

669677
// _popen isn't very reliable on WIN32
670-
// that's why we use system() and a temporary file
671-
result=system(command.c_str());
678+
// that's why we use run() and a temporary file
679+
result=run(argv[0], argv, "", "", stderr_file());
672680

673681
std::ifstream instream(tmpi());
674682

@@ -688,7 +696,11 @@ bool c_preprocess_gcc_clang(
688696
result=1;
689697
}
690698
#else
691-
command+=" \""+file+"\"";
699+
700+
std::string command;
701+
for(const auto &arg : argv)
702+
command+=" "+shell_quote(arg);
703+
692704
command += " 2> \"" + stderr_file() + "\"";
693705

694706
FILE *stream=popen(command.c_str(), "r");
@@ -738,50 +750,52 @@ bool c_preprocess_arm(
738750

739751
temporary_filet stderr_file("tmp.stderr", "");
740752

741-
std::string command;
753+
std::vector<std::string> argv;
742754

743-
command="armcc -E -D__CPROVER__";
755+
argv.push_back("armcc");
756+
argv.push_back("-E");
757+
argv.push_back("-D__CPROVER__");
744758

745759
if(config.ansi_c.endianness == configt::ansi_ct::endiannesst::IS_BIG_ENDIAN)
746-
command += " --bigend";
760+
argv.push_back("--bigend");
747761
else
748-
command += " --littleend";
762+
argv.push_back("--littleend");
749763

750764
if(config.ansi_c.char_is_unsigned)
751-
command += " --unsigned_chars";
765+
argv.push_back("--unsigned_chars");
752766
else
753-
command += " --signed_chars";
767+
argv.push_back("--signed_chars");
754768

755769
// Set the standard
756770
switch(config.ansi_c.c_standard)
757771
{
758772
case configt::ansi_ct::c_standardt::C89:
759-
command += " --c90";
773+
argv.push_back("--c90");
760774
break;
761775

762776
case configt::ansi_ct::c_standardt::C99:
763777
case configt::ansi_ct::c_standardt::C11:
764-
command += " --c99";
778+
argv.push_back("--c99");
765779
break;
766780
}
767781

768782
for(const auto &define : config.ansi_c.defines)
769-
command+=" "+shell_quote("-D"+define);
783+
argv.push_back("-D"+define);
770784

771785
for(const auto &include_path : config.ansi_c.include_paths)
772-
command+=" "+shell_quote("-I"+include_path);
786+
argv.push_back("-I"+include_path);
787+
788+
// the file that is to be preprocessed
789+
argv.push_back(file);
773790

774791
int result;
775792

776793
#ifdef _WIN32
777-
temporary_filet tmpi("tmp.cl", "");
778-
command+=" \""+file+"\"";
779-
command += " > \"" + tmpi() + "\"";
780-
command += " 2> \"" + stderr_file() + "\"";
794+
temporary_filet tmpi("tmp.armcc", "");
781795

782796
// _popen isn't very reliable on WIN32
783-
// that's why we use system() and a temporary file
784-
result=system(command.c_str());
797+
// that's why we use run() and a temporary file
798+
result=run(argv[0], argv, "", tmpi(), stderr_file());
785799

786800
std::ifstream instream(tmpi());
787801

@@ -797,7 +811,10 @@ bool c_preprocess_arm(
797811
return true;
798812
}
799813
#else
800-
command+=" \""+file+"\"";
814+
std::string command;
815+
for(const auto &arg : argv)
816+
command+=" "+shell_quote(arg);
817+
801818
command += " 2> \"" + stderr_file() + "\"";
802819

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

0 commit comments

Comments
 (0)