| OLD | NEW |
| 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/scope.h" | 5 #include "tools/gn/scope.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "tools/gn/parse_tree.h" | 9 #include "tools/gn/parse_tree.h" |
| 10 #include "tools/gn/template.h" | 10 #include "tools/gn/template.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // FLags set in the mode_flags_ of a scope. If a bit is set, it applies | 14 // FLags set in the mode_flags_ of a scope. If a bit is set, it applies |
| 15 // recursively to all dependent scopes. | 15 // recursively to all dependent scopes. |
| 16 const unsigned kProcessingBuildConfigFlag = 1; | 16 const unsigned kProcessingBuildConfigFlag = 1; |
| 17 const unsigned kProcessingImportFlag = 2; | 17 const unsigned kProcessingImportFlag = 2; |
| 18 | 18 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 Scope::Scope(const Scope* parent) | 58 Scope::Scope(const Scope* parent) |
| 59 : const_containing_(parent), | 59 : const_containing_(parent), |
| 60 mutable_containing_(nullptr), | 60 mutable_containing_(nullptr), |
| 61 settings_(parent->settings()), | 61 settings_(parent->settings()), |
| 62 mode_flags_(0), | 62 mode_flags_(0), |
| 63 item_collector_(nullptr) { | 63 item_collector_(nullptr) { |
| 64 } | 64 } |
| 65 | 65 |
| 66 Scope::~Scope() { | 66 Scope::~Scope() { |
| 67 STLDeleteContainerPairSecondPointers(target_defaults_.begin(), | |
| 68 target_defaults_.end()); | |
| 69 } | 67 } |
| 70 | 68 |
| 71 const Value* Scope::GetValue(const base::StringPiece& ident, | 69 const Value* Scope::GetValue(const base::StringPiece& ident, |
| 72 bool counts_as_used) { | 70 bool counts_as_used) { |
| 73 // First check for programmatically-provided values. | 71 // First check for programmatically-provided values. |
| 74 for (const auto& provider : programmatic_providers_) { | 72 for (const auto& provider : programmatic_providers_) { |
| 75 const Value* v = provider->GetProgrammaticValue(ident); | 73 const Value* v = provider->GetProgrammaticValue(ident); |
| 76 if (v) | 74 if (v) |
| 77 return v; | 75 return v; |
| 78 } | 76 } |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 *err = Err(node_for_err, "Target defaults collision.", | 306 *err = Err(node_for_err, "Target defaults collision.", |
| 309 "This " + desc_string + " contains target defaults for\n" | 307 "This " + desc_string + " contains target defaults for\n" |
| 310 "\"" + current_name + "\" which would clobber one for the\n" | 308 "\"" + current_name + "\" which would clobber one for the\n" |
| 311 "same target type in your current scope. It's unfortunate that I'm " | 309 "same target type in your current scope. It's unfortunate that I'm " |
| 312 "too stupid\nto tell you the location of where the target defaults " | 310 "too stupid\nto tell you the location of where the target defaults " |
| 313 "were set. Usually\nthis happens in the BUILDCONFIG.gn file."); | 311 "were set. Usually\nthis happens in the BUILDCONFIG.gn file."); |
| 314 return false; | 312 return false; |
| 315 } | 313 } |
| 316 } | 314 } |
| 317 | 315 |
| 318 // Be careful to delete any pointer we're about to clobber. | 316 std::unique_ptr<Scope>& dest_scope = dest->target_defaults_[current_name]; |
| 319 Scope** dest_scope = &dest->target_defaults_[current_name]; | 317 dest_scope = base::WrapUnique(new Scope(settings_)); |
| 320 if (*dest_scope) | 318 pair.second->NonRecursiveMergeTo(dest_scope.get(), options, node_for_err, |
| 321 delete *dest_scope; | |
| 322 *dest_scope = new Scope(settings_); | |
| 323 pair.second->NonRecursiveMergeTo(*dest_scope, options, node_for_err, | |
| 324 "<SHOULDN'T HAPPEN>", err); | 319 "<SHOULDN'T HAPPEN>", err); |
| 325 } | 320 } |
| 326 | 321 |
| 327 // Sources assignment filter. | 322 // Sources assignment filter. |
| 328 if (sources_assignment_filter_) { | 323 if (sources_assignment_filter_) { |
| 329 if (!options.clobber_existing) { | 324 if (!options.clobber_existing) { |
| 330 if (dest->GetSourcesAssignmentFilter()) { | 325 if (dest->GetSourcesAssignmentFilter()) { |
| 331 // Sources assignment filter present in both the source and the dest. | 326 // Sources assignment filter present in both the source and the dest. |
| 332 std::string desc_string(desc_for_err); | 327 std::string desc_string(desc_for_err); |
| 333 *err = Err(node_for_err, "Assignment filter collision.", | 328 *err = Err(node_for_err, "Assignment filter collision.", |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 NonRecursiveMergeTo(result.get(), options, nullptr, "<SHOULDN'T HAPPEN>", | 400 NonRecursiveMergeTo(result.get(), options, nullptr, "<SHOULDN'T HAPPEN>", |
| 406 &err); | 401 &err); |
| 407 DCHECK(!err.has_error()); | 402 DCHECK(!err.has_error()); |
| 408 return result; | 403 return result; |
| 409 } | 404 } |
| 410 | 405 |
| 411 Scope* Scope::MakeTargetDefaults(const std::string& target_type) { | 406 Scope* Scope::MakeTargetDefaults(const std::string& target_type) { |
| 412 if (GetTargetDefaults(target_type)) | 407 if (GetTargetDefaults(target_type)) |
| 413 return nullptr; | 408 return nullptr; |
| 414 | 409 |
| 415 Scope** dest = &target_defaults_[target_type]; | 410 std::unique_ptr<Scope>& dest = target_defaults_[target_type]; |
| 416 if (*dest) { | 411 if (dest) { |
| 417 NOTREACHED(); // Already set. | 412 NOTREACHED(); // Already set. |
| 418 return *dest; | 413 return dest.get(); |
| 419 } | 414 } |
| 420 *dest = new Scope(settings_); | 415 dest = base::WrapUnique(new Scope(settings_)); |
| 421 return *dest; | 416 return dest.get(); |
| 422 } | 417 } |
| 423 | 418 |
| 424 const Scope* Scope::GetTargetDefaults(const std::string& target_type) const { | 419 const Scope* Scope::GetTargetDefaults(const std::string& target_type) const { |
| 425 NamedScopeMap::const_iterator found = target_defaults_.find(target_type); | 420 NamedScopeMap::const_iterator found = target_defaults_.find(target_type); |
| 426 if (found != target_defaults_.end()) | 421 if (found != target_defaults_.end()) |
| 427 return found->second; | 422 return found->second.get(); |
| 428 if (containing()) | 423 if (containing()) |
| 429 return containing()->GetTargetDefaults(target_type); | 424 return containing()->GetTargetDefaults(target_type); |
| 430 return nullptr; | 425 return nullptr; |
| 431 } | 426 } |
| 432 | 427 |
| 433 const PatternList* Scope::GetSourcesAssignmentFilter() const { | 428 const PatternList* Scope::GetSourcesAssignmentFilter() const { |
| 434 if (sources_assignment_filter_) | 429 if (sources_assignment_filter_) |
| 435 return sources_assignment_filter_.get(); | 430 return sources_assignment_filter_.get(); |
| 436 if (containing()) | 431 if (containing()) |
| 437 return containing()->GetSourcesAssignmentFilter(); | 432 return containing()->GetSourcesAssignmentFilter(); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 } | 507 } |
| 513 | 508 |
| 514 void Scope::AddProvider(ProgrammaticProvider* p) { | 509 void Scope::AddProvider(ProgrammaticProvider* p) { |
| 515 programmatic_providers_.insert(p); | 510 programmatic_providers_.insert(p); |
| 516 } | 511 } |
| 517 | 512 |
| 518 void Scope::RemoveProvider(ProgrammaticProvider* p) { | 513 void Scope::RemoveProvider(ProgrammaticProvider* p) { |
| 519 DCHECK(programmatic_providers_.find(p) != programmatic_providers_.end()); | 514 DCHECK(programmatic_providers_.find(p) != programmatic_providers_.end()); |
| 520 programmatic_providers_.erase(p); | 515 programmatic_providers_.erase(p); |
| 521 } | 516 } |
| OLD | NEW |