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