This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Add missing cstdalign header
Needs RevisionPublic

Authored by sbc100 on May 4 2018, 11:41 AM.

Details

Event Timeline

sbc100 created this revision.May 4 2018, 11:41 AM
sbc100 updated this revision to Diff 145247.May 4 2018, 11:52 AM
  • revert
miyuki added a subscriber: miyuki.Sep 24 2018, 8:54 AM

I believe this header has been deprecated. According to http://eel.is/c++draft/diff.cpp17.library, the effect of this header is nothing. @mclow.lists can you chime in?

The resolution of LWG 2828 means that <cstdalign> exists in C++11 and C++14. The rationale from https://reviews.llvm.org/D96786#2566110 can be taken as saying that adding this header for C++11 conformance is reasonable.

ldionne requested changes to this revision.Mar 5 2021, 5:05 AM

The resolution of LWG 2828 means that <cstdalign> exists in C++11 and C++14. The rationale from https://reviews.llvm.org/D96786#2566110 can be taken as saying that adding this header for C++11 conformance is reasonable.

Yes, I agree, let's add it. But the header needs to be added in include/CMakeLists.txt. There also needs to be a test added to check for the contents of the headers, and the auto-generated header tests should be modified. Take a look at what I did in https://reviews.llvm.org/D97870 for <cuchar>.

This revision now requires changes to proceed.Mar 5 2021, 5:05 AM
sbc100 updated this revision to Diff 328522.Mar 5 2021, 7:23 AM

Rebase against mono-repo

Herald added a project: Restricted Project. · View Herald TranscriptMar 5 2021, 7:23 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
sbc100 updated this revision to Diff 328524.Mar 5 2021, 7:27 AM

feedback

curdeius added inline comments.
libcxx/include/cstdalign
19–22 ↗(On Diff #328524)

You should test that each of these macros is really defined when including cstdalign.

libcxx/test/libcxx/utilities/any/size_and_alignment.pass.cpp
16 ↗(On Diff #328524)

This doesn't seem to be necessary. Or?
In C++, alignof is a keyword.

sbc100 added inline comments.Mar 5 2021, 8:12 AM
libcxx/test/libcxx/utilities/any/size_and_alignment.pass.cpp
16 ↗(On Diff #328524)

You are right its not necessary in C++ since its a keyword. My memory of exactly why I added to this file in the original version of the CL back in 2018 is ... missing. Maybe its to ensure that the inclusion of the C header and the macro don't interfere with C++ usage?

I'm happy to revert this line and find a better way to test.

sbc100 added inline comments.Mar 5 2021, 8:26 AM
libcxx/include/cstdalign
19–22 ↗(On Diff #328524)

Done. As you can see there is not much to tests since in C++ this is essentially an empty header.

sbc100 updated this revision to Diff 328538.Mar 5 2021, 8:27 AM

add tests

ldionne requested changes to this revision.Mar 5 2021, 9:23 AM
ldionne added inline comments.
libcxx/test/std/language.support/cstdalign/cstdalign.pass.cpp
8 ↗(On Diff #328538)

I think we should add // REQUIRES: c++03 || c++11 || c++14 || c++17, because a conforming implementation could be not shipping the header in C++20 and beyond.

14–28 ↗(On Diff #328538)

This can be moved outside of main().

27 ↗(On Diff #328538)

I'm not seeing __alignof_is_defined anywhere in the spec?

This revision now requires changes to proceed.Mar 5 2021, 9:23 AM
sbc100 marked 2 inline comments as done.Mar 5 2021, 9:40 AM
sbc100 added inline comments.
libcxx/test/std/language.support/cstdalign/cstdalign.pass.cpp
14–28 ↗(On Diff #328538)

I was copying from libcxx/test/std/language.support/cstdint/cstdint.syn/cstdint.pass.cpp which does these checks inside of main.

Will move them.

27 ↗(On Diff #328538)

Removed

sbc100 updated this revision to Diff 328557.Mar 5 2021, 9:40 AM
sbc100 marked 2 inline comments as done.

feedback

sbc100 updated this revision to Diff 328561.Mar 5 2021, 9:43 AM

feedback

sbc100 marked an inline comment as done.Mar 5 2021, 9:43 AM
curdeius retitled this revision from Add missing cstdalign header to [libc++] Add missing cstdalign header.Mar 5 2021, 10:01 AM
curdeius edited the summary of this revision. (Show Details)
curdeius accepted this revision as: curdeius.Mar 5 2021, 10:03 AM

LGTM but please wait for libc++ group approval.
If you need someone to land it for you, please provide "Firstname Name <email@org>" for attribution.

ldionne accepted this revision.Mar 5 2021, 11:42 AM

Please wait for CI to finish!

This revision is now accepted and ready to land.Mar 5 2021, 11:42 AM
libcxx/include/cstdalign
24 ↗(On Diff #328561)

This seems to be assuming that the underlying C library's stdalign.h is C++ friendly. A C11 stdalign.h does define alignof and alignas as macros.

libcxx/test/std/language.support/cstdalign/cstdalign.pass.cpp
27 ↗(On Diff #328538)

Seems like a defect in the old standard. The prose doesn't match the synopsis. __alignof_is_defined is a macro in C11's stdalign.h (and so is alignof). That the C++ committee did not intend for an alignof macro can probably be assumed. I suspect the lack of an __alignof_is_defined macro was also unintended.

sbc100 added inline comments.Mar 6 2021, 6:46 PM
libcxx/include/cstdalign
24 ↗(On Diff #328561)

Should I just remove this #include then?

libcxx/test/std/language.support/cstdalign/cstdalign.pass.cpp
27 ↗(On Diff #328538)

So should I add back the check for __alignof_is_defined?

libcxx/include/cstdalign
24 ↗(On Diff #328561)

The idea would be to add a stdalign.h alongside this header that doesn't #include_next the underlying C library's stdalign.h.

libcxx/test/std/language.support/cstdalign/cstdalign.pass.cpp
27 ↗(On Diff #328538)

I think so (in addition to also checking that alignof is not defined as a macro). I think the patch needs to be confirmed again either way by the libc++ approvers though.

curdeius added inline comments.Mar 9 2021, 9:44 AM
libcxx/include/cstdalign
24 ↗(On Diff #328561)

I'm not sure if that should be the solution. At least gcc's libstdc++ assumes that stdalign.h is C++-compatbile (cf. https://github.com/gcc-mirror/gcc/blob/16e2427f50c208dfe07d07f18009969502c25dc8/libstdc%2B%2B-v3/include/c_global/cstdalign).

Clang provides a compatible header: https://github.com/llvm/llvm-project/commit/8acb4044d83ecc9df81b1c9f327d5bd4325e1756.
Gcc too of course: https://github.com/gcc-mirror/gcc/blob/16e2427f50c208dfe07d07f18009969502c25dc8/gcc/ginclude/stdalign.h.

MSVC's STL on the other hand, doesn't include <stdalign.h> (https://github.com/microsoft/STL/blob/main/stl/inc/cstdalign).

@hubert.reinterpretcast, are you aware of an environment which has non-friendly stdalign.h?

curdeius added inline comments.Mar 9 2021, 9:46 AM
libcxx/include/cstdalign
24 ↗(On Diff #328561)
libcxx/include/cstdalign
24 ↗(On Diff #328561)
Quote:

@hubert.reinterpretcast, are you aware of an environment which has non-friendly stdalign.h?

The one GCC provides disagrees with the interpretation I gave of which macros should be present. The one that Clang provides does match my interpretation. It seems the GCC one is non-friendly (albeit a different form of non-friendly than the one I opened with).

curdeius requested changes to this revision.Mar 10 2021, 12:34 AM
curdeius added inline comments.
libcxx/include/cstdalign
24 ↗(On Diff #328561)

Oh, you mean that __alignas_is_defined and __alignof_is_defined won't be defined in this case, right?
In this case, I guess we won't avoid having stdalign.h as you had suggested.
And indeed the test fails with gcc:

bin/llvm-lit -vv ../../libcxx/test/std/language.support/cstdalign/ --param=std=c++17 --param=cxx_under_test=`which g++`
...
libcxx/test/std/language.support/cstdalign/cstdalign.pass.cpp:21:2: error: #error __alignas_is_defined not defined
   21 | #error __alignas_is_defined not defined
      |  ^~~~~

That's unfortunately a configuration which is not tested in the CI.

libcxx/test/std/language.support/cstdalign/cstdalign.pass.cpp
16–18 ↗(On Diff #328561)

Please do the same for alignof.

27 ↗(On Diff #328538)

Please check __alignof_is_defined as well, but guard it with #ifdef _LIBCPP_VERSION to be conforming.
I agree that it seems to be a defect in the old standard.

This revision now requires changes to proceed.Mar 10 2021, 12:34 AM
libcxx/include/cstdalign
24 ↗(On Diff #328561)

Yes, it looks like adding a stdalign.h for libc++ is needed to reliably get __alignas_is_defined and __alignof_is_defined.

sbc100 added inline comments.Mar 10 2021, 4:26 PM
libcxx/include/cstdalign
24 ↗(On Diff #328561)

Alternatively, could we just defined them if they are not already and skip including stdalign.h here?

libcxx/include/cstdalign
24 ↗(On Diff #328561)

stdalign.h is a header defined in C++17 like stdbool.h is one. libc++ has a stdbool.h, so I think having a stdalign.h makes sense (since we know that stdalign.h in some environments does not do what we want).

ldionne requested changes to this revision.Mar 30 2021, 2:21 PM

Trying to summarize the discussion here for the author:

  1. Please add a <stdalign.h> header to libc++ that does #include_next <stdalign.h> and then defines both __alignof_is_defined and __alignas_is_defined.
  2. Please re-add a test checking for __alignof_is_defined, but guard it with #if defined(_LIBCPP_VER) so that we only test it on libc++ (since technically the Standard doesn't require it, even if it's a defect).
  3. Please re-run libcxx/utils/generate_header_tests.py and make sure that creates no diff.
libcxx/include/cstdalign
24 ↗(On Diff #328561)

However, http://eel.is/c++draft/depr.stdalign.h.syn#1 says:

The contents of the C++ header <stdalign.h> are the same as the C standard library header <stdalign.h>, with the following changes: The header <stdalign.h> does not define a macro named alignas.

I think the most efficient way of ensuring that is to use #include_next, which requires a C++ friendly <stdalign.h> on the platform.

Generally speaking, I'm not in favour of trying to work around platforms that do not provide C++ friendly headers. If we're trying to make C++ work on those platforms, they need to minimally provide C++ friendly headers.