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

Side by Side Diff: tools/gn/functions.cc

Issue 2936923002: Add not_needed function (Closed)
Patch Set: Rebase Created 3 years, 5 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 unified diff | Download patch
« no previous file with comments | « tools/gn/functions.h ('k') | tools/gn/functions_target_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 620 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 not needed.";
646 const char kNotNeeded_Help[] =
647 R"(not_needed: Mark variables from scope as not needed.
648
649 not_needed(variable_list_or_star, variable_to_ignore_list = [])
650 not_needed(from_scope, variable_list_or_star,
651 variable_to_ignore_list = [])
652
653 Mark the variables in the current or given scope as not needed, which means
654 you will not get an error about unused variables for these. The
655 variable_to_ignore_list allows excluding variables from "all matches" if
656 variable_list_or_star is "*".
657
658 Example
659
660 not_needed("*", [ "config" ])
661 not_needed([ "data_deps", "deps" ])
662 not_needed(invoker, "*", [ "config" ])
663 not_needed(invoker, [ "data_deps", "deps" ])
664 )";
665
666 Value RunNotNeeded(Scope* scope,
667 const FunctionCallNode* function,
668 const ListNode* args_list,
669 Err* err) {
670 const auto& args_vector = args_list->contents();
671 if (args_vector.size() < 1 && args_vector.size() > 3) {
672 *err = Err(function, "Wrong number of arguments.",
673 "Expecting one, two or three arguments.");
674 return Value();
675 }
676 auto args_cur = args_vector.begin();
677
678 Value* value = nullptr; // Value to use, may point to result_value.
679 Value result_value; // Storage for the "evaluate" case.
680 const IdentifierNode* identifier = (*args_cur)->AsIdentifier();
681 if (identifier) {
682 // Optimize the common case where the input scope is an identifier. This
683 // prevents a copy of a potentially large Scope object.
684 value = scope->GetMutableValue(identifier->value().value(),
685 Scope::SEARCH_NESTED, true);
686 if (!value) {
687 *err = Err(identifier, "Undefined identifier.");
688 return Value();
689 }
690 } else {
691 // Non-optimized case, just evaluate the argument.
692 result_value = (*args_cur)->Execute(scope, err);
693 if (err->has_error())
694 return Value();
695 value = &result_value;
696 }
697 args_cur++;
698
699 // Extract the source scope if different from current one.
700 Scope* source = scope;
701 if (value->type() == Value::SCOPE) {
702 source = value->scope_value();
703 result_value = (*args_cur)->Execute(scope, err);
704 if (err->has_error())
705 return Value();
706 value = &result_value;
707 args_cur++;
708 }
709
710 // Extract the exclusion list if defined.
711 Value exclusion_value;
712 std::set<std::string> exclusion_set;
713 if (args_cur != args_vector.end()) {
714 exclusion_value = (*args_cur)->Execute(source, err);
715 if (err->has_error())
716 return Value();
717
718 if (exclusion_value.type() != Value::LIST) {
719 *err = Err(exclusion_value, "Not a valid list of variables to exclude.",
720 "Expecting a list of strings.");
721 return Value();
722 }
723
724 for (const Value& cur : exclusion_value.list_value()) {
725 if (!cur.VerifyTypeIs(Value::STRING, err))
726 return Value();
727
728 exclusion_set.insert(cur.string_value());
729 }
730 }
731
732 if (value->type() == Value::STRING) {
733 if (value->string_value() == "*") {
734 source->MarkAllUsed(exclusion_set);
735 return Value();
736 }
737 } else if (value->type() == Value::LIST) {
738 if (exclusion_value.type() != Value::NONE) {
739 *err = Err(exclusion_value, "Not supported with a variable list.",
740 "Exclusion list can only be used with the string \"*\".");
741 return Value();
742 }
743 for (const Value& cur : value->list_value()) {
744 if (!cur.VerifyTypeIs(Value::STRING, err))
745 return Value();
746 source->MarkUsed(cur.string_value());
747 }
748 return Value();
749 }
750
751 // Not the right type of argument.
752 *err = Err(*value, "Not a valid list of variables.",
753 "Expecting either the string \"*\" or a list of strings.");
754 return Value();
755 }
756
641 // set_sources_assignment_filter ----------------------------------------------- 757 // set_sources_assignment_filter -----------------------------------------------
642 758
643 const char kSetSourcesAssignmentFilter[] = "set_sources_assignment_filter"; 759 const char kSetSourcesAssignmentFilter[] = "set_sources_assignment_filter";
644 const char kSetSourcesAssignmentFilter_HelpShort[] = 760 const char kSetSourcesAssignmentFilter_HelpShort[] =
645 "set_sources_assignment_filter: Set a pattern to filter source files."; 761 "set_sources_assignment_filter: Set a pattern to filter source files.";
646 const char kSetSourcesAssignmentFilter_Help[] = 762 const char kSetSourcesAssignmentFilter_Help[] =
647 R"(set_sources_assignment_filter: Set a pattern to filter source files. 763 R"(set_sources_assignment_filter: Set a pattern to filter source files.
648 764
649 The sources assignment filter is a list of patterns that remove files from 765 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 766 the list implicitly whenever the "sources" variable is assigned to. This will
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
1033 INSERT_FUNCTION(DeclareArgs, false) 1149 INSERT_FUNCTION(DeclareArgs, false)
1034 INSERT_FUNCTION(Defined, false) 1150 INSERT_FUNCTION(Defined, false)
1035 INSERT_FUNCTION(ExecScript, false) 1151 INSERT_FUNCTION(ExecScript, false)
1036 INSERT_FUNCTION(ForEach, false) 1152 INSERT_FUNCTION(ForEach, false)
1037 INSERT_FUNCTION(ForwardVariablesFrom, false) 1153 INSERT_FUNCTION(ForwardVariablesFrom, false)
1038 INSERT_FUNCTION(GetEnv, false) 1154 INSERT_FUNCTION(GetEnv, false)
1039 INSERT_FUNCTION(GetLabelInfo, false) 1155 INSERT_FUNCTION(GetLabelInfo, false)
1040 INSERT_FUNCTION(GetPathInfo, false) 1156 INSERT_FUNCTION(GetPathInfo, false)
1041 INSERT_FUNCTION(GetTargetOutputs, false) 1157 INSERT_FUNCTION(GetTargetOutputs, false)
1042 INSERT_FUNCTION(Import, false) 1158 INSERT_FUNCTION(Import, false)
1159 INSERT_FUNCTION(NotNeeded, false)
1043 INSERT_FUNCTION(Pool, false) 1160 INSERT_FUNCTION(Pool, false)
1044 INSERT_FUNCTION(Print, false) 1161 INSERT_FUNCTION(Print, false)
1045 INSERT_FUNCTION(ProcessFileTemplate, false) 1162 INSERT_FUNCTION(ProcessFileTemplate, false)
1046 INSERT_FUNCTION(ReadFile, false) 1163 INSERT_FUNCTION(ReadFile, false)
1047 INSERT_FUNCTION(RebasePath, false) 1164 INSERT_FUNCTION(RebasePath, false)
1048 INSERT_FUNCTION(SetDefaults, false) 1165 INSERT_FUNCTION(SetDefaults, false)
1049 INSERT_FUNCTION(SetDefaultToolchain, false) 1166 INSERT_FUNCTION(SetDefaultToolchain, false)
1050 INSERT_FUNCTION(SetSourcesAssignmentFilter, false) 1167 INSERT_FUNCTION(SetSourcesAssignmentFilter, false)
1051 INSERT_FUNCTION(SplitList, false) 1168 INSERT_FUNCTION(SplitList, false)
1052 INSERT_FUNCTION(Template, false) 1169 INSERT_FUNCTION(Template, false)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
1137 } 1254 }
1138 1255
1139 // Otherwise it's a no-block function. 1256 // Otherwise it's a no-block function.
1140 if (!VerifyNoBlockForFunctionCall(function, block, err)) 1257 if (!VerifyNoBlockForFunctionCall(function, block, err))
1141 return Value(); 1258 return Value();
1142 return found_function->second.no_block_runner(scope, function, 1259 return found_function->second.no_block_runner(scope, function,
1143 args.list_value(), err); 1260 args.list_value(), err);
1144 } 1261 }
1145 1262
1146 } // namespace functions 1263 } // namespace functions
OLDNEW
« no previous file with comments | « tools/gn/functions.h ('k') | tools/gn/functions_target_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698