Skip to content

[clang] __STDC_NO_THREADS__ is no longer necessary for VS 2022 1939 and above #117149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2025

Conversation

dipeshs809
Copy link
Contributor

Since __STDC_NO_THREADS__ is a reserved identifier,

  • If MSVC version < 17.9
  • C version < C11(201112L)
  • When <threads.h> is unavailable !__has_include(<threads.h>) is __has_include is defined.

Closes #115529

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

@llvm/pr-subscribers-clang

Author: Dipesh Sharma (dipeshs809)

Changes

Since __STDC_NO_THREADS__ is a reserved identifier,

  • If MSVC version &lt; 17.9
  • C version < C11(201112L)
  • When &lt;threads.h&gt; is unavailable !__has_include(&lt;threads.h&gt;) is __has_include is defined.

Closes #115529


Full diff: https://github.com/llvm/llvm-project/pull/117149.diff

2 Files Affected:

  • (modified) clang/include/clang/Basic/LangOptions.h (+1)
  • (modified) clang/lib/Basic/Targets/OSTargets.cpp (+13-2)
diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index 949c8f5d448bcf..114a5d34a008bd 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -144,6 +144,7 @@ class LangOptionsBase {
     MSVC2019_5 = 1925,
     MSVC2019_8 = 1928,
     MSVC2022_3 = 1933,
+    MSVC2022_9 = 1939,
   };
 
   enum SYCLMajorVersion {
diff --git a/clang/lib/Basic/Targets/OSTargets.cpp b/clang/lib/Basic/Targets/OSTargets.cpp
index 88c054150ab224..f8d974e9979e4d 100644
--- a/clang/lib/Basic/Targets/OSTargets.cpp
+++ b/clang/lib/Basic/Targets/OSTargets.cpp
@@ -248,8 +248,19 @@ static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
     Builder.defineMacro("_KERNEL_MODE");
 
   Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
-  Builder.defineMacro("__STDC_NO_THREADS__");
-
+  // Define __STDC_NO_THREADS__ based on MSVC version, threads.h availability,
+  // and language standard.
+  if (!Opts.isCompatibleWithMSVC(LangOptions::MSVC2022_9)) {
+    Builder.defineMacro("__STDC_NO_THREADS__");
+  } else {
+#if defined(__has_include) && !__has_include(<threads.h>)
+    Builder.defineMacro("__STDC_NO_THREADS__");
+#endif
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L
+    Builder.defineMacro("__STDC_NO_THREADS__");
+#endif
+  }
+  // Builder.defineMacro("__STDC_NO_THREADS__");
   // Starting with VS 2022 17.1, MSVC predefines the below macro to inform
   // users of the execution character set defined at compile time.
   // The value given is the Windows Code Page Identifier:

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this! Please be sure to add some test coverage for the changes to clang/test/Headers and a release note in clang/docs/ReleaseNotes.rst.

Comment on lines 256 to 272
#if defined(__has_include) && !__has_include(<threads.h>)
Builder.defineMacro("__STDC_NO_THREADS__");
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ < 201112L
Builder.defineMacro("__STDC_NO_THREADS__");
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are looking at the host compiler's behavior when building Clang, not the state of the system on which Clang is running, so they won't do what you expect.

Trying to determine if the system has a threads.h header on the search path is possible, but would be really tricky to do from here because this function doesn't have access to what's needed to do that work.

As for checking for C11, that can be done via !Opts.C11

@dipeshs809
Copy link
Contributor Author

Thank you for working on this! Please be sure to add some test coverage for the changes to clang/test/Headers and a release note in clang/docs/ReleaseNotes.rst.

Thanks for the review. I'll incorporate the changes suggested and come back to you.

@dipeshs809 dipeshs809 force-pushed the issue-115529-stdc-no-thread branch from a8dfbec to c55e4ee Compare January 16, 2025 17:33
@dipeshs809 dipeshs809 force-pushed the issue-115529-stdc-no-thread branch 2 times, most recently from 8d1758e to 76a484f Compare January 24, 2025 10:25
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a few minor nits, but I think we're pretty close!

@dipeshs809 dipeshs809 force-pushed the issue-115529-stdc-no-thread branch from 76a484f to e89b89f Compare January 24, 2025 17:37
@dipeshs809
Copy link
Contributor Author

@AaronBallman Thanks for reviewing the PR, have incorporated the changes.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thank you for the fix!

@AaronBallman AaronBallman merged commit 54928a1 into llvm:main Jan 27, 2025
7 checks passed
Copy link

@dipeshs809 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] __STDC_NO_THREADS__ is no longer necessary for VS 2022 17.8
3 participants