simon-git: putty (main): Simon Tatham

Commits to Tartarus hosted VCS tartarus-commits at lists.tartarus.org
Thu Apr 3 12:40:16 BST 2025


TL;DR:
  7f960699 Use _Countof to implement lenof, where available.

Repository:     https://git.tartarus.org/simon/putty.git
On the web:     https://git.tartarus.org/?p=simon/putty.git
Branch updated: main
Committer:      Simon Tatham <anakin at pobox.com>
Date:           2025-04-03 12:40:16

commit 7f96069954dd3c903256a2ca8ae26ece0a274c51
web diff https://git.tartarus.org/?p=simon/putty.git;a=commitdiff;h=7f96069954dd3c903256a2ca8ae26ece0a274c51;hp=5ef7f2eaf0fd75f6681c762c42efe155fe28161e
Author: Simon Tatham <anakin at pobox.com>
Date:   Thu Apr 3 08:59:39 2025 +0100

    Use _Countof to implement lenof, where available.
    
    Up-to-date trunk clang has introduced a built-in operator called
    _Countof, which is like the 'lenof' macro in this code (returns the
    number of elements in a statically-declared array object) but with the
    safety advantage that it provokes a compile error if you accidentally
    use it on a pointer. In this commit I add a cmake-time check for it,
    and conditional on that, switch over the definition of lenof.
    
    This should add a safety check for accidental uses of lenof(pointer).
    When I tested it with new clang, this whole code base compiled cleanly
    with the new setting, so there aren't currently any such accidents.
    
    clang cites C2y as the source for _Countof: WG14 document N3369
    initially proposed it under a different name, and then there was a big
    internet survey about naming (in which of course I voted for lenof!),
    and document N3469 summarises the results, which show that the name
    _Countof and/or countof won. Links:
    
      https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3369.pdf
      https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3469.htm
    
    My reading of N3469 seems to say that there will _either_ be _Countof
    by itself, _or_ lowercase 'countof' as a new keyword, but they don't
    say which. They say they _don't_ intend to do the same equivocation we
    had with _Complex and _Bool, where you have a _Countof keyword and an
    optional header file defining a lowercase non-underscore macro
    wrapping it. But there hasn't been a new whole draft published since
    N3469 yet, so I don't know what will end up in it when there is.
    
    However, as of now, _Countof exists in at least one compiler, and that
    seems like enough reason to implement it here. If it becomes 'countof'
    in the real standard, then we can always change over later. (And in
    that case it would probably make sense to rename the macro throughout
    the code base to align with what will become the new standard usage.)

 charset/internal.h | 8 +++++++-
 cmake/cmake.h.in   | 2 ++
 cmake/setup.cmake  | 6 ++++++
 misc.h             | 4 ++++
 utils/tree234.c    | 8 +++++++-
 5 files changed, 26 insertions(+), 2 deletions(-)



More information about the tartarus-commits mailing list