OLD | NEW |
---|---|
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/flags.h" | 5 #include "src/flags.h" |
6 | 6 |
7 #include <cctype> | 7 #include <cctype> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 #include <sstream> | 9 #include <sstream> |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... | |
26 // Define all of our flags default values. | 26 // Define all of our flags default values. |
27 #define FLAG_MODE_DEFINE_DEFAULTS | 27 #define FLAG_MODE_DEFINE_DEFAULTS |
28 #include "src/flag-definitions.h" // NOLINT(build/include) | 28 #include "src/flag-definitions.h" // NOLINT(build/include) |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 // This structure represents a single entry in the flag system, with a pointer | 32 // This structure represents a single entry in the flag system, with a pointer |
33 // to the actual flag, default value, comment, etc. This is designed to be POD | 33 // to the actual flag, default value, comment, etc. This is designed to be POD |
34 // initialized as to avoid requiring static constructors. | 34 // initialized as to avoid requiring static constructors. |
35 struct Flag { | 35 struct Flag { |
36 enum FlagType { TYPE_BOOL, TYPE_MAYBE_BOOL, TYPE_INT, TYPE_FLOAT, | 36 enum FlagType { |
37 TYPE_STRING, TYPE_ARGS }; | 37 TYPE_BOOL, |
38 TYPE_MAYBE_BOOL, | |
39 TYPE_INT, | |
40 TYPE_UINT, | |
41 TYPE_FLOAT, | |
42 TYPE_STRING, | |
43 TYPE_ARGS | |
44 }; | |
38 | 45 |
39 FlagType type_; // What type of flag, bool, int, or string. | 46 FlagType type_; // What type of flag, bool, int, or string. |
40 const char* name_; // Name of the flag, ex "my_flag". | 47 const char* name_; // Name of the flag, ex "my_flag". |
41 void* valptr_; // Pointer to the global flag variable. | 48 void* valptr_; // Pointer to the global flag variable. |
42 const void* defptr_; // Pointer to the default value. | 49 const void* defptr_; // Pointer to the default value. |
43 const char* cmt_; // A comment about the flags purpose. | 50 const char* cmt_; // A comment about the flags purpose. |
44 bool owns_ptr_; // Does the flag own its string value? | 51 bool owns_ptr_; // Does the flag own its string value? |
45 | 52 |
46 FlagType type() const { return type_; } | 53 FlagType type() const { return type_; } |
47 | 54 |
48 const char* name() const { return name_; } | 55 const char* name() const { return name_; } |
49 | 56 |
50 const char* comment() const { return cmt_; } | 57 const char* comment() const { return cmt_; } |
51 | 58 |
52 bool* bool_variable() const { | 59 bool* bool_variable() const { |
53 DCHECK(type_ == TYPE_BOOL); | 60 DCHECK(type_ == TYPE_BOOL); |
54 return reinterpret_cast<bool*>(valptr_); | 61 return reinterpret_cast<bool*>(valptr_); |
55 } | 62 } |
56 | 63 |
57 MaybeBoolFlag* maybe_bool_variable() const { | 64 MaybeBoolFlag* maybe_bool_variable() const { |
58 DCHECK(type_ == TYPE_MAYBE_BOOL); | 65 DCHECK(type_ == TYPE_MAYBE_BOOL); |
59 return reinterpret_cast<MaybeBoolFlag*>(valptr_); | 66 return reinterpret_cast<MaybeBoolFlag*>(valptr_); |
60 } | 67 } |
61 | 68 |
62 int* int_variable() const { | 69 int* int_variable() const { |
63 DCHECK(type_ == TYPE_INT); | 70 DCHECK(type_ == TYPE_INT); |
64 return reinterpret_cast<int*>(valptr_); | 71 return reinterpret_cast<int*>(valptr_); |
65 } | 72 } |
66 | 73 |
74 int* uint_variable() const { | |
titzer
2017/01/23 10:20:44
Should this return an unsigned int*?
ahaas
2017/01/23 11:47:37
Yes, I guess so. Thanks.
| |
75 DCHECK(type_ == TYPE_UINT); | |
76 return reinterpret_cast<int*>(valptr_); | |
77 } | |
78 | |
67 double* float_variable() const { | 79 double* float_variable() const { |
68 DCHECK(type_ == TYPE_FLOAT); | 80 DCHECK(type_ == TYPE_FLOAT); |
69 return reinterpret_cast<double*>(valptr_); | 81 return reinterpret_cast<double*>(valptr_); |
70 } | 82 } |
71 | 83 |
72 const char* string_value() const { | 84 const char* string_value() const { |
73 DCHECK(type_ == TYPE_STRING); | 85 DCHECK(type_ == TYPE_STRING); |
74 return *reinterpret_cast<const char**>(valptr_); | 86 return *reinterpret_cast<const char**>(valptr_); |
75 } | 87 } |
76 | 88 |
(...skipping 13 matching lines...) Expand all Loading... | |
90 bool bool_default() const { | 102 bool bool_default() const { |
91 DCHECK(type_ == TYPE_BOOL); | 103 DCHECK(type_ == TYPE_BOOL); |
92 return *reinterpret_cast<const bool*>(defptr_); | 104 return *reinterpret_cast<const bool*>(defptr_); |
93 } | 105 } |
94 | 106 |
95 int int_default() const { | 107 int int_default() const { |
96 DCHECK(type_ == TYPE_INT); | 108 DCHECK(type_ == TYPE_INT); |
97 return *reinterpret_cast<const int*>(defptr_); | 109 return *reinterpret_cast<const int*>(defptr_); |
98 } | 110 } |
99 | 111 |
112 int uint_default() const { | |
titzer
2017/01/23 10:20:44
unsigned int?
ahaas
2017/01/23 11:47:37
Done.
| |
113 DCHECK(type_ == TYPE_UINT); | |
114 return *reinterpret_cast<const unsigned int*>(defptr_); | |
115 } | |
116 | |
100 double float_default() const { | 117 double float_default() const { |
101 DCHECK(type_ == TYPE_FLOAT); | 118 DCHECK(type_ == TYPE_FLOAT); |
102 return *reinterpret_cast<const double*>(defptr_); | 119 return *reinterpret_cast<const double*>(defptr_); |
103 } | 120 } |
104 | 121 |
105 const char* string_default() const { | 122 const char* string_default() const { |
106 DCHECK(type_ == TYPE_STRING); | 123 DCHECK(type_ == TYPE_STRING); |
107 return *reinterpret_cast<const char* const *>(defptr_); | 124 return *reinterpret_cast<const char* const *>(defptr_); |
108 } | 125 } |
109 | 126 |
110 JSArguments args_default() const { | 127 JSArguments args_default() const { |
111 DCHECK(type_ == TYPE_ARGS); | 128 DCHECK(type_ == TYPE_ARGS); |
112 return *reinterpret_cast<const JSArguments*>(defptr_); | 129 return *reinterpret_cast<const JSArguments*>(defptr_); |
113 } | 130 } |
114 | 131 |
115 // Compare this flag's current value against the default. | 132 // Compare this flag's current value against the default. |
116 bool IsDefault() const { | 133 bool IsDefault() const { |
117 switch (type_) { | 134 switch (type_) { |
118 case TYPE_BOOL: | 135 case TYPE_BOOL: |
119 return *bool_variable() == bool_default(); | 136 return *bool_variable() == bool_default(); |
120 case TYPE_MAYBE_BOOL: | 137 case TYPE_MAYBE_BOOL: |
121 return maybe_bool_variable()->has_value == false; | 138 return maybe_bool_variable()->has_value == false; |
122 case TYPE_INT: | 139 case TYPE_INT: |
123 return *int_variable() == int_default(); | 140 return *int_variable() == int_default(); |
141 case TYPE_UINT: | |
142 return *uint_variable() == uint_default(); | |
124 case TYPE_FLOAT: | 143 case TYPE_FLOAT: |
125 return *float_variable() == float_default(); | 144 return *float_variable() == float_default(); |
126 case TYPE_STRING: { | 145 case TYPE_STRING: { |
127 const char* str1 = string_value(); | 146 const char* str1 = string_value(); |
128 const char* str2 = string_default(); | 147 const char* str2 = string_default(); |
129 if (str2 == NULL) return str1 == NULL; | 148 if (str2 == NULL) return str1 == NULL; |
130 if (str1 == NULL) return str2 == NULL; | 149 if (str1 == NULL) return str2 == NULL; |
131 return strcmp(str1, str2) == 0; | 150 return strcmp(str1, str2) == 0; |
132 } | 151 } |
133 case TYPE_ARGS: | 152 case TYPE_ARGS: |
134 return args_variable()->argc == 0; | 153 return args_variable()->argc == 0; |
135 } | 154 } |
136 UNREACHABLE(); | 155 UNREACHABLE(); |
137 return true; | 156 return true; |
138 } | 157 } |
139 | 158 |
140 // Set a flag back to it's default value. | 159 // Set a flag back to it's default value. |
141 void Reset() { | 160 void Reset() { |
142 switch (type_) { | 161 switch (type_) { |
143 case TYPE_BOOL: | 162 case TYPE_BOOL: |
144 *bool_variable() = bool_default(); | 163 *bool_variable() = bool_default(); |
145 break; | 164 break; |
146 case TYPE_MAYBE_BOOL: | 165 case TYPE_MAYBE_BOOL: |
147 *maybe_bool_variable() = MaybeBoolFlag::Create(false, false); | 166 *maybe_bool_variable() = MaybeBoolFlag::Create(false, false); |
148 break; | 167 break; |
149 case TYPE_INT: | 168 case TYPE_INT: |
150 *int_variable() = int_default(); | 169 *int_variable() = int_default(); |
151 break; | 170 break; |
171 case TYPE_UINT: | |
172 *uint_variable() = uint_default(); | |
173 break; | |
152 case TYPE_FLOAT: | 174 case TYPE_FLOAT: |
153 *float_variable() = float_default(); | 175 *float_variable() = float_default(); |
154 break; | 176 break; |
155 case TYPE_STRING: | 177 case TYPE_STRING: |
156 set_string_value(string_default(), false); | 178 set_string_value(string_default(), false); |
157 break; | 179 break; |
158 case TYPE_ARGS: | 180 case TYPE_ARGS: |
159 *args_variable() = args_default(); | 181 *args_variable() = args_default(); |
160 break; | 182 break; |
161 } | 183 } |
162 } | 184 } |
163 }; | 185 }; |
164 | 186 |
165 Flag flags[] = { | 187 Flag flags[] = { |
166 #define FLAG_MODE_META | 188 #define FLAG_MODE_META |
167 #include "src/flag-definitions.h" // NOLINT(build/include) | 189 #include "src/flag-definitions.h" // NOLINT(build/include) |
168 }; | 190 }; |
169 | 191 |
170 const size_t num_flags = sizeof(flags) / sizeof(*flags); | 192 const size_t num_flags = sizeof(flags) / sizeof(*flags); |
171 | 193 |
172 } // namespace | 194 } // namespace |
173 | 195 |
174 | 196 |
175 static const char* Type2String(Flag::FlagType type) { | 197 static const char* Type2String(Flag::FlagType type) { |
176 switch (type) { | 198 switch (type) { |
177 case Flag::TYPE_BOOL: return "bool"; | 199 case Flag::TYPE_BOOL: return "bool"; |
178 case Flag::TYPE_MAYBE_BOOL: return "maybe_bool"; | 200 case Flag::TYPE_MAYBE_BOOL: return "maybe_bool"; |
179 case Flag::TYPE_INT: return "int"; | 201 case Flag::TYPE_INT: return "int"; |
202 case Flag::TYPE_UINT: | |
203 return "uint"; | |
180 case Flag::TYPE_FLOAT: return "float"; | 204 case Flag::TYPE_FLOAT: return "float"; |
181 case Flag::TYPE_STRING: return "string"; | 205 case Flag::TYPE_STRING: return "string"; |
182 case Flag::TYPE_ARGS: return "arguments"; | 206 case Flag::TYPE_ARGS: return "arguments"; |
183 } | 207 } |
184 UNREACHABLE(); | 208 UNREACHABLE(); |
185 return NULL; | 209 return NULL; |
186 } | 210 } |
187 | 211 |
188 | 212 |
189 std::ostream& operator<<(std::ostream& os, const Flag& flag) { // NOLINT | 213 std::ostream& operator<<(std::ostream& os, const Flag& flag) { // NOLINT |
190 switch (flag.type()) { | 214 switch (flag.type()) { |
191 case Flag::TYPE_BOOL: | 215 case Flag::TYPE_BOOL: |
192 os << (*flag.bool_variable() ? "true" : "false"); | 216 os << (*flag.bool_variable() ? "true" : "false"); |
193 break; | 217 break; |
194 case Flag::TYPE_MAYBE_BOOL: | 218 case Flag::TYPE_MAYBE_BOOL: |
195 os << (flag.maybe_bool_variable()->has_value | 219 os << (flag.maybe_bool_variable()->has_value |
196 ? (flag.maybe_bool_variable()->value ? "true" : "false") | 220 ? (flag.maybe_bool_variable()->value ? "true" : "false") |
197 : "unset"); | 221 : "unset"); |
198 break; | 222 break; |
199 case Flag::TYPE_INT: | 223 case Flag::TYPE_INT: |
200 os << *flag.int_variable(); | 224 os << *flag.int_variable(); |
201 break; | 225 break; |
226 case Flag::TYPE_UINT: | |
227 os << *flag.uint_variable(); | |
228 break; | |
202 case Flag::TYPE_FLOAT: | 229 case Flag::TYPE_FLOAT: |
203 os << *flag.float_variable(); | 230 os << *flag.float_variable(); |
204 break; | 231 break; |
205 case Flag::TYPE_STRING: { | 232 case Flag::TYPE_STRING: { |
206 const char* str = flag.string_value(); | 233 const char* str = flag.string_value(); |
207 os << (str ? str : "NULL"); | 234 os << (str ? str : "NULL"); |
208 break; | 235 break; |
209 } | 236 } |
210 case Flag::TYPE_ARGS: { | 237 case Flag::TYPE_ARGS: { |
211 JSArguments args = *flag.args_variable(); | 238 JSArguments args = *flag.args_variable(); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 switch (flag->type()) { | 419 switch (flag->type()) { |
393 case Flag::TYPE_BOOL: | 420 case Flag::TYPE_BOOL: |
394 *flag->bool_variable() = !is_bool; | 421 *flag->bool_variable() = !is_bool; |
395 break; | 422 break; |
396 case Flag::TYPE_MAYBE_BOOL: | 423 case Flag::TYPE_MAYBE_BOOL: |
397 *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !is_bool); | 424 *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !is_bool); |
398 break; | 425 break; |
399 case Flag::TYPE_INT: | 426 case Flag::TYPE_INT: |
400 *flag->int_variable() = static_cast<int>(strtol(value, &endp, 10)); | 427 *flag->int_variable() = static_cast<int>(strtol(value, &endp, 10)); |
401 break; | 428 break; |
429 case Flag::TYPE_UINT: { | |
430 // We do not use strtoul because it accepts negative numbers. | |
431 int64_t val = static_cast<int64_t>(strtoll(value, &endp, 10)); | |
432 if (val < 0 || val > std::numeric_limits<unsigned int>::max()) { | |
433 PrintF(stderr, | |
434 "Error: illegal value for flag %s of type %s\n" | |
titzer
2017/01/23 10:20:44
Can you add a more helpful error message? E.g. tha
ahaas
2017/01/23 11:47:37
Done.
| |
435 "Try --help for options\n", | |
436 arg, Type2String(flag->type())); | |
437 return_code = j; | |
438 break; | |
439 } | |
440 *flag->uint_variable() = static_cast<unsigned int>(val); | |
441 break; | |
442 } | |
402 case Flag::TYPE_FLOAT: | 443 case Flag::TYPE_FLOAT: |
403 *flag->float_variable() = strtod(value, &endp); | 444 *flag->float_variable() = strtod(value, &endp); |
404 break; | 445 break; |
405 case Flag::TYPE_STRING: | 446 case Flag::TYPE_STRING: |
406 flag->set_string_value(value ? StrDup(value) : NULL, true); | 447 flag->set_string_value(value ? StrDup(value) : NULL, true); |
407 break; | 448 break; |
408 case Flag::TYPE_ARGS: { | 449 case Flag::TYPE_ARGS: { |
409 int start_pos = (value == NULL) ? i : i - 1; | 450 int start_pos = (value == NULL) ? i : i - 1; |
410 int js_argc = *argc - start_pos; | 451 int js_argc = *argc - start_pos; |
411 const char** js_argv = NewArray<const char*>(js_argc); | 452 const char** js_argv = NewArray<const char*>(js_argc); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
576 #define FLAG_MODE_DEFINE_IMPLICATIONS | 617 #define FLAG_MODE_DEFINE_IMPLICATIONS |
577 #include "src/flag-definitions.h" // NOLINT(build/include) | 618 #include "src/flag-definitions.h" // NOLINT(build/include) |
578 #undef FLAG_MODE_DEFINE_IMPLICATIONS | 619 #undef FLAG_MODE_DEFINE_IMPLICATIONS |
579 ComputeFlagListHash(); | 620 ComputeFlagListHash(); |
580 } | 621 } |
581 | 622 |
582 | 623 |
583 uint32_t FlagList::Hash() { return flag_hash; } | 624 uint32_t FlagList::Hash() { return flag_hash; } |
584 } // namespace internal | 625 } // namespace internal |
585 } // namespace v8 | 626 } // namespace v8 |
OLD | NEW |