| OLD | NEW |
| 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/template.h" | 5 #include "tools/gn/template.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "tools/gn/err.h" | 9 #include "tools/gn/err.h" |
| 10 #include "tools/gn/functions.h" | 10 #include "tools/gn/functions.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 73 |
| 74 // We jump through some hoops to avoid copying the invocation scope when | 74 // We jump through some hoops to avoid copying the invocation scope when |
| 75 // setting it in the template scope (since the invocation scope may have | 75 // setting it in the template scope (since the invocation scope may have |
| 76 // large lists of source files in it and could be expensive to copy). | 76 // large lists of source files in it and could be expensive to copy). |
| 77 // | 77 // |
| 78 // Scope.SetValue will copy the value which will in turn copy the scope, but | 78 // Scope.SetValue will copy the value which will in turn copy the scope, but |
| 79 // if we instead create a value and then set the scope on it, the copy can | 79 // if we instead create a value and then set the scope on it, the copy can |
| 80 // be avoided. | 80 // be avoided. |
| 81 template_scope.SetValue(variables::kInvoker, | 81 template_scope.SetValue(variables::kInvoker, |
| 82 Value(nullptr, std::unique_ptr<Scope>()), invocation); | 82 Value(nullptr, std::unique_ptr<Scope>()), invocation); |
| 83 Value* invoker_value = | 83 Value* invoker_value = template_scope.GetMutableValue( |
| 84 template_scope.GetMutableValue(variables::kInvoker, false); | 84 variables::kInvoker, Scope::SEARCH_NESTED, false); |
| 85 invoker_value->SetScopeValue(std::move(invocation_scope)); | 85 invoker_value->SetScopeValue(std::move(invocation_scope)); |
| 86 template_scope.set_source_dir(scope->GetSourceDir()); | 86 template_scope.set_source_dir(scope->GetSourceDir()); |
| 87 | 87 |
| 88 const base::StringPiece target_name(variables::kTargetName); | 88 const base::StringPiece target_name(variables::kTargetName); |
| 89 template_scope.SetValue(target_name, | 89 template_scope.SetValue(target_name, |
| 90 Value(invocation, args[0].string_value()), | 90 Value(invocation, args[0].string_value()), |
| 91 invocation); | 91 invocation); |
| 92 | 92 |
| 93 // Actually run the template code. | 93 // Actually run the template code. |
| 94 Value result = | 94 Value result = |
| 95 definition_->block()->Execute(&template_scope, err); | 95 definition_->block()->Execute(&template_scope, err); |
| 96 if (err->has_error()) { | 96 if (err->has_error()) { |
| 97 // If there was an error, append the caller location so the error message | 97 // If there was an error, append the caller location so the error message |
| 98 // displays a stack trace of how it got here. | 98 // displays a stack trace of how it got here. |
| 99 err->AppendSubErr(Err(invocation, "whence it was called.")); | 99 err->AppendSubErr(Err(invocation, "whence it was called.")); |
| 100 return Value(); | 100 return Value(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 // Check for unused variables in the invocation scope. This will find typos | 103 // Check for unused variables in the invocation scope. This will find typos |
| 104 // of things the caller meant to pass to the template but the template didn't | 104 // of things the caller meant to pass to the template but the template didn't |
| 105 // read out. | 105 // read out. |
| 106 // | 106 // |
| 107 // This is a bit tricky because it's theoretically possible for the template | 107 // This is a bit tricky because it's theoretically possible for the template |
| 108 // to overwrite the value of "invoker" and free the Scope owned by the | 108 // to overwrite the value of "invoker" and free the Scope owned by the |
| 109 // value. So we need to look it up again and don't do anything if it doesn't | 109 // value. So we need to look it up again and don't do anything if it doesn't |
| 110 // exist. | 110 // exist. |
| 111 invoker_value = template_scope.GetMutableValue(variables::kInvoker, false); | 111 invoker_value = template_scope.GetMutableValue( |
| 112 variables::kInvoker, Scope::SEARCH_NESTED, false); |
| 112 if (invoker_value && invoker_value->type() == Value::SCOPE) { | 113 if (invoker_value && invoker_value->type() == Value::SCOPE) { |
| 113 if (!invoker_value->scope_value()->CheckForUnusedVars(err)) | 114 if (!invoker_value->scope_value()->CheckForUnusedVars(err)) |
| 114 return Value(); | 115 return Value(); |
| 115 } | 116 } |
| 116 | 117 |
| 117 // Check for unused variables in the template itself. | 118 // Check for unused variables in the template itself. |
| 118 if (!template_scope.CheckForUnusedVars(err)) | 119 if (!template_scope.CheckForUnusedVars(err)) |
| 119 return Value(); | 120 return Value(); |
| 120 | 121 |
| 121 return result; | 122 return result; |
| 122 } | 123 } |
| 123 | 124 |
| 124 LocationRange Template::GetDefinitionRange() const { | 125 LocationRange Template::GetDefinitionRange() const { |
| 125 return definition_->GetRange(); | 126 return definition_->GetRange(); |
| 126 } | 127 } |
| OLD | NEW |