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-05-05 22:32:45 Branches: 33 62 53.2 %

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
21715
class HostPort {
17
 public:
18
4631
  HostPort(const std::string& host_name, int port)
19
4631
      : host_name_(host_name), port_(port) {}
20
13953
  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
4720
  const std::string& host() const { return host_name_; }
30
31
4804
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
4804
    CHECK_GE(port_, 0);
34
4804
    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
41392
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
34456
  virtual ~Options() = default;
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
17349
class DebugOptions : public Options {
58
 public:
59
4536
  DebugOptions() = default;
60
9302
  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
  // Used to patch the options as if --inspect-brk is passed.
79
26
  void EnableBreakFirstLine() {
80
26
    inspector_enabled = true;
81
26
    break_first_line = true;
82
26
  }
83
84
4652
  bool wait_for_connect() const {
85

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


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


941200
    virtual ~BaseOptionField() = default;
346
    virtual void* LookupImpl(Options* options) const = 0;
347
348
    template <typename T>
349
345548
    inline T* Lookup(Options* options) const {
350
345548
      return static_cast<T*>(LookupImpl(options));
351
    }
352
  };
353
354
  // Represents a field of type T within `Options` that can be looked up
355
  // as a C++ member field.
356
  template <typename T>
357






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