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 <iostream> | 7 #include <iostream> |
8 | 8 |
9 #include "base/environment.h" | 9 #include "base/environment.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "tools/gn/config.h" | 11 #include "tools/gn/config.h" |
12 #include "tools/gn/config_values_generator.h" | 12 #include "tools/gn/config_values_generator.h" |
13 #include "tools/gn/err.h" | 13 #include "tools/gn/err.h" |
14 #include "tools/gn/input_file.h" | 14 #include "tools/gn/input_file.h" |
15 #include "tools/gn/parse_tree.h" | 15 #include "tools/gn/parse_tree.h" |
16 #include "tools/gn/scheduler.h" | 16 #include "tools/gn/scheduler.h" |
17 #include "tools/gn/scope.h" | 17 #include "tools/gn/scope.h" |
18 #include "tools/gn/settings.h" | 18 #include "tools/gn/settings.h" |
19 #include "tools/gn/template.h" | 19 #include "tools/gn/template.h" |
20 #include "tools/gn/token.h" | 20 #include "tools/gn/token.h" |
21 #include "tools/gn/value.h" | 21 #include "tools/gn/value.h" |
22 | 22 |
| 23 namespace { |
| 24 |
| 25 // Some functions take a {} following them, and some don't. For the ones that |
| 26 // don't, this is used to verify that the given block node is null and will |
| 27 // set the error accordingly if it's not. Returns true if the block is null. |
| 28 bool VerifyNoBlockForFunctionCall(const FunctionCallNode* function, |
| 29 const BlockNode* block, |
| 30 Err* err) { |
| 31 if (!block) |
| 32 return true; |
| 33 |
| 34 *err = Err(block, "Unexpected '{'.", |
| 35 "This function call doesn't take a {} block following it, and you\n" |
| 36 "can't have a {} block that's not connected to something like an if\n" |
| 37 "statement or a target declaration."); |
| 38 err->AppendRange(function->function().range()); |
| 39 return false; |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
23 bool EnsureNotProcessingImport(const ParseNode* node, | 44 bool EnsureNotProcessingImport(const ParseNode* node, |
24 const Scope* scope, | 45 const Scope* scope, |
25 Err* err) { | 46 Err* err) { |
26 if (scope->IsProcessingImport()) { | 47 if (scope->IsProcessingImport()) { |
27 *err = Err(node, "Not valid from an import.", | 48 *err = Err(node, "Not valid from an import.", |
28 "Imports are for defining defaults, variables, and rules. The\n" | 49 "Imports are for defining defaults, variables, and rules. The\n" |
29 "appropriate place for this kind of thing is really in a normal\n" | 50 "appropriate place for this kind of thing is really in a normal\n" |
30 "BUILD file."); | 51 "BUILD file."); |
31 return false; | 52 return false; |
32 } | 53 } |
(...skipping 704 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 if (err->has_error()) | 758 if (err->has_error()) |
738 return Value(); | 759 return Value(); |
739 return templ->Invoke(scope, function, args.list_value(), block, err); | 760 return templ->Invoke(scope, function, args.list_value(), block, err); |
740 } | 761 } |
741 | 762 |
742 *err = Err(name, "Unknown function."); | 763 *err = Err(name, "Unknown function."); |
743 return Value(); | 764 return Value(); |
744 } | 765 } |
745 | 766 |
746 if (found_function->second.self_evaluating_args_runner) { | 767 if (found_function->second.self_evaluating_args_runner) { |
| 768 // Self evaluating args functions are special weird built-ins. Currently, |
| 769 // only foreach() takes a block following it. Rather than force them all to |
| 770 // check that they have a block or no block and risk bugs for new additions, |
| 771 // check a whitelist here. |
| 772 if (found_function->second.self_evaluating_args_runner != &RunForEach) { |
| 773 if (!VerifyNoBlockForFunctionCall(function, block, err)) |
| 774 return Value(); |
| 775 } |
747 return found_function->second.self_evaluating_args_runner( | 776 return found_function->second.self_evaluating_args_runner( |
748 scope, function, args_list, err); | 777 scope, function, args_list, err); |
749 } | 778 } |
750 | 779 |
751 // All other function types take a pre-executed set of args. | 780 // All other function types take a pre-executed set of args. |
752 Value args = args_list->Execute(scope, err); | 781 Value args = args_list->Execute(scope, err); |
753 if (err->has_error()) | 782 if (err->has_error()) |
754 return Value(); | 783 return Value(); |
755 | 784 |
756 if (found_function->second.generic_block_runner) { | 785 if (found_function->second.generic_block_runner) { |
(...skipping 20 matching lines...) Expand all Loading... |
777 function, args.list_value(), &block_scope, err); | 806 function, args.list_value(), &block_scope, err); |
778 if (err->has_error()) | 807 if (err->has_error()) |
779 return Value(); | 808 return Value(); |
780 | 809 |
781 if (!block_scope.CheckForUnusedVars(err)) | 810 if (!block_scope.CheckForUnusedVars(err)) |
782 return Value(); | 811 return Value(); |
783 return result; | 812 return result; |
784 } | 813 } |
785 | 814 |
786 // Otherwise it's a no-block function. | 815 // Otherwise it's a no-block function. |
| 816 if (!VerifyNoBlockForFunctionCall(function, block, err)) |
| 817 return Value(); |
787 return found_function->second.no_block_runner(scope, function, | 818 return found_function->second.no_block_runner(scope, function, |
788 args.list_value(), err); | 819 args.list_value(), err); |
789 } | 820 } |
790 | 821 |
791 } // namespace functions | 822 } // namespace functions |
OLD | NEW |