Skip to content

Commit c63112a

Browse files
authored
[libc][stdio] Use proxy headers of stdio.h in src and test folders. (#110067)
#60481
1 parent d071fda commit c63112a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+154
-106
lines changed

libc/hdr/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ add_proxy_header_library(
7878
libc.include.signal
7979
)
8080

81+
add_header_library(stdio_overlay HDRS stdio_overlay.h)
82+
8183
add_proxy_header_library(
8284
stdio_macros
8385
HDRS
8486
stdio_macros.h
87+
DEPENDS
88+
.stdio_overlay
8589
FULL_BUILD_DEPENDS
8690
libc.include.stdio
8791
libc.include.llvm-libc-macros.stdio_macros

libc/hdr/stdio_macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#else // Overlay mode
1818

19-
#include <stdio.h>
19+
#include "stdio_overlay.h"
2020

2121
#endif // LLVM_LIBC_FULL_BUILD
2222

libc/hdr/stdio_overlay.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===-- Including stdio.h in overlay mode ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_STDIO_OVERLAY_H
10+
#define LLVM_LIBC_HDR_STDIO_OVERLAY_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
#error "This header should only be included in overlay mode"
14+
#endif
15+
16+
// Overlay mode
17+
18+
// glibc <stdio.h> header might provide extern inline definitions for few
19+
// functions, causing external alias errors. They are guarded by
20+
// `__USE_EXTERN_INLINES` macro. We temporarily disable `__USE_EXTERN_INLINES`
21+
// macro by defining `__NO_INLINE__` before including <stdio.h>.
22+
// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
23+
// with `_FORTIFY_SOURCE`.
24+
25+
#ifdef _FORTIFY_SOURCE
26+
#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
27+
#undef _FORTIFY_SOURCE
28+
#endif
29+
30+
#ifndef __NO_INLINE__
31+
#define __NO_INLINE__ 1
32+
#define LIBC_SET_NO_INLINE
33+
#endif
34+
35+
#include <stdio.h>
36+
37+
#ifdef LIBC_OLD_FORTIFY_SOURCE
38+
#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
39+
#undef LIBC_OLD_FORTIFY_SOURCE
40+
#endif
41+
42+
#ifdef LIBC_SET_NO_INLINE
43+
#undef __NO_INLINE__
44+
#undef LIBC_SET_NO_INLINE
45+
#endif
46+
47+
#endif // LLVM_LIBC_HDR_STDIO_OVERLAY_H

libc/hdr/types/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ add_proxy_header_library(
140140
FILE
141141
HDRS
142142
FILE.h
143+
DEPENDS
144+
libc.hdr.stdio_overlay
143145
FULL_BUILD_DEPENDS
144146
libc.include.llvm-libc-types.FILE
145147
libc.include.stdio
@@ -149,6 +151,8 @@ add_proxy_header_library(
149151
off_t
150152
HDRS
151153
off_t.h
154+
DEPENDS
155+
libc.hdr.stdio_overlay
152156
FULL_BUILD_DEPENDS
153157
libc.include.llvm-libc-types.off_t
154158
libc.include.stdio
@@ -158,6 +162,8 @@ add_proxy_header_library(
158162
cookie_io_functions_t
159163
HDRS
160164
cookie_io_functions_t.h
165+
DEPENDS
166+
libc.hdr.stdio_overlay
161167
FULL_BUILD_DEPENDS
162168
libc.include.llvm-libc-types.cookie_io_functions_t
163169
libc.include.stdio

libc/hdr/types/FILE.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#else // Overlay mode
1717

18-
#include <stdio.h>
18+
#include "hdr/stdio_overlay.h"
1919

2020
#endif // LLVM_LIBC_FULL_BUILD
2121

libc/hdr/types/cookie_io_functions_t.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#else // Overlay mode
1717

18-
#include <stdio.h>
18+
#include "hdr/stdio_overlay.h"
1919

2020
#endif // LLVM_LIBC_FULL_BUILD
2121

libc/hdr/types/off_t.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#else // Overlay mode
1717

18-
#include <stdio.h>
18+
#include "hdr/stdio_overlay.h"
1919

2020
#endif // LLVM_LIBC_FULL_BUILD
2121

libc/include/llvm-libc-macros/stdio-macros.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@
99
#ifndef LLVM_LIBC_MACROS_STDIO_MACROS_H
1010
#define LLVM_LIBC_MACROS_STDIO_MACROS_H
1111

12+
#include "../llvm-libc-types/FILE.h"
13+
14+
#ifdef __cplusplus
15+
extern "C" FILE *stdin;
16+
extern "C" FILE *stdout;
17+
extern "C" FILE *stderr;
18+
#else
19+
extern FILE *stdin;
20+
extern FILE *stdout;
21+
extern FILE *stderr;
22+
#endif
23+
24+
#ifndef stdin
25+
#define stdin stdin
26+
#endif
27+
28+
#ifndef stdout
29+
#define stdout stdout
30+
#endif
31+
32+
#ifndef stderr
33+
#define stderr stderr
34+
#endif
35+
1236
#ifndef EOF
1337
#define EOF (-1)
1438
#endif
@@ -19,4 +43,16 @@
1943
#define _IOLBF 1
2044
#define _IOFBF 0
2145

46+
#ifndef SEEK_SET
47+
#define SEEK_SET 0
48+
#endif
49+
50+
#ifndef SEEK_CUR
51+
#define SEEK_CUR 1
52+
#endif
53+
54+
#ifndef SEEK_END
55+
#define SEEK_END 2
56+
#endif
57+
2258
#endif // LLVM_LIBC_MACROS_STDIO_MACROS_H

libc/src/__support/File/linux/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ add_object_library(
88
lseekImpl.h
99
DEPENDS
1010
libc.include.fcntl
11-
libc.include.stdio
1211
libc.include.sys_syscall
1312
libc.include.sys_stat
1413
libc.src.__support.CPP.new

libc/src/stdio/asprintf.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#define LLVM_LIBC_SRC_STDIO_ASPRINTF_H
1111

1212
#include "src/__support/macros/config.h"
13-
#include <stdarg.h>
14-
#include <stdio.h>
1513

1614
namespace LIBC_NAMESPACE {
1715

libc/src/stdio/gpu/CMakeLists.txt

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
1+
add_entrypoint_object(
2+
stdin
3+
SRCS
4+
stdin.cpp
5+
HDRS
6+
../stdin.h
7+
DEPENDS
8+
libc.hdr.types.FILE
9+
)
10+
11+
add_entrypoint_object(
12+
stdout
13+
SRCS
14+
stdout.cpp
15+
HDRS
16+
../stdout.h
17+
DEPENDS
18+
libc.hdr.types.FILE
19+
)
20+
21+
add_entrypoint_object(
22+
stderr
23+
SRCS
24+
stderr.cpp
25+
HDRS
26+
../stderr.h
27+
DEPENDS
28+
libc.hdr.types.FILE
29+
)
30+
131
add_header_library(
232
gpu_file
333
HDRS
434
file.h
535
DEPENDS
636
libc.hdr.types.FILE
37+
libc.hdr.stdio_macros
738
libc.src.__support.RPC.rpc_client
839
libc.src.__support.common
940
.stdin
@@ -123,7 +154,6 @@ add_entrypoint_object(
123154
../puts.h
124155
DEPENDS
125156
libc.hdr.types.FILE
126-
libc.include.stdio # needed for stdin
127157
.gpu_file
128158
)
129159

@@ -168,7 +198,6 @@ add_entrypoint_object(
168198
../putc.h
169199
DEPENDS
170200
libc.hdr.types.FILE
171-
libc.include.stdio # needed for stdin
172201
.gpu_file
173202
)
174203

@@ -180,7 +209,6 @@ add_entrypoint_object(
180209
../putchar.h
181210
DEPENDS
182211
libc.hdr.types.FILE
183-
libc.include.stdio # needed for stdin
184212
.gpu_file
185213
)
186214

@@ -203,7 +231,6 @@ add_entrypoint_object(
203231
../getc.h
204232
DEPENDS
205233
libc.hdr.types.FILE
206-
libc.include.stdio # needed for stdin
207234
.gpu_file
208235
)
209236

@@ -215,7 +242,6 @@ add_entrypoint_object(
215242
../getchar.h
216243
DEPENDS
217244
libc.hdr.types.FILE
218-
libc.include.stdio # needed for stdin
219245
.gpu_file
220246
)
221247

@@ -304,33 +330,3 @@ add_entrypoint_object(
304330
libc.hdr.types.FILE
305331
.gpu_file
306332
)
307-
308-
add_entrypoint_object(
309-
stdin
310-
SRCS
311-
stdin.cpp
312-
HDRS
313-
../stdin.h
314-
DEPENDS
315-
libc.hdr.types.FILE
316-
)
317-
318-
add_entrypoint_object(
319-
stdout
320-
SRCS
321-
stdout.cpp
322-
HDRS
323-
../stdout.h
324-
DEPENDS
325-
libc.hdr.types.FILE
326-
)
327-
328-
add_entrypoint_object(
329-
stderr
330-
SRCS
331-
stderr.cpp
332-
HDRS
333-
../stderr.h
334-
DEPENDS
335-
libc.hdr.types.FILE
336-
)

libc/src/stdio/gpu/file.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
#include "src/__support/macros/config.h"
1111
#include "src/string/string_utils.h"
1212

13+
#include "hdr/stdio_macros.h" // For stdin/out/err
1314
#include "hdr/types/FILE.h"
1415

15-
#include <stdio.h> //needed for stdin/out/err
16-
1716
namespace LIBC_NAMESPACE_DECL {
1817
namespace file {
1918

libc/src/stdio/gpu/fprintf.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
#include "src/stdio/fprintf.h"
1010

11+
#include "hdr/types/FILE.h"
1112
#include "src/__support/CPP/string_view.h"
1213
#include "src/__support/arg_list.h"
1314
#include "src/errno/libc_errno.h"
1415
#include "src/stdio/gpu/vfprintf_utils.h"
1516

16-
#include <stdio.h>
17+
#include <stdarg.h>
1718

1819
namespace LIBC_NAMESPACE {
1920

libc/src/stdio/gpu/getchar.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
#include "file.h"
1111
#include "src/__support/macros/config.h"
1212

13-
#include "hdr/stdio_macros.h" // for EOF.
14-
#include "hdr/types/FILE.h"
15-
16-
#include <stdio.h> //needed for stdin
13+
#include "hdr/stdio_macros.h" // for EOF and stdin.
1714

1815
namespace LIBC_NAMESPACE_DECL {
1916

libc/src/stdio/gpu/printf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "src/errno/libc_errno.h"
1414
#include "src/stdio/gpu/vfprintf_utils.h"
1515

16-
#include <stdio.h>
16+
#include <stdarg.h>
1717

1818
namespace LIBC_NAMESPACE {
1919

libc/src/stdio/gpu/putchar.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
#include "file.h"
1111
#include "src/__support/macros/config.h"
1212

13-
#include "hdr/stdio_macros.h" // for EOF.
14-
#include "hdr/types/FILE.h"
15-
16-
#include <stdio.h> //needed for stdout
13+
#include "hdr/stdio_macros.h" // for EOF and stdout.
1714

1815
namespace LIBC_NAMESPACE_DECL {
1916

libc/src/stdio/gpu/puts.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
#include "src/errno/libc_errno.h"
1313
#include "src/stdio/gpu/file.h"
1414

15-
#include "hdr/stdio_macros.h" // for EOF.
16-
#include "hdr/types/FILE.h"
17-
18-
#include <stdio.h> //needed for stdout
15+
#include "hdr/stdio_macros.h" // for EOF and stdout.
1916

2017
namespace LIBC_NAMESPACE_DECL {
2118

libc/src/stdio/gpu/vfprintf.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88

99
#include "src/stdio/vfprintf.h"
1010

11+
#include "hdr/types/FILE.h"
1112
#include "src/__support/CPP/string_view.h"
1213
#include "src/__support/arg_list.h"
1314
#include "src/errno/libc_errno.h"
1415
#include "src/stdio/gpu/vfprintf_utils.h"
1516

16-
#include <stdio.h>
17-
1817
namespace LIBC_NAMESPACE {
1918

2019
LLVM_LIBC_FUNCTION(int, vfprintf,

0 commit comments

Comments
 (0)