Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a77fc96

Browse files
committed
Ruby: AST ref docs - note about desugaring and synthesized AstNodes
1 parent 530b29c commit a77fc96

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

docs/codeql/codeql-language-guides/abstract-syntax-tree-classes-for-working-with-ruby-programs.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,27 @@ All classes in this subsection are subclasses of VariableAccess_.
416416
| ``self`` | SelfVariableReadAccess_ | VariableReadAccess_, SelfVariableAccess_ | |
417417
+----------------------------+------------------------------+-----------------------------------------------+------------------+
418418

419+
Desugaring
420+
~~~~~~~~~~
421+
422+
Certain Ruby language features are implemented using syntactic sugar. For example, supposing that ``x`` is an object with an attribute ``foo``, the assignment::
423+
424+
x.foo = y
425+
426+
is desugared to code similar to::
427+
428+
x.foo=(__synth_0 = y);
429+
__synth_0;
430+
431+
In other words, there is effectively a call to the SetterMethodCall_ ``foo=`` on ``x`` with argument ``__synth_0 = y``, followed by a read of the ``__synth_0`` variable.
432+
433+
In CodeQL, this is implemented by syntheisizing AstNode_ instances corresponding to this desugared version of the code.
434+
435+
Note that both the original AssignExpr_ and the desugared SetterMethodCall_ versions are both available to CodeQL queries, and it is usually not necessary to be aware of any desugaring that may take place. However, if a codebase explicitly uses ``x.foo=(y)`` SetterMethodCall_ syntax, then this will not be found by a query for AssignExpr_ instances.
436+
437+
Other synthesized AstNode_ instances exist, see the isSynthesized_ and getDesugared_ predicates for details.
438+
439+
419440
.. _BlockArgument: https://codeql.github.com/codeql-standard-libraries/ruby/codeql/ruby/ast/Call.qll/type.Call$BlockArgument.html
420441
.. _Call: https://codeql.github.com/codeql-standard-libraries/ruby/codeql/ruby/ast/Call.qll/type.Call$Call.html
421442
.. _ElementReference: https://codeql.github.com/codeql-standard-libraries/ruby/codeql/ruby/ast/Call.qll/type.Call$ElementReference.html
@@ -618,3 +639,5 @@ All classes in this subsection are subclasses of VariableAccess_.
618639
.. _VariableAccess: https://codeql.github.com/codeql-standard-libraries/ruby/codeql/ruby/ast/Variable.qll/type.Variable$VariableAccess.html
619640
.. _VariableReadAccess: https://codeql.github.com/codeql-standard-libraries/ruby/codeql/ruby/ast/Variable.qll/type.Variable$VariableReadAccess.html
620641
.. _VariableWriteAccess: https://codeql.github.com/codeql-standard-libraries/ruby/codeql/ruby/ast/Variable.qll/type.Variable$VariableWriteAccess.html
642+
.. _isSynthesized: https://codeql.github.com/codeql-standard-libraries/ruby/codeql/ruby/AST.qll/predicate.AST$AstNode$isSynthesized.0.html
643+
.. _getDesugared: https://codeql.github.com/codeql-standard-libraries/ruby/codeql/ruby/AST.qll/predicate.AST$AstNode$getDesugared.0.html

0 commit comments

Comments
 (0)