CBMC
bcc_cmdline.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: A special command line object for Bruce's C Compiler
4 
5 Author: Michael Tautschnig
6 
7 \*******************************************************************/
8 
11 
12 #include "bcc_cmdline.h"
13 
14 #include <util/invariant.h>
15 #include <util/prefix.h>
16 
17 #include <iostream>
18 
19 // non-bcc options
21 {
22  "--verbosity",
23  "--function",
24  "--native-compiler",
25  "--native-linker",
26  "--print-rejected-preprocessed-source",
27  nullptr
28 };
29 
31 {
32  "-0",
33  "-3",
34  "-E",
35  "-G",
36  "-N",
37  "-O",
38  "-P",
39  "-S",
40  "-V",
41  "-c",
42  "-e",
43  "-g",
44  "-v",
45  "-w",
46  "-x",
47  "-W",
48  "-ansi",
49  nullptr
50 };
51 
53 {
54  "-A",
55  "-B",
56  "-D",
57  "-U",
58  "-M",
59  "-o",
60  "-C",
61  "-P",
62  "-I",
63  "-L",
64  "-T",
65  "-Q",
66  "-t",
67  nullptr
68 };
69 
70 bool bcc_cmdlinet::parse(int argc, const char **argv)
71 {
72  PRECONDITION(argc > 0);
73  add_arg(argv[0]);
74 
75  for(int i=1; i<argc; i++)
76  {
77  std::string argv_i=argv[i];
78 
79  // file?
80  if(argv_i=="-" || !has_prefix(argv_i, "-"))
81  {
82  add_infile_arg(argv_i);
83  continue;
84  }
85 
86  bool found=false;
87 
88  // separated only, and also allow concatenation with "="
89  for(const char **o=goto_bcc_options_with_argument;
90  *o!=nullptr && !found;
91  ++o)
92  {
93  std::string os(*o);
94 
95  if(argv_i==os) // separated
96  {
97  found=true;
98  if(i!=argc-1)
99  {
100  set(argv_i, argv[i+1]);
101  ++i;
102  }
103  else
104  set(argv_i, "");
105  }
106  else if(has_prefix(argv_i, os+"=")) // concatenated with "="
107  {
108  found=true;
109  set(os, argv_i.substr(os.size()+1));
110  }
111  }
112 
113  // goto-bcc-only command line argument found
114  if(found)
115  continue;
116 
117  // add to new_argv
118  add_arg(argv_i);
119 
120  // without argument; also store in cmdlinet
121  if(in_list(argv_i.c_str(), bcc_options_without_argument))
122  {
123  set(argv_i);
124  continue;
125  }
126 
127  for(const char **o=bcc_options_with_argument;
128  *o!=nullptr && !found;
129  ++o)
130  {
131  std::string os(*o);
132 
133  if(argv_i==os) // separated
134  {
135  found=true;
136  if(i!=argc-1)
137  {
138  set(argv_i, argv[i+1]);
139  add_arg(argv[i+1]);
140  ++i;
141  }
142  else
143  set(argv_i, "");
144  }
145  else if(has_prefix(argv_i, os))
146  {
147  found=true;
148  set(os, argv[i]+os.size());
149  }
150  }
151 
152  if(!found)
153  {
154  // unrecognized option
155  std::cerr << "Warning: uninterpreted bcc option '" << argv_i
156  << "'\n";
157  }
158  }
159 
160  return false;
161 }
const char * goto_bcc_options_with_argument[]
Definition: bcc_cmdline.cpp:20
const char * bcc_options_with_argument[]
Definition: bcc_cmdline.cpp:52
const char * bcc_options_without_argument[]
Definition: bcc_cmdline.cpp:30
A special command line object for Bruce's C Compiler Author: Michael Tautschnig Date: July 2016.
void set(const std::string &opt, const char *value) override
Set option option to value.
void add_infile_arg(const std::string &arg)
static bool in_list(const char *option, const char **list)
virtual bool parse(int argc, const char **argv, const char *optstring)
Parses a commandline according to a specification given in optstring.
Definition: cmdline.cpp:153
void add_arg(const std::string &arg)
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
#define PRECONDITION(CONDITION)
Definition: invariant.h:463