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

Unified Diff: tools/gn/functions.cc

Issue 2936923002: Add not_needed function (Closed)
Patch Set: Support current scope Created 3 years, 6 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
Index: tools/gn/functions.cc
diff --git a/tools/gn/functions.cc b/tools/gn/functions.cc
index 272cd9430bc96d82fef310a8f8ef5600cd3e2725..78ab7b68e49b8c746b56a29d59dcb6a34fc12e1c 100644
--- a/tools/gn/functions.cc
+++ b/tools/gn/functions.cc
@@ -638,6 +638,116 @@ Value RunImport(Scope* scope,
return Value();
}
+// not_needed -----------------------------------------------------------------
+
+const char kNotNeeded[] = "not_needed";
+const char kNotNeeded_HelpShort[] =
+ "not_needed: Mark variables from scope as not needed.";
+const char kNotNeeded_Help[] =
+ R"(not_needed: Mark variables from scope as not needed.
+
+ not_needed(variable_list_or_star, variable_to_ignore_list = [])
+ not_needed(from_scope, variable_list_or_star,
+ variable_to_ignore_list = [])
+
+ Mark the variables in the current or given scope as not needed, which means
+ you will not get an error about unused variables for these.
brettw 2017/06/28 20:16:49 Can you make more explicit here that variable_to_i
Petr Hosek 2017/06/28 23:21:32 Done.
+
+Example
+
+ not_needed("*", [ "config" ])
+ not_needed([ "data_deps", "deps" ])
+ not_needed(invoker, "*", [ "config" ])
+ not_needed(invoker, [ "data_deps", "deps" ])
+)";
+
+Value RunNotNeeded(Scope* scope,
+ const FunctionCallNode* function,
+ const ListNode* args_list,
+ Err* err) {
+ const auto& args_vector = args_list->contents();
+ if (args_vector.size() < 1 && args_vector.size() > 3) {
+ *err = Err(function, "Wrong number of arguments.",
+ "Expecting one, two or three arguments.");
+ return Value();
+ }
+ auto args_cur = args_vector.begin();
+
+ Value* value = nullptr; // Value to use, may point to result_value.
+ Value result_value; // Storage for the "evaluate" case.
+ const IdentifierNode* identifier = (*args_cur)->AsIdentifier();
+ if (identifier) {
+ // Optimize the common case where the input scope is an identifier. This
+ // prevents a copy of a potentially large Scope object.
+ value = scope->GetMutableValue(identifier->value().value(),
+ Scope::SEARCH_NESTED, true);
+ if (!value) {
+ *err = Err(identifier, "Undefined identifier.");
+ return Value();
+ }
+ } else {
+ // Non-optimized case, just evaluate the argument.
+ result_value = (*args_cur)->Execute(scope, err);
+ if (err->has_error())
+ return Value();
+ value = &result_value;
+ }
+ args_cur++;
+
+ // Extract the source scope if different from current one.
+ Scope* source = scope;
+ if (value->type() == Value::SCOPE) {
+ source = value->scope_value();
+ result_value = (*args_cur)->Execute(scope, err);
+ if (err->has_error())
+ return Value();
+ value = &result_value;
+ args_cur++;
+ }
+
+ // Extract the exclusion list if defined.
+ std::set<std::string> exclusion_set;
+ if (args_cur != args_vector.end()) {
+ Value exclusion_value = (*args_cur)->Execute(scope, err);
+ if (err->has_error())
+ return Value();
+
+ if (exclusion_value.type() != Value::LIST) {
+ *err = Err(exclusion_value, "Not a valid list of variables to exclude.",
+ "Expecting a list of strings.");
+ return Value();
+ }
+
+ for (const Value& cur : exclusion_value.list_value()) {
+ if (!cur.VerifyTypeIs(Value::STRING, err))
+ return Value();
+
+ exclusion_set.insert(cur.string_value());
+ }
+ }
+
+ if (value->type() == Value::STRING) {
+ if (value->string_value() == "*") {
+ source->MarkAllUsed(exclusion_set);
+ return Value();
+ }
+ } else {
brettw 2017/06/28 20:16:49 You can just do "else if" here and avoid the extra
Petr Hosek 2017/06/28 23:21:32 Done.
+ if (value->type() == Value::LIST) {
brettw 2017/06/28 20:16:49 Can you throw an error here if an exclusion list i
Petr Hosek 2017/06/28 23:21:32 Done.
+ for (const Value& cur : value->list_value()) {
+ if (!cur.VerifyTypeIs(Value::STRING, err))
+ return Value();
+ source->MarkUsed(cur.string_value());
+ }
+ return Value();
+ }
+ }
+
+ // Not the right type of argument.
+ *err = Err(*value, "Not a valid list of variables.",
+ "Expecting either the string \"*\" or a list of strings.");
+ return Value();
+}
+
// set_sources_assignment_filter -----------------------------------------------
const char kSetSourcesAssignmentFilter[] = "set_sources_assignment_filter";
@@ -1040,6 +1150,7 @@ struct FunctionInfoInitializer {
INSERT_FUNCTION(GetPathInfo, false)
INSERT_FUNCTION(GetTargetOutputs, false)
INSERT_FUNCTION(Import, false)
+ INSERT_FUNCTION(NotNeeded, false)
INSERT_FUNCTION(Pool, false)
INSERT_FUNCTION(Print, false)
INSERT_FUNCTION(ProcessFileTemplate, false)

Powered by Google App Engine
This is Rietveld 408576698