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.h Lines: 41 43 95.3 %
Date: 2019-02-26 22:23:30 Branches: 36 68 52.9 %

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 "node_mutex.h"
12
#include "util.h"
13
14
namespace node {
15
16
20524
class HostPort {
17
 public:
18
4384
  HostPort(const std::string& host_name, int port)
19
4384
      : host_name_(host_name), port_(port) {}
20
13209
  HostPort(const HostPort&) = default;
21
  HostPort& operator=(const HostPort&) = default;
22
  HostPort(HostPort&&) = default;
23
  HostPort& operator=(HostPort&&) = default;
24
25
  void set_host(const std::string& host) { host_name_ = host; }
26
27
74
  void set_port(int port) { port_ = port; }
28
29
4471
  const std::string& host() const { return host_name_; }
30
31
4554
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
4554
    CHECK_GE(port_, 0);
34
4554
    return port_;
35
  }
36
37
95
  void Update(const HostPort& other) {
38
95
    if (!other.host_name_.empty()) host_name_ = other.host_name_;
39
95
    if (other.port_ >= 0) port_ = other.port_;
40
95
  }
41
42
 private:
43
  std::string host_name_;
44
  int port_;
45
};
46
47
39165
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
32554
  virtual ~Options() {}
51
};
52
53
// These options are currently essentially per-Environment, but it can be nice
54
// to keep them separate since they are a group of options applying to a very
55
// specific part of Node. It might also make more sense for them to be
56
// per-Isolate, rather than per-Environment.
57
16394
class DebugOptions : public Options {
58
 public:
59
4289
  DebugOptions() = default;
60
8806
  DebugOptions(const DebugOptions&) = default;
61
  DebugOptions& operator=(const DebugOptions&) = default;
62
  DebugOptions(DebugOptions&&) = default;
63
  DebugOptions& operator=(DebugOptions&&) = default;
64
65
  // --inspect
66
  bool inspector_enabled = false;
67
  // --debug
68
  bool deprecated_debug = false;
69
  // --inspect-brk
70
  bool break_first_line = false;
71
  // --inspect-brk-node
72
  bool break_node_first_line = false;
73
74
  enum { kDefaultInspectorPort = 9229 };
75
76
  HostPort host_port{"127.0.0.1", kDefaultInspectorPort};
77
78
13200
  bool wait_for_connect() const {
79

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


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


697966
    virtual ~BaseOptionField() {}
328
    virtual void* LookupImpl(Options* options) const = 0;
329
330
    template <typename T>
331
260941
    inline T* Lookup(Options* options) const {
332
260941
      return static_cast<T*>(LookupImpl(options));
333
    }
334
  };
335
336
  // Represents a field of type T within `Options` that can be looked up
337
  // as a C++ member field.
338
  template <typename T>
339






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