Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: vm/flags.cc

Issue 9668034: Fix issue 1950: (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « vm/flags.h ('k') | vm/parser.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/flags.h" 5 #include "vm/flags.h"
6 6
7 #include "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/os.h" 8 #include "vm/os.h"
9 9
10 namespace dart { 10 namespace dart {
11 11
12 DEFINE_FLAG(bool, print_flags, false, "Print flags as they are being parsed."); 12 DEFINE_FLAG(bool, print_flags, false, "Print flags as they are being parsed.");
13 DEFINE_FLAG(bool, ignore_unrecognized_flags, false, 13 DEFINE_FLAG(bool, ignore_unrecognized_flags, false,
14 "Ignore unrecognized flags."); 14 "Ignore unrecognized flags.");
15 15
16 // List of registered flags. 16 // List of registered flags.
17 Flag* Flags::flags_ = NULL; 17 Flag* Flags::flags_ = NULL;
18 18
19 bool Flags::initialized_ = false; 19 bool Flags::initialized_ = false;
20 20
21 class Flag { 21 class Flag {
22 public: 22 public:
23 enum FlagType { 23 enum FlagType {
24 kBoolean, 24 kBoolean,
25 kInteger, 25 kInteger,
26 kString, 26 kString,
27 kFunc,
27 kNumFlagTypes 28 kNumFlagTypes
28 }; 29 };
29 30
30 Flag(const char* name, const char* comment, void* addr, FlagType type) 31 Flag(const char* name, const char* comment, void* addr, FlagType type)
31 : name_(name), comment_(comment), addr_(addr), type_(type) { 32 : name_(name), comment_(comment), addr_(addr), type_(type) {
32 } 33 }
34 Flag(const char* name, const char* comment, FlagHandler handler)
35 : name_(name), comment_(comment), handler_(handler), type_(kFunc) {
36 }
33 37
34 void Print() { 38 void Print() {
35 if (IsUnrecognized()) { 39 if (IsUnrecognized()) {
36 OS::Print("%s: unrecognized\n", name_); 40 OS::Print("%s: unrecognized\n", name_);
37 return; 41 return;
38 } 42 }
39 switch (type_) { 43 switch (type_) {
40 case kBoolean: { 44 case kBoolean: {
41 OS::Print("%s: %s (%s)\n", 45 OS::Print("%s: %s (%s)\n",
42 name_, *this->bool_ptr_ ? "true" : "false", comment_); 46 name_, *this->bool_ptr_ ? "true" : "false", comment_);
43 break; 47 break;
44 } 48 }
45 case kInteger: { 49 case kInteger: {
46 OS::Print("%s: %d (%s)\n", name_, *this->int_ptr_, comment_); 50 OS::Print("%s: %d (%s)\n", name_, *this->int_ptr_, comment_);
47 break; 51 break;
48 } 52 }
49 case kString: { 53 case kString: {
50 if (*this->charp_ptr_ != NULL) { 54 if (*this->charp_ptr_ != NULL) {
51 OS::Print("%s: '%s' (%s)\n", name_, *this->charp_ptr_, comment_); 55 OS::Print("%s: '%s' (%s)\n", name_, *this->charp_ptr_, comment_);
52 } else { 56 } else {
53 OS::Print("%s: (null) (%s)\n", name_, comment_); 57 OS::Print("%s: (null) (%s)\n", name_, comment_);
54 } 58 }
55 break; 59 break;
56 } 60 }
61 case kFunc: {
62 // No value to print here.
regis 2012/03/09 23:16:03 You could maybe print the name of the flag.
Ivan Posva 2012/03/09 23:20:37 Done.
63 break;
64 }
57 default: 65 default:
58 UNREACHABLE(); 66 UNREACHABLE();
59 break; 67 break;
60 } 68 }
61 } 69 }
62 70
63 bool IsUnrecognized() const { 71 bool IsUnrecognized() const {
64 return (type_ == kBoolean) && (bool_ptr_ == NULL); 72 return (type_ == kBoolean) && (bool_ptr_ == NULL);
65 } 73 }
66 74
67 Flag* next_; 75 Flag* next_;
68 const char* name_; 76 const char* name_;
69 const char* comment_; 77 const char* comment_;
70 union { 78 union {
71 void* addr_; 79 void* addr_;
72 bool* bool_ptr_; 80 bool* bool_ptr_;
73 int* int_ptr_; 81 int* int_ptr_;
74 charp* charp_ptr_; 82 charp* charp_ptr_;
83 FlagHandler handler_;
75 }; 84 };
76 FlagType type_; 85 FlagType type_;
77 }; 86 };
78 87
79 88
80 Flag* Flags::Lookup(const char* name) { 89 Flag* Flags::Lookup(const char* name) {
81 Flag* cur = Flags::flags_; 90 Flag* cur = Flags::flags_;
82 while (cur != NULL) { 91 while (cur != NULL) {
83 if (strcmp(cur->name_, name) == 0) { 92 if (strcmp(cur->name_, name) == 0) {
84 return cur; 93 return cur;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 const char* default_value, 131 const char* default_value,
123 const char* comment) { 132 const char* comment) {
124 ASSERT(Lookup(name) == NULL); 133 ASSERT(Lookup(name) == NULL);
125 Flag* flag = new Flag(name, comment, addr, Flag::kString); 134 Flag* flag = new Flag(name, comment, addr, Flag::kString);
126 flag->next_ = Flags::flags_; 135 flag->next_ = Flags::flags_;
127 Flags::flags_ = flag; 136 Flags::flags_ = flag;
128 return default_value; 137 return default_value;
129 } 138 }
130 139
131 140
141 bool Flags::Register_func(FlagHandler handler,
142 const char* name,
143 const char* comment) {
144 ASSERT(Lookup(name) == NULL);
145 Flag* flag = new Flag(name, comment, handler);
146 flag->next_ = Flags::flags_;
147 Flags::flags_ = flag;
148 return false;
149 }
150
151
132 static void Normalize(char* s) { 152 static void Normalize(char* s) {
133 intptr_t len = strlen(s); 153 intptr_t len = strlen(s);
134 for (intptr_t i = 0; i < len; i++) { 154 for (intptr_t i = 0; i < len; i++) {
135 if (s[i] == '-') { 155 if (s[i] == '-') {
136 s[i] = '_'; 156 s[i] = '_';
137 } 157 }
138 } 158 }
139 } 159 }
140 160
141 161
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 break; 219 break;
200 } 220 }
201 case Flag::kInteger: { 221 case Flag::kInteger: {
202 char* endptr = NULL; 222 char* endptr = NULL;
203 int val = strtol(argument, &endptr, 10); 223 int val = strtol(argument, &endptr, 10);
204 if (endptr != argument) { 224 if (endptr != argument) {
205 *flag->int_ptr_ = val; 225 *flag->int_ptr_ = val;
206 } 226 }
207 break; 227 break;
208 } 228 }
229 case Flag::kFunc: {
230 if (strcmp(argument, "true") == 0) {
231 (flag->handler_)(true);
232 } else if (strcmp(argument, "false") == 0) {
233 (flag->handler_)(false);
234 } else {
235 OS::Print("Ignoring flag: %s is a bool flag.\n", name);
236 }
237 break;
238 }
209 default: { 239 default: {
210 UNREACHABLE(); 240 UNREACHABLE();
211 break; 241 break;
212 } 242 }
213 } 243 }
214 } 244 }
215 } 245 }
216 246
217 delete[] name; 247 delete[] name;
218 } 248 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 while (flag != NULL) { 301 while (flag != NULL) {
272 flag->Print(); 302 flag->Print();
273 flag = flag->next_; 303 flag = flag->next_;
274 } 304 }
275 } 305 }
276 306
277 return true; 307 return true;
278 } 308 }
279 309
280 } // namespace dart 310 } // namespace dart
OLDNEW
« no previous file with comments | « vm/flags.h ('k') | vm/parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698