Bug 147021 - Use std::size() instead of SAL_N_ELEMENTS() macro
Summary: Use std::size() instead of SAL_N_ELEMENTS() macro
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:7.4.0 reviewed:2022 target:7.5.0
Keywords: difficultyBeginner, easyHack, skillCpp
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2022-01-27 15:01 UTC by Hossein
Modified: 2022-06-20 10:26 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:
Regression By:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hossein 2022-01-27 15:01:18 UTC
The SAL_N_ELEMENTS() macro is defined in 'include/sal/macros.h'. It calculates the number of elements in an array at compile time. According the comments, "the argument must be an array and not a pointer".

These are some examples from the above file:

    SAL_N_ELEMENTS("foo");

    char aFoo[]="foo";
    SAL_N_ELEMENTS(aFoo);

This macro, uses sizeof() operator to calculate the "sizeof (arr) / sizeof ((arr)[0]))".

Now we can use std::size() instead of SAL_N_ELEMENTS() macro. The result would be constexpr, compile-time constant expression.

As an example, this code:

    for (unsigned i = 0; i != SAL_N_ELEMENTS(aFoo); ++i)
    {
        ...
    }

can be converted into this one:

    for (unsigned i = 0; i != std::size(aFoo); ++i)
    {
        ...
    }

One pitfall would be comparing the result with a signed value. Then, either you have to cast the result to a signed value, or use an unsigned value for the other side of the comparison.
Comment 1 Stephan Bergmann 2022-01-27 15:47:54 UTC
(In reply to Hossein from comment #0)
> One pitfall would be comparing the result with a signed value. Then, either
> you have to cast the result to a signed value, or use an unsigned value for
> the other side of the comparison.

If there's need, we can make available C++20

> template<class T, ptrdiff_t N> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept;

as o3tl::ssize for the time being.  (Also, if it is known that the other side of the comparison, even though of signed type, must be non-negative, like e.g. a call to OUString::getLength, then o3tl::make_unsigned from o3tl/safeint.hxx is a good choice.)
Comment 2 Mike Kaganski 2022-01-29 10:05:28 UTC
Note also, that there are cases where std::size can't give a compile-time constant, unlike the SAL_N_ELEMENTS macro. That may be e.g. when a class contains an array member, and a static_assert checks the array dimension in its constructor. In that case, a "'this' can't be used in constant expression" error would result after the replacement. Usually, there's a way to restructure such cases, to avoid use of static asserts, and ensure the tested invariant some other way.
Comment 3 Commit Notification 2022-02-06 20:39:10 UTC
VaibhavMalik4187 committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/8b327cd86d71d71d2f5f883321e5d53e3b42ed4e

tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 4 Commit Notification 2022-02-09 22:18:18 UTC
Hossein committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/a5c6fbe247ee9f9b2fba828d1360748c3fe4642b

tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 5 Hossein 2022-02-09 22:20:47 UTC
(In reply to Commit Notification from comment #4)
> Hossein committed a patch related to this issue.
> It has been pushed to "master":
This is an example for changing the type of the variable to a suitable unsigned type.
https://git.libreoffice.org/core/commit/a5c6fbe247ee9f9b2fba828d1360748c3fe4642b

The signed loop index variable of type 'int' is converted into 'size_t' to match the return value of std::size() which is unsigned
Comment 6 Commit Notification 2022-03-09 23:40:51 UTC
Gautham Krishnan committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/51fb84829afbc1c0957fd1a489085613ad199f1a

tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 7 Commit Notification 2022-03-16 08:14:34 UTC
psidiumcode committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/5b320aa058504becae6ac6746926b481b326a24d

tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 8 Commit Notification 2022-03-18 17:31:37 UTC
Sinduja Y committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/a40ed687a3e2f8e1d17705fcd7d397ba09398cb6

tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 9 Commit Notification 2022-05-09 18:43:02 UTC
Pragat Pandya committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/6abc09926c9b55a445b906303f56c6ec7fdeabf9

tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 10 Commit Notification 2022-05-30 10:22:25 UTC
Roman Kuznetsov committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/d694b572c7262a326ac7ceac2e558f3fc6c78df4

tdf#147021 Use std::size() instead SAL_N_ELEMENTS macro

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 11 Commit Notification 2022-05-30 10:24:35 UTC
Roman Kuznetsov committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/81aad6d53fa792dd95732e401ecb298714f699f4

tdf#147021 Use std::size() instead SAL_N_ELEMENTS macro

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 12 Commit Notification 2022-06-20 10:26:25 UTC
jsala committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/2c68c419c1fce6de1a81e1f13a84b7069125a204

tdf#147021 Use std::size() instead of SAL_N_ELEMENTS() macro

It will be available in 7.5.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.