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: 45 47 95.7 %
Date: 2020-02-27 22:14:15 Branches: 7 10 70.0 %

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
20254
class HostPort {
17
 public:
18
4331
  HostPort(const std::string& host_name, int port)
19
4331
      : host_name_(host_name), port_(port) {}
20
13176
  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
79
  void set_port(int port) { port_ = port; }
28
29
4452
  const std::string& host() const { return host_name_; }
30
31
4548
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
4548
    CHECK_GE(port_, 0);
34
4548
    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
38839
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
32037
  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
16171
class DebugOptions : public Options {
63
 public:
64
4233
  DebugOptions() = default;
65
8784
  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
4368
  bool wait_for_connect() const {
94

4368
    return break_first_line || break_node_first_line;
95
  }
96
97
  void CheckOptions(std::vector<std::string>* errors) override;
98
};
99
100
24257
class EnvironmentOptions : public Options {
101
 public:
102
  bool abort_on_uncaught_exception = false;
103
  bool enable_source_maps = false;
104
  bool experimental_json_modules = false;
105
  bool experimental_modules = false;
106
  std::string experimental_specifier_resolution;
107
  std::string es_module_specifier_resolution;
108
  bool experimental_wasm_modules = false;
109
  bool experimental_import_meta_resolve = false;
110
  std::string module_type;
111
  std::string experimental_policy;
112
  std::string experimental_policy_integrity;
113
  bool has_policy_integrity_string;
114
  bool experimental_repl_await = false;
115
  bool experimental_vm_modules = false;
116
  bool expose_internals = false;
117
  bool frozen_intrinsics = false;
118
  std::string heap_snapshot_signal;
119
  uint64_t max_http_header_size = 8 * 1024;
120
  bool no_deprecation = false;
121
  bool no_force_async_hooks_checks = false;
122
  bool no_warnings = false;
123
  bool force_context_aware = false;
124
  bool pending_deprecation = false;
125
  bool preserve_symlinks = false;
126
  bool preserve_symlinks_main = false;
127
  bool prof_process = false;
128
#if HAVE_INSPECTOR
129
  std::string cpu_prof_dir;
130
  static const uint64_t kDefaultCpuProfInterval = 1000;
131
  uint64_t cpu_prof_interval = kDefaultCpuProfInterval;
132
  std::string cpu_prof_name;
133
  bool cpu_prof = false;
134
  std::string heap_prof_dir;
135
  std::string heap_prof_name;
136
  static const uint64_t kDefaultHeapProfInterval = 512 * 1024;
137
  uint64_t heap_prof_interval = kDefaultHeapProfInterval;
138
  bool heap_prof = false;
139
#endif  // HAVE_INSPECTOR
140
  std::string redirect_warnings;
141
  bool test_udp_no_try_send = false;
142
  bool throw_deprecation = false;
143
  bool trace_deprecation = false;
144
  bool trace_exit = false;
145
  bool trace_sync_io = false;
146
  bool trace_tls = false;
147
  bool trace_uncaught = false;
148
  bool trace_warnings = false;
149
  std::string unhandled_rejections;
150
  std::string userland_loader;
151
152
  bool syntax_check_only = false;
153
  bool has_eval_string = false;
154
#ifdef NODE_REPORT
155
  bool experimental_report = false;
156
#endif  //  NODE_REPORT
157
  bool experimental_wasi = false;
158
  std::string eval_string;
159
  bool print_eval = false;
160
  bool force_repl = false;
161
162
  bool insecure_http_parser = false;
163
164
  bool tls_min_v1_0 = false;
165
  bool tls_min_v1_1 = false;
166
  bool tls_min_v1_2 = false;
167
  bool tls_min_v1_3 = false;
168
  bool tls_max_v1_2 = false;
169
  bool tls_max_v1_3 = false;
170
  std::string tls_keylog;
171
172
  std::vector<std::string> preload_modules;
173
174
  std::vector<std::string> user_argv;
175
176
30780
  inline DebugOptions* get_debug_options() { return &debug_options_; }
177
17518
  inline const DebugOptions& debug_options() const { return debug_options_; }
178
179
  void CheckOptions(std::vector<std::string>* errors) override;
180
181
 private:
182
  DebugOptions debug_options_;
183
};
184
185
29306
class PerIsolateOptions : public Options {
186
 public:
187
4233
  std::shared_ptr<EnvironmentOptions> per_env { new EnvironmentOptions() };
188
  bool track_heap_objects = false;
189
  bool no_node_snapshot = false;
190
191
#ifdef NODE_REPORT
192
  bool report_uncaught_exception = false;
193
  bool report_on_signal = false;
194
  bool report_on_fatalerror = false;
195
  std::string report_signal;
196
  std::string report_filename;
197
  std::string report_directory;
198
#endif  //  NODE_REPORT
199
  inline EnvironmentOptions* get_per_env_options();
200
  void CheckOptions(std::vector<std::string>* errors) override;
201
};
202
203
16808
class PerProcessOptions : public Options {
204
 public:
205
4202
  std::shared_ptr<PerIsolateOptions> per_isolate { new PerIsolateOptions() };
206
207
  std::string title;
208
  std::string trace_event_categories;
209
  std::string trace_event_file_pattern = "node_trace.${rotation}.log";
210
  int64_t v8_thread_pool_size = 4;
211
  bool zero_fill_all_buffers = false;
212
  bool debug_arraybuffer_allocations = false;
213
214
  std::vector<std::string> security_reverts;
215
  bool print_bash_completion = false;
216
  bool print_help = false;
217
  bool print_v8_help = false;
218
  bool print_version = false;
219
220
#ifdef NODE_HAVE_I18N_SUPPORT
221
  std::string icu_data_dir;
222
#endif
223
224
  // TODO(addaleax): Some of these could probably be per-Environment.
225
#if HAVE_OPENSSL
226
  std::string openssl_config;
227
  std::string tls_cipher_list = DEFAULT_CIPHER_LIST_CORE;
228
#ifdef NODE_OPENSSL_CERT_STORE
229
  bool ssl_openssl_cert_store = true;
230
#else
231
  bool ssl_openssl_cert_store = false;
232
#endif
233
  bool use_openssl_ca = false;
234
  bool use_bundled_ca = false;
235
#if NODE_FIPS_MODE
236
  bool enable_fips_crypto = false;
237
  bool force_fips_crypto = false;
238
#endif
239
#endif
240
  std::string use_largepages = "off";
241
  bool trace_sigint = false;
242
243
#ifdef NODE_REPORT
244
  std::vector<std::string> cmdline;
245
#endif  //  NODE_REPORT
246
247
  inline PerIsolateOptions* get_per_isolate_options();
248
  void CheckOptions(std::vector<std::string>* errors) override;
249
};
250
251
// The actual options parser, as opposed to the structs containing them:
252
253
namespace options_parser {
254
255
HostPort SplitHostPort(const std::string& arg,
256
    std::vector<std::string>* errors);
257
void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
258
std::string GetBashCompletion();
259
260
enum OptionType {
261
  kNoOp,
262
  kV8Option,
263
  kBoolean,
264
  kInteger,
265
  kUInteger,
266
  kString,
267
  kHostPort,
268
  kStringList,
269
};
270
271
template <typename Options>
272
16808
class OptionsParser {
273
 public:
274
16808
  virtual ~OptionsParser() = default;
275
276
  typedef Options TargetType;
277
278
  struct NoOp {};
279
  struct V8Option {};
280
281
  // These methods add a single option to the parser. Optionally, it can be
282
  // specified whether the option should be allowed from environment variable
283
  // sources (i.e. NODE_OPTIONS).
284
  void AddOption(const char* name,
285
                 const char* help_text,
286
                 bool Options::* field,
287
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
288
  void AddOption(const char* name,
289
                 const char* help_text,
290
                 uint64_t Options::* field,
291
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
292
  void AddOption(const char* name,
293
                 const char* help_text,
294
                 int64_t Options::* field,
295
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
296
  void AddOption(const char* name,
297
                 const char* help_text,
298
                 std::string Options::* field,
299
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
300
  void AddOption(const char* name,
301
                 const char* help_text,
302
                 std::vector<std::string> Options::* field,
303
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
304
  void AddOption(const char* name,
305
                 const char* help_text,
306
                 HostPort Options::* field,
307
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
308
  void AddOption(const char* name,
309
                 const char* help_text,
310
                 NoOp no_op_tag,
311
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
312
  void AddOption(const char* name,
313
                 const char* help_text,
314
                 V8Option v8_option_tag,
315
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
316
317
  // Adds aliases. An alias can be of the form "--option-a" -> "--option-b",
318
  // or have a more complex group expansion, like
319
  //   "--option-a" -> { "--option-b", "--harmony-foobar", "--eval", "42" }
320
  // If `from` has the form "--option-a=", the alias will only be expanded if
321
  // the option is presented in that form (i.e. with a '=').
322
  // If `from` has the form "--option-a <arg>", the alias will only be expanded
323
  // if the option has a non-option argument (not starting with -) following it.
324
  void AddAlias(const char* from, const char* to);
325
  void AddAlias(const char* from, const std::vector<std::string>& to);
326
  void AddAlias(const char* from,
327
                const std::initializer_list<std::string>& to);
328
329
  // Add implications from some arbitrary option to a boolean one, either
330
  // in a way that makes `from` set `to` to true or to false.
331
  void Implies(const char* from, const char* to);
332
  void ImpliesNot(const char* from, const char* to);
333
334
  // Insert options from another options parser into this one, along with
335
  // a method that yields the target options type from this parser's options
336
  // type.
337
  template <typename ChildOptions>
338
  void Insert(const OptionsParser<ChildOptions>& child_options_parser,
339
              ChildOptions* (Options::* get_child)());
340
341
  // Parse a sequence of options into an options struct, a list of
342
  // arguments that were parsed as options, a list of unknown/JS engine options,
343
  // and leave the remainder in the input `args` vector.
344
  //
345
  // For example, an `args` input of
346
  //
347
  //   node --foo --harmony-bar --fizzle=42 -- /path/to/cow moo
348
  //
349
  // expands as
350
  //
351
  // - `args` -> { "node", "/path/to/cow", "moo" }
352
  // - `exec_args` -> { "--foo", "--harmony-bar", "--fizzle=42" }
353
  // - `v8_args` -> `{ "node", "--harmony-bar" }
354
  // - `options->foo == true`, `options->fizzle == 42`.
355
  //
356
  // If `*error` is set, the result of the parsing should be discarded and the
357
  // contents of any of the argument vectors should be considered undefined.
358
  void Parse(std::vector<std::string>* const args,
359
             std::vector<std::string>* const exec_args,
360
             std::vector<std::string>* const v8_args,
361
             Options* const options,
362
             OptionEnvvarSettings required_env_settings,
363
             std::vector<std::string>* const errors) const;
364
365
 private:
366
  // We support the wide variety of different option types by remembering
367
  // how to access them, given a certain `Options` struct.
368
369
  // Represents a field within `Options`.
370
1130338
  class BaseOptionField {
371
   public:
372
1130338
    virtual ~BaseOptionField() = default;
373
    virtual void* LookupImpl(Options* options) const = 0;
374
375
    template <typename T>
376
412360
    inline T* Lookup(Options* options) const {
377
412360
      return static_cast<T*>(LookupImpl(options));
378
    }
379
  };
380
381
  // Represents a field of type T within `Options` that can be looked up
382
  // as a C++ member field.
383
  template <typename T>
384
394988
  class SimpleOptionField : public BaseOptionField {
385
   public:
386
394988
    explicit SimpleOptionField(T Options::* field) : field_(field) {}
387
412360
    void* LookupImpl(Options* options) const override {
388
412360
      return static_cast<void*>(&(options->*field_));
389
    }
390
391
   private:
392
    T Options::* field_;
393
  };
394
395
  template <typename T>
396
412018
  inline T* Lookup(std::shared_ptr<BaseOptionField> field,
397
                   Options* options) const {
398
412018
    return field->template Lookup<T>(options);
399
  }
400
401
  // An option consists of:
402
  // - A type.
403
  // - A way to store/access the property value.
404
  // - The information of whether it may occur in an env var or not.
405
4781876
  struct OptionInfo {
406
    OptionType type;
407
    std::shared_ptr<BaseOptionField> field;
408
    OptionEnvvarSettings env_setting;
409
    std::string help_text;
410
  };
411
412
  // An implied option is composed of the information on where to store a
413
  // specific boolean value (if another specific option is encountered).
414
319352
  struct Implication {
415
    std::shared_ptr<BaseOptionField> target_field;
416
    bool target_value;
417
  };
418
419
  // These are helpers that make `Insert()` support properties of other
420
  // options structs, if we know how to access them.
421
  template <typename OriginalField, typename ChildOptions>
422
  static auto Convert(
423
      std::shared_ptr<OriginalField> original,
424
      ChildOptions* (Options::* get_child)());
425
  template <typename ChildOptions>
426
  static auto Convert(
427
      typename OptionsParser<ChildOptions>::OptionInfo original,
428
      ChildOptions* (Options::* get_child)());
429
  template <typename ChildOptions>
430
  static auto Convert(
431
      typename OptionsParser<ChildOptions>::Implication original,
432
      ChildOptions* (Options::* get_child)());
433
434
  std::unordered_map<std::string, OptionInfo> options_;
435
  std::unordered_map<std::string, std::vector<std::string>> aliases_;
436
  std::unordered_multimap<std::string, Implication> implications_;
437
438
  template <typename OtherOptions>
439
  friend class OptionsParser;
440
441
  friend void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
442
  friend std::string GetBashCompletion();
443
};
444
445
using StringVector = std::vector<std::string>;
446
template <class OptionsType, class = Options>
447
void Parse(
448
  StringVector* const args, StringVector* const exec_args,
449
  StringVector* const v8_args, OptionsType* const options,
450
  OptionEnvvarSettings required_env_settings, StringVector* const errors);
451
452
}  // namespace options_parser
453
454
namespace per_process {
455
456
extern Mutex cli_options_mutex;
457
extern std::shared_ptr<PerProcessOptions> cli_options;
458
459
}  // namespace per_process
460
461
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options);
462
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
463
                      std::function<std::string(const char*)> opt_getter);
464
465
std::vector<std::string> ParseNodeOptionsEnvVar(
466
    const std::string& node_options, std::vector<std::string>* errors);
467
}  // namespace node
468
469
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
470
471
#endif  // SRC_NODE_OPTIONS_H_