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

Side by Side Diff: tools/gn/args.cc

Issue 610293003: Replace more for loops in GN (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review Created 6 years, 2 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 | « no previous file | tools/gn/binary_target_generator.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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium 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 "tools/gn/args.h" 5 #include "tools/gn/args.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 #include "tools/gn/variables.h" 8 #include "tools/gn/variables.h"
9 9
10 #if defined(OS_WIN) 10 #if defined(OS_WIN)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 void Args::AddArgOverride(const char* name, const Value& value) { 82 void Args::AddArgOverride(const char* name, const Value& value) {
83 base::AutoLock lock(lock_); 83 base::AutoLock lock(lock_);
84 84
85 overrides_[base::StringPiece(name)] = value; 85 overrides_[base::StringPiece(name)] = value;
86 all_overrides_[base::StringPiece(name)] = value; 86 all_overrides_[base::StringPiece(name)] = value;
87 } 87 }
88 88
89 void Args::AddArgOverrides(const Scope::KeyValueMap& overrides) { 89 void Args::AddArgOverrides(const Scope::KeyValueMap& overrides) {
90 base::AutoLock lock(lock_); 90 base::AutoLock lock(lock_);
91 91
92 for (Scope::KeyValueMap::const_iterator i = overrides.begin(); 92 for (const auto& cur_override : overrides) {
93 i != overrides.end(); ++i) { 93 overrides_[cur_override.first] = cur_override.second;
94 overrides_[i->first] = i->second; 94 all_overrides_[cur_override.first] = cur_override.second;
95 all_overrides_[i->first] = i->second;
96 } 95 }
97 } 96 }
98 97
99 const Value* Args::GetArgOverride(const char* name) const { 98 const Value* Args::GetArgOverride(const char* name) const {
100 base::AutoLock lock(lock_); 99 base::AutoLock lock(lock_);
101 100
102 Scope::KeyValueMap::const_iterator found = 101 Scope::KeyValueMap::const_iterator found =
103 all_overrides_.find(base::StringPiece(name)); 102 all_overrides_.find(base::StringPiece(name));
104 if (found == all_overrides_.end()) 103 if (found == all_overrides_.end())
105 return NULL; 104 return NULL;
(...skipping 13 matching lines...) Expand all
119 ApplyOverridesLocked(overrides_, dest); 118 ApplyOverridesLocked(overrides_, dest);
120 ApplyOverridesLocked(toolchain_overrides, dest); 119 ApplyOverridesLocked(toolchain_overrides, dest);
121 SaveOverrideRecordLocked(toolchain_overrides); 120 SaveOverrideRecordLocked(toolchain_overrides);
122 } 121 }
123 122
124 bool Args::DeclareArgs(const Scope::KeyValueMap& args, 123 bool Args::DeclareArgs(const Scope::KeyValueMap& args,
125 Scope* scope_to_set, 124 Scope* scope_to_set,
126 Err* err) const { 125 Err* err) const {
127 base::AutoLock lock(lock_); 126 base::AutoLock lock(lock_);
128 127
129 for (Scope::KeyValueMap::const_iterator i = args.begin(); 128 for (const auto& arg : args) {
130 i != args.end(); ++i) {
131 // Verify that the value hasn't already been declared. We want each value 129 // Verify that the value hasn't already been declared. We want each value
132 // to be declared only once. 130 // to be declared only once.
133 // 131 //
134 // The tricky part is that a buildfile can be interpreted multiple times 132 // The tricky part is that a buildfile can be interpreted multiple times
135 // when used from different toolchains, so we can't just check that we've 133 // when used from different toolchains, so we can't just check that we've
136 // seen it before. Instead, we check that the location matches. 134 // seen it before. Instead, we check that the location matches.
137 Scope::KeyValueMap::iterator previously_declared = 135 Scope::KeyValueMap::iterator previously_declared =
138 declared_arguments_.find(i->first); 136 declared_arguments_.find(arg.first);
139 if (previously_declared != declared_arguments_.end()) { 137 if (previously_declared != declared_arguments_.end()) {
140 if (previously_declared->second.origin() != i->second.origin()) { 138 if (previously_declared->second.origin() != arg.second.origin()) {
141 // Declaration location mismatch. 139 // Declaration location mismatch.
142 *err = Err(i->second.origin(), "Duplicate build argument declaration.", 140 *err = Err(arg.second.origin(),
141 "Duplicate build argument declaration.",
143 "Here you're declaring an argument that was already declared " 142 "Here you're declaring an argument that was already declared "
144 "elsewhere.\nYou can only declare each argument once in the entire " 143 "elsewhere.\nYou can only declare each argument once in the entire "
145 "build so there is one\ncanonical place for documentation and the " 144 "build so there is one\ncanonical place for documentation and the "
146 "default value. Either move this\nargument to the build config " 145 "default value. Either move this\nargument to the build config "
147 "file (for visibility everywhere) or to a .gni file\nthat you " 146 "file (for visibility everywhere) or to a .gni file\nthat you "
148 "\"import\" from the files where you need it (preferred)."); 147 "\"import\" from the files where you need it (preferred).");
149 err->AppendSubErr(Err(previously_declared->second.origin(), 148 err->AppendSubErr(Err(previously_declared->second.origin(),
150 "Previous declaration.", 149 "Previous declaration.",
151 "See also \"gn help buildargs\" for more on how " 150 "See also \"gn help buildargs\" for more on how "
152 "build arguments work.")); 151 "build arguments work."));
153 return false; 152 return false;
154 } 153 }
155 } else { 154 } else {
156 declared_arguments_.insert(*i); 155 declared_arguments_.insert(arg);
157 } 156 }
158 157
159 // Only set on the current scope to the new value if it hasn't been already 158 // Only set on the current scope to the new value if it hasn't been already
160 // set. Mark the variable used so the build script can override it in 159 // set. Mark the variable used so the build script can override it in
161 // certain cases without getting unused value errors. 160 // certain cases without getting unused value errors.
162 if (!scope_to_set->GetValue(i->first)) { 161 if (!scope_to_set->GetValue(arg.first)) {
163 scope_to_set->SetValue(i->first, i->second, i->second.origin()); 162 scope_to_set->SetValue(arg.first, arg.second, arg.second.origin());
164 scope_to_set->MarkUsed(i->first); 163 scope_to_set->MarkUsed(arg.first);
165 } 164 }
166 } 165 }
167 166
168 return true; 167 return true;
169 } 168 }
170 169
171 bool Args::VerifyAllOverridesUsed(Err* err) const { 170 bool Args::VerifyAllOverridesUsed(Err* err) const {
172 base::AutoLock lock(lock_); 171 base::AutoLock lock(lock_);
173 return VerifyAllOverridesUsed(all_overrides_, declared_arguments_, err); 172 return VerifyAllOverridesUsed(all_overrides_, declared_arguments_, err);
174 } 173 }
175 174
176 bool Args::VerifyAllOverridesUsed( 175 bool Args::VerifyAllOverridesUsed(
177 const Scope::KeyValueMap& overrides, 176 const Scope::KeyValueMap& overrides,
178 const Scope::KeyValueMap& declared_arguments, 177 const Scope::KeyValueMap& declared_arguments,
179 Err* err) { 178 Err* err) {
180 for (Scope::KeyValueMap::const_iterator i = overrides.begin(); 179 for (const auto& override : overrides) {
181 i != overrides.end(); ++i) { 180 if (declared_arguments.find(override.first) == declared_arguments.end()) {
182 if (declared_arguments.find(i->first) == declared_arguments.end()) {
183 // Get a list of all possible overrides for help with error finding. 181 // Get a list of all possible overrides for help with error finding.
184 // 182 //
185 // It might be nice to do edit distance checks to see if we can find one 183 // It might be nice to do edit distance checks to see if we can find one
186 // close to what you typed. 184 // close to what you typed.
187 std::string all_declared_str; 185 std::string all_declared_str;
188 for (Scope::KeyValueMap::const_iterator cur_str = 186 for (Scope::KeyValueMap::const_iterator cur_str =
189 declared_arguments.begin(); 187 declared_arguments.begin();
190 cur_str != declared_arguments.end(); ++cur_str) { 188 cur_str != declared_arguments.end(); ++cur_str) {
191 if (cur_str != declared_arguments.begin()) 189 if (cur_str != declared_arguments.begin())
192 all_declared_str += ", "; 190 all_declared_str += ", ";
193 all_declared_str += cur_str->first.as_string(); 191 all_declared_str += cur_str->first.as_string();
194 } 192 }
195 193
196 *err = Err(i->second.origin(), "Build argument has no effect.", 194 *err = Err(override.second.origin(), "Build argument has no effect.",
197 "The variable \"" + i->first.as_string() + "\" was set as a build " 195 "The variable \"" + override.first.as_string() +
196 "\" was set as a build "
198 "argument\nbut never appeared in a declare_args() block in any " 197 "argument\nbut never appeared in a declare_args() block in any "
199 "buildfile.\n\nPossible arguments: " + all_declared_str); 198 "buildfile.\n\nPossible arguments: " + all_declared_str);
200 return false; 199 return false;
201 } 200 }
202 } 201 }
203 return true; 202 return true;
204 } 203 }
205 204
206 void Args::MergeDeclaredArguments(Scope::KeyValueMap* dest) const { 205 void Args::MergeDeclaredArguments(Scope::KeyValueMap* dest) const {
207 base::AutoLock lock(lock_); 206 base::AutoLock lock(lock_);
208 207 for (const auto& arg : declared_arguments_)
209 for (Scope::KeyValueMap::const_iterator i = declared_arguments_.begin(); 208 (*dest)[arg.first] = arg.second;
210 i != declared_arguments_.end(); ++i)
211 (*dest)[i->first] = i->second;
212 } 209 }
213 210
214 void Args::SetSystemVarsLocked(Scope* dest) const { 211 void Args::SetSystemVarsLocked(Scope* dest) const {
215 lock_.AssertAcquired(); 212 lock_.AssertAcquired();
216 213
217 // Host OS. 214 // Host OS.
218 const char* os = NULL; 215 const char* os = NULL;
219 #if defined(OS_WIN) 216 #if defined(OS_WIN)
220 os = "win"; 217 os = "win";
221 #elif defined(OS_MACOSX) 218 #elif defined(OS_MACOSX)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 // without geting a warning about overwriting an unused variable. 279 // without geting a warning about overwriting an unused variable.
283 declared_arguments_[variables::kOs] = os_val; 280 declared_arguments_[variables::kOs] = os_val;
284 declared_arguments_[variables::kCpuArch] = arch_val; 281 declared_arguments_[variables::kCpuArch] = arch_val;
285 dest->MarkUsed(variables::kCpuArch); 282 dest->MarkUsed(variables::kCpuArch);
286 dest->MarkUsed(variables::kOs); 283 dest->MarkUsed(variables::kOs);
287 } 284 }
288 285
289 void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values, 286 void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values,
290 Scope* scope) const { 287 Scope* scope) const {
291 lock_.AssertAcquired(); 288 lock_.AssertAcquired();
292 for (Scope::KeyValueMap::const_iterator i = values.begin(); 289 for (const auto& val : values)
293 i != values.end(); ++i) 290 scope->SetValue(val.first, val.second, val.second.origin());
294 scope->SetValue(i->first, i->second, i->second.origin());
295 } 291 }
296 292
297 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const { 293 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const {
298 lock_.AssertAcquired(); 294 lock_.AssertAcquired();
299 for (Scope::KeyValueMap::const_iterator i = values.begin(); 295 for (const auto& val : values)
300 i != values.end(); ++i) 296 all_overrides_[val.first] = val.second;
301 all_overrides_[i->first] = i->second;
302 } 297 }
OLDNEW
« no previous file with comments | « no previous file | tools/gn/binary_target_generator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698