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

Side by Side Diff: tools/gn/substitution_writer.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 | « tools/gn/substitution_list.cc ('k') | tools/gn/trace.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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/substitution_writer.h" 5 #include "tools/gn/substitution_writer.h"
6 6
7 #include "tools/gn/build_settings.h" 7 #include "tools/gn/build_settings.h"
8 #include "tools/gn/escape.h" 8 #include "tools/gn/escape.h"
9 #include "tools/gn/filesystem_utils.h" 9 #include "tools/gn/filesystem_utils.h"
10 #include "tools/gn/output_file.h" 10 #include "tools/gn/output_file.h"
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 const EscapeOptions& escape_options, 144 const EscapeOptions& escape_options,
145 std::ostream& out) { 145 std::ostream& out) {
146 // The result needs to be quoted as if it was one string, but the $ for 146 // The result needs to be quoted as if it was one string, but the $ for
147 // the inserted Ninja variables can't be escaped. So write to a buffer with 147 // the inserted Ninja variables can't be escaped. So write to a buffer with
148 // no quoting, and then quote the whole thing if necessary. 148 // no quoting, and then quote the whole thing if necessary.
149 EscapeOptions no_quoting(escape_options); 149 EscapeOptions no_quoting(escape_options);
150 no_quoting.inhibit_quoting = true; 150 no_quoting.inhibit_quoting = true;
151 151
152 bool needs_quotes = false; 152 bool needs_quotes = false;
153 std::string result; 153 std::string result;
154 for (size_t i = 0; i < pattern.ranges().size(); i++) { 154 for (const auto& range : pattern.ranges()) {
155 const SubstitutionPattern::Subrange range = pattern.ranges()[i];
156 if (range.type == SUBSTITUTION_LITERAL) { 155 if (range.type == SUBSTITUTION_LITERAL) {
157 result.append(EscapeString(range.literal, no_quoting, &needs_quotes)); 156 result.append(EscapeString(range.literal, no_quoting, &needs_quotes));
158 } else { 157 } else {
159 result.append("${"); 158 result.append("${");
160 result.append(kSubstitutionNinjaNames[range.type]); 159 result.append(kSubstitutionNinjaNames[range.type]);
161 result.append("}"); 160 result.append("}");
162 } 161 }
163 } 162 }
164 163
165 if (needs_quotes && !escape_options.inhibit_quoting) 164 if (needs_quotes && !escape_options.inhibit_quoting)
166 out << "\"" << result << "\""; 165 out << "\"" << result << "\"";
167 else 166 else
168 out << result; 167 out << result;
169 } 168 }
170 169
171 // static 170 // static
172 void SubstitutionWriter::GetListAsSourceFiles( 171 void SubstitutionWriter::GetListAsSourceFiles(
173 const SubstitutionList& list, 172 const SubstitutionList& list,
174 std::vector<SourceFile>* output) { 173 std::vector<SourceFile>* output) {
175 for (size_t i = 0; i < list.list().size(); i++) { 174 for (const auto& pattern : list.list()) {
176 const SubstitutionPattern& pattern = list.list()[i];
177 CHECK(pattern.ranges().size() == 1 && 175 CHECK(pattern.ranges().size() == 1 &&
178 pattern.ranges()[0].type == SUBSTITUTION_LITERAL) 176 pattern.ranges()[0].type == SUBSTITUTION_LITERAL)
179 << "The substitution patterm \"" 177 << "The substitution patterm \""
180 << pattern.AsString() 178 << pattern.AsString()
181 << "\" was expected to be a literal with no {{substitutions}}."; 179 << "\" was expected to be a literal with no {{substitutions}}.";
182 const std::string& literal = pattern.ranges()[0].literal; 180 const std::string& literal = pattern.ranges()[0].literal;
183 CHECK(literal.size() >= 1 && literal[0] == '/') 181 CHECK(literal.size() >= 1 && literal[0] == '/')
184 << "The result of the pattern \"" 182 << "The result of the pattern \""
185 << pattern.AsString() 183 << pattern.AsString()
186 << "\" was not an absolute path."; 184 << "\" was not an absolute path.";
187 output->push_back(SourceFile(literal)); 185 output->push_back(SourceFile(literal));
188 } 186 }
189 } 187 }
190 188
191 // static 189 // static
192 void SubstitutionWriter::GetListAsOutputFiles( 190 void SubstitutionWriter::GetListAsOutputFiles(
193 const Settings* settings, 191 const Settings* settings,
194 const SubstitutionList& list, 192 const SubstitutionList& list,
195 std::vector<OutputFile>* output) { 193 std::vector<OutputFile>* output) {
196 std::vector<SourceFile> output_as_sources; 194 std::vector<SourceFile> output_as_sources;
197 GetListAsSourceFiles(list, &output_as_sources); 195 GetListAsSourceFiles(list, &output_as_sources);
198 for (size_t i = 0; i < output_as_sources.size(); i++) { 196 for (const auto& file : output_as_sources)
199 output->push_back(OutputFile(settings->build_settings(), 197 output->push_back(OutputFile(settings->build_settings(), file));
200 output_as_sources[i]));
201 }
202 } 198 }
203 199
204 // static 200 // static
205 SourceFile SubstitutionWriter::ApplyPatternToSource( 201 SourceFile SubstitutionWriter::ApplyPatternToSource(
206 const Settings* settings, 202 const Settings* settings,
207 const SubstitutionPattern& pattern, 203 const SubstitutionPattern& pattern,
208 const SourceFile& source) { 204 const SourceFile& source) {
209 std::string result_value = ApplyPatternToSourceAsString( 205 std::string result_value = ApplyPatternToSourceAsString(
210 settings, pattern, source); 206 settings, pattern, source);
211 CHECK(!result_value.empty() && result_value[0] == '/') 207 CHECK(!result_value.empty() && result_value[0] == '/')
212 << "The result of the pattern \"" 208 << "The result of the pattern \""
213 << pattern.AsString() 209 << pattern.AsString()
214 << "\" was not a path beginning in \"/\" or \"//\"."; 210 << "\" was not a path beginning in \"/\" or \"//\".";
215 return SourceFile(SourceFile::SWAP_IN, &result_value); 211 return SourceFile(SourceFile::SWAP_IN, &result_value);
216 } 212 }
217 213
218 // static 214 // static
219 std::string SubstitutionWriter::ApplyPatternToSourceAsString( 215 std::string SubstitutionWriter::ApplyPatternToSourceAsString(
220 const Settings* settings, 216 const Settings* settings,
221 const SubstitutionPattern& pattern, 217 const SubstitutionPattern& pattern,
222 const SourceFile& source) { 218 const SourceFile& source) {
223 std::string result_value; 219 std::string result_value;
224 for (size_t i = 0; i < pattern.ranges().size(); i++) { 220 for (const auto& subrange : pattern.ranges()) {
225 const SubstitutionPattern::Subrange& subrange = pattern.ranges()[i];
226 if (subrange.type == SUBSTITUTION_LITERAL) { 221 if (subrange.type == SUBSTITUTION_LITERAL) {
227 result_value.append(subrange.literal); 222 result_value.append(subrange.literal);
228 } else { 223 } else {
229 result_value.append( 224 result_value.append(
230 GetSourceSubstitution(settings, source, subrange.type, 225 GetSourceSubstitution(settings, source, subrange.type,
231 OUTPUT_ABSOLUTE, SourceDir())); 226 OUTPUT_ABSOLUTE, SourceDir()));
232 } 227 }
233 } 228 }
234 return result_value; 229 return result_value;
235 } 230 }
(...skipping 10 matching lines...) Expand all
246 << "\" was not an absolute path beginning in \"//\"."; 241 << "\" was not an absolute path beginning in \"//\".";
247 return OutputFile(settings->build_settings(), result_as_source); 242 return OutputFile(settings->build_settings(), result_as_source);
248 } 243 }
249 244
250 // static 245 // static
251 void SubstitutionWriter::ApplyListToSource( 246 void SubstitutionWriter::ApplyListToSource(
252 const Settings* settings, 247 const Settings* settings,
253 const SubstitutionList& list, 248 const SubstitutionList& list,
254 const SourceFile& source, 249 const SourceFile& source,
255 std::vector<SourceFile>* output) { 250 std::vector<SourceFile>* output) {
256 for (size_t i = 0; i < list.list().size(); i++) { 251 for (const auto& item : list.list())
257 output->push_back(ApplyPatternToSource( 252 output->push_back(ApplyPatternToSource(settings, item, source));
258 settings, list.list()[i], source));
259 }
260 } 253 }
261 254
262 // static 255 // static
263 void SubstitutionWriter::ApplyListToSourceAsString( 256 void SubstitutionWriter::ApplyListToSourceAsString(
264 const Settings* settings, 257 const Settings* settings,
265 const SubstitutionList& list, 258 const SubstitutionList& list,
266 const SourceFile& source, 259 const SourceFile& source,
267 std::vector<std::string>* output) { 260 std::vector<std::string>* output) {
268 for (size_t i = 0; i < list.list().size(); i++) { 261 for (const auto& item : list.list())
269 output->push_back(ApplyPatternToSourceAsString( 262 output->push_back(ApplyPatternToSourceAsString(settings, item, source));
270 settings, list.list()[i], source));
271 }
272 } 263 }
273 264
274 // static 265 // static
275 void SubstitutionWriter::ApplyListToSourceAsOutputFile( 266 void SubstitutionWriter::ApplyListToSourceAsOutputFile(
276 const Settings* settings, 267 const Settings* settings,
277 const SubstitutionList& list, 268 const SubstitutionList& list,
278 const SourceFile& source, 269 const SourceFile& source,
279 std::vector<OutputFile>* output) { 270 std::vector<OutputFile>* output) {
280 for (size_t i = 0; i < list.list().size(); i++) { 271 for (const auto& item : list.list())
281 output->push_back(ApplyPatternToSourceAsOutputFile( 272 output->push_back(ApplyPatternToSourceAsOutputFile(settings, item, source));
282 settings, list.list()[i], source));
283 }
284 } 273 }
285 274
286 // static 275 // static
287 void SubstitutionWriter::ApplyListToSources( 276 void SubstitutionWriter::ApplyListToSources(
288 const Settings* settings, 277 const Settings* settings,
289 const SubstitutionList& list, 278 const SubstitutionList& list,
290 const std::vector<SourceFile>& sources, 279 const std::vector<SourceFile>& sources,
291 std::vector<SourceFile>* output) { 280 std::vector<SourceFile>* output) {
292 output->clear(); 281 output->clear();
293 for (size_t i = 0; i < sources.size(); i++) 282 for (const auto& source : sources)
294 ApplyListToSource(settings, list, sources[i], output); 283 ApplyListToSource(settings, list, source, output);
295 } 284 }
296 285
297 // static 286 // static
298 void SubstitutionWriter::ApplyListToSourcesAsString( 287 void SubstitutionWriter::ApplyListToSourcesAsString(
299 const Settings* settings, 288 const Settings* settings,
300 const SubstitutionList& list, 289 const SubstitutionList& list,
301 const std::vector<SourceFile>& sources, 290 const std::vector<SourceFile>& sources,
302 std::vector<std::string>* output) { 291 std::vector<std::string>* output) {
303 output->clear(); 292 output->clear();
304 for (size_t i = 0; i < sources.size(); i++) 293 for (const auto& source : sources)
305 ApplyListToSourceAsString(settings, list, sources[i], output); 294 ApplyListToSourceAsString(settings, list, source, output);
306 } 295 }
307 296
308 // static 297 // static
309 void SubstitutionWriter::ApplyListToSourcesAsOutputFile( 298 void SubstitutionWriter::ApplyListToSourcesAsOutputFile(
310 const Settings* settings, 299 const Settings* settings,
311 const SubstitutionList& list, 300 const SubstitutionList& list,
312 const std::vector<SourceFile>& sources, 301 const std::vector<SourceFile>& sources,
313 std::vector<OutputFile>* output) { 302 std::vector<OutputFile>* output) {
314 output->clear(); 303 output->clear();
315 for (size_t i = 0; i < sources.size(); i++) 304 for (const auto& source : sources)
316 ApplyListToSourceAsOutputFile(settings, list, sources[i], output); 305 ApplyListToSourceAsOutputFile(settings, list, source, output);
317 } 306 }
318 307
319 // static 308 // static
320 void SubstitutionWriter::WriteNinjaVariablesForSource( 309 void SubstitutionWriter::WriteNinjaVariablesForSource(
321 const Settings* settings, 310 const Settings* settings,
322 const SourceFile& source, 311 const SourceFile& source,
323 const std::vector<SubstitutionType>& types, 312 const std::vector<SubstitutionType>& types,
324 const EscapeOptions& escape_options, 313 const EscapeOptions& escape_options,
325 std::ostream& out) { 314 std::ostream& out) {
326 for (size_t i = 0; i < types.size(); i++) { 315 for (const auto& type : types) {
327 // Don't write SOURCE since that just maps to Ninja's $in variable, which 316 // Don't write SOURCE since that just maps to Ninja's $in variable, which
328 // is implicit in the rule. 317 // is implicit in the rule.
329 if (types[i] != SUBSTITUTION_SOURCE) { 318 if (type != SUBSTITUTION_SOURCE) {
330 out << " " << kSubstitutionNinjaNames[types[i]] << " = "; 319 out << " " << kSubstitutionNinjaNames[type] << " = ";
331 EscapeStringToStream( 320 EscapeStringToStream(
332 out, 321 out,
333 GetSourceSubstitution(settings, source, types[i], OUTPUT_RELATIVE, 322 GetSourceSubstitution(settings, source, type, OUTPUT_RELATIVE,
334 settings->build_settings()->build_dir()), 323 settings->build_settings()->build_dir()),
335 escape_options); 324 escape_options);
336 out << std::endl; 325 out << std::endl;
337 } 326 }
338 } 327 }
339 } 328 }
340 329
341 // static 330 // static
342 std::string SubstitutionWriter::GetSourceSubstitution( 331 std::string SubstitutionWriter::GetSourceSubstitution(
343 const Settings* settings, 332 const Settings* settings,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 return to_rebase; 384 return to_rebase;
396 return RebaseSourceAbsolutePath(to_rebase, relative_to); 385 return RebaseSourceAbsolutePath(to_rebase, relative_to);
397 } 386 }
398 387
399 // static 388 // static
400 OutputFile SubstitutionWriter::ApplyPatternToTargetAsOutputFile( 389 OutputFile SubstitutionWriter::ApplyPatternToTargetAsOutputFile(
401 const Target* target, 390 const Target* target,
402 const Tool* tool, 391 const Tool* tool,
403 const SubstitutionPattern& pattern) { 392 const SubstitutionPattern& pattern) {
404 std::string result_value; 393 std::string result_value;
405 for (size_t i = 0; i < pattern.ranges().size(); i++) { 394 for (const auto& subrange : pattern.ranges()) {
406 const SubstitutionPattern::Subrange& subrange = pattern.ranges()[i];
407 if (subrange.type == SUBSTITUTION_LITERAL) { 395 if (subrange.type == SUBSTITUTION_LITERAL) {
408 result_value.append(subrange.literal); 396 result_value.append(subrange.literal);
409 } else { 397 } else {
410 std::string subst; 398 std::string subst;
411 CHECK(GetTargetSubstitution(target, subrange.type, &subst)); 399 CHECK(GetTargetSubstitution(target, subrange.type, &subst));
412 result_value.append(subst); 400 result_value.append(subst);
413 } 401 }
414 } 402 }
415 return OutputFile(result_value); 403 return OutputFile(result_value);
416 } 404 }
417 405
418 // static 406 // static
419 void SubstitutionWriter::ApplyListToTargetAsOutputFile( 407 void SubstitutionWriter::ApplyListToTargetAsOutputFile(
420 const Target* target, 408 const Target* target,
421 const Tool* tool, 409 const Tool* tool,
422 const SubstitutionList& list, 410 const SubstitutionList& list,
423 std::vector<OutputFile>* output) { 411 std::vector<OutputFile>* output) {
424 for (size_t i = 0; i < list.list().size(); i++) { 412 for (const auto& item : list.list())
425 output->push_back(ApplyPatternToTargetAsOutputFile( 413 output->push_back(ApplyPatternToTargetAsOutputFile(target, tool, item));
426 target, tool, list.list()[i]));
427 }
428 } 414 }
429 415
430 // static 416 // static
431 bool SubstitutionWriter::GetTargetSubstitution( 417 bool SubstitutionWriter::GetTargetSubstitution(
432 const Target* target, 418 const Target* target,
433 SubstitutionType type, 419 SubstitutionType type,
434 std::string* result) { 420 std::string* result) {
435 switch (type) { 421 switch (type) {
436 case SUBSTITUTION_LABEL: 422 case SUBSTITUTION_LABEL:
437 // Only include the toolchain for non-default toolchains. 423 // Only include the toolchain for non-default toolchains.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 GetTargetSubstitution(target, type, &result); 461 GetTargetSubstitution(target, type, &result);
476 return result; 462 return result;
477 } 463 }
478 464
479 // static 465 // static
480 OutputFile SubstitutionWriter::ApplyPatternToCompilerAsOutputFile( 466 OutputFile SubstitutionWriter::ApplyPatternToCompilerAsOutputFile(
481 const Target* target, 467 const Target* target,
482 const SourceFile& source, 468 const SourceFile& source,
483 const SubstitutionPattern& pattern) { 469 const SubstitutionPattern& pattern) {
484 OutputFile result; 470 OutputFile result;
485 for (size_t i = 0; i < pattern.ranges().size(); i++) { 471 for (const auto& subrange : pattern.ranges()) {
486 const SubstitutionPattern::Subrange& subrange = pattern.ranges()[i];
487 if (subrange.type == SUBSTITUTION_LITERAL) { 472 if (subrange.type == SUBSTITUTION_LITERAL) {
488 result.value().append(subrange.literal); 473 result.value().append(subrange.literal);
489 } else { 474 } else {
490 result.value().append( 475 result.value().append(
491 GetCompilerSubstitution(target, source, subrange.type)); 476 GetCompilerSubstitution(target, source, subrange.type));
492 } 477 }
493 } 478 }
494 return result; 479 return result;
495 } 480 }
496 481
497 // static 482 // static
498 void SubstitutionWriter::ApplyListToCompilerAsOutputFile( 483 void SubstitutionWriter::ApplyListToCompilerAsOutputFile(
499 const Target* target, 484 const Target* target,
500 const SourceFile& source, 485 const SourceFile& source,
501 const SubstitutionList& list, 486 const SubstitutionList& list,
502 std::vector<OutputFile>* output) { 487 std::vector<OutputFile>* output) {
503 for (size_t i = 0; i < list.list().size(); i++) { 488 for (const auto& item : list.list())
504 output->push_back(ApplyPatternToCompilerAsOutputFile( 489 output->push_back(ApplyPatternToCompilerAsOutputFile(target, source, item));
505 target, source, list.list()[i]));
506 }
507 } 490 }
508 491
509 // static 492 // static
510 std::string SubstitutionWriter::GetCompilerSubstitution( 493 std::string SubstitutionWriter::GetCompilerSubstitution(
511 const Target* target, 494 const Target* target,
512 const SourceFile& source, 495 const SourceFile& source,
513 SubstitutionType type) { 496 SubstitutionType type) {
514 // First try the common tool ones. 497 // First try the common tool ones.
515 std::string result; 498 std::string result;
516 if (GetTargetSubstitution(target, type, &result)) 499 if (GetTargetSubstitution(target, type, &result))
517 return result; 500 return result;
518 501
519 // Fall-through to the source ones. 502 // Fall-through to the source ones.
520 return GetSourceSubstitution( 503 return GetSourceSubstitution(
521 target->settings(), source, type, OUTPUT_RELATIVE, 504 target->settings(), source, type, OUTPUT_RELATIVE,
522 target->settings()->build_settings()->build_dir()); 505 target->settings()->build_settings()->build_dir());
523 } 506 }
524 507
525 // static 508 // static
526 OutputFile SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( 509 OutputFile SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
527 const Target* target, 510 const Target* target,
528 const Tool* tool, 511 const Tool* tool,
529 const SubstitutionPattern& pattern) { 512 const SubstitutionPattern& pattern) {
530 OutputFile result; 513 OutputFile result;
531 for (size_t i = 0; i < pattern.ranges().size(); i++) { 514 for (const auto& subrange : pattern.ranges()) {
532 const SubstitutionPattern::Subrange& subrange = pattern.ranges()[i];
533 if (subrange.type == SUBSTITUTION_LITERAL) { 515 if (subrange.type == SUBSTITUTION_LITERAL) {
534 result.value().append(subrange.literal); 516 result.value().append(subrange.literal);
535 } else { 517 } else {
536 result.value().append(GetLinkerSubstitution(target, tool, subrange.type)); 518 result.value().append(GetLinkerSubstitution(target, tool, subrange.type));
537 } 519 }
538 } 520 }
539 return result; 521 return result;
540 } 522 }
541 523
542 // static 524 // static
543 void SubstitutionWriter::ApplyListToLinkerAsOutputFile( 525 void SubstitutionWriter::ApplyListToLinkerAsOutputFile(
544 const Target* target, 526 const Target* target,
545 const Tool* tool, 527 const Tool* tool,
546 const SubstitutionList& list, 528 const SubstitutionList& list,
547 std::vector<OutputFile>* output) { 529 std::vector<OutputFile>* output) {
548 for (size_t i = 0; i < list.list().size(); i++) { 530 for (const auto& item : list.list())
549 output->push_back(ApplyPatternToLinkerAsOutputFile( 531 output->push_back(ApplyPatternToLinkerAsOutputFile(target, tool, item));
550 target, tool, list.list()[i]));
551 }
552 } 532 }
553 533
554 // static 534 // static
555 std::string SubstitutionWriter::GetLinkerSubstitution( 535 std::string SubstitutionWriter::GetLinkerSubstitution(
556 const Target* target, 536 const Target* target,
557 const Tool* tool, 537 const Tool* tool,
558 SubstitutionType type) { 538 SubstitutionType type) {
559 // First try the common tool ones. 539 // First try the common tool ones.
560 std::string result; 540 std::string result;
561 if (GetTargetSubstitution(target, type, &result)) 541 if (GetTargetSubstitution(target, type, &result))
562 return result; 542 return result;
563 543
564 // Fall-through to the linker-specific ones. 544 // Fall-through to the linker-specific ones.
565 switch (type) { 545 switch (type) {
566 case SUBSTITUTION_OUTPUT_EXTENSION: 546 case SUBSTITUTION_OUTPUT_EXTENSION:
567 // Use the extension provided on the target if nonempty, otherwise 547 // Use the extension provided on the target if nonempty, otherwise
568 // fall back on the default. Note that the target's output extension 548 // fall back on the default. Note that the target's output extension
569 // does not include the dot but the tool's does. 549 // does not include the dot but the tool's does.
570 if (target->output_extension().empty()) 550 if (target->output_extension().empty())
571 return tool->default_output_extension(); 551 return tool->default_output_extension();
572 return std::string(".") + target->output_extension(); 552 return std::string(".") + target->output_extension();
573 553
574 default: 554 default:
575 NOTREACHED(); 555 NOTREACHED();
576 return std::string(); 556 return std::string();
577 } 557 }
578 } 558 }
OLDNEW
« no previous file with comments | « tools/gn/substitution_list.cc ('k') | tools/gn/trace.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698