Index: tools/gn/function_template.cc |
diff --git a/tools/gn/function_template.cc b/tools/gn/function_template.cc |
index 17dda0662d9fa8bfb82f943f57149d11f26e27d2..6136f41ec36fab722457bcc6b978b160e1ce7b38 100644 |
--- a/tools/gn/function_template.cc |
+++ b/tools/gn/function_template.cc |
@@ -192,6 +192,25 @@ Value RunTemplate(Scope* scope, |
} |
scope->AddTemplate(template_name, new Template(scope, function)); |
+ |
+ // The template object above created a closure around the variables in the |
+ // current scope. The template code will execute in that context when it's |
+ // invoked. But this means that any variables defined above that are used |
+ // by the template won't get marked used just by defining the template. The |
+ // result can be spurious unused variable errors. |
+ // |
+ // The "right" thing to do would be to walk the syntax tree inside the |
+ // template, find all identifier references, and mark those variables used. |
+ // This is annoying and error-prone to implement and takes extra time to run |
+ // for this narrow use case. |
+ // |
+ // Templates are most often defined in .gni files which don't get |
+ // used-variable checking anyway, and this case is annoying enough that the |
+ // incremental value of unused variable checking isn't worth the |
+ // alternatives. So all values in scope before this template definition are |
+ // exempted from unused variable checking. |
+ scope->MarkAllUsed(); |
+ |
return Value(); |
} |