| 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/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" |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 198 |
| 199 const Template* Scope::GetTemplate(const std::string& name) const { | 199 const Template* Scope::GetTemplate(const std::string& name) const { |
| 200 TemplateMap::const_iterator found = templates_.find(name); | 200 TemplateMap::const_iterator found = templates_.find(name); |
| 201 if (found != templates_.end()) | 201 if (found != templates_.end()) |
| 202 return found->second.get(); | 202 return found->second.get(); |
| 203 if (containing()) | 203 if (containing()) |
| 204 return containing()->GetTemplate(name); | 204 return containing()->GetTemplate(name); |
| 205 return nullptr; | 205 return nullptr; |
| 206 } | 206 } |
| 207 | 207 |
| 208 void Scope::MarkUsed(const base::StringPiece& ident) { | 208 void Scope::MarkUsed(const base::StringPiece& ident, Err* err) { |
| 209 RecordMap::iterator found = values_.find(ident); | 209 RecordMap::iterator found = values_.find(ident); |
| 210 if (found == values_.end()) { | 210 if (found == values_.end()) { |
| 211 NOTREACHED(); | 211 NOTREACHED(); |
| 212 return; | 212 return; |
| 213 } | 213 } |
| 214 if (found->second.unusable) { |
| 215 *err = Err(found->second.value.origin(), "Variable was marked as unused.", |
| 216 "You marked the variable \"" + found->first.as_string() + |
| 217 "\" as unused, but it is being used."); |
| 218 return; |
| 219 } |
| 214 found->second.used = true; | 220 found->second.used = true; |
| 215 } | 221 } |
| 216 | 222 |
| 217 void Scope::MarkAllUsed() { | 223 void Scope::MarkAllUsed(Err* err) { |
| 218 for (auto& cur : values_) | 224 for (auto& cur : values_) { |
| 225 if (cur.second.unusable) { |
| 226 *err = Err(cur.second.value.origin(), "Variable was marked as unused.", |
| 227 "You marked the variable \"" + cur.first.as_string() + |
| 228 "\" as unused, but it is being used."); |
| 229 return; |
| 230 } |
| 219 cur.second.used = true; | 231 cur.second.used = true; |
| 232 } |
| 220 } | 233 } |
| 221 | 234 |
| 222 void Scope::MarkUnused(const base::StringPiece& ident) { | 235 void Scope::MarkUnused(const base::StringPiece& ident) { |
| 223 RecordMap::iterator found = values_.find(ident); | 236 RecordMap::iterator found = values_.find(ident); |
| 224 if (found == values_.end()) { | 237 if (found == values_.end()) { |
| 225 NOTREACHED(); | 238 NOTREACHED(); |
| 226 return; | 239 return; |
| 227 } | 240 } |
| 228 found->second.used = false; | 241 found->second.used = false; |
| 229 } | 242 } |
| 230 | 243 |
| 244 void Scope::MarkAllUnusable(Err* err, std::set<std::string> excluded_values) { |
| 245 for (auto& cur : values_) { |
| 246 if (!excluded_values.empty() && |
| 247 excluded_values.find(cur.first.as_string()) != excluded_values.end()) { |
| 248 continue; // Skip this excluded value. |
| 249 } |
| 250 |
| 251 if (cur.second.used) { |
| 252 *err = Err(cur.second.value.origin(), "Variable was marked as unused.", |
| 253 "You marked the variable \"" + cur.first.as_string() + |
| 254 "\" as unused, but it is being used."); |
| 255 return; |
| 256 } |
| 257 cur.second.unusable = true; |
| 258 } |
| 259 } |
| 260 |
| 261 void Scope::MarkUnusable(const base::StringPiece& ident, Err* err) { |
| 262 RecordMap::iterator found = values_.find(ident); |
| 263 if (found == values_.end()) { |
| 264 NOTREACHED(); |
| 265 return; |
| 266 } |
| 267 if (found->second.used) { |
| 268 *err = Err(found->second.value.origin(), "Variable was marked as unused.", |
| 269 "You marked the variable \"" + ident.as_string() + |
| 270 "\" as unused, but it is being used."); |
| 271 return; |
| 272 } |
| 273 found->second.unusable = true; |
| 274 } |
| 275 |
| 231 bool Scope::IsSetButUnused(const base::StringPiece& ident) const { | 276 bool Scope::IsSetButUnused(const base::StringPiece& ident) const { |
| 232 RecordMap::const_iterator found = values_.find(ident); | 277 RecordMap::const_iterator found = values_.find(ident); |
| 233 if (found != values_.end()) { | 278 if (found != values_.end()) { |
| 234 if (!found->second.used) { | 279 if (!found->second.used) { |
| 235 return true; | 280 return true; |
| 236 } | 281 } |
| 237 } | 282 } |
| 238 return false; | 283 return false; |
| 239 } | 284 } |
| 240 | 285 |
| 241 bool Scope::CheckForUnusedVars(Err* err) const { | 286 bool Scope::CheckForUnusedVars(Err* err) const { |
| 242 for (const auto& pair : values_) { | 287 for (const auto& pair : values_) { |
| 243 if (!pair.second.used) { | 288 if (!pair.second.unusable && !pair.second.used) { |
| 244 std::string help = "You set the variable \"" + pair.first.as_string() + | 289 std::string help = "You set the variable \"" + pair.first.as_string() + |
| 245 "\" here and it was unused before it went\nout of scope."; | 290 "\" here and it was unused before it went\nout of scope."; |
| 246 | 291 |
| 247 const BinaryOpNode* binary = pair.second.value.origin()->AsBinaryOp(); | 292 const BinaryOpNode* binary = pair.second.value.origin()->AsBinaryOp(); |
| 248 if (binary && binary->op().type() == Token::EQUAL) { | 293 if (binary && binary->op().type() == Token::EQUAL) { |
| 249 // Make a nicer error message for normal var sets. | 294 // Make a nicer error message for normal var sets. |
| 250 *err = Err(binary->left()->GetRange(), "Assignment had no effect.", | 295 *err = Err(binary->left()->GetRange(), "Assignment had no effect.", |
| 251 help); | 296 help); |
| 252 } else { | 297 } else { |
| 253 // This will happen for internally-generated variables. | 298 // This will happen for internally-generated variables. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 "Which would clobber the one in your current scope")); | 339 "Which would clobber the one in your current scope")); |
| 295 err->AppendSubErr(Err(*existing_value, "defined here.", | 340 err->AppendSubErr(Err(*existing_value, "defined here.", |
| 296 "Executing " + desc_string + " should not conflict with anything " | 341 "Executing " + desc_string + " should not conflict with anything " |
| 297 "in the current\nscope unless the values are identical.")); | 342 "in the current\nscope unless the values are identical.")); |
| 298 return false; | 343 return false; |
| 299 } | 344 } |
| 300 } | 345 } |
| 301 dest->values_[current_name] = pair.second; | 346 dest->values_[current_name] = pair.second; |
| 302 | 347 |
| 303 if (options.mark_dest_used) | 348 if (options.mark_dest_used) |
| 304 dest->MarkUsed(current_name); | 349 dest->MarkUsed(current_name, err); |
| 305 } | 350 } |
| 306 | 351 |
| 307 // Target defaults are owning pointers. | 352 // Target defaults are owning pointers. |
| 308 for (const auto& pair : target_defaults_) { | 353 for (const auto& pair : target_defaults_) { |
| 309 const std::string& current_name = pair.first; | 354 const std::string& current_name = pair.first; |
| 310 if (!options.excluded_values.empty() && | 355 if (!options.excluded_values.empty() && |
| 311 options.excluded_values.find(current_name) != | 356 options.excluded_values.find(current_name) != |
| 312 options.excluded_values.end()) { | 357 options.excluded_values.end()) { |
| 313 continue; // Skip the excluded value. | 358 continue; // Skip the excluded value. |
| 314 } | 359 } |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 return false; | 584 return false; |
| 540 for (const auto& pair : a) { | 585 for (const auto& pair : a) { |
| 541 const auto& found_b = b.find(pair.first); | 586 const auto& found_b = b.find(pair.first); |
| 542 if (found_b == b.end()) | 587 if (found_b == b.end()) |
| 543 return false; // Item in 'a' but not 'b'. | 588 return false; // Item in 'a' but not 'b'. |
| 544 if (pair.second.value != found_b->second.value) | 589 if (pair.second.value != found_b->second.value) |
| 545 return false; // Values for variable in 'a' and 'b' are different. | 590 return false; // Values for variable in 'a' and 'b' are different. |
| 546 } | 591 } |
| 547 return true; | 592 return true; |
| 548 } | 593 } |
| OLD | NEW |