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

Side by Side Diff: src/flags.h

Issue 1935: New static flags system (Closed)
Patch Set: Merge, again. Created 12 years, 3 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
« no previous file with comments | « src/execution.cc ('k') | src/flags.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 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef V8_FLAGS_H_ 27 #ifndef V8_FLAGS_H_
28 #define V8_FLAGS_H_ 28 #define V8_FLAGS_H_
29 29
30 #include "checks.h"
31
30 namespace v8 { namespace internal { 32 namespace v8 { namespace internal {
31 33
32 // Internal use only. 34 // Declare all of our flags.
33 union FlagValue { 35 #define FLAG_MODE_DECLARE
34 static FlagValue New_BOOL(bool b) { 36 #include "flags.defs"
35 FlagValue v;
36 v.b = b;
37 return v;
38 }
39 static FlagValue New_INT(int i) {
40 FlagValue v;
41 v.i = i;
42 return v;
43 }
44 static FlagValue New_FLOAT(float f) {
45 FlagValue v;
46 v.f = f;
47 return v;
48 }
49 static FlagValue New_STRING(const char* s) {
50 FlagValue v;
51 v.s = s;
52 return v;
53 }
54
55 bool b;
56 int i;
57 double f;
58 const char* s;
59 };
60
61
62 // Each flag can be accessed programmatically via a Flag object.
63 class Flag {
64 public:
65 enum Type { BOOL, INT, FLOAT, STRING };
66
67 // Internal use only.
68 Flag(const char* file, const char* name, const char* comment,
69 Type type, void* variable, FlagValue default_);
70
71 // General flag information
72 const char* file() const { return file_; }
73 const char* name() const { return name_; }
74 const char* comment() const { return comment_; }
75
76 // Flag type
77 Type type() const { return type_; }
78
79 // Flag variables
80 inline bool* bool_variable() const;
81 inline int* int_variable() const;
82 inline double* float_variable() const;
83 inline const char** string_variable() const;
84
85 // Default values
86 inline bool bool_default() const;
87 inline int int_default() const;
88 inline double float_default() const;
89 inline const char* string_default() const;
90
91 // Resets a flag to its default value
92 void SetToDefault();
93
94 // True if a flag is set to its default value
95 bool IsDefault() const;
96
97 // Iteration support
98 Flag* next() const { return next_; }
99
100 // Prints flag information. The current flag value is only printed
101 // if print_current_value is set.
102 void Print(bool print_current_value);
103
104
105 // Returns the string formatted value of the flag. The caller is responsible
106 // for disposing the string.
107 char* StringValue() const;
108
109 private:
110 const char* file_;
111 const char* name_;
112 const char* comment_;
113
114 Type type_;
115 FlagValue* variable_;
116 FlagValue default_;
117
118 Flag* next_;
119
120 friend class FlagList; // accesses next_
121 };
122
123
124 // Internal use only.
125 #define DEFINE_FLAG(type, c_type, name, default, comment) \
126 /* define and initialize the flag */ \
127 c_type FLAG_##name = (default); \
128 /* register the flag */ \
129 static v8::internal::Flag Flag_##name(__FILE__, \
130 #name, \
131 (comment), \
132 v8::internal::Flag::type, \
133 &FLAG_##name, \
134 v8::internal::FlagValue::New_##type(default))
135
136
137 // Internal use only.
138 #define DECLARE_FLAG(c_type, name) \
139 /* declare the external flag */ \
140 extern c_type FLAG_##name
141
142
143 // Use the following macros to define a new flag:
144 #define DEFINE_bool(name, default, comment) \
145 DEFINE_FLAG(BOOL, bool, name, default, comment)
146 #define DEFINE_int(name, default, comment) \
147 DEFINE_FLAG(INT, int, name, default, comment)
148 #define DEFINE_float(name, default, comment) \
149 DEFINE_FLAG(FLOAT, double, name, default, comment)
150 #define DEFINE_string(name, default, comment) \
151 DEFINE_FLAG(STRING, const char*, name, default, comment)
152
153
154 // Use the following macros to declare a flag defined elsewhere:
155 #define DECLARE_bool(name) DECLARE_FLAG(bool, name)
156 #define DECLARE_int(name) DECLARE_FLAG(int, name)
157 #define DECLARE_float(name) DECLARE_FLAG(double, name)
158 #define DECLARE_string(name) DECLARE_FLAG(const char*, name)
159
160 37
161 // The global list of all flags. 38 // The global list of all flags.
162 class FlagList { 39 class FlagList {
163 public: 40 public:
164 // The NULL-terminated list of all flags. Traverse with Flag::next().
165 static Flag* list() { return list_; }
166
167 // The list of all flags with a value different from the default 41 // The list of all flags with a value different from the default
168 // and their values. The format of the list is like the format of the 42 // and their values. The format of the list is like the format of the
169 // argv array passed to the main function, e.g. 43 // argv array passed to the main function, e.g.
170 // ("--prof", "--log-file", "v8.prof", "--nolazy"). 44 // ("--prof", "--log-file", "v8.prof", "--nolazy").
171 // 45 //
172 // The caller is responsible for disposing the list. 46 // The caller is responsible for disposing the list.
173 static List<char *>* argv(); 47 static List<char *>* argv();
174 48
175 // If file != NULL, prints information for all flags defined in file;
176 // otherwise prints information for all flags in all files. The current
177 // flag value is only printed if print_current_value is set.
178 static void Print(const char* file, bool print_current_value);
179
180 // Lookup a flag by name. Returns the matching flag or NULL.
181 static Flag* Lookup(const char* name);
182
183 // Helper function to parse flags: Takes an argument arg and splits it into
184 // a flag name and flag value (or NULL if they are missing). is_bool is set
185 // if the arg started with "-no" or "--no". The buffer may be used to NUL-
186 // terminate the name, it must be large enough to hold any possible name.
187 static void SplitArgument(const char* arg,
188 char* buffer,
189 int buffer_size,
190 const char** name,
191 const char** value,
192 bool* is_bool);
193
194 // Set the flag values by parsing the command line. If remove_flags is 49 // Set the flag values by parsing the command line. If remove_flags is
195 // set, the flags and associated values are removed from (argc, 50 // set, the flags and associated values are removed from (argc,
196 // argv). Returns 0 if no error occurred. Otherwise, returns the argv 51 // argv). Returns 0 if no error occurred. Otherwise, returns the argv
197 // index > 0 for the argument where an error occurred. In that case, 52 // index > 0 for the argument where an error occurred. In that case,
198 // (argc, argv) will remain unchanged indepdendent of the remove_flags 53 // (argc, argv) will remain unchanged indepdendent of the remove_flags
199 // value, and no assumptions about flag settings should be made. 54 // value, and no assumptions about flag settings should be made.
200 // 55 //
201 // The following syntax for flags is accepted (both '-' and '--' are ok): 56 // The following syntax for flags is accepted (both '-' and '--' are ok):
202 // 57 //
203 // --flag (bool flags only) 58 // --flag (bool flags only)
204 // --noflag (bool flags only) 59 // --noflag (bool flags only)
205 // --flag=value (non-bool flags only, no spaces around '=') 60 // --flag=value (non-bool flags only, no spaces around '=')
206 // --flag value (non-bool flags only) 61 // --flag value (non-bool flags only)
207 static int SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags); 62 static int SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags);
208 63
209 // Set the flag values by parsing the string str. Splits string into argc 64 // Set the flag values by parsing the string str. Splits string into argc
210 // substrings argv[], each of which consisting of non-white-space chars, 65 // substrings argv[], each of which consisting of non-white-space chars,
211 // and then calls SetFlagsFromCommandLine() and returns its result. 66 // and then calls SetFlagsFromCommandLine() and returns its result.
212 static int SetFlagsFromString(const char* str, int len); 67 static int SetFlagsFromString(const char* str, int len);
213 68
214 // Registers a new flag. Called during program initialization. Not 69 // Reset all flags to their default value.
215 // thread-safe. 70 static void ResetAllFlags();
216 static void Register(Flag* flag);
217 71
218 private: 72 // Print help to stdout with flags, types, and default values.
219 static Flag* list_; 73 static void PrintHelp();
220 }; 74 };
221 75
222 } } // namespace v8::internal 76 } } // namespace v8::internal
223 77
224 #endif // V8_FLAGS_H_ 78 #endif // V8_FLAGS_H_
OLDNEW
« no previous file with comments | « src/execution.cc ('k') | src/flags.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698