Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VARRAY_FOREACH and VPTRS_ITER macros to iterate over arrays and pointer arrays #4239

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Commits on Nov 28, 2024

  1. Add VARRAY_FOREACH macro for array iteration

    This macro is intended to simplify loops over elements of an array using pointer
    arithmetic instead of an array index.
    
    The usual pattern of iterating over the array index and then accessing the i-th
    element of the array is replaced with calculating the end pointer and looping
    over the array elements.
    
    The macro basically implements
    
    	for (pointer = array, end = array + length; pointer < end; pointer++)
    
    This pattern is more efficient but less clear to read when written out. The
    macro is similar to the *_FOREACH pattern which we already use in many other
    places with trees, lists and queues, but declares the loop variable locally.
    
    Remarks on the implementation:
    
    - typeof() is now formalized with C23 but has been a GNU extension for long. We
      use typeof() since e1ac593 as of March 2021.
    
    - The construct 'typeof(*(arr)) * var' (using the element type declaring a
      pointer on it) is used to un-const arr while keeping the constness of the
      element type: If arr is const, '(typeof arr) var = arr' does not work because
      var can not be modified.
    
      The additional (typeof(var)) cast is to avoid a Flexelint warning (reference
      to pointer).
    nigoroll committed Nov 28, 2024
    Configuration menu
    Copy the full SHA
    1902d4c View commit details
    Browse the repository at this point in the history
  2. Add a semantic patch and start using VARRAY_FOREACH

    The coccinelle patch only catches some cases, makes spatch enter a spinloop and
    the patch result needs manual polish on top, but at this point the purpose is
    only to show use cases for the new macro.
    nigoroll committed Nov 28, 2024
    Configuration menu
    Copy the full SHA
    2f50f6c View commit details
    Browse the repository at this point in the history
  3. Add VPTRS_ITER to further simplify our "array of pointers" case

    Based on VARRAY_FOREACH, this additional macro further simplifies the case of
    arrays of pointer as, for example, used in struct strands.
    
    Here, the loop variable is to be declared outside the macro to support keeping
    found elements from within the loop.
    nigoroll committed Nov 28, 2024
    Configuration menu
    Copy the full SHA
    ccf1195 View commit details
    Browse the repository at this point in the history
  4. Use VPTRS_ITER

    we add a semantic patch to turn VARRAY_FOREACH cases into VPTRS_ITER cases where
    applicable.
    nigoroll committed Nov 28, 2024
    Configuration menu
    Copy the full SHA
    9275330 View commit details
    Browse the repository at this point in the history