Skip to content
  • Eric Blake's avatar
    maint: pick consistent spacing style for casts · 17d0bc6a
    Eric Blake authored
    We are inconsistent on whether a cast operator should be separated
    from its argument by a space.  As a unary prefix operator, casts bind
    with relatively tight precedence, where only postfix operations
    (including function calls) come higher.  We generally don't use a
    space after other prefix operators (*, &, ~, and unary - tend to not
    be followed by space; we are less consistent on !, but that would be a
    separate patch).
    
    Furthermore, we've already decided (in commit b5101fbc
    
    ) that we prefer
    spaces before function invocations.  At the time, discussion on the
    list[1] pointed out that thanks to the complexity of C with its
    context-sensitive parsing needing to know whether an identifier is a
    type name, you would be able to tell the difference between a
    2-argument function call with two parameters through a function
    pointer (or intentional function-like macro suppression):
    
      (foo) (bar, baz)
    
    from the (admittedly rare) application of a cast operator to the
    results of a comma operator:
    
      (foo)(bar, baz)
    
    if we consistently avoid spaces after casts.
    
    Determining cast operators is not always trivial, but the regex used
    below seems to have a pretty low false positive rate (lines it selects
    usually are casts and not in a comment line), and can be modified by
    removing the space to see where we already had casts without a
    space. (Obviously, I can't tell how many false negatives there are of
    casts I missed out on).  While this changes more lines than would be
    done by instead always using a space in a cast, it is still manageable
    to do a bulk change.
    
    $ git grep '\(_t\|int\|long\|signed\|char\|\*\)) [a-zA-Z0-9&"]' -- \
      '**/*.[hc]' '**/*.ml' | grep -v '^ */\?\*'
    
    'git show -w' shows that this change is whitespace only.
    
    [1] https://listman.redhat.com/archives/libguestfs/2023-February/030771.html
    
    Signed-off-by: Eric Blake's avatarEric Blake <eblake@redhat.com>
    Message-Id: <20230524021244.261832-1-eblake@redhat.com>
    Reviewed-by: default avatarLaszlo Ersek <lersek@redhat.com>
    Acked-by: default avatarRichard W.M. Jones <rjones@redhat.com>
    17d0bc6a