Skip to content
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

[libc][wchar] Move wchar's types to proxy headers. #109334

Merged
merged 2 commits into from
Sep 20, 2024
Merged

Conversation

lntue
Copy link
Contributor

@lntue lntue commented Sep 19, 2024

Also protect against extern inline function definitions added when building with gcc: #60481.

@llvmbot
Copy link
Member

llvmbot commented Sep 19, 2024

@llvm/pr-subscribers-libc

Author: None (lntue)

Changes

Also protect against extern inline function definitions added when building with gcc: #60481.


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

15 Files Affected:

  • (modified) libc/hdr/CMakeLists.txt (+9)
  • (modified) libc/hdr/types/CMakeLists.txt (+18)
  • (added) libc/hdr/types/wchar_t.h (+50)
  • (added) libc/hdr/types/wint_t.h (+50)
  • (added) libc/hdr/wchar_macros.h (+49)
  • (modified) libc/include/llvm-libc-types/wchar_t.h (+1-6)
  • (modified) libc/include/llvm-libc-types/wint_t.h (+1-6)
  • (modified) libc/src/__support/CMakeLists.txt (+2)
  • (modified) libc/src/__support/wctype_utils.h (+1-4)
  • (modified) libc/src/wchar/CMakeLists.txt (+1-2)
  • (modified) libc/src/wchar/btowc.cpp (+1)
  • (modified) libc/src/wchar/btowc.h (+1-1)
  • (modified) libc/src/wchar/wctob.cpp (+1)
  • (modified) libc/src/wchar/wctob.h (+1-1)
  • (modified) libc/test/src/wchar/btowc_test.cpp (+1-3)
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index e0b65b7c2eb02d..67c13ec4e19c27 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -161,4 +161,13 @@ add_proxy_header_library(
     libc.include.sys_auxv
 )
 
+add_proxy_header_library(
+  wchar_macros
+  HDRS
+    wchar_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-macros.wchar_macros
+    libc.include.wchar
+)
+
 add_subdirectory(types)
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 12641c4d93ffe8..4db60f2d470fee 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -199,3 +199,21 @@ add_proxy_header_library(
     libc.include.setjmp
 )
 
+add_proxy_header_library(
+  wchar_t
+  HDRS
+    wchar_t.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.wchar_t
+    libc.include.wchar
+)
+
+add_proxy_header_library(
+  wint_t
+  HDRS
+    wint_t.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.wint_t
+    libc.include.wchar
+)
+
diff --git a/libc/hdr/types/wchar_t.h b/libc/hdr/types/wchar_t.h
new file mode 100644
index 00000000000000..dda612afbcce34
--- /dev/null
+++ b/libc/hdr/types/wchar_t.h
@@ -0,0 +1,50 @@
+//===-- Definition of wchar_t.h -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_WCHAR_T_H
+#define LLVM_LIBC_HDR_TYPES_WCHAR_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/wchar_t.h"
+
+#else // overlay mode
+
+// glibc <wchar.h header might provide extern inline definitions for few
+// functions, causing external alias errors.  They are guarded by
+// `__USE_EXTERN_INLINES` macro.  We temporarily disable `__USE_EXTERN_INLINES`
+// macro by defining `__NO_INLINE__` before including <wchar.h>.
+// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
+// with `_FORTIFY_SOURCE`.
+
+#ifdef _FORTIFY_SOURCE
+#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
+#undef _FORTIFY_SOURCE
+#endif
+
+#ifndef __NO_INLINE__
+#define __NO_INLINE__ 1
+#define LIBC_SET_NO_INLINE
+#endif
+
+#include <wchar.h>
+
+#ifdef LIBC_OLD_FORTIFY_SOURCE
+#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
+#undef LIBC_OLD_FORTIFY_SOURCE
+#endif
+
+#ifdef LIBC_SET_NO_INLINE
+#undef __NO_INLINE__
+#undef LIBC_SET_NO_INLINE
+#endif
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_WCHAR_T_H
diff --git a/libc/hdr/types/wint_t.h b/libc/hdr/types/wint_t.h
new file mode 100644
index 00000000000000..4a133d82207da0
--- /dev/null
+++ b/libc/hdr/types/wint_t.h
@@ -0,0 +1,50 @@
+//===-- Definition of wint_t.h --------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_WINT_T_H
+#define LLVM_LIBC_HDR_TYPES_WINT_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/wint_t.h"
+
+#else // overlay mode
+
+// glibc <wchar.h header might provide extern inline definitions for few
+// functions, causing external alias errors.  They are guarded by
+// `__USE_EXTERN_INLINES` macro.  We temporarily disable `__USE_EXTERN_INLINES`
+// macro by defining `__NO_INLINE__` before including <wchar.h>.
+// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
+// with `_FORTIFY_SOURCE`.
+
+#ifdef _FORTIFY_SOURCE
+#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
+#undef _FORTIFY_SOURCE
+#endif
+
+#ifndef __NO_INLINE__
+#define __NO_INLINE__ 1
+#define LIBC_SET_NO_INLINE
+#endif
+
+#include <wchar.h>
+
+#ifdef LIBC_OLD_FORTIFY_SOURCE
+#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
+#undef LIBC_OLD_FORTIFY_SOURCE
+#endif
+
+#ifdef LIBC_SET_NO_INLINE
+#undef __NO_INLINE__
+#undef LIBC_SET_NO_INLINE
+#endif
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_WINT_T_H
diff --git a/libc/hdr/wchar_macros.h b/libc/hdr/wchar_macros.h
new file mode 100644
index 00000000000000..b471f6d31e4cce
--- /dev/null
+++ b/libc/hdr/wchar_macros.h
@@ -0,0 +1,49 @@
+//===-- Definition of macros from wchar.h ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_WCHAR_MACROS_H
+#define LLVM_LIBC_HDR_WCHAR_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/wchar-macros.h"
+
+#else // Overlay mode
+
+// glibc <wchar.h header might provide extern inline definitions for few
+// functions, causing external alias errors.  They are guarded by
+// `__USE_EXTERN_INLINES` macro.  We temporarily disable `__USE_EXTERN_INLINES`
+// macro by defining `__NO_INLINE__` before including <wchar.h>.
+// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
+// with `_FORTIFY_SOURCE`.
+
+#ifdef _FORTIFY_SOURCE
+#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
+#undef _FORTIFY_SOURCE
+#endif
+
+#ifndef __NO_INLINE__
+#define __NO_INLINE__ 1
+#define LIBC_SET_NO_INLINE
+#endif
+
+#include <wchar.h>
+
+#ifdef LIBC_OLD_FORTIFY_SOURCE
+#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
+#undef LIBC_OLD_FORTIFY_SOURCE
+#endif
+
+#ifdef LIBC_SET_NO_INLINE
+#undef __NO_INLINE__
+#undef LIBC_SET_NO_INLINE
+#endif
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_WCHAR_MACROS_H
diff --git a/libc/include/llvm-libc-types/wchar_t.h b/libc/include/llvm-libc-types/wchar_t.h
index 3e9a70b8afe6a6..faa40d96efc4d2 100644
--- a/libc/include/llvm-libc-types/wchar_t.h
+++ b/libc/include/llvm-libc-types/wchar_t.h
@@ -9,11 +9,6 @@
 #ifndef LLVM_LIBC_TYPES_WCHAR_T_H
 #define LLVM_LIBC_TYPES_WCHAR_T_H
 
-// Since __need_wchar_t is defined, we get the definition of wchar_t from the
-// standalone C header stddef.h. Also, because __need_wchar_t is defined,
-// including stddef.h will pull only the type wchar_t and nothing else.
-#define __need_wchar_t
-#include <stddef.h>
-#undef __need_wchar_t
+typedef __WCHAR_TYPE__ wchar_t;
 
 #endif // LLVM_LIBC_TYPES_WCHAR_T_H
diff --git a/libc/include/llvm-libc-types/wint_t.h b/libc/include/llvm-libc-types/wint_t.h
index 2758685a084575..a53c6e3cb622c9 100644
--- a/libc/include/llvm-libc-types/wint_t.h
+++ b/libc/include/llvm-libc-types/wint_t.h
@@ -9,11 +9,6 @@
 #ifndef LLVM_LIBC_TYPES_WINT_T_H
 #define LLVM_LIBC_TYPES_WINT_T_H
 
-// Since __need_wint_t is defined, we get the definition of wint_t from the
-// standalone C header stddef.h. Also, because __need_wint_t is defined,
-// including stddef.h will pull only the type wint_t and nothing else.
-#define __need_wint_t
-#include <stddef.h>
-#undef __need_wint_t
+typedef __WINT_TYPE__ wint_t;
 
 #endif // LLVM_LIBC_TYPES_WINT_T_H
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 0302ad64f8b5df..4785895b562b5e 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -117,6 +117,8 @@ add_header_library(
   wctype_utils
   HDRS
     wctype_utils.h
+  DEPENDS
+    libc.hdr.types.wint_t
 )
 
 add_header_library(
diff --git a/libc/src/__support/wctype_utils.h b/libc/src/__support/wctype_utils.h
index 469d81250b535a..aa137c278323e3 100644
--- a/libc/src/__support/wctype_utils.h
+++ b/libc/src/__support/wctype_utils.h
@@ -9,14 +9,11 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
 #define LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
 
+#include "hdr/types/wint_t.h"
 #include "src/__support/CPP/optional.h"
 #include "src/__support/macros/attributes.h" // LIBC_INLINE
 #include "src/__support/macros/config.h"
 
-#define __need_wint_t
-#define __need_wchar_t
-#include <stddef.h> // needed for wint_t and wchar_t
-
 namespace LIBC_NAMESPACE_DECL {
 namespace internal {
 
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 1d62399c1fe9d7..c89b05e80e5d82 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -6,7 +6,6 @@ add_entrypoint_object(
   HDRS
     wctob.h
   DEPENDS
-    libc.include.stdio
-    libc.include.wchar
+    libc.hdr.types.wint_t
     libc.src.__support.wctype_utils
 )
diff --git a/libc/src/wchar/btowc.cpp b/libc/src/wchar/btowc.cpp
index 6e67f1ba02975c..2129ba557603fc 100644
--- a/libc/src/wchar/btowc.cpp
+++ b/libc/src/wchar/btowc.cpp
@@ -12,6 +12,7 @@
 #include "src/__support/wctype_utils.h"
 
 #include "hdr/stdio_macros.h" // for EOF.
+#include "hdr/types/wint_t.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/wchar/btowc.h b/libc/src/wchar/btowc.h
index 1f41bd68f9c175..ef70ee26cc197c 100644
--- a/libc/src/wchar/btowc.h
+++ b/libc/src/wchar/btowc.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_WCHAR_BTOWC_H
 #define LLVM_LIBC_SRC_WCHAR_BTOWC_H
 
+#include "hdr/types/wint_t.h"
 #include "src/__support/macros/config.h"
-#include <wchar.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/wchar/wctob.cpp b/libc/src/wchar/wctob.cpp
index 33f67feb90b8d5..45240d6052eb4e 100644
--- a/libc/src/wchar/wctob.cpp
+++ b/libc/src/wchar/wctob.cpp
@@ -12,6 +12,7 @@
 #include "src/__support/wctype_utils.h"
 
 #include "hdr/stdio_macros.h" // for EOF.
+#include "hdr/types/wint_t.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/wchar/wctob.h b/libc/src/wchar/wctob.h
index 95f349ed8e4485..8ae89fa3aa6596 100644
--- a/libc/src/wchar/wctob.h
+++ b/libc/src/wchar/wctob.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_WCHAR_WCTOB_H
 #define LLVM_LIBC_SRC_WCHAR_WCTOB_H
 
+#include "hdr/types/wint_t.h"
 #include "src/__support/macros/config.h"
-#include <wchar.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/test/src/wchar/btowc_test.cpp b/libc/test/src/wchar/btowc_test.cpp
index b5fcb451bd07b0..8479d541a20ee9 100644
--- a/libc/test/src/wchar/btowc_test.cpp
+++ b/libc/test/src/wchar/btowc_test.cpp
@@ -6,10 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <wchar.h> //for WEOF
-
+#include "hdr/wchar_macros.h" // for WEOF
 #include "src/wchar/btowc.h"
-
 #include "test/UnitTest/Test.h"
 
 TEST(LlvmLibcBtowc, DefaultLocale) {


#else // overlay mode

// glibc <wchar.h header might provide extern inline definitions for few
Copy link
Contributor

Choose a reason for hiding this comment

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

since this is the same in all of the proxy headers for wchar.h would it be best to create a proxy header for wchar.h overall that can have the fortify handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's actually what I was thinking after posting the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

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

LGTM

@lntue lntue merged commit 95d4c97 into llvm:main Sep 20, 2024
7 checks passed
@lntue lntue deleted the wchar branch September 20, 2024 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants