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

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

Issue 2509333003: Change GN to disallow reading args defined in the same declare_args() call. (Closed)
Patch Set: initial patch for review Created 4 years, 1 month 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
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 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 350
351 Introduces the given arguments into the current scope. If they are not 351 Introduces the given arguments into the current scope. If they are not
352 specified on the command line or in a toolchain's arguments, the default 352 specified on the command line or in a toolchain's arguments, the default
353 values given in the declare_args block will be used. However, these defaults 353 values given in the declare_args block will be used. However, these defaults
354 will not override command-line values. 354 will not override command-line values.
355 355
356 See also "gn help buildargs" for an overview. 356 See also "gn help buildargs" for an overview.
357 357
358 The precise behavior of declare args is: 358 The precise behavior of declare args is:
359 359
360 1. The declare_arg block executes. Any variables in the enclosing scope are 360 1. The declare_args() block executes. Any variable defined in the enclosing
361 available for reading. 361 scope is available for reading, but any variable defined earlier in
362 the current scope is not (since the overrides haven't been applied yet).
362 363
363 2. At the end of executing the block, any variables set within that scope 364 2. At the end of executing the block, any variables set within that scope
364 are saved globally as build arguments, with their current values being 365 are saved globally as build arguments, with their current values being
365 saved as the "default value" for that argument. 366 saved as the "default value" for that argument.
366 367
367 3. User-defined overrides are applied. Anything set in "gn args" now 368 3. User-defined overrides are applied. Anything set in "gn args" now
368 overrides any default values. The resulting set of variables is promoted 369 overrides any default values. The resulting set of variables is promoted
369 to be readable from the following code in the file. 370 to be readable from the following code in the file.
370 371
371 This has some ramifications that may not be obvious: 372 This has some ramifications that may not be obvious:
372 373
373 - You should not perform difficult work inside a declare_args block since 374 - You should not perform difficult work inside a declare_args block since
374 this only sets a default value that may be discarded. In particular, 375 this only sets a default value that may be discarded. In particular,
375 don't use the result of exec_script() to set the default value. If you 376 don't use the result of exec_script() to set the default value. If you
376 want to have a script-defined default, set some default "undefined" value 377 want to have a script-defined default, set some default "undefined" value
377 like [], "", or -1, and after the declare_args block, call exec_script if 378 like [], "", or -1, and after the declare_args block, call exec_script if
378 the value is unset by the user. 379 the value is unset by the user.
379 380
380 - Any code inside of the declare_args block will see the default values of 381 - Because you cannot read the value of a variable defined in the same
381 previous variables defined in the block rather than the user-overridden 382 block, if you need to make the default value of one arg depend
382 value. This can be surprising because you will be used to seeing the 383 on the possibly-overridden value of another, write two separate
383 overridden value. If you need to make the default value of one arg 384 declare_args() blocks:
384 dependent on the possibly-overridden value of another, write two separate
385 declare_args blocks:
386 385
387 declare_args() { 386 declare_args() {
388 enable_foo = true 387 enable_foo = true
389 } 388 }
390 declare_args() { 389 declare_args() {
391 # Bar defaults to same user-overridden state as foo. 390 # Bar defaults to same user-overridden state as foo.
392 enable_bar = enable_foo 391 enable_bar = enable_foo
393 } 392 }
394 393
395 Example 394 Example
(...skipping 12 matching lines...) Expand all
408 Value RunDeclareArgs(Scope* scope, 407 Value RunDeclareArgs(Scope* scope,
409 const FunctionCallNode* function, 408 const FunctionCallNode* function,
410 const std::vector<Value>& args, 409 const std::vector<Value>& args,
411 BlockNode* block, 410 BlockNode* block,
412 Err* err) { 411 Err* err) {
413 NonNestableBlock non_nestable(scope, function, "declare_args"); 412 NonNestableBlock non_nestable(scope, function, "declare_args");
414 if (!non_nestable.Enter(err)) 413 if (!non_nestable.Enter(err))
415 return Value(); 414 return Value();
416 415
417 Scope block_scope(scope); 416 Scope block_scope(scope);
417
418 // Mark that this scope is part of a declare_args() call, in order to
419 // prevent reads of any variables that are defined earlier in the same call
420 // (see `gn help declare_args` for more).
421 block_scope.SetProperty(&kInDeclareArgsKey, (void *)&kInDeclareArgsKey);
brettw 2016/11/18 22:07:43 Are you sure you need the void* cast here? I think
Dirk Pranke 2016/11/18 22:35:39 I thought I didn't need it, either, but the compil
422
418 block->Execute(&block_scope, err); 423 block->Execute(&block_scope, err);
419 if (err->has_error()) 424 if (err->has_error())
420 return Value(); 425 return Value();
421 426
422 // Pass the values from our scope into the Args object for adding to the 427 // Pass the values from our scope into the Args object for adding to the
423 // scope with the proper values (taking into account the defaults given in 428 // scope with the proper values (taking into account the defaults given in
424 // the block_scope, and arguments passed into the build). 429 // the block_scope, and arguments passed into the build).
425 Scope::KeyValueMap values; 430 Scope::KeyValueMap values;
426 block_scope.GetCurrentScopeValues(&values); 431 block_scope.GetCurrentScopeValues(&values);
427 scope->settings()->build_settings()->build_args().DeclareArgs( 432 scope->settings()->build_settings()->build_args().DeclareArgs(
(...skipping 679 matching lines...) Expand 10 before | Expand all | Expand 10 after
1107 } 1112 }
1108 1113
1109 // Otherwise it's a no-block function. 1114 // Otherwise it's a no-block function.
1110 if (!VerifyNoBlockForFunctionCall(function, block, err)) 1115 if (!VerifyNoBlockForFunctionCall(function, block, err))
1111 return Value(); 1116 return Value();
1112 return found_function->second.no_block_runner(scope, function, 1117 return found_function->second.no_block_runner(scope, function,
1113 args.list_value(), err); 1118 args.list_value(), err);
1114 } 1119 }
1115 1120
1116 } // namespace functions 1121 } // namespace functions
OLDNEW
« no previous file with comments | « tools/gn/docs/reference.md ('k') | tools/gn/parse_tree.cc » ('j') | tools/gn/parse_tree.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698