Skip to content

Commit 2ecfa63

Browse files
authored
Use selective imports when importing from core.stdc in rt (#20742)
1 parent 5a02544 commit 2ecfa63

21 files changed

+166
-190
lines changed

druntime/src/rt/adi.d

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111

1212
module rt.adi;
1313

14-
//debug=adi; // uncomment to turn on debugging printf's
14+
// debug = adi; // uncomment to turn on debugging printf's
1515

16-
private
17-
{
18-
debug(adi) import core.stdc.stdio;
19-
}
16+
debug (adi) import core.stdc.stdio : printf;
2017

2118
/***************************************
2219
* Support for array equality test.

druntime/src/rt/arraycat.d

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
module rt.arraycat;
1212

13-
private
14-
{
15-
import core.stdc.string;
16-
import core.internal.util.array;
17-
debug(PRINTF) import core.stdc.stdio;
18-
}
13+
// debug = PRINTF;
14+
15+
import core.internal.util.array;
16+
import core.stdc.string : memcpy;
17+
18+
debug(PRINTF) import core.stdc.stdio : printf;
1919

2020
extern (C) @trusted nothrow:
2121

druntime/src/rt/cmath2.d

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
*/
1515
module rt.cmath2;
1616

17-
import core.stdc.math;
17+
import core.stdc.math : fabs;
18+
19+
debug import core.stdc.stdio : printf;
1820

1921
extern (C):
2022

druntime/src/rt/cover.d

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,29 @@
1111

1212
module rt.cover;
1313

14+
import core.internal.utf;
1415
import core.internal.util.math : max, min;
16+
import core.stdc.stdio : EOF, fclose, fgetc, FILE, fileno, fprintf, fread, fseek, ftell, printf, SEEK_END, SEEK_SET,
17+
stderr;
18+
import core.stdc.stdlib : exit, EXIT_FAILURE;
1519

16-
private
20+
version (Windows)
1721
{
18-
version (Windows)
19-
{
20-
import core.sys.windows.basetsd /+: HANDLE+/;
21-
import core.sys.windows.winbase /+: LOCKFILE_EXCLUSIVE_LOCK, LockFileEx, OVERLAPPED, SetEndOfFile+/;
22-
}
23-
else version (Posix)
24-
{
25-
import core.sys.posix.fcntl : O_CREAT, O_RDWR, open, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR;
26-
import core.sys.posix.unistd : F_LOCK, ftruncate, lockf;
27-
}
28-
import core.internal.utf;
29-
import core.stdc.config : c_long;
30-
import core.stdc.stdio;
31-
import core.stdc.stdlib;
22+
import core.stdc.stdio : _fdopen, _get_osfhandle, _O_BINARY, _O_CREAT, _O_RDWR, _S_IREAD, _S_IWRITE, _wopen;
23+
import core.sys.windows.basetsd;
24+
import core.sys.windows.winbase;
25+
}
26+
else version (Posix)
27+
{
28+
import core.stdc.stdio : fopen;
29+
import core.sys.posix.fcntl : O_CREAT, O_RDWR, open, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR;
30+
import core.sys.posix.unistd : F_LOCK, ftruncate, lockf;
31+
}
32+
else
33+
static assert(0, "Unsupported platform");
3234

35+
private
36+
{
3337
struct BitArray
3438
{
3539
size_t len;
@@ -321,14 +325,14 @@ shared static ~this()
321325

322326
version (Windows)
323327
SetEndOfFile(handle(fileno(flst)));
324-
else
328+
else version (Posix)
325329
ftruncate(fileno(flst), ftell(flst));
326330
}
327331
}
328332

329333
uint digits(uint number)
330334
{
331-
import core.stdc.math;
335+
import core.stdc.math : floor, log10;
332336
return number ? cast(uint)floor(log10(number)) + 1 : 1;
333337
}
334338

@@ -457,16 +461,14 @@ string chomp( string str, string delim = null )
457461
// open/create file for read/write, pointer at beginning
458462
FILE* openOrCreateFile(string name)
459463
{
460-
import core.internal.utf : toUTF16z;
461-
462464
version (Windows)
463465
immutable fd = _wopen(toUTF16z(name), _O_RDWR | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE);
464-
else
466+
else version (Posix)
465467
immutable fd = open((name ~ '\0').ptr, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
466468
S_IROTH | S_IWOTH);
467469
version (CRuntime_Microsoft)
468470
alias fdopen = _fdopen;
469-
version (Posix)
471+
else version (Posix)
470472
import core.sys.posix.stdio : fdopen;
471473
return fdopen(fd, "r+b");
472474
}
@@ -492,8 +494,6 @@ void lockFile(int fd)
492494
// exclusively lock first byte
493495
LockFileEx(handle(fd), LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &off);
494496
}
495-
else
496-
static assert(0, "unimplemented");
497497
}
498498

499499
bool readFile(FILE* file, ref char[] buf)
@@ -520,11 +520,9 @@ version (Windows) extern (C) nothrow @nogc FILE* _wfopen(scope const wchar* file
520520

521521
bool readFile(string name, ref char[] buf)
522522
{
523-
import core.internal.utf : toUTF16z;
524-
525523
version (Windows)
526524
auto file = _wfopen(toUTF16z(name), "rb"w.ptr);
527-
else
525+
else version (Posix)
528526
auto file = fopen((name ~ '\0').ptr, "rb".ptr);
529527
if (file is null) return false;
530528
scope(exit) fclose(file);

druntime/src/rt/deh_win32.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import core.sys.windows.basetsd /+: ULONG_PTR+/;
1717
import core.sys.windows.windef /+: BOOL, BYTE, DWORD+/;
1818
import core.sys.windows.winnt /+: PVOID+/;
1919
import rt.monitor_;
20-
//import core.stdc.stdio;
20+
21+
debug import core.stdc.stdio : printf;
2122

2223
version (D_InlineAsm_X86)
2324
{

druntime/src/rt/dmain2.d

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,37 @@
1111

1212
module rt.dmain2;
1313

14-
import rt.memory;
15-
import rt.sections;
1614
import core.atomic;
17-
import core.stdc.stddef;
18-
import core.stdc.stdlib;
19-
import core.stdc.string;
20-
import core.stdc.stdio; // for printf()
15+
import core.internal.parseoptions : rt_parseOption;
2116
import core.stdc.errno : errno;
17+
import core.stdc.stdio : fflush, fprintf, fwrite, stderr, stdout;
18+
import core.stdc.stdlib : alloca, EXIT_FAILURE, EXIT_SUCCESS, free, malloc, realloc;
19+
import core.stdc.string : strerror;
20+
import rt.config : rt_cmdline_enabled, rt_configOption;
21+
import rt.memory;
22+
import rt.sections;
2223

2324
version (Windows)
2425
{
25-
import core.stdc.wchar_;
26+
import core.stdc.stdio : fileno;
27+
import core.stdc.wchar_ : wcslen;
2628
import core.sys.windows.basetsd : HANDLE;
2729
import core.sys.windows.shellapi : CommandLineToArgvW;
28-
import core.sys.windows.winbase : FreeLibrary, GetCommandLineW, GetProcAddress,
29-
IsDebuggerPresent, LoadLibraryW, LocalFree, WriteFile;
30+
import core.sys.windows.winbase : FreeLibrary, GetCommandLineW, GetProcAddress, IsDebuggerPresent, LoadLibraryW,
31+
LocalFree, WriteFile;
3032
import core.sys.windows.wincon : CONSOLE_SCREEN_BUFFER_INFO, GetConsoleOutputCP,
3133
GetConsoleScreenBufferInfo;
3234
import core.sys.windows.winnls : CP_UTF8, MultiByteToWideChar, WideCharToMultiByte;
3335
import core.sys.windows.winnt : WCHAR;
3436
import core.sys.windows.winuser : MB_ICONERROR, MessageBoxW;
3537

3638
pragma(lib, "shell32.lib"); // needed for CommandLineToArgvW
37-
}
3839

39-
version (FreeBSD)
40-
{
41-
import core.stdc.fenv;
42-
}
43-
version (NetBSD)
44-
{
45-
import core.stdc.fenv;
40+
import core.stdc.stdio : _get_osfhandle;
4641
}
47-
version (DragonFlyBSD)
42+
else version (Posix)
4843
{
49-
import core.stdc.fenv;
44+
import core.stdc.string : strlen;
5045
}
5146

5247
// not sure why we can't define this in one place, but this is to keep this
@@ -444,7 +439,6 @@ private extern (C) int _d_run_main2(char[][] args, size_t totalArgsLength, MainF
444439
char[][] argsCopy = buff[0 .. args.length];
445440
auto argBuff = cast(char*) (buff + args.length);
446441
size_t j = 0;
447-
import rt.config : rt_cmdline_enabled;
448442
bool parseOpts = rt_cmdline_enabled!();
449443
foreach (arg; args)
450444
{
@@ -587,8 +581,6 @@ private void formatThrowable(Throwable t, scope void delegate(in char[] s) nothr
587581

588582
private auto parseExceptionOptions()
589583
{
590-
import rt.config : rt_configOption;
591-
import core.internal.parseoptions : rt_parseOption;
592584
const optName = "trapExceptions";
593585
auto option = rt_configOption(optName);
594586
auto trap = rt_trapExceptions;

druntime/src/rt/dwarfeh.d

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
module rt.dwarfeh;
1313

14-
// debug = EH_personality;
15-
1614
version (Posix):
1715

18-
import rt.dmain2: _d_print_throwable;
16+
// debug = EH_personality;
17+
1918
import core.internal.backtrace.unwind;
20-
import core.stdc.stdio;
21-
import core.stdc.stdlib;
19+
import core.stdc.stdio : fprintf, printf, stderr;
20+
import core.stdc.stdlib : abort, calloc, free;
21+
import rt.dmain2 : _d_print_throwable;
2222

2323
/* These are the register numbers for _Unwind_SetGR().
2424
* Hints for these can be found by looking at the EH_RETURN_DATA_REGNO macro in
@@ -125,7 +125,8 @@ debug (EH_personality)
125125
{
126126
private void writeln(in char* format, ...) @nogc nothrow
127127
{
128-
import core.stdc.stdarg;
128+
import core.stdc.stdarg : va_list, va_start;
129+
import core.stdc.stdio : fflush, stdout, vfprintf;
129130

130131
va_list args;
131132
va_start(args, format);
@@ -181,7 +182,7 @@ struct ExceptionHeader
181182
auto eh = &ehstorage;
182183
if (eh.object) // if in use
183184
{
184-
eh = cast(ExceptionHeader*)core.stdc.stdlib.calloc(1, ExceptionHeader.sizeof);
185+
eh = cast(ExceptionHeader*).calloc(1, ExceptionHeader.sizeof);
185186
if (!eh)
186187
terminate(__LINE__); // out of memory while throwing - not much else can be done
187188
}
@@ -204,7 +205,7 @@ struct ExceptionHeader
204205
*/
205206
*eh = ExceptionHeader.init;
206207
if (eh != &ehstorage)
207-
core.stdc.stdlib.free(eh);
208+
.free(eh);
208209
}
209210

210211
/*************************

druntime/src/rt/ehalloc.d

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ module rt.ehalloc;
1313

1414
//debug = PRINTF;
1515

16-
debug(PRINTF)
17-
{
18-
import core.stdc.stdio;
19-
}
16+
debug (PRINTF) import core.stdc.stdio : printf;
2017

2118

2219
/********************************************

0 commit comments

Comments
 (0)