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: 179 183 97.8 %
Date: 2020-09-03 22:13:26 Branches: 235 422 55.7 %

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 <cstdlib>
7
#include "node_options.h"
8
#include "util.h"
9
10
namespace node {
11
12
363955
PerIsolateOptions* PerProcessOptions::get_per_isolate_options() {
13
363955
  return per_isolate.get();
14
}
15
16
335020
EnvironmentOptions* PerIsolateOptions::get_per_env_options() {
17
335020
  return per_env.get();
18
}
19
20
namespace options_parser {
21
22
template <typename Options>
23
281736
void OptionsParser<Options>::AddOption(const char* name,
24
                                       const char* help_text,
25
                                       bool Options::* field,
26
                                       OptionEnvvarSettings env_setting) {
27
281736
  options_.emplace(name,
28
                   OptionInfo{kBoolean,
29
                              std::make_shared<SimpleOptionField<bool>>(field),
30
                              env_setting,
31
                              help_text});
32
281736
}
33
34
template <typename Options>
35
13416
void OptionsParser<Options>::AddOption(const char* name,
36
                                       const char* help_text,
37
                                       uint64_t Options::* field,
38
                                       OptionEnvvarSettings env_setting) {
39
13416
  options_.emplace(
40
      name,
41
      OptionInfo{kUInteger,
42
                 std::make_shared<SimpleOptionField<uint64_t>>(field),
43
                 env_setting,
44
                 help_text});
45
13416
}
46
47
template <typename Options>
48
4472
void OptionsParser<Options>::AddOption(const char* name,
49
                                       const char* help_text,
50
                                       int64_t Options::* field,
51
                                       OptionEnvvarSettings env_setting) {
52
4472
  options_.emplace(
53
      name,
54
      OptionInfo{kInteger,
55
                 std::make_shared<SimpleOptionField<int64_t>>(field),
56
                 env_setting,
57
                 help_text});
58
4472
}
59
60
template <typename Options>
61
125216
void OptionsParser<Options>::AddOption(const char* name,
62
                                       const char* help_text,
63
                                       std::string Options::* field,
64
                                       OptionEnvvarSettings env_setting) {
65
125216
  options_.emplace(
66
      name,
67
      OptionInfo{kString,
68
                 std::make_shared<SimpleOptionField<std::string>>(field),
69
                 env_setting,
70
                 help_text});
71
125216
}
72
73
template <typename Options>
74
13416
void OptionsParser<Options>::AddOption(
75
    const char* name,
76
    const char* help_text,
77
    std::vector<std::string> Options::* field,
78
    OptionEnvvarSettings env_setting) {
79
13416
  options_.emplace(name, OptionInfo {
80
    kStringList,
81
    std::make_shared<SimpleOptionField<std::vector<std::string>>>(field),
82
    env_setting,
83
    help_text
84
  });
85
13416
}
86
87
template <typename Options>
88
4472
void OptionsParser<Options>::AddOption(const char* name,
89
                                       const char* help_text,
90
                                       HostPort Options::* field,
91
                                       OptionEnvvarSettings env_setting) {
92
4472
  options_.emplace(
93
      name,
94
      OptionInfo{kHostPort,
95
                 std::make_shared<SimpleOptionField<HostPort>>(field),
96
                 env_setting,
97
                 help_text});
98
4472
}
99
100
template <typename Options>
101
22360
void OptionsParser<Options>::AddOption(const char* name,
102
                                       const char* help_text,
103
                                       NoOp no_op_tag,
104
                                       OptionEnvvarSettings env_setting) {
105
22360
  options_.emplace(name, OptionInfo{kNoOp, nullptr, env_setting, help_text});
106
22360
}
107
108
template <typename Options>
109
53664
void OptionsParser<Options>::AddOption(const char* name,
110
                                       const char* help_text,
111
                                       V8Option v8_option_tag,
112
                                       OptionEnvvarSettings env_setting) {
113
53664
  options_.emplace(name,
114
                   OptionInfo{kV8Option, nullptr, env_setting, help_text});
115
53664
}
116
117
template <typename Options>
118
62608
void OptionsParser<Options>::AddAlias(const char* from,
119
                                      const char* to) {
120

62608
  aliases_[from] = { to };
121
62608
}
122
123
template <typename Options>
124
26832
void OptionsParser<Options>::AddAlias(const char* from,
125
                                      const std::vector<std::string>& to) {
126
26832
  aliases_[from] = to;
127
26832
}
128
129
template <typename Options>
130
26832
void OptionsParser<Options>::AddAlias(
131
    const char* from,
132
    const std::initializer_list<std::string>& to) {
133
26832
  AddAlias(from, std::vector<std::string>(to));
134
26832
}
135
136
template <typename Options>
137
35776
void OptionsParser<Options>::Implies(const char* from,
138
                                     const char* to) {
139
35776
  auto it = options_.find(to);
140


35776
  CHECK_NE(it, options_.end());
141






35776
  CHECK(it->second.type == kBoolean || it->second.type == kV8Option);
142
71552
  implications_.emplace(
143
71552
      from, Implication{it->second.type, to, it->second.field, true});
144
35776
}
145
146
template <typename Options>
147
8944
void OptionsParser<Options>::ImpliesNot(const char* from,
148
                                        const char* to) {
149
8944
  auto it = options_.find(to);
150

8944
  CHECK_NE(it, options_.end());
151

8944
  CHECK_EQ(it->second.type, kBoolean);
152
17888
  implications_.emplace(
153
17888
      from, Implication{it->second.type, to, it->second.field, false});
154
8944
}
155
156
template <typename Options>
157
template <typename OriginalField, typename ChildOptions>
158
836264
auto OptionsParser<Options>::Convert(
159
    std::shared_ptr<OriginalField> original,
160
    ChildOptions* (Options::* get_child)()) {
161
  // If we have a field on ChildOptions, and we want to access it from an
162
  // Options instance, we call get_child() on the original Options and then
163
  // access it, i.e. this class implements a kind of function chaining.
164
1672528
  struct AdaptedField : BaseOptionField {
165
732973
    void* LookupImpl(Options* options) const override {
166

732973
      return original->LookupImpl((options->*get_child)());
167
    }
168
169
836264
    AdaptedField(
170
        std::shared_ptr<OriginalField> original,
171
        ChildOptions* (Options::* get_child)())
172
836264
          : original(original), get_child(get_child) {}
173
174
    std::shared_ptr<OriginalField> original;
175
    ChildOptions* (Options::* get_child)();
176
  };
177
178
  return std::shared_ptr<BaseOptionField>(
179
836264
      new AdaptedField(original, get_child));
180
}
181
template <typename Options>
182
template <typename ChildOptions>
183
773656
auto OptionsParser<Options>::Convert(
184
    typename OptionsParser<ChildOptions>::OptionInfo original,
185
    ChildOptions* (Options::* get_child)()) {
186
773656
  return OptionInfo{original.type,
187
                    Convert(original.field, get_child),
188
773656
                    original.env_setting,
189
1547312
                    original.help_text};
190
}
191
192
template <typename Options>
193
template <typename ChildOptions>
194
62608
auto OptionsParser<Options>::Convert(
195
    typename OptionsParser<ChildOptions>::Implication original,
196
    ChildOptions* (Options::* get_child)()) {
197
  return Implication{
198
62608
      original.type,
199
      original.name,
200
      Convert(original.target_field, get_child),
201
62608
      original.target_value,
202
125216
  };
203
}
204
205
template <typename Options>
206
template <typename ChildOptions>
207
13416
void OptionsParser<Options>::Insert(
208
    const OptionsParser<ChildOptions>& child_options_parser,
209
    ChildOptions* (Options::* get_child)()) {
210
13416
  aliases_.insert(std::begin(child_options_parser.aliases_),
211
                  std::end(child_options_parser.aliases_));
212
213

787072
  for (const auto& pair : child_options_parser.options_)
214
773656
    options_.emplace(pair.first, Convert(pair.second, get_child));
215
216

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

2240
      if (exec_args != nullptr && ret != "--")
265
2121
        exec_args->push_back(ret);
266
2240
      underlying->erase(underlying->begin() + 1);
267
    } else {
268
208
      synthetic_args.erase(synthetic_args.begin());
269
    }
270
2448
    return ret;
271
  }
272
};
273
274
template <typename Options>
275
9012
void OptionsParser<Options>::Parse(
276
    std::vector<std::string>* const orig_args,
277
    std::vector<std::string>* const exec_args,
278
    std::vector<std::string>* const v8_args,
279
    Options* const options,
280
    OptionEnvvarSettings required_env_settings,
281
    std::vector<std::string>* const errors) const {
282
18024
  ArgsInfo args(orig_args, exec_args);
283
284
  // The first entry is the process name. Make sure it ends up in the V8 argv,
285
  // since V8::SetFlagsFromCommandLine() expects that to hold true for that
286
  // array as well.
287

9012
  if (v8_args->empty())
288
9012
    v8_args->push_back(args.program_name());
289
290




12720
  while (!args.empty() && errors->empty()) {
291




10076
    if (args.first().size() <= 1 || args.first()[0] != '-') break;
292
293
    // We know that we're either going to consume this
294
    // argument or fail completely.
295
3608
    const std::string arg = args.pop_first();
296
297

1893
    if (arg == "--") {
298

16
      if (required_env_settings == kAllowedInEnvironment)
299
1
        errors->push_back(NotAllowedInEnvErr("--"));
300
16
      break;
301
    }
302
303
    // Only allow --foo=bar notation for options starting with double dashes.
304
    // (E.g. -e=a is not allowed as shorthand for --eval=a, which would
305
    // otherwise be the result of alias expansion.)
306
    const std::string::size_type equals_index =
307



1877
        arg[0] == '-' && arg[1] == '-' ? arg.find('=') : std::string::npos;
308
    std::string name =
309



3592
      equals_index == std::string::npos ? arg : arg.substr(0, equals_index);
310
311
    // Store the 'original name' of the argument. This name differs from
312
    // 'name' in that it contains a possible '=' sign and is not affected
313
    // by alias expansion.
314


3592
    std::string original_name = name;
315

1877
    if (equals_index != std::string::npos)
316
176
      original_name += '=';
317
318
7
    auto missing_argument = [&]() {
319
7
      errors->push_back(RequiresArgumentErr(original_name));
320
1884
    };
321
322
    // Normalize by replacing `_` with `-` in options.
323

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

21121
      if (name[i] == '_')
325
39
        name[i] = '-';
326
    }
327
328
    {
329
1877
      auto it = aliases_.end();
330
      // Expand aliases:
331
      // - If `name` can be found in `aliases_`.
332
      // - If `name` + '=' can be found in `aliases_`.
333
      // - If `name` + " <arg>" can be found in `aliases_`, and we have
334
      //   a subsequent argument that does not start with '-' itself.
335



10299
      while ((it = aliases_.find(name)) != aliases_.end() ||
336

239
             (equals_index != std::string::npos &&
337




10476
              (it = aliases_.find(name + '=')) != aliases_.end()) ||
338

3783
             (!args.empty() &&
339

3636
                 !args.first().empty() &&
340

3120
                 args.first()[0] != '-' &&
341

3840
              (it = aliases_.find(name + " <arg>")) != aliases_.end())) {
342
1322
        const std::string prev_name = std::move(name);
343
662
        const std::vector<std::string>& expansion = it->second;
344
345
        // Use the first entry in the expansion as the new 'name'.
346
662
        name = expansion.front();
347
348

662
        if (expansion.size() > 1) {
349
          // The other arguments, if any, are going to be handled later.
350
627
          args.synthetic_args.insert(
351
418
              args.synthetic_args.begin(),
352
418
              expansion.begin() + 1,
353
              expansion.end());
354
        }
355
356



662
        if (name == prev_name) break;
357
      }
358
    }
359
360
1877
    auto it = options_.find(name);
361
362



5492
    if ((it == options_.end() ||
363



3012
         it->second.env_setting == kDisallowedInEnvironment) &&
364
        required_env_settings == kAllowedInEnvironment) {
365
16
      errors->push_back(NotAllowedInEnvErr(original_name));
366
16
      break;
367
    }
368
369
    {
370
1861
      auto implications = implications_.equal_range(name);
371

2219
      for (auto it = implications.first; it != implications.second; ++it) {
372

358
        if (it->second.type == kV8Option) {
373
          v8_args->push_back(it->second.name);
374
        } else {
375
716
          *it->second.target_field->template Lookup<bool>(options) =
376
358
              it->second.target_value;
377
        }
378
      }
379
    }
380
381

1861
    if (it == options_.end()) {
382
139
      v8_args->push_back(arg);
383
139
      continue;
384
    }
385
386
1722
    const OptionInfo& info = it->second;
387


3437
    std::string value;
388




1722
    if (info.type != kBoolean && info.type != kNoOp && info.type != kV8Option) {
389

725
      if (equals_index != std::string::npos) {
390
165
        value = arg.substr(equals_index + 1);
391

165
        if (value.empty()) {
392
2
          missing_argument();
393
2
          break;
394
        }
395
      } else {
396

560
        if (args.empty()) {
397
5
          missing_argument();
398
5
          break;
399
        }
400
401
555
        value = args.pop_first();
402
403




555
        if (!value.empty() && value[0] == '-') {
404
          missing_argument();
405
          break;
406
        } else {
407






555
          if (!value.empty() && value[0] == '\\' && value[1] == '-')
408
1
            value = value.substr(1);  // Treat \- as escaping an -.
409
        }
410
      }
411
    }
412
413






1715
    switch (info.type) {
414
      case kBoolean:
415
953
        *Lookup<bool>(info.field, options) = true;
416
953
        break;
417
      case kInteger:
418
1
        *Lookup<int64_t>(info.field, options) = std::atoll(value.c_str());
419
1
        break;
420
      case kUInteger:
421
27
        *Lookup<uint64_t>(info.field, options) = std::stoull(value);
422
27
        break;
423
      case kString:
424
538
        *Lookup<std::string>(info.field, options) = value;
425
538
        break;
426
      case kStringList:
427
52
        Lookup<std::vector<std::string>>(info.field, options)
428
52
            ->emplace_back(std::move(value));
429
52
        break;
430
      case kHostPort:
431
100
        Lookup<HostPort>(info.field, options)
432
            ->Update(SplitHostPort(value, errors));
433
100
        break;
434
      case kNoOp:
435
1
        break;
436
      case kV8Option:
437
43
        v8_args->push_back(arg);
438

43
        break;
439
      default:
440
        UNREACHABLE();
441
    }
442
  }
443
9012
  options->CheckOptions(errors);
444
9012
}
445
446
}  // namespace options_parser
447
}  // namespace node
448
449
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
450
451
#endif  // SRC_NODE_OPTIONS_INL_H_