Skip to content

Conversation

ac-bcc
Copy link

@ac-bcc ac-bcc commented Sep 30, 2025

When building Glibc with Clang, several conformance and math tests related to <tgmath.h> fail. This is because Clang's built-in <tgmath.h> is used by default, which is not fully compatible with the expectations of the Glibc test suite.

Glibc provides its own <tgmath.h> which is specifically designed to work with its library implementation, supporting all necessary GNU extensions and features.

This patch modifies Clang's <tgmath.h> to detect when it is being used in a Glibc environment (by checking for the __GLIBC__ macro). When __GLIBC__ is defined, Clang's header will use include_next to include Glibc's system header, effectively deferring to Glibc's implementation. This is the same mechanism already used for macOS (__APPLE__).

This change allows Glibc to be successfully built and tested with Clang, resolving the following test failures:

  • conform/ISO11/tgmath.h/conform
  • conform/ISO99/tgmath.h/conform
  • conform/POSIX2008/tgmath.h/conform
  • conform/XOPEN2K/tgmath.h/conform
  • conform/XOPEN2K8/tgmath.h/conform
  • math/test-tgmath
  • math/test-tgmath2

This approach addresses the compatibility issue at its source within Clang, rather than requiring workarounds in Glibc.

When building Glibc with Clang, several conformance and math tests related to <tgmath.h> fail. This is because Clang's built-in <tgmath.h> is used by default, which is not fully compatible with the expectations of the Glibc test suite.

Glibc provides its own <tgmath.h> which is specifically designed to work with its library implementation, supporting all necessary GNU extensions and features.

This patch modifies Clang's <tgmath.h> to detect when it is being used in a Glibc environment (by checking for the `__GLIBC__` macro). When `__GLIBC__` is defined, Clang's header will use `include_next` to include Glibc's system header, effectively deferring to Glibc's implementation. This is the same mechanism already used for macOS (`__APPLE__`).

This change allows Glibc to be successfully built and tested with Clang, resolving the following test failures:
- conform/ISO11/tgmath.h/conform
- conform/ISO99/tgmath.h/conform
- conform/POSIX2008/tgmath.h/conform
- conform/XOPEN2K/tgmath.h/conform
- conform/XOPEN2K8/tgmath.h/conform
- math/test-tgmath
- math/test-tgmath2

This approach addresses the compatibility issue at its source within Clang, rather than requiring workarounds in Glibc.
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 backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics labels Sep 30, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-backend-x86

Author: Zhao Mao (ac-bcc)

Changes

When building Glibc with Clang, several conformance and math tests related to <tgmath.h> fail. This is because Clang's built-in <tgmath.h> is used by default, which is not fully compatible with the expectations of the Glibc test suite.

Glibc provides its own <tgmath.h> which is specifically designed to work with its library implementation, supporting all necessary GNU extensions and features.

This patch modifies Clang's <tgmath.h> to detect when it is being used in a Glibc environment (by checking for the __GLIBC__ macro). When __GLIBC__ is defined, Clang's header will use include_next to include Glibc's system header, effectively deferring to Glibc's implementation. This is the same mechanism already used for macOS (__APPLE__).

This change allows Glibc to be successfully built and tested with Clang, resolving the following test failures:

  • conform/ISO11/tgmath.h/conform
  • conform/ISO99/tgmath.h/conform
  • conform/POSIX2008/tgmath.h/conform
  • conform/XOPEN2K/tgmath.h/conform
  • conform/XOPEN2K8/tgmath.h/conform
  • math/test-tgmath
  • math/test-tgmath2

This approach addresses the compatibility issue at its source within Clang, rather than requiring workarounds in Glibc.


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

1 Files Affected:

  • (modified) clang/lib/Headers/tgmath.h (+1-1)
diff --git a/clang/lib/Headers/tgmath.h b/clang/lib/Headers/tgmath.h
index 7acf18b9dd357..e79e84b1c72e5 100644
--- a/clang/lib/Headers/tgmath.h
+++ b/clang/lib/Headers/tgmath.h
@@ -17,7 +17,7 @@
  * platforms. This is done after #include <math.h> to avoid depcycle conflicts
  * between libcxx and darwin in C++ modules builds.
  */
-#if defined(__APPLE__) && __STDC_HOSTED__ && __has_include_next(<tgmath.h>)
+#if (defined(__APPLE__) && __STDC_HOSTED__ && __has_include_next(<tgmath.h>)) || defined(__GLIBC__)
 #  include_next <tgmath.h>
 #else
 

Copy link

github-actions bot commented Oct 1, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions h -- clang/lib/Headers/tgmath.h

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/clang/lib/Headers/tgmath.h b/clang/lib/Headers/tgmath.h
index e79e84b1c..594e44d55 100644
--- a/clang/lib/Headers/tgmath.h
+++ b/clang/lib/Headers/tgmath.h
@@ -17,7 +17,8 @@
  * platforms. This is done after #include <math.h> to avoid depcycle conflicts
  * between libcxx and darwin in C++ modules builds.
  */
-#if (defined(__APPLE__) && __STDC_HOSTED__ && __has_include_next(<tgmath.h>)) || defined(__GLIBC__)
+#if (defined(__APPLE__) && __STDC_HOSTED__ &&                                  \
+     __has_include_next(<tgmath.h>)) || defined(__GLIBC__)
 #  include_next <tgmath.h>
 #else
 

Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

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

Is there a reason to not just do #if __has_include_next(<tgmath.h>)?

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

I have no idea on the headers... Aaron is perhaps a better person to review?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants