From 513e42cf30aa5739bd3ee8d31b2b2130a7903938 Mon Sep 17 00:00:00 2001 From: Tue Ly Date: Wed, 25 Sep 2024 21:43:53 -0400 Subject: [PATCH 1/3] [libc][stdio] Use proxy headers of stdio.h in src and test folders. --- libc/hdr/CMakeLists.txt | 4 ++ libc/hdr/stdio_macros.h | 2 +- libc/hdr/stdio_overlay.h | 47 +++++++++++++ libc/hdr/types/CMakeLists.txt | 6 ++ libc/hdr/types/FILE.h | 2 +- libc/hdr/types/cookie_io_functions_t.h | 2 +- libc/hdr/types/off_t.h | 2 +- libc/include/llvm-libc-macros/stdio-macros.h | 36 ++++++++++ libc/src/__support/File/linux/CMakeLists.txt | 1 - libc/src/stdio/asprintf.h | 2 - libc/src/stdio/gpu/CMakeLists.txt | 66 +++++++++---------- libc/src/stdio/gpu/file.h | 3 +- libc/src/stdio/gpu/fprintf.cpp | 3 +- libc/src/stdio/gpu/getchar.cpp | 5 +- libc/src/stdio/gpu/printf.cpp | 2 +- libc/src/stdio/gpu/putchar.cpp | 5 +- libc/src/stdio/gpu/puts.cpp | 5 +- libc/src/stdio/gpu/vfprintf.cpp | 3 +- libc/src/stdio/gpu/vfprintf_utils.h | 3 +- libc/src/stdio/gpu/vprintf.cpp | 2 - libc/src/stdio/linux/CMakeLists.txt | 2 - libc/src/stdio/printf_core/CMakeLists.txt | 1 - libc/src/stdio/vsscanf.cpp | 2 +- libc/test/src/__support/File/file_test.cpp | 1 - .../src/__support/File/platform_file_test.cpp | 2 +- libc/test/src/fcntl/fcntl_test.cpp | 2 +- libc/test/src/math/smoke/RIntTest.h | 1 - libc/test/src/stdio/fgetc_test.cpp | 2 +- libc/test/src/stdio/fgetc_unlocked_test.cpp | 2 +- libc/test/src/stdio/fgets_test.cpp | 1 - libc/test/src/stdio/fileop_test.cpp | 2 +- libc/test/src/stdio/fopencookie_test.cpp | 2 +- libc/test/src/stdio/fprintf_test.cpp | 2 - libc/test/src/stdio/fscanf_test.cpp | 2 - libc/test/src/stdio/ftell_test.cpp | 3 +- libc/test/src/stdio/putc_test.cpp | 2 - libc/test/src/stdio/setbuf_test.cpp | 3 +- libc/test/src/stdio/setvbuf_test.cpp | 2 +- libc/test/src/stdio/sprintf_test.cpp | 55 ++++++++++------ libc/test/src/stdio/sscanf_test.cpp | 5 +- libc/test/src/stdio/ungetc_test.cpp | 3 +- libc/test/src/stdio/unlocked_fileop_test.cpp | 1 - libc/test/src/stdio/vfprintf_test.cpp | 2 - libc/test/src/stdio/vfscanf_test.cpp | 2 - libc/test/src/unistd/getopt_test.cpp | 2 - libc/test/src/wchar/wctob_test.cpp | 4 +- 46 files changed, 185 insertions(+), 124 deletions(-) create mode 100644 libc/hdr/stdio_overlay.h diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt index 5e3122f59de9e..13dc892978bb8 100644 --- a/libc/hdr/CMakeLists.txt +++ b/libc/hdr/CMakeLists.txt @@ -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 diff --git a/libc/hdr/stdio_macros.h b/libc/hdr/stdio_macros.h index a212846dd8f41..a4d6a972ec9ac 100644 --- a/libc/hdr/stdio_macros.h +++ b/libc/hdr/stdio_macros.h @@ -16,7 +16,7 @@ #else // Overlay mode -#include +#include "stdio_overlay.h" #endif // LLVM_LIBC_FULL_BUILD diff --git a/libc/hdr/stdio_overlay.h b/libc/hdr/stdio_overlay.h new file mode 100644 index 0000000000000..cec55abfde7bf --- /dev/null +++ b/libc/hdr/stdio_overlay.h @@ -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 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 . +// 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 + +#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 diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt index b4de39621416f..fab5245816bbe 100644 --- a/libc/hdr/types/CMakeLists.txt +++ b/libc/hdr/types/CMakeLists.txt @@ -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 diff --git a/libc/hdr/types/FILE.h b/libc/hdr/types/FILE.h index 60e95f07e37f9..ecb52b7102cb0 100644 --- a/libc/hdr/types/FILE.h +++ b/libc/hdr/types/FILE.h @@ -15,7 +15,7 @@ #else // Overlay mode -#include +#include "hdr/stdio_overlay.h" #endif // LLVM_LIBC_FULL_BUILD diff --git a/libc/hdr/types/cookie_io_functions_t.h b/libc/hdr/types/cookie_io_functions_t.h index d8fe7731a84bd..7323a05001c40 100644 --- a/libc/hdr/types/cookie_io_functions_t.h +++ b/libc/hdr/types/cookie_io_functions_t.h @@ -15,7 +15,7 @@ #else // Overlay mode -#include +#include "hdr/stdio_overlay.h" #endif // LLVM_LIBC_FULL_BUILD diff --git a/libc/hdr/types/off_t.h b/libc/hdr/types/off_t.h index abc3aa659365f..52337e5b63e2d 100644 --- a/libc/hdr/types/off_t.h +++ b/libc/hdr/types/off_t.h @@ -15,7 +15,7 @@ #else // Overlay mode -#include +#include "hdr/stdio_overlay.h" #endif // LLVM_LIBC_FULL_BUILD diff --git a/libc/include/llvm-libc-macros/stdio-macros.h b/libc/include/llvm-libc-macros/stdio-macros.h index 69fb71ad3f651..96f0e6933ade6 100644 --- a/libc/include/llvm-libc-macros/stdio-macros.h +++ b/libc/include/llvm-libc-macros/stdio-macros.h @@ -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 diff --git a/libc/src/__support/File/linux/CMakeLists.txt b/libc/src/__support/File/linux/CMakeLists.txt index 5556b812596f8..5abbf11b3671c 100644 --- a/libc/src/__support/File/linux/CMakeLists.txt +++ b/libc/src/__support/File/linux/CMakeLists.txt @@ -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 diff --git a/libc/src/stdio/asprintf.h b/libc/src/stdio/asprintf.h index fd2b908db171d..0c0d5a350829e 100644 --- a/libc/src/stdio/asprintf.h +++ b/libc/src/stdio/asprintf.h @@ -10,8 +10,6 @@ #define LLVM_LIBC_SRC_STDIO_ASPRINTF_H #include "src/__support/macros/config.h" -#include -#include namespace LIBC_NAMESPACE { diff --git a/libc/src/stdio/gpu/CMakeLists.txt b/libc/src/stdio/gpu/CMakeLists.txt index 9cac42ed71fb7..c4ad333e25163 100644 --- a/libc/src/stdio/gpu/CMakeLists.txt +++ b/libc/src/stdio/gpu/CMakeLists.txt @@ -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 -) diff --git a/libc/src/stdio/gpu/file.h b/libc/src/stdio/gpu/file.h index 5de76842d7bea..0856a3430803a 100644 --- a/libc/src/stdio/gpu/file.h +++ b/libc/src/stdio/gpu/file.h @@ -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 //needed for stdin/out/err - namespace LIBC_NAMESPACE_DECL { namespace file { diff --git a/libc/src/stdio/gpu/fprintf.cpp b/libc/src/stdio/gpu/fprintf.cpp index 42d6ad0087773..6222589cc4bab 100644 --- a/libc/src/stdio/gpu/fprintf.cpp +++ b/libc/src/stdio/gpu/fprintf.cpp @@ -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 +#include namespace LIBC_NAMESPACE { diff --git a/libc/src/stdio/gpu/getchar.cpp b/libc/src/stdio/gpu/getchar.cpp index 048cf23b0d64a..d99b97b5c5a00 100644 --- a/libc/src/stdio/gpu/getchar.cpp +++ b/libc/src/stdio/gpu/getchar.cpp @@ -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 //needed for stdin +#include "hdr/stdio_macros.h" // for EOF and stdin. namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/stdio/gpu/printf.cpp b/libc/src/stdio/gpu/printf.cpp index 63af6fffeea73..d9903193ef165 100644 --- a/libc/src/stdio/gpu/printf.cpp +++ b/libc/src/stdio/gpu/printf.cpp @@ -13,7 +13,7 @@ #include "src/errno/libc_errno.h" #include "src/stdio/gpu/vfprintf_utils.h" -#include +#include namespace LIBC_NAMESPACE { diff --git a/libc/src/stdio/gpu/putchar.cpp b/libc/src/stdio/gpu/putchar.cpp index d03a3fe68daf7..c49b02e2f1f10 100644 --- a/libc/src/stdio/gpu/putchar.cpp +++ b/libc/src/stdio/gpu/putchar.cpp @@ -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 //needed for stdout +#include "hdr/stdio_macros.h" // for EOF and stdout. namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/stdio/gpu/puts.cpp b/libc/src/stdio/gpu/puts.cpp index af84432d1ef8c..20f7a889a378a 100644 --- a/libc/src/stdio/gpu/puts.cpp +++ b/libc/src/stdio/gpu/puts.cpp @@ -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 //needed for stdout +#include "hdr/stdio_macros.h" // for EOF and stdout. namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/stdio/gpu/vfprintf.cpp b/libc/src/stdio/gpu/vfprintf.cpp index f314f6872ad0e..961cfa48579e0 100644 --- a/libc/src/stdio/gpu/vfprintf.cpp +++ b/libc/src/stdio/gpu/vfprintf.cpp @@ -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 - namespace LIBC_NAMESPACE { LLVM_LIBC_FUNCTION(int, vfprintf, diff --git a/libc/src/stdio/gpu/vfprintf_utils.h b/libc/src/stdio/gpu/vfprintf_utils.h index f364646fcea58..7c012d139ba5d 100644 --- a/libc/src/stdio/gpu/vfprintf_utils.h +++ b/libc/src/stdio/gpu/vfprintf_utils.h @@ -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 - namespace LIBC_NAMESPACE { template diff --git a/libc/src/stdio/gpu/vprintf.cpp b/libc/src/stdio/gpu/vprintf.cpp index 1356aceeb51c5..2bb74d7f017b5 100644 --- a/libc/src/stdio/gpu/vprintf.cpp +++ b/libc/src/stdio/gpu/vprintf.cpp @@ -13,8 +13,6 @@ #include "src/errno/libc_errno.h" #include "src/stdio/gpu/vfprintf_utils.h" -#include - namespace LIBC_NAMESPACE { LLVM_LIBC_FUNCTION(int, vprintf, diff --git a/libc/src/stdio/linux/CMakeLists.txt b/libc/src/stdio/linux/CMakeLists.txt index fa36732a159be..d6241e1ca0439 100644 --- a/libc/src/stdio/linux/CMakeLists.txt +++ b/libc/src/stdio/linux/CMakeLists.txt @@ -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 ) diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt index 1095f01d71f24..542327ad5a49a 100644 --- a/libc/src/stdio/printf_core/CMakeLists.txt +++ b/libc/src/stdio/printf_core/CMakeLists.txt @@ -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 diff --git a/libc/src/stdio/vsscanf.cpp b/libc/src/stdio/vsscanf.cpp index fcf0b88885f17..f3f56bce64292 100644 --- a/libc/src/stdio/vsscanf.cpp +++ b/libc/src/stdio/vsscanf.cpp @@ -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 -#include namespace LIBC_NAMESPACE_DECL { diff --git a/libc/test/src/__support/File/file_test.cpp b/libc/test/src/__support/File/file_test.cpp index 2f68c3faa0ad0..5977ea7c8e0b5 100644 --- a/libc/test/src/__support/File/file_test.cpp +++ b/libc/test/src/__support/File/file_test.cpp @@ -12,7 +12,6 @@ #include "test/UnitTest/MemoryMatcher.h" #include "test/UnitTest/Test.h" -#include #include using ModeFlags = LIBC_NAMESPACE::File::ModeFlags; diff --git a/libc/test/src/__support/File/platform_file_test.cpp b/libc/test/src/__support/File/platform_file_test.cpp index 8aa07219a6527..82f17d2ef2e17 100644 --- a/libc/test/src/__support/File/platform_file_test.cpp +++ b/libc/test/src/__support/File/platform_file_test.cpp @@ -9,7 +9,7 @@ #include "src/__support/File/file.h" #include "test/UnitTest/Test.h" -#include // For SEEK_* macros +#include // For SEEK_* macros using File = LIBC_NAMESPACE::File; constexpr char TEXT[] = "Hello, File"; diff --git a/libc/test/src/fcntl/fcntl_test.cpp b/libc/test/src/fcntl/fcntl_test.cpp index ffbb3ec337ed4..1a21afe51085b 100644 --- a/libc/test/src/fcntl/fcntl_test.cpp +++ b/libc/test/src/fcntl/fcntl_test.cpp @@ -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 #include // For S_IRWXU TEST(LlvmLibcFcntlTest, FcntlDupfd) { diff --git a/libc/test/src/math/smoke/RIntTest.h b/libc/test/src/math/smoke/RIntTest.h index 1412c3f27a2d5..fb2c89c4980b4 100644 --- a/libc/test/src/math/smoke/RIntTest.h +++ b/libc/test/src/math/smoke/RIntTest.h @@ -17,7 +17,6 @@ #include "hdr/fenv_macros.h" #include "hdr/math_macros.h" -#include static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO, FE_TONEAREST}; diff --git a/libc/test/src/stdio/fgetc_test.cpp b/libc/test/src/stdio/fgetc_test.cpp index 989bb312afadf..2cc8436bd66f2 100644 --- a/libc/test/src/stdio/fgetc_test.cpp +++ b/libc/test/src/stdio/fgetc_test.cpp @@ -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 class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test { public: diff --git a/libc/test/src/stdio/fgetc_unlocked_test.cpp b/libc/test/src/stdio/fgetc_unlocked_test.cpp index 48d7a043cad7c..46cf12c2c253b 100644 --- a/libc/test/src/stdio/fgetc_unlocked_test.cpp +++ b/libc/test/src/stdio/fgetc_unlocked_test.cpp @@ -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 class LlvmLibcGetcTest : public LIBC_NAMESPACE::testing::Test { public: diff --git a/libc/test/src/stdio/fgets_test.cpp b/libc/test/src/stdio/fgets_test.cpp index d005a71710d21..984acbdd371a9 100644 --- a/libc/test/src/stdio/fgets_test.cpp +++ b/libc/test/src/stdio/fgets_test.cpp @@ -15,7 +15,6 @@ #include "test/UnitTest/Test.h" #include "src/errno/libc_errno.h" -#include TEST(LlvmLibcFgetsTest, WriteAndReadCharacters) { constexpr char FILENAME[] = "testdata/fgets.test"; diff --git a/libc/test/src/stdio/fileop_test.cpp b/libc/test/src/stdio/fileop_test.cpp index 0fbe19cf08d83..98ead6edd38b4 100644 --- a/libc/test/src/stdio/fileop_test.cpp +++ b/libc/test/src/stdio/fileop_test.cpp @@ -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 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::EQ; using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::NE; diff --git a/libc/test/src/stdio/fopencookie_test.cpp b/libc/test/src/stdio/fopencookie_test.cpp index 6c86b8759801e..016722aa11ab8 100644 --- a/libc/test/src/stdio/fopencookie_test.cpp +++ b/libc/test/src/stdio/fopencookie_test.cpp @@ -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 #include using MemoryView = LIBC_NAMESPACE::testing::MemoryView; diff --git a/libc/test/src/stdio/fprintf_test.cpp b/libc/test/src/stdio/fprintf_test.cpp index 08b31795b435b..82a3e039d9baa 100644 --- a/libc/test/src/stdio/fprintf_test.cpp +++ b/libc/test/src/stdio/fprintf_test.cpp @@ -17,8 +17,6 @@ #include "test/UnitTest/Test.h" -#include - namespace printf_test { #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE using LIBC_NAMESPACE::fclose; diff --git a/libc/test/src/stdio/fscanf_test.cpp b/libc/test/src/stdio/fscanf_test.cpp index 701090788ca10..e5b8c4f422bac 100644 --- a/libc/test/src/stdio/fscanf_test.cpp +++ b/libc/test/src/stdio/fscanf_test.cpp @@ -19,8 +19,6 @@ #include "test/UnitTest/Test.h" -#include - namespace scanf_test { #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE using LIBC_NAMESPACE::fclose; diff --git a/libc/test/src/stdio/ftell_test.cpp b/libc/test/src/stdio/ftell_test.cpp index 62745e2194be6..01ff071f2ee78 100644 --- a/libc/test/src/stdio/ftell_test.cpp +++ b/libc/test/src/stdio/ftell_test.cpp @@ -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 - class LlvmLibcFTellTest : public LIBC_NAMESPACE::testing::Test { protected: void test_with_bufmode(int bufmode) { diff --git a/libc/test/src/stdio/putc_test.cpp b/libc/test/src/stdio/putc_test.cpp index 7349a97d80e19..e881a0e2d0108 100644 --- a/libc/test/src/stdio/putc_test.cpp +++ b/libc/test/src/stdio/putc_test.cpp @@ -15,8 +15,6 @@ #include "test/UnitTest/Test.h" -#include - TEST(LlvmLibcPutcTest, WriteToFile) { constexpr char FILENAME[] = "testdata/putc_output.test"; ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w"); diff --git a/libc/test/src/stdio/setbuf_test.cpp b/libc/test/src/stdio/setbuf_test.cpp index b0abca4acf731..25fea59076626 100644 --- a/libc/test/src/stdio/setbuf_test.cpp +++ b/libc/test/src/stdio/setbuf_test.cpp @@ -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 - 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. diff --git a/libc/test/src/stdio/setvbuf_test.cpp b/libc/test/src/stdio/setvbuf_test.cpp index d42ebac12ead2..a1e1fee25db31 100644 --- a/libc/test/src/stdio/setvbuf_test.cpp +++ b/libc/test/src/stdio/setvbuf_test.cpp @@ -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 TEST(LlvmLibcSetvbufTest, SetNBFBuffer) { // The idea in this test is that we open a file for writing and reading, and diff --git a/libc/test/src/stdio/sprintf_test.cpp b/libc/test/src/stdio/sprintf_test.cpp index 54076eb64f205..36b3c6ae1b040 100644 --- a/libc/test/src/stdio/sprintf_test.cpp +++ b/libc/test/src/stdio/sprintf_test.cpp @@ -17,17 +17,6 @@ // TODO: Add a comment here explaining the printf format string. -// #include -// namespace LIBC_NAMESPACE_DECL { -// using ::sprintf; -// } - -class LlvmLibcSPrintfTest : public LIBC_NAMESPACE::testing::Test { -protected: - char buff[1000]; - int written; -}; - using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode; using LIBC_NAMESPACE::fputil::testing::RoundingMode; @@ -806,7 +795,10 @@ TEST(LlvmLibcSPrintfTest, OctConv) { #ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT -TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) { +TEST(LlvmLibcSPrintfTest, FloatHexExpConv) { + char buff[128]; + int written; + ForceRoundingMode r(RoundingMode::Nearest); double inf = LIBC_NAMESPACE::fputil::FPBits::inf().get_val(); double nan = LIBC_NAMESPACE::fputil::FPBits::quiet_nan().get_val(); @@ -1170,7 +1162,10 @@ TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) { " 0x1.00000000000000000000000000000000000000000000000000p+0"); } -TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) { +TEST(LlvmLibcSPrintfTest, FloatDecimalConv) { + char buff[1000]; + int written; + ForceRoundingMode r(RoundingMode::Nearest); double inf = LIBC_NAMESPACE::fputil::FPBits::inf().get_val(); double nan = LIBC_NAMESPACE::fputil::FPBits::quiet_nan().get_val(); @@ -1685,7 +1680,10 @@ TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) { // The long double tests are separated so that their performance can be directly // measured. -TEST_F(LlvmLibcSPrintfTest, FloatDecimalLongDoubleConv) { +TEST(LlvmLibcSPrintfTest, FloatDecimalLongDoubleConv) { + char buff[1000]; + int written; + ForceRoundingMode r(RoundingMode::Nearest); // Length Modifier Tests. @@ -2022,7 +2020,10 @@ TEST_F(LlvmLibcSPrintfTest, FloatDecimalLongDoubleConv) { #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80 } -TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) { +TEST(LlvmLibcSPrintfTest, FloatExponentConv) { + char buff[1000]; + int written; + ForceRoundingMode r(RoundingMode::Nearest); double inf = LIBC_NAMESPACE::fputil::FPBits::inf().get_val(); double nan = LIBC_NAMESPACE::fputil::FPBits::quiet_nan().get_val(); @@ -2508,7 +2509,10 @@ TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) { ASSERT_STREQ_LEN(written, buff, "+1.256e-01 001.256e+03"); } -TEST_F(LlvmLibcSPrintfTest, FloatExponentLongDoubleConv) { +TEST(LlvmLibcSPrintfTest, FloatExponentLongDoubleConv) { + char buff[1000]; + int written; + ForceRoundingMode r(RoundingMode::Nearest); // Length Modifier Tests. @@ -2629,7 +2633,10 @@ TEST_F(LlvmLibcSPrintfTest, FloatExponentLongDoubleConv) { */ } -TEST_F(LlvmLibcSPrintfTest, FloatAutoConv) { +TEST(LlvmLibcSPrintfTest, FloatAutoConv) { + char buff[1000]; + int written; + ForceRoundingMode r(RoundingMode::Nearest); double inf = LIBC_NAMESPACE::fputil::FPBits::inf().get_val(); double nan = LIBC_NAMESPACE::fputil::FPBits::quiet_nan().get_val(); @@ -3137,7 +3144,10 @@ TEST_F(LlvmLibcSPrintfTest, FloatAutoConv) { ASSERT_STREQ_LEN(written, buff, "+0.126 0001.26e+03"); } -TEST_F(LlvmLibcSPrintfTest, FloatAutoLongDoubleConv) { +TEST(LlvmLibcSPrintfTest, FloatAutoLongDoubleConv) { + char buff[1000]; + int written; + ForceRoundingMode r(RoundingMode::Nearest); // Length Modifier Tests. @@ -3292,7 +3302,9 @@ TEST_F(LlvmLibcSPrintfTest, FloatAutoLongDoubleConv) { #if defined(LIBC_COMPILER_HAS_FIXED_POINT) && \ !defined(LIBC_COPT_PRINTF_DISABLE_FIXED_POINT) -TEST_F(LlvmLibcSPrintfTest, FixedConv) { +TEST(LlvmLibcSPrintfTest, FixedConv) { + char buff[1000]; + int written; // These numeric tests are potentially a little weak, but the fuzz test is // more thorough than my handwritten tests tend to be. @@ -3502,7 +3514,10 @@ TEST_F(LlvmLibcSPrintfTest, FixedConv) { // !defined(LIBC_COPT_PRINTF_DISABLE_FIXED_POINT) #ifndef LIBC_COPT_PRINTF_DISABLE_STRERROR -TEST_F(LlvmLibcSPrintfTest, StrerrorConv) { +TEST(LlvmLibcSPrintfTest, StrerrorConv) { + char buff[1000]; + int written; + LIBC_NAMESPACE::libc_errno = 0; written = LIBC_NAMESPACE::sprintf(buff, "%m"); ASSERT_STREQ_LEN(written, buff, "Success"); diff --git a/libc/test/src/stdio/sscanf_test.cpp b/libc/test/src/stdio/sscanf_test.cpp index 59be4e6de6ed6..50c0441c5eb4a 100644 --- a/libc/test/src/stdio/sscanf_test.cpp +++ b/libc/test/src/stdio/sscanf_test.cpp @@ -8,13 +8,10 @@ #include "src/__support/CPP/limits.h" #include "src/__support/FPUtil/FPBits.h" - #include "src/stdio/sscanf.h" - -#include // For EOF - #include "test/UnitTest/FPMatcher.h" #include "test/UnitTest/Test.h" +#include // For EOF TEST(LlvmLibcSScanfTest, SimpleStringConv) { int ret_val; diff --git a/libc/test/src/stdio/ungetc_test.cpp b/libc/test/src/stdio/ungetc_test.cpp index c98995ff0811b..5015623387f24 100644 --- a/libc/test/src/stdio/ungetc_test.cpp +++ b/libc/test/src/stdio/ungetc_test.cpp @@ -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 - TEST(LlvmLibcUngetcTest, UngetAndReadBack) { constexpr char FILENAME[] = "testdata/ungetc_test.test"; ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w"); diff --git a/libc/test/src/stdio/unlocked_fileop_test.cpp b/libc/test/src/stdio/unlocked_fileop_test.cpp index 09697a6452f48..67f1b0ff513bc 100644 --- a/libc/test/src/stdio/unlocked_fileop_test.cpp +++ b/libc/test/src/stdio/unlocked_fileop_test.cpp @@ -18,7 +18,6 @@ #include "test/UnitTest/Test.h" #include "src/errno/libc_errno.h" -#include TEST(LlvmLibcFILETest, UnlockedReadAndWrite) { constexpr char fNAME[] = "testdata/unlocked_read_and_write.test"; diff --git a/libc/test/src/stdio/vfprintf_test.cpp b/libc/test/src/stdio/vfprintf_test.cpp index 9bad2c831e5c4..80d484500d5f2 100644 --- a/libc/test/src/stdio/vfprintf_test.cpp +++ b/libc/test/src/stdio/vfprintf_test.cpp @@ -21,8 +21,6 @@ #include "test/UnitTest/Test.h" -#include - namespace printf_test { #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE using LIBC_NAMESPACE::fclose; diff --git a/libc/test/src/stdio/vfscanf_test.cpp b/libc/test/src/stdio/vfscanf_test.cpp index fa4e27582375f..b66538671f620 100644 --- a/libc/test/src/stdio/vfscanf_test.cpp +++ b/libc/test/src/stdio/vfscanf_test.cpp @@ -19,8 +19,6 @@ #include "test/UnitTest/Test.h" -#include - namespace scanf_test { #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE using LIBC_NAMESPACE::fclose; diff --git a/libc/test/src/unistd/getopt_test.cpp b/libc/test/src/unistd/getopt_test.cpp index 1ca7c99e1ce37..e6e87720cde48 100644 --- a/libc/test/src/unistd/getopt_test.cpp +++ b/libc/test/src/unistd/getopt_test.cpp @@ -13,8 +13,6 @@ #include "src/stdio/fflush.h" #include "src/stdio/fopencookie.h" -#include - using LIBC_NAMESPACE::cpp::array; namespace test_globals { diff --git a/libc/test/src/wchar/wctob_test.cpp b/libc/test/src/wchar/wctob_test.cpp index 3f911884a7c12..977224bf77abe 100644 --- a/libc/test/src/wchar/wctob_test.cpp +++ b/libc/test/src/wchar/wctob_test.cpp @@ -6,10 +6,8 @@ // //===----------------------------------------------------------------------===// -#include //for EOF - +#include "hdr/stdio_macros.h" //for EOF #include "src/wchar/wctob.h" - #include "test/UnitTest/Test.h" TEST(LlvmLibcWctob, DefaultLocale) { From 0025554df6c890338576699d48c07373dc0a8fd5 Mon Sep 17 00:00:00 2001 From: Tue Ly Date: Thu, 26 Sep 2024 22:14:10 -0400 Subject: [PATCH 2/3] Address comments. --- libc/test/src/__support/File/platform_file_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc/test/src/__support/File/platform_file_test.cpp b/libc/test/src/__support/File/platform_file_test.cpp index 82f17d2ef2e17..6b2be2a149329 100644 --- a/libc/test/src/__support/File/platform_file_test.cpp +++ b/libc/test/src/__support/File/platform_file_test.cpp @@ -9,7 +9,7 @@ #include "src/__support/File/file.h" #include "test/UnitTest/Test.h" -#include // For SEEK_* macros +#include "hdr/stdio_macros.h" // For SEEK_* macros using File = LIBC_NAMESPACE::File; constexpr char TEXT[] = "Hello, File"; From 84c85671b0ee6cd6ad699b22036663f303e99f0f Mon Sep 17 00:00:00 2001 From: Tue Ly Date: Mon, 30 Sep 2024 22:35:04 -0400 Subject: [PATCH 3/3] Address comments. --- libc/test/src/stdio/sprintf_test.cpp | 55 ++++++++++------------------ libc/test/src/stdio/sscanf_test.cpp | 5 ++- libc/test/src/stdio/ungetc_test.cpp | 3 +- 3 files changed, 25 insertions(+), 38 deletions(-) diff --git a/libc/test/src/stdio/sprintf_test.cpp b/libc/test/src/stdio/sprintf_test.cpp index 36b3c6ae1b040..54076eb64f205 100644 --- a/libc/test/src/stdio/sprintf_test.cpp +++ b/libc/test/src/stdio/sprintf_test.cpp @@ -17,6 +17,17 @@ // TODO: Add a comment here explaining the printf format string. +// #include +// namespace LIBC_NAMESPACE_DECL { +// using ::sprintf; +// } + +class LlvmLibcSPrintfTest : public LIBC_NAMESPACE::testing::Test { +protected: + char buff[1000]; + int written; +}; + using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode; using LIBC_NAMESPACE::fputil::testing::RoundingMode; @@ -795,10 +806,7 @@ TEST(LlvmLibcSPrintfTest, OctConv) { #ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT -TEST(LlvmLibcSPrintfTest, FloatHexExpConv) { - char buff[128]; - int written; - +TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) { ForceRoundingMode r(RoundingMode::Nearest); double inf = LIBC_NAMESPACE::fputil::FPBits::inf().get_val(); double nan = LIBC_NAMESPACE::fputil::FPBits::quiet_nan().get_val(); @@ -1162,10 +1170,7 @@ TEST(LlvmLibcSPrintfTest, FloatHexExpConv) { " 0x1.00000000000000000000000000000000000000000000000000p+0"); } -TEST(LlvmLibcSPrintfTest, FloatDecimalConv) { - char buff[1000]; - int written; - +TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) { ForceRoundingMode r(RoundingMode::Nearest); double inf = LIBC_NAMESPACE::fputil::FPBits::inf().get_val(); double nan = LIBC_NAMESPACE::fputil::FPBits::quiet_nan().get_val(); @@ -1680,10 +1685,7 @@ TEST(LlvmLibcSPrintfTest, FloatDecimalConv) { // The long double tests are separated so that their performance can be directly // measured. -TEST(LlvmLibcSPrintfTest, FloatDecimalLongDoubleConv) { - char buff[1000]; - int written; - +TEST_F(LlvmLibcSPrintfTest, FloatDecimalLongDoubleConv) { ForceRoundingMode r(RoundingMode::Nearest); // Length Modifier Tests. @@ -2020,10 +2022,7 @@ TEST(LlvmLibcSPrintfTest, FloatDecimalLongDoubleConv) { #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80 } -TEST(LlvmLibcSPrintfTest, FloatExponentConv) { - char buff[1000]; - int written; - +TEST_F(LlvmLibcSPrintfTest, FloatExponentConv) { ForceRoundingMode r(RoundingMode::Nearest); double inf = LIBC_NAMESPACE::fputil::FPBits::inf().get_val(); double nan = LIBC_NAMESPACE::fputil::FPBits::quiet_nan().get_val(); @@ -2509,10 +2508,7 @@ TEST(LlvmLibcSPrintfTest, FloatExponentConv) { ASSERT_STREQ_LEN(written, buff, "+1.256e-01 001.256e+03"); } -TEST(LlvmLibcSPrintfTest, FloatExponentLongDoubleConv) { - char buff[1000]; - int written; - +TEST_F(LlvmLibcSPrintfTest, FloatExponentLongDoubleConv) { ForceRoundingMode r(RoundingMode::Nearest); // Length Modifier Tests. @@ -2633,10 +2629,7 @@ TEST(LlvmLibcSPrintfTest, FloatExponentLongDoubleConv) { */ } -TEST(LlvmLibcSPrintfTest, FloatAutoConv) { - char buff[1000]; - int written; - +TEST_F(LlvmLibcSPrintfTest, FloatAutoConv) { ForceRoundingMode r(RoundingMode::Nearest); double inf = LIBC_NAMESPACE::fputil::FPBits::inf().get_val(); double nan = LIBC_NAMESPACE::fputil::FPBits::quiet_nan().get_val(); @@ -3144,10 +3137,7 @@ TEST(LlvmLibcSPrintfTest, FloatAutoConv) { ASSERT_STREQ_LEN(written, buff, "+0.126 0001.26e+03"); } -TEST(LlvmLibcSPrintfTest, FloatAutoLongDoubleConv) { - char buff[1000]; - int written; - +TEST_F(LlvmLibcSPrintfTest, FloatAutoLongDoubleConv) { ForceRoundingMode r(RoundingMode::Nearest); // Length Modifier Tests. @@ -3302,9 +3292,7 @@ TEST(LlvmLibcSPrintfTest, FloatAutoLongDoubleConv) { #if defined(LIBC_COMPILER_HAS_FIXED_POINT) && \ !defined(LIBC_COPT_PRINTF_DISABLE_FIXED_POINT) -TEST(LlvmLibcSPrintfTest, FixedConv) { - char buff[1000]; - int written; +TEST_F(LlvmLibcSPrintfTest, FixedConv) { // These numeric tests are potentially a little weak, but the fuzz test is // more thorough than my handwritten tests tend to be. @@ -3514,10 +3502,7 @@ TEST(LlvmLibcSPrintfTest, FixedConv) { // !defined(LIBC_COPT_PRINTF_DISABLE_FIXED_POINT) #ifndef LIBC_COPT_PRINTF_DISABLE_STRERROR -TEST(LlvmLibcSPrintfTest, StrerrorConv) { - char buff[1000]; - int written; - +TEST_F(LlvmLibcSPrintfTest, StrerrorConv) { LIBC_NAMESPACE::libc_errno = 0; written = LIBC_NAMESPACE::sprintf(buff, "%m"); ASSERT_STREQ_LEN(written, buff, "Success"); diff --git a/libc/test/src/stdio/sscanf_test.cpp b/libc/test/src/stdio/sscanf_test.cpp index 50c0441c5eb4a..33bb0acba3e66 100644 --- a/libc/test/src/stdio/sscanf_test.cpp +++ b/libc/test/src/stdio/sscanf_test.cpp @@ -6,12 +6,13 @@ // //===----------------------------------------------------------------------===// +#include "src/stdio/sscanf.h" + +#include "hdr/stdio_macros.h" // For EOF #include "src/__support/CPP/limits.h" #include "src/__support/FPUtil/FPBits.h" -#include "src/stdio/sscanf.h" #include "test/UnitTest/FPMatcher.h" #include "test/UnitTest/Test.h" -#include // For EOF TEST(LlvmLibcSScanfTest, SimpleStringConv) { int ret_val; diff --git a/libc/test/src/stdio/ungetc_test.cpp b/libc/test/src/stdio/ungetc_test.cpp index 5015623387f24..b9d7530fc7177 100644 --- a/libc/test/src/stdio/ungetc_test.cpp +++ b/libc/test/src/stdio/ungetc_test.cpp @@ -6,13 +6,14 @@ // //===----------------------------------------------------------------------===// +#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" TEST(LlvmLibcUngetcTest, UngetAndReadBack) {