cpp

Coverage Report

Created: 2022-09-21 22:22

/home/andy/git/oilshell/oil/cpp/leaky_frontend_flag_spec.cc
Line
Count
Source (jump to first uncovered line)
1
// frontend_flag_spec.cc
2
3
#include "cpp/leaky_frontend_flag_spec.h"
4
5
#include "_gen/frontend/arg_types.h"
6
#include "mycpp/builtins.h"
7
// for definition of args::Reader, etc.
8
#include "prebuilt/frontend/args.mycpp.h"
9
10
namespace flag_spec {
11
12
using arg_types::kFlagSpecs;
13
using arg_types::kFlagSpecsAndMore;
14
using runtime_asdl::flag_type_e;
15
using runtime_asdl::value__Bool;
16
using runtime_asdl::value__Undef;
17
using runtime_asdl::value_t;
18
19
3
void _CreateStrList(const char** in, List<Str*>* out) {
20
3
  int i = 0;
21
16
  while (true) {
22
16
    const char* s = in[i];
23
16
    if (!s) {
24
3
      break;
25
3
    }
26
    // log("a0 %s", s);
27
13
    out->append(StrFromC(s));
28
13
    ++i;
29
13
  }
30
3
}
31
32
void _CreateDefaults(DefaultPair_c* in,
33
2
                     Dict<Str*, runtime_asdl::value_t*>* out) {
34
2
  int i = 0;
35
15
  while (true) {
36
15
    DefaultPair_c* pair = &(in[i]);
37
15
    if (!pair->name) {
38
2
      break;
39
2
    }
40
13
    value_t* val;
41
13
    switch (pair->typ) {
42
10
    case flag_type_e::Bool:
43
10
      val = Alloc<value__Bool>(pair->val.b);
44
10
      break;
45
0
    case flag_type_e::Int:
46
0
      val = Alloc<value__Int>(pair->val.i);
47
0
      break;
48
0
    case flag_type_e::Float:
49
0
      val = Alloc<value__Float>(pair->val.f);
50
0
      break;
51
3
    case flag_type_e::Str: {
52
3
      const char* s = pair->val.s;
53
3
      if (s == nullptr) {
54
3
        val = Alloc<value__Undef>();
55
3
      } else {
56
0
        val = Alloc<value__Str>(StrFromC(s));
57
0
      }
58
3
    } break;
59
0
    default:
60
0
      assert(0);  // NOTE(Jesse): Pretty sure this is InvalidCodePath()
61
13
    }
62
13
    out->set(StrFromC(pair->name), val);
63
13
    ++i;
64
13
  }
65
2
}
66
67
#ifndef CPP_UNIT_TEST
68
void _CreateActions(Action_c* in, Dict<Str*, args::_Action*>* out) {
69
  int i = 0;
70
  while (true) {
71
    Action_c* p = &(in[i]);
72
    if (!p->key) {
73
      break;
74
    }
75
    // log("a1 %s", p->name);
76
    args::_Action* action = nullptr;
77
    switch (p->type) {
78
    case ActionType_c::SetToString: {
79
      List<Str*>* valid = nullptr;
80
      if (p->strs) {
81
        valid = NewList<Str*>();
82
        _CreateStrList(p->strs, valid);
83
      }
84
      auto a = Alloc<args::SetToString>(StrFromC(p->name), false, valid);
85
      action = a;
86
    } break;
87
    case ActionType_c::SetToString_q:
88
      action = Alloc<args::SetToString>(StrFromC(p->name), true, nullptr);
89
      break;
90
    case ActionType_c::SetToInt:
91
      action = Alloc<args::SetToInt>(StrFromC(p->name));
92
      break;
93
    case ActionType_c::SetToFloat:
94
      action = Alloc<args::SetToFloat>(StrFromC(p->name));
95
      break;
96
    case ActionType_c::SetToTrue:
97
      action = Alloc<args::SetToTrue>(StrFromC(p->name));
98
      break;
99
    case ActionType_c::SetAttachedBool:
100
      action = Alloc<args::SetAttachedBool>(StrFromC(p->name));
101
      break;
102
    case ActionType_c::SetOption:
103
      action = Alloc<args::SetOption>(StrFromC(p->name));
104
      break;
105
    case ActionType_c::SetNamedOption: {
106
      auto a = Alloc<args::SetNamedOption>(false);
107
      if (p->strs) {
108
        _CreateStrList(p->strs, a->names);
109
      }
110
      action = a;
111
    } break;
112
    case ActionType_c::SetNamedOption_shopt: {
113
      auto a = Alloc<args::SetNamedOption>(true);
114
      if (p->strs) {
115
        _CreateStrList(p->strs, a->names);
116
      }
117
      action = a;
118
    } break;
119
    case ActionType_c::SetAction:
120
      action = Alloc<args::SetAction>(StrFromC(p->name));
121
      break;
122
    case ActionType_c::SetNamedAction: {
123
      auto a = Alloc<args::SetNamedAction>();
124
      if (p->strs) {
125
        _CreateStrList(p->strs, a->names);
126
      }
127
      action = a;
128
    } break;
129
    }
130
131
    if (action) {
132
      out->set(StrFromC(p->key), action);
133
    }
134
    ++i;
135
  }
136
}
137
#endif
138
139
// "Inflate" the static C data into a heap-allocated ASDL data structure.
140
//
141
// TODO: Make a GLOBAL CACHE?  It could be shared between subinterpreters even?
142
2
flag_spec::_FlagSpec* CreateSpec(FlagSpec_c* in) {
143
2
  auto out = Alloc<flag_spec::_FlagSpec>();
144
2
  out->arity0 = NewList<Str*>();
145
2
  out->arity1 = NewDict<Str*, args::_Action*>();
146
2
  out->actions_long = NewDict<Str*, args::_Action*>();
147
2
  out->plus_flags = NewList<Str*>();
148
2
  out->defaults = NewDict<Str*, runtime_asdl::value_t*>();
149
150
2
  if (in->arity0) {
151
2
    _CreateStrList(in->arity0, out->arity0);
152
2
  }
153
#ifndef CPP_UNIT_TEST
154
  if (in->arity1) {
155
    _CreateActions(in->arity1, out->arity1);
156
  }
157
  if (in->actions_long) {
158
    _CreateActions(in->actions_long, out->actions_long);
159
  }
160
#endif
161
2
  if (in->plus_flags) {
162
1
    _CreateStrList(in->plus_flags, out->plus_flags);
163
1
  }
164
2
  if (in->defaults) {
165
2
    _CreateDefaults(in->defaults, out->defaults);
166
2
  }
167
2
  return out;
168
2
}
169
170
0
flag_spec::_FlagSpecAndMore* CreateSpec2(FlagSpecAndMore_c* in) {
171
0
  auto out = Alloc<flag_spec::_FlagSpecAndMore>();
172
0
  out->actions_short = NewDict<Str*, args::_Action*>();
173
0
  out->actions_long = NewDict<Str*, args::_Action*>();
174
0
  out->plus_flags = NewList<Str*>();
175
0
  out->defaults = NewDict<Str*, runtime_asdl::value_t*>();
176
177
#ifndef CPP_UNIT_TEST
178
  if (in->actions_short) {
179
    _CreateActions(in->actions_short, out->actions_short);
180
  }
181
182
  if (in->actions_long) {
183
    _CreateActions(in->actions_long, out->actions_long);
184
  }
185
#endif
186
0
  if (in->plus_flags) {
187
0
    _CreateStrList(in->plus_flags, out->plus_flags);
188
0
  }
189
0
  if (in->defaults) {
190
0
    _CreateDefaults(in->defaults, out->defaults);
191
0
  }
192
0
  return out;
193
0
}
194
195
3
flag_spec::_FlagSpec* LookupFlagSpec(Str* spec_name) {
196
3
  int i = 0;
197
89
  while (true) {
198
89
    const char* name = kFlagSpecs[i].name;
199
89
    if (name == nullptr) {
200
1
      break;
201
1
    }
202
88
    if (str_equals0(name, spec_name)) {
203
      // log("%s found", spec_name->data_);
204
2
      return CreateSpec(&kFlagSpecs[i]);
205
2
    }
206
207
86
    i++;
208
86
  }
209
  // log("%s not found", spec_name->data_);
210
1
  return nullptr;
211
3
}
212
213
0
flag_spec::_FlagSpecAndMore* LookupFlagSpec2(Str* spec_name) {
214
0
  int i = 0;
215
0
  while (true) {
216
0
    const char* name = kFlagSpecsAndMore[i].name;
217
0
    if (name == nullptr) {
218
0
      break;
219
0
    }
220
0
    if (str_equals0(name, spec_name)) {
221
      // log("%s found", spec_name->data_);
222
0
      return CreateSpec2(&kFlagSpecsAndMore[i]);
223
0
    }
224
225
0
    i++;
226
0
  }
227
  // log("%s not found", spec_name->data_);
228
0
  return nullptr;
229
0
}
230
231
0
args::_Attributes* Parse(Str* spec_name, args::Reader* arg_r) {
232
0
  flag_spec::_FlagSpec* spec = LookupFlagSpec(spec_name);
233
0
  assert(spec);  // should always be found
234
235
0
#ifdef CPP_UNIT_TEST
236
  // hack because we don't want to depend on a translation of args.py
237
0
  return nullptr;
238
#else
239
  return args::Parse(spec, arg_r);
240
#endif
241
0
}
242
243
Tuple2<args::_Attributes*, args::Reader*> ParseCmdVal(
244
0
    Str* spec_name, runtime_asdl::cmd_value__Argv* cmd_val) {
245
0
#ifdef CPP_UNIT_TEST
246
0
  return Tuple2<args::_Attributes*, args::Reader*>(nullptr, nullptr);
247
#else
248
  auto arg_r = Alloc<args::Reader>(cmd_val->argv, cmd_val->arg_spids);
249
  arg_r->Next();  // move past the builtin name
250
251
  flag_spec::_FlagSpec* spec = LookupFlagSpec(spec_name);
252
  assert(spec);  // should always be found
253
  return Tuple2<args::_Attributes*, args::Reader*>(args::Parse(spec, arg_r),
254
                                                   arg_r);
255
#endif
256
0
}
257
258
// With optional arg
259
Tuple2<args::_Attributes*, args::Reader*> ParseCmdVal(
260
    Str* spec_name, runtime_asdl::cmd_value__Argv* cmd_val,
261
0
    bool accept_typed_args) {
262
  // TODO: disallow typed args!
263
0
  return ParseCmdVal(spec_name, cmd_val);
264
0
}
265
266
Tuple2<args::_Attributes*, args::Reader*> ParseLikeEcho(
267
0
    Str* spec_name, runtime_asdl::cmd_value__Argv* cmd_val) {
268
0
#ifdef CPP_UNIT_TEST
269
0
  return Tuple2<args::_Attributes*, args::Reader*>(nullptr, nullptr);
270
#else
271
  auto arg_r = Alloc<args::Reader>(cmd_val->argv, cmd_val->arg_spids);
272
  arg_r->Next();  // move past the builtin name
273
274
  flag_spec::_FlagSpec* spec = LookupFlagSpec(spec_name);
275
  assert(spec);  // should always be found
276
  return Tuple2<args::_Attributes*, args::Reader*>(
277
      args::ParseLikeEcho(spec, arg_r), arg_r);
278
#endif
279
0
}
280
281
0
args::_Attributes* ParseMore(Str* spec_name, args::Reader* arg_r) {
282
0
#ifdef CPP_UNIT_TEST
283
0
  return nullptr;
284
#else
285
  // TODO: Fill this in from constant data!
286
  flag_spec::_FlagSpecAndMore* spec = LookupFlagSpec2(spec_name);
287
  assert(spec);
288
  return args::ParseMore(spec, arg_r);
289
#endif
290
0
}
291
292
}  // namespace flag_spec