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

Unified Diff: tools/gn/function_template.cc

Issue 2233893005: GN: Mark all variables used when defining a template. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tests Created 4 years, 4 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gn/BUILD.gn ('k') | tools/gn/function_template_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
}
« no previous file with comments | « tools/gn/BUILD.gn ('k') | tools/gn/function_template_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698