Skip to content

[libc][stdio] Use proxy headers of stdio.h in src and test folders. #110067

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 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ add_proxy_header_library(
libc.include.signal
)

add_header_library(stdio_overlay HDRS stdio_overlay.h)

add_proxy_header_library(
stdio_macros
HDRS
stdio_macros.h
DEPENDS
.stdio_overlay
FULL_BUILD_DEPENDS
libc.include.stdio
libc.include.llvm-libc-macros.stdio_macros
Expand Down
2 changes: 1 addition & 1 deletion libc/hdr/stdio_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#else // Overlay mode

#include <stdio.h>
#include "stdio_overlay.h"

#endif // LLVM_LIBC_FULL_BUILD

Expand Down
47 changes: 47 additions & 0 deletions libc/hdr/stdio_overlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===-- Including stdio.h in overlay mode ---------------------------------===//
//
// 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_STDIO_OVERLAY_H
#define LLVM_LIBC_HDR_STDIO_OVERLAY_H

#ifdef LIBC_FULL_BUILD
#error "This header should only be included in overlay mode"
#endif

// Overlay mode

// glibc <stdio.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 <stdio.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 <stdio.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_HDR_STDIO_OVERLAY_H
6 changes: 6 additions & 0 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ add_proxy_header_library(
FILE
HDRS
FILE.h
DEPENDS
libc.hdr.stdio_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.FILE
libc.include.stdio
Expand All @@ -149,6 +151,8 @@ add_proxy_header_library(
off_t
HDRS
off_t.h
DEPENDS
libc.hdr.stdio_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.off_t
libc.include.stdio
Expand All @@ -158,6 +162,8 @@ add_proxy_header_library(
cookie_io_functions_t
HDRS
cookie_io_functions_t.h
DEPENDS
libc.hdr.stdio_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.cookie_io_functions_t
libc.include.stdio
Expand Down
2 changes: 1 addition & 1 deletion libc/hdr/types/FILE.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#else // Overlay mode

#include <stdio.h>
#include "hdr/stdio_overlay.h"

#endif // LLVM_LIBC_FULL_BUILD

Expand Down
2 changes: 1 addition & 1 deletion libc/hdr/types/cookie_io_functions_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#else // Overlay mode

#include <stdio.h>
#include "hdr/stdio_overlay.h"

#endif // LLVM_LIBC_FULL_BUILD

Expand Down
2 changes: 1 addition & 1 deletion libc/hdr/types/off_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#else // Overlay mode

#include <stdio.h>
#include "hdr/stdio_overlay.h"

#endif // LLVM_LIBC_FULL_BUILD

Expand Down
36 changes: 36 additions & 0 deletions libc/include/llvm-libc-macros/stdio-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@
#ifndef LLVM_LIBC_MACROS_STDIO_MACROS_H
#define LLVM_LIBC_MACROS_STDIO_MACROS_H

#include "../llvm-libc-types/FILE.h"

#ifdef __cplusplus
extern "C" FILE *stdin;
extern "C" FILE *stdout;
extern "C" FILE *stderr;
#else
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
#endif
Comment on lines +14 to +22
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be in FILE.h?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

C99 (and C++) define these as macros https://en.cppreference.com/w/c/io/std_streams, and so I put their definition here together with the macros. Probably we could factor them out to stdio-streams.h instead. WDYT?


#ifndef stdin
#define stdin stdin
#endif

#ifndef stdout
#define stdout stdout
#endif

#ifndef stderr
#define stderr stderr
#endif

#ifndef EOF
#define EOF (-1)
#endif
Expand All @@ -19,4 +43,16 @@
#define _IOLBF 1
#define _IOFBF 0

#ifndef SEEK_SET
#define SEEK_SET 0
#endif

#ifndef SEEK_CUR
#define SEEK_CUR 1
#endif

#ifndef SEEK_END
#define SEEK_END 2
#endif

#endif // LLVM_LIBC_MACROS_STDIO_MACROS_H
1 change: 0 additions & 1 deletion libc/src/__support/File/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ add_object_library(
lseekImpl.h
DEPENDS
libc.include.fcntl
libc.include.stdio
libc.include.sys_syscall
libc.include.sys_stat
libc.src.__support.CPP.new
Expand Down
2 changes: 0 additions & 2 deletions libc/src/stdio/asprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#define LLVM_LIBC_SRC_STDIO_ASPRINTF_H

#include "src/__support/macros/config.h"
#include <stdarg.h>
#include <stdio.h>

namespace LIBC_NAMESPACE {

Expand Down
66 changes: 31 additions & 35 deletions libc/src/stdio/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
add_entrypoint_object(
stdin
SRCS
stdin.cpp
HDRS
../stdin.h
DEPENDS
libc.hdr.types.FILE
)

add_entrypoint_object(
stdout
SRCS
stdout.cpp
HDRS
../stdout.h
DEPENDS
libc.hdr.types.FILE
)

add_entrypoint_object(
stderr
SRCS
stderr.cpp
HDRS
../stderr.h
DEPENDS
libc.hdr.types.FILE
)

add_header_library(
gpu_file
HDRS
file.h
DEPENDS
libc.hdr.types.FILE
libc.hdr.stdio_macros
libc.src.__support.RPC.rpc_client
libc.src.__support.common
.stdin
Expand Down Expand Up @@ -123,7 +154,6 @@ add_entrypoint_object(
../puts.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)

Expand Down Expand Up @@ -168,7 +198,6 @@ add_entrypoint_object(
../putc.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)

Expand All @@ -180,7 +209,6 @@ add_entrypoint_object(
../putchar.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)

Expand All @@ -203,7 +231,6 @@ add_entrypoint_object(
../getc.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)

Expand All @@ -215,7 +242,6 @@ add_entrypoint_object(
../getchar.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)

Expand Down Expand Up @@ -304,33 +330,3 @@ add_entrypoint_object(
libc.hdr.types.FILE
.gpu_file
)

add_entrypoint_object(
stdin
SRCS
stdin.cpp
HDRS
../stdin.h
DEPENDS
libc.hdr.types.FILE
)

add_entrypoint_object(
stdout
SRCS
stdout.cpp
HDRS
../stdout.h
DEPENDS
libc.hdr.types.FILE
)

add_entrypoint_object(
stderr
SRCS
stderr.cpp
HDRS
../stderr.h
DEPENDS
libc.hdr.types.FILE
)
3 changes: 1 addition & 2 deletions libc/src/stdio/gpu/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
#include "src/__support/macros/config.h"
#include "src/string/string_utils.h"

#include "hdr/stdio_macros.h" // For stdin/out/err
#include "hdr/types/FILE.h"

#include <stdio.h> //needed for stdin/out/err

namespace LIBC_NAMESPACE_DECL {
namespace file {

Expand Down
3 changes: 2 additions & 1 deletion libc/src/stdio/gpu/fprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

#include "src/stdio/fprintf.h"

#include "hdr/types/FILE.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/arg_list.h"
#include "src/errno/libc_errno.h"
#include "src/stdio/gpu/vfprintf_utils.h"

#include <stdio.h>
#include <stdarg.h>

namespace LIBC_NAMESPACE {

Expand Down
5 changes: 1 addition & 4 deletions libc/src/stdio/gpu/getchar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
#include "file.h"
#include "src/__support/macros/config.h"

#include "hdr/stdio_macros.h" // for EOF.
#include "hdr/types/FILE.h"

#include <stdio.h> //needed for stdin
#include "hdr/stdio_macros.h" // for EOF and stdin.

namespace LIBC_NAMESPACE_DECL {

Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdio/gpu/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "src/errno/libc_errno.h"
#include "src/stdio/gpu/vfprintf_utils.h"

#include <stdio.h>
#include <stdarg.h>

namespace LIBC_NAMESPACE {

Expand Down
5 changes: 1 addition & 4 deletions libc/src/stdio/gpu/putchar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
#include "file.h"
#include "src/__support/macros/config.h"

#include "hdr/stdio_macros.h" // for EOF.
#include "hdr/types/FILE.h"

#include <stdio.h> //needed for stdout
#include "hdr/stdio_macros.h" // for EOF and stdout.

namespace LIBC_NAMESPACE_DECL {

Expand Down
5 changes: 1 addition & 4 deletions libc/src/stdio/gpu/puts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
#include "src/errno/libc_errno.h"
#include "src/stdio/gpu/file.h"

#include "hdr/stdio_macros.h" // for EOF.
#include "hdr/types/FILE.h"

#include <stdio.h> //needed for stdout
#include "hdr/stdio_macros.h" // for EOF and stdout.

namespace LIBC_NAMESPACE_DECL {

Expand Down
3 changes: 1 addition & 2 deletions libc/src/stdio/gpu/vfprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

#include "src/stdio/vfprintf.h"

#include "hdr/types/FILE.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/arg_list.h"
#include "src/errno/libc_errno.h"
#include "src/stdio/gpu/vfprintf_utils.h"

#include <stdio.h>

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, vfprintf,
Expand Down
Loading
Loading