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

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

Issue 2509333003: Change GN to disallow reading args defined in the same declare_args() call. (Closed)
Patch Set: initial patch for review Created 4 years, 1 month 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
« tools/gn/parse_tree.cc ('K') | « tools/gn/scope.h ('k') | no next file » | 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/scope.h" 5 #include "tools/gn/scope.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/ptr_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 const int kInDeclareArgsKey = 0;
13
12 namespace { 14 namespace {
13 15
14 // FLags set in the mode_flags_ of a scope. If a bit is set, it applies 16 // FLags set in the mode_flags_ of a scope. If a bit is set, it applies
15 // recursively to all dependent scopes. 17 // recursively to all dependent scopes.
16 const unsigned kProcessingBuildConfigFlag = 1; 18 const unsigned kProcessingBuildConfigFlag = 1;
17 const unsigned kProcessingImportFlag = 2; 19 const unsigned kProcessingImportFlag = 2;
18 20
19 // Returns true if this variable name should be considered private. Private 21 // Returns true if this variable name should be considered private. Private
20 // values start with an underscore, and are not imported from "gni" files 22 // values start with an underscore, and are not imported from "gni" files
21 // when processing an import. 23 // when processing an import.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 mutable_containing_ = nullptr; 73 mutable_containing_ = nullptr;
72 } 74 }
73 75
74 bool Scope::HasValues(SearchNested search_nested) const { 76 bool Scope::HasValues(SearchNested search_nested) const {
75 DCHECK(search_nested == SEARCH_CURRENT); 77 DCHECK(search_nested == SEARCH_CURRENT);
76 return !values_.empty(); 78 return !values_.empty();
77 } 79 }
78 80
79 const Value* Scope::GetValue(const base::StringPiece& ident, 81 const Value* Scope::GetValue(const base::StringPiece& ident,
80 bool counts_as_used) { 82 bool counts_as_used) {
83 const Scope* found_in_scope = nullptr;
84 return GetValueWithScope(ident, counts_as_used, &found_in_scope);
85 }
86
87 const Value* Scope::GetValueWithScope(const base::StringPiece& ident,
88 bool counts_as_used,
89 const Scope** found_in_scope) {
81 // First check for programmatically-provided values. 90 // First check for programmatically-provided values.
82 for (auto* provider : programmatic_providers_) { 91 for (auto* provider : programmatic_providers_) {
83 const Value* v = provider->GetProgrammaticValue(ident); 92 const Value* v = provider->GetProgrammaticValue(ident);
84 if (v) 93 if (v) {
94 *found_in_scope = nullptr;
85 return v; 95 return v;
96 }
86 } 97 }
87 98
88 RecordMap::iterator found = values_.find(ident); 99 RecordMap::iterator found = values_.find(ident);
89 if (found != values_.end()) { 100 if (found != values_.end()) {
90 if (counts_as_used) 101 if (counts_as_used)
91 found->second.used = true; 102 found->second.used = true;
103 *found_in_scope = this;
92 return &found->second.value; 104 return &found->second.value;
93 } 105 }
94 106
95 // Search in the parent scope. 107 // Search in the parent scope.
96 if (const_containing_) 108 if (const_containing_)
97 return const_containing_->GetValue(ident); 109 return const_containing_->GetValueWithScope(ident, found_in_scope);
98 if (mutable_containing_) 110 if (mutable_containing_) {
99 return mutable_containing_->GetValue(ident, counts_as_used); 111 return mutable_containing_->GetValueWithScope(ident, counts_as_used,
112 found_in_scope);
113 }
100 return nullptr; 114 return nullptr;
101 } 115 }
102 116
103 Value* Scope::GetMutableValue(const base::StringPiece& ident, 117 Value* Scope::GetMutableValue(const base::StringPiece& ident,
104 SearchNested search_mode, 118 SearchNested search_mode,
105 bool counts_as_used) { 119 bool counts_as_used) {
106 // Don't do programmatic values, which are not mutable. 120 // Don't do programmatic values, which are not mutable.
107 RecordMap::iterator found = values_.find(ident); 121 RecordMap::iterator found = values_.find(ident);
108 if (found != values_.end()) { 122 if (found != values_.end()) {
109 if (counts_as_used) 123 if (counts_as_used)
(...skipping 14 matching lines...) Expand all
124 if (found != values_.end()) 138 if (found != values_.end())
125 return found->first; 139 return found->first;
126 140
127 // Search in parent scope. 141 // Search in parent scope.
128 if (containing()) 142 if (containing())
129 return containing()->GetStorageKey(ident); 143 return containing()->GetStorageKey(ident);
130 return base::StringPiece(); 144 return base::StringPiece();
131 } 145 }
132 146
133 const Value* Scope::GetValue(const base::StringPiece& ident) const { 147 const Value* Scope::GetValue(const base::StringPiece& ident) const {
148 const Scope *found_in_scope = nullptr;
149 return GetValueWithScope(ident, &found_in_scope);
150 }
151
152 const Value* Scope::GetValueWithScope(const base::StringPiece& ident,
153 const Scope** found_in_scope) const {
134 RecordMap::const_iterator found = values_.find(ident); 154 RecordMap::const_iterator found = values_.find(ident);
135 if (found != values_.end()) 155 if (found != values_.end()) {
156 *found_in_scope = this;
136 return &found->second.value; 157 return &found->second.value;
158 }
137 if (containing()) 159 if (containing())
138 return containing()->GetValue(ident); 160 return containing()->GetValueWithScope(ident, found_in_scope);
139 return nullptr; 161 return nullptr;
140 } 162 }
141 163
142 Value* Scope::SetValue(const base::StringPiece& ident, 164 Value* Scope::SetValue(const base::StringPiece& ident,
143 Value v, 165 Value v,
144 const ParseNode* set_node) { 166 const ParseNode* set_node) {
145 Record& r = values_[ident]; // Clears any existing value. 167 Record& r = values_[ident]; // Clears any existing value.
146 r.value = std::move(v); 168 r.value = std::move(v);
147 r.value.set_origin(set_node); 169 r.value.set_origin(set_node);
148 return &r.value; 170 return &r.value;
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 return false; 541 return false;
520 for (const auto& pair : a) { 542 for (const auto& pair : a) {
521 const auto& found_b = b.find(pair.first); 543 const auto& found_b = b.find(pair.first);
522 if (found_b == b.end()) 544 if (found_b == b.end())
523 return false; // Item in 'a' but not 'b'. 545 return false; // Item in 'a' but not 'b'.
524 if (pair.second.value != found_b->second.value) 546 if (pair.second.value != found_b->second.value)
525 return false; // Values for variable in 'a' and 'b' are different. 547 return false; // Values for variable in 'a' and 'b' are different.
526 } 548 }
527 return true; 549 return true;
528 } 550 }
OLDNEW
« tools/gn/parse_tree.cc ('K') | « tools/gn/scope.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698