llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang on Windows should define __STDCPP_THREADS__ to be 1

Open StephanTLavavej opened this issue 9 years ago • 15 comments

Bugzilla Link 33230
Resolution FIXED
Resolved on Aug 10, 2021 12:40
Version 4.0
OS Windows NT
Attachments Self-contained test case
CC @CaseyCarter,@compnerd,@nico,@zygoloid,@rnk,@jwakely

Extended Description

C:\Temp>type meow.cpp /*** N4659 19.8 [cpp.predefined]/2: "The following macro names are conditionally defined by the implementation: [...] STDCPP_THREADS Defined, and has the value integer literal 1, if and only if a program can have more than one thread of execution (4.7)." ***/

#ifdef STDCPP_THREADS #if STDCPP_THREADS == 1 #include <stdio.h> int main() { puts("Success!"); } #else #error WOOF #endif #else #error BOOM #endif

C:\Temp>cl Microsoft (R) C/C++ Optimizing Compiler Version 19.11.25326 for x64 Copyright (C) Microsoft Corporation. All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

C:\Temp>cl /EHsc /nologo /W4 meow.cpp && meow meow.cpp Success!

C:\Temp>clang-cl -v clang version 4.0.0 (tags/RELEASE_400/final) Target: x86_64-pc-windows-msvc Thread model: posix InstalledDir: S:\WCFB01\vctools\NonShip\ClangLLVM\bin

C:\Temp>clang-cl /EHsc /nologo /W4 meow.cpp && meow meow.cpp(17,6): error: BOOM #error BOOM ^ 1 error generated.

C:\Temp>clang-cl /EHsc /nologo /W4 /std:c++latest meow.cpp && meow meow.cpp(17,6): error: BOOM #error BOOM ^ 1 error generated.

StephanTLavavej avatar May 30 '17 20:05 StephanTLavavej

Fixed by https://reviews.llvm.org/rG15a3ae1ab1a64cc62041c32ba54914a9dd7b8361.

ZequanWu avatar Aug 10 '21 19:08 ZequanWu

Yes, I'll take this one.

ZequanWu avatar Nov 17 '20 19:11 ZequanWu

It sounds like we should go ahead and do this. The compiler should define STDCPP_THREADS to 1 unless "-mthread-model single" is passed.

Zequan, can you take this? I think it would naturally go here: https://github.com/llvm/llvm-project/blob/master/clang/lib/Frontend/InitPreprocessor.cpp#L405 Please add Richard Smith and Eric Fiselier as code reviewers.

rnk avatar Nov 17 '20 18:11 rnk

N.B. defining the macro in the library but not the compiler fails to meet this requirement in [cpp.predefined] p3:

"The values of the predefined macros (except for FILE and LINE) remain constant throughout the translation unit."

i.e. this fails:

#ifdef STDCPP_THREADS #define PREDEFINED STDCPP_THREADS #endif

#include

#ifdef STDCPP_THREADS

ifndef PREDEFINED

error "STDCPP_THREADS was undefined, but is defined after "

endif

#elif defined PREDEFINED

error "STDCPP_THREADS was defined, but is undefined after (and that's just weird)"

#endif

cl.exe defines STDCPP_THREADS unconditionally; no compiler options (including undocumented ones) affect it. The only thing that affects it is C versus C++ mode. I checked the compiler's source code to verify this (this is one of the few parts I understand fairly well, having implemented feature-test macros there).

StephanTLavavej avatar Jul 31 '19 01:07 StephanTLavavej

STL, does cl.exe have a way to ask it to not define STDCPP_THREADS? Or does it always define it unconditionally?

nico avatar Jul 30 '19 14:07 nico

r367316 made libc++ set the define to 1, and only if it's not already set.

It should now be possible to define this in clang.

nico avatar Jul 30 '19 14:07 nico

Eric & Richard, I looked at the standard, and I saw this text:

STDCPP_THREADS Defined, and has the value integer literal 1, if and only if a program can have more than one thread of execution (1.10).

I think what libc++ does today is slightly incorrect, this macro should be 1, not __cplusplus. There are other defines in [cpp.predefined] that we seem to miss.

rnk avatar Jul 16 '19 23:07 rnk

Eric, should we change libc++ to take the definition of STDCPP_THREADS provided by the compiler if its set?

I think so.

So, standard aside, if we define the macro in the compiler, we will have compatibility issues with libc++ if we do not define STDCPP_THREADS to exactly __cplusplus, which isn't 1.

Hmm, this is problematic. @​Reid I still think libc++ should be changed to define it as 1, but this problem still somewhat persists. Thankfully libc++ is going to be treated as a system header, so the redefinition should be handled silently instead of as an error.

llvmbot avatar Jun 20 '17 01:06 llvmbot

Actually, I was just looking at the libc++ header synopsis comments, and saw this:

/*

thread synopsis

#define STDCPP_THREADS __cplusplus

Later on, it defines the macro for real:

#define STDCPP_THREADS __cplusplus

#ifdef _LIBCPP_HAS_NO_THREADS #error is not supported on this single threaded system #else // !_LIBCPP_HAS_NO_THREADS

So, standard aside, if we define the macro in the compiler, we will have compatibility issues with libc++ if we do not define STDCPP_THREADS to exactly __cplusplus, which isn't 1.

Eric, should we change libc++ to take the definition of STDCPP_THREADS provided by the compiler if its set?

rnk avatar Jun 19 '17 15:06 rnk

Programmer-archaeology - I just looked at N3291 (the C++11-equivalent WP) and in that WP, the macro moved from [thread.threads] (the synopsis there) to [cpp.predefined]. I suspect you were looking at a pre-C++11 WP, but in C++11/14/17, the compiler is responsible for defining this macro.

StephanTLavavej avatar Jun 01 '17 21:06 StephanTLavavej

Can you comment on why this macro is defined by the VC compiler instead of the STL?

Because the Standard says so. [cpp.predefined] is Core, not Library.

It's part of the header synopsis.

I'm not sure what you're looking at, but that isn't the case in N4659 as far as I can tell. The macro is mentioned only in [cpp.predefined] and the index, not in the synopsis. (I double-checked [thread.syn] to be sure.)

Is there some flag that disables threading?

Not in MSVC.

Actually, this would cause problems with libc++ on Windows, correct?

I'm not sure whether libc++ is attempting to define this macro (but libc++'s test brought this to my attention).

StephanTLavavej avatar Jun 01 '17 21:06 StephanTLavavej

Actually, this would cause problems with libc++ on Windows, correct? It's nice if clang can behave the same regardless of whether libc++ or the msvc STL is being used.

rnk avatar Jun 01 '17 17:06 rnk

I'll do this soon. Can you comment on why this macro is defined by the VC compiler instead of the STL? It's part of the header synopsis. Is there some flag that disables threading?

rnk avatar Jun 01 '17 15:06 rnk

assigned to @ZequanWu

StephanTLavavej avatar May 30 '17 20:05 StephanTLavavej