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-07-28 22:34:34 Branches: 34 64 53.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 "node_mutex.h"
12
#include "util.h"
13
14
namespace node {
15
16
23216
class HostPort {
17
 public:
18
4938
  HostPort(const std::string& host_name, int port)
19
4938
      : host_name_(host_name), port_(port) {}
20
14895
  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
5046
  const std::string& host() const { return host_name_; }
30
31
5130
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
5130
    CHECK_GE(port_, 0);
34
5130
    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
44182
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
36847
  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
18549
class DebugOptions : public Options {
63
 public:
64
4840
  DebugOptions() = default;
65
9930
  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
4965
  bool wait_for_connect() const {
94

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


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


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







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