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 <stddef.h> | 7 #include <stddef.h> |
8 #include <iostream> | 8 #include <iostream> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 | 127 |
128 // The name is the single argument to the target function. | 128 // The name is the single argument to the target function. |
129 if (!EnsureSingleStringArg(function, args, err)) | 129 if (!EnsureSingleStringArg(function, args, err)) |
130 return false; | 130 return false; |
131 | 131 |
132 // Set the target name variable to the current target, and mark it used | 132 // Set the target name variable to the current target, and mark it used |
133 // because we don't want to issue an error if the script ignores it. | 133 // because we don't want to issue an error if the script ignores it. |
134 const base::StringPiece target_name(variables::kTargetName); | 134 const base::StringPiece target_name(variables::kTargetName); |
135 block_scope->SetValue(target_name, Value(function, args[0].string_value()), | 135 block_scope->SetValue(target_name, Value(function, args[0].string_value()), |
136 function); | 136 function); |
137 block_scope->MarkUsed(target_name); | 137 block_scope->MarkUsed(target_name, err); |
138 return true; | 138 return true; |
139 } | 139 } |
140 | 140 |
141 void FillNeedsBlockError(const FunctionCallNode* function, Err* err) { | 141 void FillNeedsBlockError(const FunctionCallNode* function, Err* err) { |
142 *err = Err(function->function(), "This function call requires a block.", | 142 *err = Err(function->function(), "This function call requires a block.", |
143 "The block's \"{\" must be on the same line as the function " | 143 "The block's \"{\" must be on the same line as the function " |
144 "call's \")\"."); | 144 "call's \")\"."); |
145 } | 145 } |
146 | 146 |
147 bool EnsureSingleStringArg(const FunctionCallNode* function, | 147 bool EnsureSingleStringArg(const FunctionCallNode* function, |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
631 SourceFile import_file = | 631 SourceFile import_file = |
632 input_dir.ResolveRelativeFile(args[0], err, | 632 input_dir.ResolveRelativeFile(args[0], err, |
633 scope->settings()->build_settings()->root_path_utf8()); | 633 scope->settings()->build_settings()->root_path_utf8()); |
634 if (!err->has_error()) { | 634 if (!err->has_error()) { |
635 scope->settings()->import_manager().DoImport(import_file, function, | 635 scope->settings()->import_manager().DoImport(import_file, function, |
636 scope, err); | 636 scope, err); |
637 } | 637 } |
638 return Value(); | 638 return Value(); |
639 } | 639 } |
640 | 640 |
641 // not_needed ----------------------------------------------------------------- | |
642 | |
643 const char kNotNeeded[] = "not_needed"; | |
644 const char kNotNeeded_HelpShort[] = | |
645 "not_needed: Mark variables from scope as unusedd."; | |
brettw
2017/06/20 17:27:25
"unusedd" spelling
Petr Hosek
2017/06/21 02:17:40
Done.
| |
646 const char kNotNeeded_Help[] = | |
647 R"(not_needed: Mark variables from scope as unused. | |
648 | |
649 not_needed(from_scope, variable_list_or_star, | |
650 variable_to_ignore_list = []) | |
651 | |
652 Mark the variables from the given scope as not being used in the current | |
653 scope. Any past or future uses of these variables within the current scope | |
654 will fail with an error. | |
655 | |
656 Example | |
657 | |
658 not_needed(invoker, "*", [ "config" ]) | |
659 not_needed(invoker, [ "data_deps", "deps" ]) | |
brettw
2017/06/20 17:27:25
Can you add support for a third form that modifies
Petr Hosek
2017/06/21 02:17:40
Done.
| |
660 )"; | |
661 | |
662 Value RunNotNeeded(Scope* scope, | |
663 const FunctionCallNode* function, | |
664 const ListNode* args_list, | |
665 Err* err) { | |
666 const auto& args_vector = args_list->contents(); | |
667 if (args_vector.size() != 2 && args_vector.size() != 3) { | |
668 *err = Err(function, "Wrong number of arguments.", | |
669 "Expecting two or three arguments."); | |
670 return Value(); | |
671 } | |
672 | |
673 Value* value = nullptr; // Value to use, may point to result_value. | |
674 Value result_value; // Storage for the "evaluate" case. | |
675 const IdentifierNode* identifier = args_vector[0]->AsIdentifier(); | |
676 if (identifier) { | |
677 // Optimize the common case where the input scope is an identifier. This | |
678 // prevents a copy of a potentially large Scope object. | |
679 value = scope->GetMutableValue(identifier->value().value(), | |
680 Scope::SEARCH_NESTED, true); | |
681 if (!value) { | |
682 *err = Err(identifier, "Undefined identifier."); | |
683 return Value(); | |
684 } | |
685 } else { | |
686 // Non-optimized case, just evaluate the argument. | |
687 result_value = args_vector[0]->Execute(scope, err); | |
688 if (err->has_error()) | |
689 return Value(); | |
690 value = &result_value; | |
691 } | |
692 | |
693 // Extract the source scope. | |
694 if (!value->VerifyTypeIs(Value::SCOPE, err)) | |
695 return Value(); | |
696 Scope* source = value->scope_value(); | |
697 | |
698 // Extract the exclusion list if defined. | |
699 std::set<std::string> exclusion_set; | |
700 if (args_vector.size() == 3) { | |
701 Value exclusion_value = args_vector[2]->Execute(scope, err); | |
702 if (err->has_error()) | |
703 return Value(); | |
704 | |
705 if (exclusion_value.type() != Value::LIST) { | |
706 *err = Err(exclusion_value, "Not a valid list of variables to exclude.", | |
707 "Expecting a list of strings."); | |
708 return Value(); | |
709 } | |
710 | |
711 for (const Value& cur : exclusion_value.list_value()) { | |
712 if (!cur.VerifyTypeIs(Value::STRING, err)) | |
713 return Value(); | |
714 | |
715 exclusion_set.insert(cur.string_value()); | |
716 } | |
717 } | |
718 | |
719 // Extract the list. If all_values is not set, the what_value will be a list. | |
720 Value what_value = args_vector[1]->Execute(scope, err); | |
721 if (err->has_error()) | |
722 return Value(); | |
723 if (what_value.type() == Value::STRING) { | |
724 if (what_value.string_value() == "*") { | |
725 source->MarkAllUnusable(err, exclusion_set); | |
726 return Value(); | |
727 } | |
728 } else { | |
729 if (what_value.type() == Value::LIST) { | |
730 for (const Value& cur : what_value.list_value()) { | |
731 if (!cur.VerifyTypeIs(Value::STRING, err)) | |
732 return Value(); | |
733 source->MarkUnusable(cur.string_value(), err); | |
734 } | |
735 return Value(); | |
736 } | |
737 } | |
738 | |
739 // Not the right type of argument. | |
740 *err = Err(what_value, "Not a valid list of variables.", | |
741 "Expecting either the string \"*\" or a list of strings."); | |
742 return Value(); | |
743 } | |
744 | |
641 // set_sources_assignment_filter ----------------------------------------------- | 745 // set_sources_assignment_filter ----------------------------------------------- |
642 | 746 |
643 const char kSetSourcesAssignmentFilter[] = "set_sources_assignment_filter"; | 747 const char kSetSourcesAssignmentFilter[] = "set_sources_assignment_filter"; |
644 const char kSetSourcesAssignmentFilter_HelpShort[] = | 748 const char kSetSourcesAssignmentFilter_HelpShort[] = |
645 "set_sources_assignment_filter: Set a pattern to filter source files."; | 749 "set_sources_assignment_filter: Set a pattern to filter source files."; |
646 const char kSetSourcesAssignmentFilter_Help[] = | 750 const char kSetSourcesAssignmentFilter_Help[] = |
647 R"(set_sources_assignment_filter: Set a pattern to filter source files. | 751 R"(set_sources_assignment_filter: Set a pattern to filter source files. |
648 | 752 |
649 The sources assignment filter is a list of patterns that remove files from | 753 The sources assignment filter is a list of patterns that remove files from |
650 the list implicitly whenever the "sources" variable is assigned to. This will | 754 the list implicitly whenever the "sources" variable is assigned to. This will |
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1033 INSERT_FUNCTION(DeclareArgs, false) | 1137 INSERT_FUNCTION(DeclareArgs, false) |
1034 INSERT_FUNCTION(Defined, false) | 1138 INSERT_FUNCTION(Defined, false) |
1035 INSERT_FUNCTION(ExecScript, false) | 1139 INSERT_FUNCTION(ExecScript, false) |
1036 INSERT_FUNCTION(ForEach, false) | 1140 INSERT_FUNCTION(ForEach, false) |
1037 INSERT_FUNCTION(ForwardVariablesFrom, false) | 1141 INSERT_FUNCTION(ForwardVariablesFrom, false) |
1038 INSERT_FUNCTION(GetEnv, false) | 1142 INSERT_FUNCTION(GetEnv, false) |
1039 INSERT_FUNCTION(GetLabelInfo, false) | 1143 INSERT_FUNCTION(GetLabelInfo, false) |
1040 INSERT_FUNCTION(GetPathInfo, false) | 1144 INSERT_FUNCTION(GetPathInfo, false) |
1041 INSERT_FUNCTION(GetTargetOutputs, false) | 1145 INSERT_FUNCTION(GetTargetOutputs, false) |
1042 INSERT_FUNCTION(Import, false) | 1146 INSERT_FUNCTION(Import, false) |
1147 INSERT_FUNCTION(NotNeeded, false) | |
1043 INSERT_FUNCTION(Pool, false) | 1148 INSERT_FUNCTION(Pool, false) |
1044 INSERT_FUNCTION(Print, false) | 1149 INSERT_FUNCTION(Print, false) |
1045 INSERT_FUNCTION(ProcessFileTemplate, false) | 1150 INSERT_FUNCTION(ProcessFileTemplate, false) |
1046 INSERT_FUNCTION(ReadFile, false) | 1151 INSERT_FUNCTION(ReadFile, false) |
1047 INSERT_FUNCTION(RebasePath, false) | 1152 INSERT_FUNCTION(RebasePath, false) |
1048 INSERT_FUNCTION(SetDefaults, false) | 1153 INSERT_FUNCTION(SetDefaults, false) |
1049 INSERT_FUNCTION(SetDefaultToolchain, false) | 1154 INSERT_FUNCTION(SetDefaultToolchain, false) |
1050 INSERT_FUNCTION(SetSourcesAssignmentFilter, false) | 1155 INSERT_FUNCTION(SetSourcesAssignmentFilter, false) |
1051 INSERT_FUNCTION(SplitList, false) | 1156 INSERT_FUNCTION(SplitList, false) |
1052 INSERT_FUNCTION(Template, false) | 1157 INSERT_FUNCTION(Template, false) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1137 } | 1242 } |
1138 | 1243 |
1139 // Otherwise it's a no-block function. | 1244 // Otherwise it's a no-block function. |
1140 if (!VerifyNoBlockForFunctionCall(function, block, err)) | 1245 if (!VerifyNoBlockForFunctionCall(function, block, err)) |
1141 return Value(); | 1246 return Value(); |
1142 return found_function->second.no_block_runner(scope, function, | 1247 return found_function->second.no_block_runner(scope, function, |
1143 args.list_value(), err); | 1248 args.list_value(), err); |
1144 } | 1249 } |
1145 | 1250 |
1146 } // namespace functions | 1251 } // namespace functions |
OLD | NEW |