GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_options-inl.h Lines: 142 173 82.1 %
Date: 2019-02-01 22:03:38 Branches: 120 560 21.4 %

Line Branch Exec Source
1
#ifndef SRC_NODE_OPTIONS_INL_H_
2
#define SRC_NODE_OPTIONS_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "node_options.h"
7
#include "util.h"
8
#include <cstdlib>
9
10
namespace node {
11
12
6918
PerIsolateOptions* PerProcessOptions::get_per_isolate_options() {
13
6918
  return per_isolate.get();
14
}
15
16
984
DebugOptions* EnvironmentOptions::get_debug_options() {
17
984
  return &debug_options_;
18
}
19
20
1312
const DebugOptions& EnvironmentOptions::debug_options() const {
21
1312
  return debug_options_;
22
}
23
24
5606
EnvironmentOptions* PerIsolateOptions::get_per_env_options() {
25
5606
  return per_env.get();
26
}
27
28
namespace options_parser {
29
30
template <typename Options>
31
6560
void OptionsParser<Options>::AddOption(const std::string& name,
32
                                       const std::string& help_text,
33
                                       bool Options::* field,
34
                                       OptionEnvvarSettings env_setting) {
35
6560
  options_.emplace(name,
36
                   OptionInfo{kBoolean,
37
                              std::make_shared<SimpleOptionField<bool>>(field),
38
                              env_setting,
39
                              help_text});
40
6560
}
41
42
template <typename Options>
43
164
void OptionsParser<Options>::AddOption(const std::string& name,
44
                                       const std::string& help_text,
45
                                       uint64_t Options::* field,
46
                                       OptionEnvvarSettings env_setting) {
47
164
  options_.emplace(
48
      name,
49
      OptionInfo{kUInteger,
50
                 std::make_shared<SimpleOptionField<uint64_t>>(field),
51
                 env_setting,
52
                 help_text});
53
164
}
54
55
template <typename Options>
56
164
void OptionsParser<Options>::AddOption(const std::string& name,
57
                                       const std::string& help_text,
58
                                       int64_t Options::* field,
59
                                       OptionEnvvarSettings env_setting) {
60
164
  options_.emplace(
61
      name,
62
      OptionInfo{kInteger,
63
                 std::make_shared<SimpleOptionField<int64_t>>(field),
64
                 env_setting,
65
                 help_text});
66
164
}
67
68
template <typename Options>
69
2296
void OptionsParser<Options>::AddOption(const std::string& name,
70
                                       const std::string& help_text,
71
                                       std::string Options::* field,
72
                                       OptionEnvvarSettings env_setting) {
73
2296
  options_.emplace(
74
      name,
75
      OptionInfo{kString,
76
                 std::make_shared<SimpleOptionField<std::string>>(field),
77
                 env_setting,
78
                 help_text});
79
2296
}
80
81
template <typename Options>
82
328
void OptionsParser<Options>::AddOption(
83
    const std::string& name,
84
    const std::string& help_text,
85
    std::vector<std::string> Options::* field,
86
    OptionEnvvarSettings env_setting) {
87
328
  options_.emplace(name, OptionInfo {
88
    kStringList,
89
    std::make_shared<SimpleOptionField<std::vector<std::string>>>(field),
90
    env_setting,
91
    help_text
92
  });
93
328
}
94
95
template <typename Options>
96
164
void OptionsParser<Options>::AddOption(const std::string& name,
97
                                       const std::string& help_text,
98
                                       HostPort Options::* field,
99
                                       OptionEnvvarSettings env_setting) {
100
164
  options_.emplace(
101
      name,
102
      OptionInfo{kHostPort,
103
                 std::make_shared<SimpleOptionField<HostPort>>(field),
104
                 env_setting,
105
                 help_text});
106
164
}
107
108
template <typename Options>
109
328
void OptionsParser<Options>::AddOption(const std::string& name,
110
                                       const std::string& help_text,
111
                                       NoOp no_op_tag,
112
                                       OptionEnvvarSettings env_setting) {
113
328
  options_.emplace(name, OptionInfo{kNoOp, nullptr, env_setting, help_text});
114
328
}
115
116
template <typename Options>
117
820
void OptionsParser<Options>::AddOption(const std::string& name,
118
                                       const std::string& help_text,
119
                                       V8Option v8_option_tag,
120
                                       OptionEnvvarSettings env_setting) {
121
820
  options_.emplace(name,
122
                   OptionInfo{kV8Option, nullptr, env_setting, help_text});
123
820
}
124
125
template <typename Options>
126
1476
void OptionsParser<Options>::AddAlias(const std::string& from,
127
                                      const std::string& to) {
128

1476
  aliases_[from] = { to };
129
1476
}
130
131
template <typename Options>
132
1312
void OptionsParser<Options>::AddAlias(const std::string& from,
133
                                      const std::vector<std::string>& to) {
134
1312
  aliases_[from] = to;
135
1312
}
136
137
template <typename Options>
138
1312
void OptionsParser<Options>::AddAlias(
139
    const std::string& from,
140
    const std::initializer_list<std::string>& to) {
141
1312
  AddAlias(from, std::vector<std::string>(to));
142
1312
}
143
144
template <typename Options>
145
984
void OptionsParser<Options>::Implies(const std::string& from,
146
                                     const std::string& to) {
147
984
  auto it = options_.find(to);
148


984
  CHECK_NE(it, options_.end());
149


984
  CHECK_EQ(it->second.type, kBoolean);
150
984
  implications_.emplace(from, Implication {
151
984
    std::static_pointer_cast<OptionField<bool>>(it->second.field), true
152
  });
153
984
}
154
155
template <typename Options>
156
164
void OptionsParser<Options>::ImpliesNot(const std::string& from,
157
                                        const std::string& to) {
158
164
  auto it = options_.find(to);
159
164
  CHECK_NE(it, options_.end());
160
164
  CHECK_EQ(it->second.type, kBoolean);
161
164
  implications_.emplace(from, Implication {
162
164
    std::static_pointer_cast<OptionField<bool>>(it->second.field), false
163
  });
164
164
}
165
166
template <typename Options>
167
template <typename OriginalField, typename ChildOptions>
168
16892
auto OptionsParser<Options>::Convert(
169
    std::shared_ptr<OriginalField> original,
170
    ChildOptions* (Options::* get_child)()) {
171
  // If we have a field on ChildOptions, and we want to access it from an
172
  // Options instance, we call get_child() on the original Options and then
173
  // access it, i.e. this class implements a kind of function chaining.
174



33784
  struct AdaptedField : BaseOptionField {
175
13508
    void* LookupImpl(Options* options) const override {
176



13508
      return original->LookupImpl((options->*get_child)());
177
    }
178
179
16892
    AdaptedField(
180
        std::shared_ptr<OriginalField> original,
181
        ChildOptions* (Options::* get_child)())
182
16892
          : original(original), get_child(get_child) {}
183
184
    std::shared_ptr<OriginalField> original;
185
    ChildOptions* (Options::* get_child)();
186
  };
187
188
  return std::shared_ptr<BaseOptionField>(
189
16892
      new AdaptedField(original, get_child));
190
}
191
template <typename Options>
192
template <typename ChildOptions>
193
14924
auto OptionsParser<Options>::Convert(
194
    typename OptionsParser<ChildOptions>::OptionInfo original,
195
    ChildOptions* (Options::* get_child)()) {
196
  return OptionInfo{original.type,
197
                    Convert(original.field, get_child),
198
                    original.env_setting,
199
14924
                    original.help_text};
200
}
201
202
template <typename Options>
203
template <typename ChildOptions>
204
1968
auto OptionsParser<Options>::Convert(
205
    typename OptionsParser<ChildOptions>::Implication original,
206
    ChildOptions* (Options::* get_child)()) {
207
  return Implication {
208
    std::static_pointer_cast<OptionField<bool>>(
209
        Convert(original.target_field, get_child)),
210
    original.target_value
211
1968
  };
212
}
213
214
template <typename Options>
215
template <typename ChildOptions>
216
492
void OptionsParser<Options>::Insert(
217
    const OptionsParser<ChildOptions>* child_options_parser,
218
    ChildOptions* (Options::* get_child)()) {
219
492
  aliases_.insert(child_options_parser->aliases_.begin(),
220
                  child_options_parser->aliases_.end());
221
222

15416
  for (const auto& pair : child_options_parser->options_)
223
14924
    options_.emplace(pair.first, Convert(pair.second, get_child));
224
225

2460
  for (const auto& pair : child_options_parser->implications_)
226
1968
    implications_.emplace(pair.first, Convert(pair.second, get_child));
227
492
}
228
229
inline std::string NotAllowedInEnvErr(const std::string& arg) {
230
  return arg + " is not allowed in NODE_OPTIONS";
231
}
232
233
inline std::string RequiresArgumentErr(const std::string& arg) {
234
  return arg + " requires an argument";
235
}
236
237
// We store some of the basic information around a single Parse call inside
238
// this struct, to separate storage of command line arguments and their
239
// handling. In particular, this makes it easier to introduce 'synthetic'
240
// arguments that get inserted by expanding option aliases.
241
230
struct ArgsInfo {
242
  // Generally, the idea here is that the first entry in `*underlying` stores
243
  // the "0th" argument (the program name), then `synthetic_args` are inserted,
244
  // followed by the remainder of `*underlying`.
245
  std::vector<std::string>* underlying;
246
  std::vector<std::string> synthetic_args;
247
248
  std::vector<std::string>* exec_args;
249
250
230
  ArgsInfo(std::vector<std::string>* args,
251
           std::vector<std::string>* exec_args)
252
230
    : underlying(args), exec_args(exec_args) {}
253
254
304
  size_t remaining() const {
255
    // -1 to account for the program name.
256
304
    return underlying->size() - 1 + synthetic_args.size();
257
  }
258
259
304
  bool empty() const { return remaining() == 0; }
260
230
  const std::string& program_name() const { return underlying->at(0); }
261
262
539
  std::string& first() {
263
539
    return synthetic_args.empty() ? underlying->at(1) : synthetic_args.front();
264
  }
265
266
37
  std::string pop_first() {
267
37
    std::string ret = std::move(first());
268
37
    if (synthetic_args.empty()) {
269
      // Only push arguments to `exec_args` that were also originally passed
270
      // on the command line (i.e. not generated through alias expansion).
271
      // '--' is a special case here since its purpose is to end `exec_argv`,
272
      // which is why we do not include it.
273

27
      if (exec_args != nullptr && ret != "--")
274
27
        exec_args->push_back(ret);
275
27
      underlying->erase(underlying->begin() + 1);
276
    } else {
277
10
      synthetic_args.erase(synthetic_args.begin());
278
    }
279
37
    return ret;
280
  }
281
};
282
283
template <typename Options>
284
230
void OptionsParser<Options>::Parse(
285
    std::vector<std::string>* const orig_args,
286
    std::vector<std::string>* const exec_args,
287
    std::vector<std::string>* const v8_args,
288
    Options* const options,
289
    OptionEnvvarSettings required_env_settings,
290
    std::vector<std::string>* const errors) const {
291
230
  ArgsInfo args(orig_args, exec_args);
292
293
  // The first entry is the process name. Make sure it ends up in the V8 argv,
294
  // since V8::SetFlagsFromCommandLine() expects that to hold true for that
295
  // array as well.
296


230
  if (v8_args->empty())
297
230
    v8_args->push_back(args.program_name());
298
299






507
  while (!args.empty() && errors->empty()) {
300






467
    if (args.first().size() <= 1 || args.first()[0] != '-') break;
301
302
    // We know that we're either going to consume this
303
    // argument or fail completely.
304
27
    const std::string arg = args.pop_first();
305
306


27
    if (arg == "--") {
307
      if (required_env_settings == kAllowedInEnvironment)
308
        errors->push_back(NotAllowedInEnvErr("--"));
309
      break;
310
    }
311
312
    // Only allow --foo=bar notation for options starting with double dashes.
313
    // (E.g. -e=a is not allowed as shorthand for --eval=a, which would
314
    // otherwise be the result of alias expansion.)
315
    const std::string::size_type equals_index =
316




27
        arg[0] == '-' && arg[1] == '-' ? arg.find('=') : std::string::npos;
317
    std::string name =
318





47
      equals_index == std::string::npos ? arg : arg.substr(0, equals_index);
319
320
    // Store the 'original name' of the argument. This name differs from
321
    // 'name' in that it contains a possible '=' sign and is not affected
322
    // by alias expansion.
323



47
    std::string original_name = name;
324


27
    if (equals_index != std::string::npos)
325
      original_name += '=';
326
327
    // Normalize by replacing `_` with `-` in options.
328


130
    for (std::string::size_type i = 2; i < name.size(); ++i) {
329


103
      if (name[i] == '_')
330
        name[i] = '-';
331
    }
332
333
    {
334
27
      auto it = aliases_.end();
335
      // Expand aliases:
336
      // - If `name` can be found in `aliases_`.
337
      // - If `name` + '=' can be found in `aliases_`.
338
      // - If `name` + " <arg>" can be found in `aliases_`, and we have
339
      //   a subsequent argument that does not start with '-' itself.
340


















252
      while ((it = aliases_.find(name)) != aliases_.end() ||
341
             (equals_index != std::string::npos &&
342




57
              (it = aliases_.find(name + '=')) != aliases_.end()) ||
343
74
             (!args.empty() &&
344
37
                 !args.first().empty() &&
345
37
                 args.first()[0] != '-' &&
346




111
              (it = aliases_.find(name + " <arg>")) != aliases_.end())) {
347
30
        const std::string prev_name = std::move(name);
348
30
        const std::vector<std::string>& expansion = it->second;
349
350
        // Use the first entry in the expansion as the new 'name'.
351
30
        name = expansion.front();
352
353


30
        if (expansion.size() > 1) {
354
          // The other arguments, if any, are going to be handled later.
355
10
          args.synthetic_args.insert(
356
              args.synthetic_args.begin(),
357
              expansion.begin() + 1,
358
20
              expansion.end());
359
        }
360
361




30
        if (name == prev_name) break;
362
      }
363
    }
364
365
27
    auto it = options_.find(name);
366
367










47
    if ((it == options_.end() ||
368
20
         it->second.env_setting == kDisallowedInEnvironment) &&
369
        required_env_settings == kAllowedInEnvironment) {
370
      errors->push_back(NotAllowedInEnvErr(original_name));
371
      break;
372
    }
373
374


27
    if (it == options_.end()) {
375
7
      v8_args->push_back(arg);
376
7
      continue;
377
    }
378
379
    {
380
20
      auto implications = implications_.equal_range(name);
381


30
      for (auto it = implications.first; it != implications.second; ++it)
382
10
        *it->second.target_field->Lookup(options) = it->second.target_value;
383
    }
384
385
20
    const OptionInfo& info = it->second;
386



40
    std::string value;
387






20
    if (info.type != kBoolean && info.type != kNoOp && info.type != kV8Option) {
388


10
      if (equals_index != std::string::npos) {
389
        value = arg.substr(equals_index + 1);
390
        if (value.empty()) {
391
        missing_argument:
392
          errors->push_back(RequiresArgumentErr(original_name));
393
          break;
394
        }
395
      } else {
396


10
        if (args.empty())
397
          goto missing_argument;
398
399
10
        value = args.pop_first();
400
401






10
        if (!value.empty() && value[0] == '-') {
402
          goto missing_argument;
403
        } else {
404








10
          if (!value.empty() && value[0] == '\\' && value[1] == '-')
405
            value = value.substr(1);  // Treat \- as escaping an -.
406
        }
407
      }
408
    }
409
410









20
    switch (info.type) {
411
      case kBoolean:
412
10
        *Lookup<bool>(info.field, options) = true;
413
10
        break;
414
      case kInteger:
415
        *Lookup<int64_t>(info.field, options) = std::atoll(value.c_str());
416
        break;
417
      case kUInteger:
418
        *Lookup<uint64_t>(info.field, options) = std::stoull(value.c_str());
419
        break;
420
      case kString:
421
10
        *Lookup<std::string>(info.field, options) = value;
422
10
        break;
423
      case kStringList:
424
        Lookup<std::vector<std::string>>(info.field, options)
425
            ->emplace_back(std::move(value));
426
        break;
427
      case kHostPort:
428
        Lookup<HostPort>(info.field, options)
429
            ->Update(SplitHostPort(value, errors));
430
        break;
431
      case kNoOp:
432
        break;
433
      case kV8Option:
434
        v8_args->push_back(arg);
435
        break;
436
      default:
437
        UNREACHABLE();
438
    }
439
  }
440
230
  options->CheckOptions(errors);
441
230
}
442
443
}  // namespace options_parser
444
}  // namespace node
445
446
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
447
448
#endif  // SRC_NODE_OPTIONS_INL_H_