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/functions.h" | 5 #include "tools/gn/functions.h" |
6 | 6 |
7 #include "tools/gn/parse_tree.h" | 7 #include "tools/gn/parse_tree.h" |
8 #include "tools/gn/scope.h" | 8 #include "tools/gn/scope.h" |
9 #include "tools/gn/template.h" | 9 #include "tools/gn/template.h" |
10 #include "tools/gn/value.h" | 10 #include "tools/gn/value.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 const Template* existing_template = scope->GetTemplate(template_name); | 185 const Template* existing_template = scope->GetTemplate(template_name); |
186 if (existing_template) { | 186 if (existing_template) { |
187 *err = Err(function, "Duplicate template definition.", | 187 *err = Err(function, "Duplicate template definition.", |
188 "A template with this name was already defined."); | 188 "A template with this name was already defined."); |
189 err->AppendSubErr(Err(existing_template->GetDefinitionRange(), | 189 err->AppendSubErr(Err(existing_template->GetDefinitionRange(), |
190 "Previous definition.")); | 190 "Previous definition.")); |
191 return Value(); | 191 return Value(); |
192 } | 192 } |
193 | 193 |
194 scope->AddTemplate(template_name, new Template(scope, function)); | 194 scope->AddTemplate(template_name, new Template(scope, function)); |
| 195 |
| 196 // The template object above created a closure around the variables in the |
| 197 // current scope. The template code will execute in that context when it's |
| 198 // invoked. But this means that any variables defined above that are used |
| 199 // by the template won't get marked used just by defining the template. The |
| 200 // result can be spurious unused variable errors. |
| 201 // |
| 202 // The "right" thing to do would be to walk the syntax tree inside the |
| 203 // template, find all identifier references, and mark those variables used. |
| 204 // This is annoying and error-prone to implement and takes extra time to run |
| 205 // for this narrow use case. |
| 206 // |
| 207 // Templates are most often defined in .gni files which don't get |
| 208 // used-variable checking anyway, and this case is annoying enough that the |
| 209 // incremental value of unused variable checking isn't worth the |
| 210 // alternatives. So all values in scope before this template definition are |
| 211 // exempted from unused variable checking. |
| 212 scope->MarkAllUsed(); |
| 213 |
195 return Value(); | 214 return Value(); |
196 } | 215 } |
197 | 216 |
198 } // namespace functions | 217 } // namespace functions |
OLD | NEW |