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-09-23 22:30:09 Branches: 35 64 54.7 %

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
24309
class HostPort {
17
 public:
18
5158
  HostPort(const std::string& host_name, int port)
19
5158
      : host_name_(host_name), port_(port) {}
20
15581
  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
80
  void set_port(int port) { port_ = port; }
28
29
5279
  const std::string& host() const { return host_name_; }
30
31
5363
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
5363
    CHECK_GE(port_, 0);
34
5363
    return port_;
35
  }
36
37
98
  void Update(const HostPort& other) {
38
98
    if (!other.host_name_.empty()) host_name_ = other.host_name_;
39
98
    if (other.port_ >= 0) port_ = other.port_;
40
98
  }
41
42
 private:
43
  std::string host_name_;
44
  int port_;
45
};
46
47
46210
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
38577
  virtual ~Options() = default;
51
};
52
53
struct InspectPublishUid {
54
  bool console;
55
  bool http;
56
};
57
58
// These options are currently essentially per-Environment, but it can be nice
59
// to keep them separate since they are a group of options applying to a very
60
// specific part of Node. It might also make more sense for them to be
61
// per-Isolate, rather than per-Environment.
62
19424
class DebugOptions : public Options {
63
 public:
64
5060
  DebugOptions() = default;
65
10388
  DebugOptions(const DebugOptions&) = default;
66
  DebugOptions& operator=(const DebugOptions&) = default;
67
  DebugOptions(DebugOptions&&) = default;
68
  DebugOptions& operator=(DebugOptions&&) = default;
69
70
  // --inspect
71
  bool inspector_enabled = false;
72
  // --debug
73
  bool deprecated_debug = false;
74
  // --inspect-brk
75
  bool break_first_line = false;
76
  // --inspect-brk-node
77
  bool break_node_first_line = false;
78
  // --inspect-publish-uid
79
  std::string inspect_publish_uid_string = "stderr,http";
80
81
  InspectPublishUid inspect_publish_uid;
82
83
  enum { kDefaultInspectorPort = 9229 };
84
85
  HostPort host_port{"127.0.0.1", kDefaultInspectorPort};
86
87
  // Used to patch the options as if --inspect-brk is passed.
88
32
  void EnableBreakFirstLine() {
89
32
    inspector_enabled = true;
90
32
    break_first_line = true;
91
32
  }
92
93
5195
  bool wait_for_connect() const {
94

5195
    return break_first_line || break_node_first_line;
95
  }
96
97
  void CheckOptions(std::vector<std::string>* errors) override;
98
};
99
100
29138
class EnvironmentOptions : public Options {
101
 public:
102
  bool abort_on_uncaught_exception = false;
103
  bool experimental_exports = false;
104
  bool experimental_modules = false;
105
  std::string es_module_specifier_resolution;
106
  bool experimental_wasm_modules = false;
107
  std::string module_type;
108
  std::string experimental_policy;
109
  std::string experimental_policy_integrity;
110
  bool has_policy_integrity_string;
111
  bool experimental_repl_await = false;
112
  bool experimental_vm_modules = false;
113
  bool expose_internals = false;
114
  bool frozen_intrinsics = false;
115
  std::string heap_snapshot_signal;
116
  bool no_deprecation = false;
117
  bool no_force_async_hooks_checks = false;
118
  bool no_warnings = false;
119
  bool pending_deprecation = false;
120
  bool preserve_symlinks = false;
121
  bool preserve_symlinks_main = false;
122
  bool prof_process = false;
123
#if HAVE_INSPECTOR
124
  std::string cpu_prof_dir;
125
  static const uint64_t kDefaultCpuProfInterval = 1000;
126
  uint64_t cpu_prof_interval = kDefaultCpuProfInterval;
127
  std::string cpu_prof_name;
128
  bool cpu_prof = false;
129
  std::string heap_prof_dir;
130
  std::string heap_prof_name;
131
  static const uint64_t kDefaultHeapProfInterval = 512 * 1024;
132
  uint64_t heap_prof_interval = kDefaultHeapProfInterval;
133
  bool heap_prof = false;
134
#endif  // HAVE_INSPECTOR
135
  std::string redirect_warnings;
136
  bool throw_deprecation = false;
137
  bool trace_deprecation = false;
138
  bool trace_sync_io = false;
139
  bool trace_tls = false;
140
  bool trace_warnings = false;
141
  std::string unhandled_rejections;
142
  std::string userland_loader;
143
144
  bool syntax_check_only = false;
145
  bool has_eval_string = false;
146
#ifdef NODE_REPORT
147
  bool experimental_report = false;
148
#endif  //  NODE_REPORT
149
  std::string eval_string;
150
  bool print_eval = false;
151
  bool force_repl = false;
152
153
  bool tls_min_v1_0 = false;
154
  bool tls_min_v1_1 = false;
155
  bool tls_min_v1_2 = false;
156
  bool tls_min_v1_3 = false;
157
  bool tls_max_v1_2 = false;
158
  bool tls_max_v1_3 = false;
159
160
  std::vector<std::string> preload_modules;
161
162
  std::vector<std::string> user_argv;
163
164
  inline DebugOptions* get_debug_options();
165
  inline const DebugOptions& debug_options() const;
166
  void CheckOptions(std::vector<std::string>* errors) override;
167
168
 private:
169
  DebugOptions debug_options_;
170
};
171
172
29981
class PerIsolateOptions : public Options {
173
 public:
174
  std::shared_ptr<EnvironmentOptions> per_env { new EnvironmentOptions() };
175
  bool track_heap_objects = false;
176
  bool no_node_snapshot = false;
177
178
#ifdef NODE_REPORT
179
  bool report_uncaught_exception = false;
180
  bool report_on_signal = false;
181
  bool report_on_fatalerror = false;
182
  std::string report_signal;
183
  std::string report_filename;
184
  std::string report_directory;
185
#endif  //  NODE_REPORT
186
  inline EnvironmentOptions* get_per_env_options();
187
  void CheckOptions(std::vector<std::string>* errors) override;
188
};
189
190
15144
class PerProcessOptions : public Options {
191
 public:
192
  std::shared_ptr<PerIsolateOptions> per_isolate { new PerIsolateOptions() };
193
194
  std::string title;
195
  std::string trace_event_categories;
196
  std::string trace_event_file_pattern = "node_trace.${rotation}.log";
197
  uint64_t max_http_header_size = 8 * 1024;
198
  int64_t v8_thread_pool_size = 4;
199
  bool zero_fill_all_buffers = false;
200
  bool debug_arraybuffer_allocations = false;
201
202
  std::vector<std::string> security_reverts;
203
  bool print_bash_completion = false;
204
  bool print_help = false;
205
  bool print_v8_help = false;
206
  bool print_version = false;
207
208
#ifdef NODE_HAVE_I18N_SUPPORT
209
  std::string icu_data_dir;
210
#endif
211
212
  // TODO(addaleax): Some of these could probably be per-Environment.
213
#if HAVE_OPENSSL
214
  std::string openssl_config;
215
  std::string tls_cipher_list = DEFAULT_CIPHER_LIST_CORE;
216
#ifdef NODE_OPENSSL_CERT_STORE
217
  bool ssl_openssl_cert_store = true;
218
#else
219
  bool ssl_openssl_cert_store = false;
220
#endif
221
  bool use_openssl_ca = false;
222
  bool use_bundled_ca = false;
223
#if NODE_FIPS_MODE
224
  bool enable_fips_crypto = false;
225
  bool force_fips_crypto = false;
226
#endif
227
#endif
228
229
#ifdef NODE_REPORT
230
  std::vector<std::string> cmdline;
231
#endif  //  NODE_REPORT
232
233
  inline PerIsolateOptions* get_per_isolate_options();
234
  void CheckOptions(std::vector<std::string>* errors) override;
235
};
236
237
// The actual options parser, as opposed to the structs containing them:
238
239
namespace options_parser {
240
241
HostPort SplitHostPort(const std::string& arg,
242
    std::vector<std::string>* errors);
243
void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
244
245
enum OptionEnvvarSettings {
246
  kAllowedInEnvironment,
247
  kDisallowedInEnvironment
248
};
249
250
enum OptionType {
251
  kNoOp,
252
  kV8Option,
253
  kBoolean,
254
  kInteger,
255
  kUInteger,
256
  kString,
257
  kHostPort,
258
  kStringList,
259
};
260
261
template <typename Options>
262
20192
class OptionsParser {
263
 public:
264


20192
  virtual ~OptionsParser() = default;
265
266
  typedef Options TargetType;
267
268
  struct NoOp {};
269
  struct V8Option {};
270
271
  // These methods add a single option to the parser. Optionally, it can be
272
  // specified whether the option should be allowed from environment variable
273
  // sources (i.e. NODE_OPTIONS).
274
  void AddOption(const char* name,
275
                 const char* help_text,
276
                 bool Options::* field,
277
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
278
  void AddOption(const char* name,
279
                 const char* help_text,
280
                 uint64_t Options::* field,
281
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
282
  void AddOption(const char* name,
283
                 const char* help_text,
284
                 int64_t Options::* field,
285
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
286
  void AddOption(const char* name,
287
                 const char* help_text,
288
                 std::string Options::* field,
289
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
290
  void AddOption(const char* name,
291
                 const char* help_text,
292
                 std::vector<std::string> Options::* field,
293
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
294
  void AddOption(const char* name,
295
                 const char* help_text,
296
                 HostPort Options::* field,
297
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
298
  void AddOption(const char* name,
299
                 const char* help_text,
300
                 NoOp no_op_tag,
301
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
302
  void AddOption(const char* name,
303
                 const char* help_text,
304
                 V8Option v8_option_tag,
305
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
306
307
  // Adds aliases. An alias can be of the form "--option-a" -> "--option-b",
308
  // or have a more complex group expansion, like
309
  //   "--option-a" -> { "--option-b", "--harmony-foobar", "--eval", "42" }
310
  // If `from` has the form "--option-a=", the alias will only be expanded if
311
  // the option is presented in that form (i.e. with a '=').
312
  // If `from` has the form "--option-a <arg>", the alias will only be expanded
313
  // if the option has a non-option argument (not starting with -) following it.
314
  void AddAlias(const char* from, const char* to);
315
  void AddAlias(const char* from, const std::vector<std::string>& to);
316
  void AddAlias(const char* from,
317
                const std::initializer_list<std::string>& to);
318
319
  // Add implications from some arbitrary option to a boolean one, either
320
  // in a way that makes `from` set `to` to true or to false.
321
  void Implies(const char* from, const char* to);
322
  void ImpliesNot(const char* from, const char* to);
323
324
  // Insert options from another options parser into this one, along with
325
  // a method that yields the target options type from this parser's options
326
  // type.
327
  template <typename ChildOptions>
328
  void Insert(const OptionsParser<ChildOptions>& child_options_parser,
329
              ChildOptions* (Options::* get_child)());
330
331
  // Parse a sequence of options into an options struct, a list of
332
  // arguments that were parsed as options, a list of unknown/JS engine options,
333
  // and leave the remainder in the input `args` vector.
334
  //
335
  // For example, an `args` input of
336
  //
337
  //   node --foo --harmony-bar --fizzle=42 -- /path/to/cow moo
338
  //
339
  // expands as
340
  //
341
  // - `args` -> { "node", "/path/to/cow", "moo" }
342
  // - `exec_args` -> { "--foo", "--harmony-bar", "--fizzle=42" }
343
  // - `v8_args` -> `{ "node", "--harmony-bar" }
344
  // - `options->foo == true`, `options->fizzle == 42`.
345
  //
346
  // If `*error` is set, the result of the parsing should be discarded and the
347
  // contents of any of the argument vectors should be considered undefined.
348
  void Parse(std::vector<std::string>* const args,
349
             std::vector<std::string>* const exec_args,
350
             std::vector<std::string>* const v8_args,
351
             Options* const options,
352
             OptionEnvvarSettings required_env_settings,
353
             std::vector<std::string>* const errors) const;
354
355
 private:
356
  // We support the wide variety of different option types by remembering
357
  // how to access them, given a certain `Options` struct.
358
359
  // Represents a field within `Options`.
360
1191328
  class BaseOptionField {
361
   public:
362


1191328
    virtual ~BaseOptionField() = default;
363
    virtual void* LookupImpl(Options* options) const = 0;
364
365
    template <typename T>
366
428334
    inline T* Lookup(Options* options) const {
367
428334
      return static_cast<T*>(LookupImpl(options));
368
    }
369
  };
370
371
  // Represents a field of type T within `Options` that can be looked up
372
  // as a C++ member field.
373
  template <typename T>
374







413936
  class SimpleOptionField : public BaseOptionField {
375
   public:
376
413936
    explicit SimpleOptionField(T Options::* field) : field_(field) {}
377
428334
    void* LookupImpl(Options* options) const override {
378
428334
      return static_cast<void*>(&(options->*field_));
379
    }
380
381
   private:
382
    T Options::* field_;
383
  };
384
385
  template <typename T>
386
427855
  inline T* Lookup(std::shared_ptr<BaseOptionField> field,
387
                   Options* options) const {
388
427855
    return field->template Lookup<T>(options);
389
  }
390
391
  // An option consists of:
392
  // - A type.
393
  // - A way to store/access the property value.
394
  // - The information of whether it may occur in an env var or not.
395
4967232
  struct OptionInfo {
396
    OptionType type;
397
    std::shared_ptr<BaseOptionField> field;
398
    OptionEnvvarSettings env_setting;
399
    std::string help_text;
400
  };
401
402
  // An implied option is composed of the information on where to store a
403
  // specific boolean value (if another specific option is encountered).
404
449272
  struct Implication {
405
    std::shared_ptr<BaseOptionField> target_field;
406
    bool target_value;
407
  };
408
409
  // These are helpers that make `Insert()` support properties of other
410
  // options structs, if we know how to access them.
411
  template <typename OriginalField, typename ChildOptions>
412
  static auto Convert(
413
      std::shared_ptr<OriginalField> original,
414
      ChildOptions* (Options::* get_child)());
415
  template <typename ChildOptions>
416
  static auto Convert(
417
      typename OptionsParser<ChildOptions>::OptionInfo original,
418
      ChildOptions* (Options::* get_child)());
419
  template <typename ChildOptions>
420
  static auto Convert(
421
      typename OptionsParser<ChildOptions>::Implication original,
422
      ChildOptions* (Options::* get_child)());
423
424
  std::unordered_map<std::string, OptionInfo> options_;
425
  std::unordered_map<std::string, std::vector<std::string>> aliases_;
426
  std::unordered_multimap<std::string, Implication> implications_;
427
428
  template <typename OtherOptions>
429
  friend class OptionsParser;
430
431
  friend void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
432
};
433
434
using StringVector = std::vector<std::string>;
435
template <class OptionsType, class = Options>
436
void Parse(
437
  StringVector* const args, StringVector* const exec_args,
438
  StringVector* const v8_args, OptionsType* const options,
439
  OptionEnvvarSettings required_env_settings, StringVector* const errors);
440
441
}  // namespace options_parser
442
443
namespace per_process {
444
445
extern Mutex cli_options_mutex;
446
extern std::shared_ptr<PerProcessOptions> cli_options;
447
448
}  // namespace per_process
449
}  // namespace node
450
451
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
452
453
#endif  // SRC_NODE_OPTIONS_H_