GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_options.h Lines: 49 50 98.0 %
Date: 2019-01-07 12:15:22 Branches: 55 98 56.1 %

Line Branch Exec Source
1
#ifndef SRC_NODE_OPTIONS_H_
2
#define SRC_NODE_OPTIONS_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include <memory>
7
#include <string>
8
#include <unordered_map>
9
#include <vector>
10
#include "node_constants.h"
11
#include "util.h"
12
13
namespace node {
14
15
16988
class HostPort {
16
 public:
17
3691
  HostPort(const std::string& host_name, int port)
18
3691
      : host_name_(host_name), port_(port) {}
19
11055
  HostPort(const HostPort&) = default;
20
  HostPort& operator=(const HostPort&) = default;
21
  HostPort(HostPort&&) = default;
22
  HostPort& operator=(HostPort&&) = default;
23
24
  void set_host(const std::string& host) { host_name_ = host; }
25
26
75
  void set_port(int port) { port_ = port; }
27
28
7420
  const std::string& host() const { return host_name_; }
29
30
7504
  int port() const {
31
    // TODO(joyeecheung): make port a uint16_t
32
7504
    CHECK_GE(port_, 0);
33
7504
    return port_;
34
  }
35
36
94
  void Update(const HostPort& other) {
37
94
    if (!other.host_name_.empty()) host_name_ = other.host_name_;
38
94
    if (other.port_ >= 0) port_ = other.port_;
39
94
  }
40
41
 private:
42
  std::string host_name_;
43
  int port_;
44
};
45
46
32800
class Options {
47
 public:
48
7027
  virtual void CheckOptions(std::vector<std::string>* errors) {}
49
26962
  virtual ~Options() {}
50
};
51
52
// These options are currently essentially per-Environment, but it can be nice
53
// to keep them separate since they are a group of options applying to a very
54
// specific part of Node. It might also make more sense for them to be
55
// per-Isolate, rather than per-Environment.
56
13556
class DebugOptions : public Options {
57
 public:
58
3597
  DebugOptions() = default;
59
7370
  DebugOptions(const DebugOptions&) = default;
60
  DebugOptions& operator=(const DebugOptions&) = default;
61
  DebugOptions(DebugOptions&&) = default;
62
  DebugOptions& operator=(DebugOptions&&) = default;
63
64
  // --inspect
65
  bool inspector_enabled = false;
66
  // --debug
67
  bool deprecated_debug = false;
68
  // --inspect-brk
69
  bool break_first_line = false;
70
  // --inspect-brk-node
71
  bool break_node_first_line = false;
72
73
  enum { kDefaultInspectorPort = 9229 };
74
75
  HostPort host_port{"127.0.0.1", kDefaultInspectorPort};
76
77
3685
  bool deprecated_invocation() const {
78
3
    return deprecated_debug &&
79

3686
      inspector_enabled &&
80
3685
      break_first_line;
81
  }
82
83
3685
  bool invalid_invocation() const {
84

3685
    return deprecated_debug && !inspector_enabled;
85
  }
86
87
11018
  bool wait_for_connect() const {
88

11018
    return break_first_line || break_node_first_line;
89
  }
90
};
91
92
20374
class EnvironmentOptions : public Options {
93
 public:
94
  bool abort_on_uncaught_exception = false;
95
  bool experimental_modules = false;
96
  bool experimental_repl_await = false;
97
  bool experimental_vm_modules = false;
98
  bool experimental_worker = false;
99
  bool expose_internals = false;
100
  std::string http_parser = "llhttp";
101
  bool no_deprecation = false;
102
  bool no_force_async_hooks_checks = false;
103
  bool no_warnings = false;
104
  bool pending_deprecation = false;
105
  bool preserve_symlinks = false;
106
  bool preserve_symlinks_main = false;
107
  bool prof_process = false;
108
  std::string redirect_warnings;
109
  bool throw_deprecation = false;
110
  bool trace_deprecation = false;
111
  bool trace_sync_io = false;
112
  bool trace_warnings = false;
113
  std::string userland_loader;
114
115
  bool syntax_check_only = false;
116
  bool has_eval_string = false;
117
  std::string eval_string;
118
  bool print_eval = false;
119
  bool force_repl = false;
120
121
#if HAVE_OPENSSL
122
  bool tls_v1_0 = false;
123
  bool tls_v1_1 = false;
124
#endif
125
126
  std::vector<std::string> preload_modules;
127
128
  std::vector<std::string> user_argv;
129
130
  inline DebugOptions* get_debug_options();
131
  inline const DebugOptions& debug_options() const;
132
  void CheckOptions(std::vector<std::string>* errors);
133
134
 private:
135
  DebugOptions debug_options_;
136
};
137
138
21152
class PerIsolateOptions : public Options {
139
 public:
140
  std::shared_ptr<EnvironmentOptions> per_env { new EnvironmentOptions() };
141
  bool track_heap_objects = false;
142
143
  inline EnvironmentOptions* get_per_env_options();
144
  void CheckOptions(std::vector<std::string>* errors);
145
};
146
147
10791
class PerProcessOptions : public Options {
148
 public:
149
  std::shared_ptr<PerIsolateOptions> per_isolate { new PerIsolateOptions() };
150
151
  std::string title;
152
  std::string trace_event_categories;
153
  std::string trace_event_file_pattern = "node_trace.${rotation}.log";
154
  uint64_t max_http_header_size = 8 * 1024;
155
  int64_t v8_thread_pool_size = 4;
156
  bool zero_fill_all_buffers = false;
157
158
  std::vector<std::string> security_reverts;
159
  bool print_bash_completion = false;
160
  bool print_help = false;
161
  bool print_v8_help = false;
162
  bool print_version = false;
163
164
#ifdef NODE_HAVE_I18N_SUPPORT
165
  std::string icu_data_dir;
166
#endif
167
168
  // TODO(addaleax): Some of these could probably be per-Environment.
169
#if HAVE_OPENSSL
170
  std::string openssl_config;
171
  std::string tls_cipher_list = DEFAULT_CIPHER_LIST_CORE;
172
#ifdef NODE_OPENSSL_CERT_STORE
173
  bool ssl_openssl_cert_store = true;
174
#else
175
  bool ssl_openssl_cert_store = false;
176
#endif
177
  bool use_openssl_ca = false;
178
  bool use_bundled_ca = false;
179
#if NODE_FIPS_MODE
180
  bool enable_fips_crypto = false;
181
  bool force_fips_crypto = false;
182
#endif
183
#endif
184
185
  inline PerIsolateOptions* get_per_isolate_options();
186
  void CheckOptions(std::vector<std::string>* errors);
187
};
188
189
// The actual options parser, as opposed to the structs containing them:
190
191
namespace options_parser {
192
193
HostPort SplitHostPort(const std::string& arg,
194
    std::vector<std::string>* errors);
195
void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
196
197
enum OptionEnvvarSettings {
198
  kAllowedInEnvironment,
199
  kDisallowedInEnvironment
200
};
201
202
enum OptionType {
203
  kNoOp,
204
  kV8Option,
205
  kBoolean,
206
  kInteger,
207
  kUInteger,
208
  kString,
209
  kHostPort,
210
  kStringList,
211
};
212
213
template <typename Options>
214
14388
class OptionsParser {
215
 public:
216


14388
  virtual ~OptionsParser() {}
217
218
  typedef Options TargetType;
219
220
  struct NoOp {};
221
  struct V8Option {};
222
223
  // TODO(addaleax): A lot of the `std::string` usage here could be reduced
224
  // to simple `const char*`s if it's reasonable to expect the values to be
225
  // known at compile-time.
226
227
  // These methods add a single option to the parser. Optionally, it can be
228
  // specified whether the option should be allowed from environment variable
229
  // sources (i.e. NODE_OPTIONS).
230
  void AddOption(const std::string& name,
231
                 const std::string& help_text,
232
                 bool Options::* field,
233
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
234
  void AddOption(const std::string& name,
235
                 const std::string& help_text,
236
                 uint64_t Options::* field,
237
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
238
  void AddOption(const std::string& name,
239
                 const std::string& help_text,
240
                 int64_t Options::* field,
241
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
242
  void AddOption(const std::string& name,
243
                 const std::string& help_text,
244
                 std::string Options::* field,
245
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
246
  void AddOption(const std::string& name,
247
                 const std::string& help_text,
248
                 std::vector<std::string> Options::* field,
249
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
250
  void AddOption(const std::string& name,
251
                 const std::string& help_text,
252
                 HostPort Options::* field,
253
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
254
  void AddOption(const std::string& name,
255
                 const std::string& help_text,
256
                 NoOp no_op_tag,
257
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
258
  void AddOption(const std::string& name,
259
                 const std::string& help_text,
260
                 V8Option v8_option_tag,
261
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
262
263
  // Adds aliases. An alias can be of the form "--option-a" -> "--option-b",
264
  // or have a more complex group expansion, like
265
  //   "--option-a" -> { "--option-b", "--harmony-foobar", "--eval", "42" }
266
  // If `from` has the form "--option-a=", the alias will only be expanded if
267
  // the option is presented in that form (i.e. with a '=').
268
  // If `from` has the form "--option-a <arg>", the alias will only be expanded
269
  // if the option has a non-option argument (not starting with -) following it.
270
  void AddAlias(const std::string& from, const std::string& to);
271
  void AddAlias(const std::string& from, const std::vector<std::string>& to);
272
  void AddAlias(const std::string& from,
273
                const std::initializer_list<std::string>& to);
274
275
  // Add implications from some arbitrary option to a boolean one, either
276
  // in a way that makes `from` set `to` to true or to false.
277
  void Implies(const std::string& from, const std::string& to);
278
  void ImpliesNot(const std::string& from, const std::string& to);
279
280
  // Insert options from another options parser into this one, along with
281
  // a method that yields the target options type from this parser's options
282
  // type.
283
  template <typename ChildOptions>
284
  void Insert(const OptionsParser<ChildOptions>* child_options_parser,
285
              ChildOptions* (Options::* get_child)());
286
287
  // Parse a sequence of options into an options struct, a list of
288
  // arguments that were parsed as options, a list of unknown/JS engine options,
289
  // and leave the remainder in the input `args` vector.
290
  //
291
  // For example, an `args` input of
292
  //
293
  //   node --foo --harmony-bar --fizzle=42 -- /path/to/cow moo
294
  //
295
  // expands as
296
  //
297
  // - `args` -> { "node", "/path/to/cow", "moo" }
298
  // - `exec_args` -> { "--foo", "--harmony-bar", "--fizzle=42" }
299
  // - `v8_args` -> `{ "node", "--harmony-bar" }
300
  // - `options->foo == true`, `options->fizzle == 42`.
301
  //
302
  // If `*error` is set, the result of the parsing should be discarded and the
303
  // contents of any of the argument vectors should be considered undefined.
304
  virtual void Parse(std::vector<std::string>* const args,
305
                     std::vector<std::string>* const exec_args,
306
                     std::vector<std::string>* const v8_args,
307
                     Options* const options,
308
                     OptionEnvvarSettings required_env_settings,
309
                     std::vector<std::string>* const errors) const;
310
311
 private:
312
  // We support the wide variety of different option types by remembering
313
  // how to access them, given a certain `Options` struct.
314
315
  // Represents a field within `Options`.
316
510774
  class BaseOptionField {
317
   public:
318


510774
    virtual ~BaseOptionField() {}
319
    virtual void* LookupImpl(Options* options) const = 0;
320
  };
321
322
  // Represents a field of type T within `Options`.
323
  template <typename T>
324





366894
  class OptionField : public BaseOptionField {
325
   public:
326
    typedef T Type;
327
328
188708
    T* Lookup(Options* options) const {
329
188708
      return static_cast<T*>(this->LookupImpl(options));
330
    }
331
  };
332
333
  // Represents a field of type T within `Options` that can be looked up
334
  // as a C++ member field.
335
  template <typename T>
336





183447
  class SimpleOptionField : public OptionField<T> {
337
   public:
338
183447
    explicit SimpleOptionField(T Options::* field) : field_(field) {}
339
188708
    void* LookupImpl(Options* options) const override {
340
188708
      return static_cast<void*>(&(options->*field_));
341
    }
342
343
   private:
344
    T Options::* field_;
345
  };
346
347
  template <typename T>
348
188410
  inline T* Lookup(std::shared_ptr<BaseOptionField> field,
349
                   Options* options) const {
350
188410
    return std::static_pointer_cast<OptionField<T>>(field)->Lookup(options);
351
  }
352
353
  // An option consists of:
354
  // - A type.
355
  // - A way to store/access the property value.
356
  // - The information of whether it may occur in an env var or not.
357
2053887
  struct OptionInfo {
358
    OptionType type;
359
    std::shared_ptr<BaseOptionField> field;
360
    OptionEnvvarSettings env_setting;
361
    std::string help_text;
362
  };
363
364
  // An implied option is composed of the information on where to store a
365
  // specific boolean value (if another specific option is encountered).
366
262581
  struct Implication {
367
    std::shared_ptr<OptionField<bool>> target_field;
368
    bool target_value;
369
  };
370
371
  // These are helpers that make `Insert()` support properties of other
372
  // options structs, if we know how to access them.
373
  template <typename OriginalField, typename ChildOptions>
374
  static auto Convert(
375
      std::shared_ptr<OriginalField> original,
376
      ChildOptions* (Options::* get_child)());
377
  template <typename ChildOptions>
378
  static auto Convert(
379
      typename OptionsParser<ChildOptions>::OptionInfo original,
380
      ChildOptions* (Options::* get_child)());
381
  template <typename ChildOptions>
382
  static auto Convert(
383
      typename OptionsParser<ChildOptions>::Implication original,
384
      ChildOptions* (Options::* get_child)());
385
386
  std::unordered_map<std::string, OptionInfo> options_;
387
  std::unordered_map<std::string, std::vector<std::string>> aliases_;
388
  std::unordered_multimap<std::string, Implication> implications_;
389
390
  template <typename OtherOptions>
391
  friend class OptionsParser;
392
393
  friend void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
394
};
395
396
3597
class DebugOptionsParser : public OptionsParser<DebugOptions> {
397
 public:
398
  DebugOptionsParser();
399
400
  static const DebugOptionsParser instance;
401
};
402
403
3597
class EnvironmentOptionsParser : public OptionsParser<EnvironmentOptions> {
404
 public:
405
  EnvironmentOptionsParser();
406
407
  static const EnvironmentOptionsParser instance;
408
};
409
410
3597
class PerIsolateOptionsParser : public OptionsParser<PerIsolateOptions> {
411
 public:
412
  PerIsolateOptionsParser();
413
414
  static const PerIsolateOptionsParser instance;
415
};
416
417
3597
class PerProcessOptionsParser : public OptionsParser<PerProcessOptions> {
418
 public:
419
  PerProcessOptionsParser();
420
421
  static const PerProcessOptionsParser instance;
422
};
423
424
}  // namespace options_parser
425
}  // namespace node
426
427
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
428
429
#endif  // SRC_NODE_OPTIONS_H_