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

https://github.com/llvm/llvm-project/issues/60481
This commit is contained in:
lntue 2024-10-01 11:48:07 -04:00 committed by GitHub
parent d071fdab44
commit c63112a911
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
45 changed files with 154 additions and 106 deletions

View File

@ -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

View File

@ -16,7 +16,7 @@
#else // Overlay mode
#include <stdio.h>
#include "stdio_overlay.h"
#endif // LLVM_LIBC_FULL_BUILD

47
libc/hdr/stdio_overlay.h Normal file
View File

@ -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

View File

@ -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
@ -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
@ -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

View File

@ -15,7 +15,7 @@
#else // Overlay mode
#include <stdio.h>
#include "hdr/stdio_overlay.h"
#endif // LLVM_LIBC_FULL_BUILD

View File

@ -15,7 +15,7 @@
#else // Overlay mode
#include <stdio.h>
#include "hdr/stdio_overlay.h"
#endif // LLVM_LIBC_FULL_BUILD

View File

@ -15,7 +15,7 @@
#else // Overlay mode
#include <stdio.h>
#include "hdr/stdio_overlay.h"
#endif // LLVM_LIBC_FULL_BUILD

View File

@ -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
#ifndef stdin
#define stdin stdin
#endif
#ifndef stdout
#define stdout stdout
#endif
#ifndef stderr
#define stderr stderr
#endif
#ifndef EOF
#define EOF (-1)
#endif
@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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
@ -123,7 +154,6 @@ add_entrypoint_object(
../puts.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)
@ -168,7 +198,6 @@ add_entrypoint_object(
../putc.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)
@ -180,7 +209,6 @@ add_entrypoint_object(
../putchar.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)
@ -203,7 +231,6 @@ add_entrypoint_object(
../getc.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)
@ -215,7 +242,6 @@ add_entrypoint_object(
../getchar.h
DEPENDS
libc.hdr.types.FILE
libc.include.stdio # needed for stdin
.gpu_file
)
@ -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
)

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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,

View File

@ -6,13 +6,12 @@
//
//===----------------------------------------------------------------------===//
#include "hdr/types/FILE.h"
#include "src/__support/RPC/rpc_client.h"
#include "src/__support/arg_list.h"
#include "src/stdio/gpu/file.h"
#include "src/string/string_utils.h"
#include <stdio.h>
namespace LIBC_NAMESPACE {
template <uint16_t opcode>

View File

@ -13,8 +13,6 @@
#include "src/errno/libc_errno.h"
#include "src/stdio/gpu/vfprintf_utils.h"
#include <stdio.h>
namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(int, vprintf,

View File

@ -6,7 +6,6 @@ add_entrypoint_object(
../remove.h
DEPENDS
libc.include.fcntl
libc.include.stdio
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@ -32,7 +31,6 @@ add_entrypoint_object(
HDRS
../fdopen.h
DEPENDS
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

View File

@ -141,7 +141,6 @@ add_header_library(
HDRS
vfprintf_internal.h
DEPENDS
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.arg_list
libc.src.stdio.printf_core.printf_main

View File

@ -8,13 +8,13 @@
#include "src/stdio/vsscanf.h"
#include "hdr/stdio_macros.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/arg_list.h"
#include "src/stdio/scanf_core/reader.h"
#include "src/stdio/scanf_core/scanf_main.h"
#include <stdarg.h>
#include <stdio.h>
namespace LIBC_NAMESPACE_DECL {

View File

@ -12,7 +12,6 @@
#include "test/UnitTest/MemoryMatcher.h"
#include "test/UnitTest/Test.h"
#include <stdio.h>
#include <stdlib.h>
using ModeFlags = LIBC_NAMESPACE::File::ModeFlags;

View File

@ -9,7 +9,7 @@
#include "src/__support/File/file.h"
#include "test/UnitTest/Test.h"
#include <stdio.h> // For SEEK_* macros
#include "hdr/stdio_macros.h" // For SEEK_* macros
using File = LIBC_NAMESPACE::File;
constexpr char TEXT[] = "Hello, File";

View File

@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "hdr/fcntl_macros.h"
#include "hdr/stdio_macros.h"
#include "hdr/types/struct_flock.h"
#include "src/errno/libc_errno.h"
#include "src/fcntl/fcntl.h"
@ -16,7 +17,6 @@
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include <stdio.h>
#include <sys/stat.h> // For S_IRWXU
TEST(LlvmLibcFcntlTest, FcntlDupfd) {

View File

@ -17,7 +17,6 @@
#include "hdr/fenv_macros.h"
#include "hdr/math_macros.h"
#include <stdio.h>
static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
FE_TONEAREST};

View File

@ -16,8 +16,8 @@
#include "src/stdio/getc.h"
#include "test/UnitTest/Test.h"
#include "hdr/stdio_macros.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test {
public:

View File

@ -19,8 +19,8 @@
#include "src/stdio/getc_unlocked.h"
#include "test/UnitTest/Test.h"
#include "hdr/stdio_macros.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test {
public:

View File

@ -15,7 +15,6 @@
#include "test/UnitTest/Test.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
TEST(LlvmLibcFgetsTest, WriteAndReadCharacters) {
constexpr char FILENAME[] = "testdata/fgets.test";

View File

@ -20,8 +20,8 @@
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include "hdr/stdio_macros.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::EQ;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::NE;

View File

@ -18,8 +18,8 @@
#include "test/UnitTest/MemoryMatcher.h"
#include "test/UnitTest/Test.h"
#include "hdr/stdio_macros.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
#include <stdlib.h>
using MemoryView = LIBC_NAMESPACE::testing::MemoryView;

View File

@ -17,8 +17,6 @@
#include "test/UnitTest/Test.h"
#include <stdio.h>
namespace printf_test {
#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
using LIBC_NAMESPACE::fclose;

View File

@ -19,8 +19,6 @@
#include "test/UnitTest/Test.h"
#include <stdio.h>
namespace scanf_test {
#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
using LIBC_NAMESPACE::fclose;

View File

@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
#include "hdr/stdio_macros.h"
#include "src/stdio/fclose.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
@ -17,8 +18,6 @@
#include "src/stdio/setvbuf.h"
#include "test/UnitTest/Test.h"
#include <stdio.h>
class LlvmLibcFTellTest : public LIBC_NAMESPACE::testing::Test {
protected:
void test_with_bufmode(int bufmode) {

View File

@ -15,8 +15,6 @@
#include "test/UnitTest/Test.h"
#include <stdio.h>
TEST(LlvmLibcPutcTest, WriteToFile) {
constexpr char FILENAME[] = "testdata/putc_output.test";
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");

View File

@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
#include "hdr/stdio_macros.h"
#include "src/stdio/fclose.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
@ -14,8 +15,6 @@
#include "src/stdio/ungetc.h"
#include "test/UnitTest/Test.h"
#include <stdio.h>
TEST(LlvmLibcSetbufTest, DefaultBufsize) {
// The idea in this test is to change the buffer after opening a file and
// ensure that read and write work as expected.

View File

@ -13,8 +13,8 @@
#include "src/stdio/setvbuf.h"
#include "test/UnitTest/Test.h"
#include "hdr/stdio_macros.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
TEST(LlvmLibcSetvbufTest, SetNBFBuffer) {
// The idea in this test is that we open a file for writing and reading, and

View File

@ -6,13 +6,11 @@
//
//===----------------------------------------------------------------------===//
#include "src/__support/CPP/limits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/stdio/sscanf.h"
#include <stdio.h> // For EOF
#include "hdr/stdio_macros.h" // For EOF
#include "src/__support/CPP/limits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

View File

@ -6,16 +6,16 @@
//
//===----------------------------------------------------------------------===//
#include "src/stdio/ungetc.h"
#include "hdr/stdio_macros.h"
#include "src/stdio/fclose.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
#include "src/stdio/fseek.h"
#include "src/stdio/fwrite.h"
#include "src/stdio/ungetc.h"
#include "test/UnitTest/Test.h"
#include <stdio.h>
TEST(LlvmLibcUngetcTest, UngetAndReadBack) {
constexpr char FILENAME[] = "testdata/ungetc_test.test";
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");

View File

@ -18,7 +18,6 @@
#include "test/UnitTest/Test.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
TEST(LlvmLibcFILETest, UnlockedReadAndWrite) {
constexpr char fNAME[] = "testdata/unlocked_read_and_write.test";

View File

@ -21,8 +21,6 @@
#include "test/UnitTest/Test.h"
#include <stdio.h>
namespace printf_test {
#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
using LIBC_NAMESPACE::fclose;

View File

@ -19,8 +19,6 @@
#include "test/UnitTest/Test.h"
#include <stdio.h>
namespace scanf_test {
#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
using LIBC_NAMESPACE::fclose;

View File

@ -13,8 +13,6 @@
#include "src/stdio/fflush.h"
#include "src/stdio/fopencookie.h"
#include <stdio.h>
using LIBC_NAMESPACE::cpp::array;
namespace test_globals {

View File

@ -6,10 +6,8 @@
//
//===----------------------------------------------------------------------===//
#include <stdio.h> //for EOF
#include "hdr/stdio_macros.h" //for EOF
#include "src/wchar/wctob.h"
#include "test/UnitTest/Test.h"
TEST(LlvmLibcWctob, DefaultLocale) {