CBMC
as_cmdline.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: A special command line object for GNU Assembler
4 
5 Author: Michael Tautschnig
6 
7 \*******************************************************************/
8 
11 
12 #include "as_cmdline.h"
13 
14 #include <util/invariant.h>
15 #include <util/prefix.h>
16 
17 #include <iostream>
18 
19 // non-as options
21 {
22  "--verbosity",
23  "--function",
24  "--native-assembler",
25  "--print-rejected-preprocessed-source",
26  nullptr
27 };
28 
30 {
31  "-a", // [-a[cdghlns][=file]]
32  "--alternate",
33  "-D",
34  "--divide", // i386
35  "-f",
36  "-g",
37  "--gstabs",
38  "--gstabs+",
39  "--gdwarf-2",
40  "--help",
41  "-J",
42  "-K",
43  "-L",
44  "--keep-locals",
45  "-Qy",
46  "-R",
47  "--reduce-memory-overheads",
48  "--statistics",
49  "-v",
50  "-version",
51  "--version",
52  "-W",
53  "--warn",
54  "--fatal-warnings",
55  "-w",
56  "-x",
57  "-Z",
58  "--target-help",
59  "--32", // i386
60  "--64", // i386
61  "-n", // i386
62  nullptr
63 };
64 
66 {
67  "--debug-prefix-map",
68  "--defsym",
69  "-I",
70  "--listing-lhs-width",
71  "--listing-lhs-width2",
72  "--listing-rhs-width",
73  "--listing-cont-lines",
74  "-o",
75  "-march", // i386
76  "-mtune", // i386
77  nullptr
78 };
79 
80 bool as_cmdlinet::parse(int argc, const char **argv)
81 {
82  PRECONDITION(argc > 0);
83  add_arg(argv[0]);
84 
85  for(int i=1; i<argc; i++)
86  {
87  std::string argv_i=argv[i];
88 
89  // options file?
90  if(has_prefix(argv_i, "@"))
91  {
92  // TODO
93  continue;
94  }
95 
96  // file?
97  if(argv_i=="-" || !has_prefix(argv_i, "-"))
98  {
99  add_infile_arg(argv_i);
100  continue;
101  }
102 
103  bool found=false;
104 
105  // separated only, and also allow concatenation with "="
106  for(const char **o=goto_as_options_with_argument;
107  *o!=nullptr && !found;
108  ++o)
109  {
110  std::string os(*o);
111 
112  if(argv_i==os) // separated
113  {
114  found=true;
115  if(i!=argc-1)
116  {
117  set(argv_i, argv[i+1]);
118  ++i;
119  }
120  else
121  set(argv_i, "");
122  }
123  else if(has_prefix(argv_i, os+"=")) // concatenated with "="
124  {
125  found=true;
126  set(os, argv_i.substr(os.size()+1));
127  }
128  }
129 
130  // goto-as-only command line argument found
131  if(found)
132  continue;
133 
134  // add to new_argv
135  add_arg(argv_i);
136 
137  // also store in cmdlinet
138  if(has_prefix(argv_i, "-a")) // a-options
139  {
140  // may have an =file argument
141  std::size_t equal_pos=argv_i.find('=');
142 
143  std::string a_opts="hls";
144  if(argv_i.size()>2 &&
145  equal_pos!=std::string::npos &&
146  equal_pos>2)
147  a_opts=argv_i.substr(2, equal_pos-2);
148  else if(argv_i.size()>2 &&
149  equal_pos==std::string::npos)
150  a_opts=argv_i.substr(2);
151 
152  for(std::string::const_iterator
153  it=a_opts.begin();
154  it!=a_opts.end();
155  ++it)
156  {
157  if(equal_pos==std::string::npos)
158  set(std::string("-a")+*it); // no value
159  else
160  set(std::string("-a")+*it, argv_i.substr(equal_pos+1));
161  }
162 
163  continue;
164  }
165  // without argument
166  else if(in_list(argv_i.c_str(), as_options_without_argument))
167  {
168  set(argv_i);
169  continue;
170  }
171 
172  for(const char **o=as_options_with_argument;
173  *o!=nullptr && !found;
174  ++o)
175  {
176  std::string os(*o);
177 
178  if(argv_i==os) // separated
179  {
180  found=true;
181  if(i!=argc-1)
182  {
183  set(argv_i, argv[i+1]);
184  add_arg(argv[i+1]);
185  ++i;
186  }
187  else
188  set(argv_i, "");
189  }
190  else if(has_prefix(argv_i, os+"=")) // concatenated with "="
191  {
192  found=true;
193  set(os, argv_i.substr(os.size()+1));
194  }
195  }
196 
197  if(!found)
198  {
199  // unrecognized option
200  std::cerr << "Warning: uninterpreted as option '" << argv_i
201  << "'\n";
202  }
203  }
204 
205  return false;
206 }
const char * as_options_with_argument[]
Definition: as_cmdline.cpp:65
const char * goto_as_options_with_argument[]
Definition: as_cmdline.cpp:20
const char * as_options_without_argument[]
Definition: as_cmdline.cpp:29
A special command line object for GNU Assembler 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:163
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