1 /**
2 * Give import hints for common symbol names that couldn't be resolved.
3 *
4 * For example, prompt to `import std.stdio` when using `writeln`.
5 *
6 * Copyright: Copyright (C) 1999-2022 by The D Language Foundation, All Rights Reserved
7 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
8 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/imphint.d, _imphint.d)
10 * Documentation: https://dlang.org/phobos/dmd_imphint.html
11 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/imphint.d
12 */
13
14 module dmd.imphint;
15
16 /******************************************
17 * Looks for undefined identifier s to see
18 * if it might be undefined because an import
19 * was not specified.
20 * Not meant to be a comprehensive list of names in each module,
21 * just the most common ones.
22 */
importHint(const (char)[]s)23 const(char)[] importHint(const(char)[] s)
24 {
25 if (auto entry = s in hints)
26 return *entry;
27 return null;
28 }
29
30 private immutable string[string] hints;
31
this()32 shared static this()
33 {
34 // in alphabetic order
35 hints = [
36 "AliasSeq": "std.meta",
37 "appender": "std.array",
38 "array": "std.array",
39 "calloc": "core.stdc.stdlib",
40 "chdir": "std.file",
41 "cos": "std.math",
42 "dirEntries": "std.file",
43 "drop": "std.range",
44 "each": "std.algorithm",
45 "empty": "std.range",
46 "endsWith": "std.algorithm",
47 "enforce": "std.exception",
48 "enumerate": "std.range",
49 "equal": "std.algorithm",
50 "exists": "std.file",
51 "fabs": "std.math",
52 "filter": "std.algorithm",
53 "format": "std.format",
54 "free": "core.stdc.stdlib",
55 "front": "std.range",
56 "iota": "std.range",
57 "isDir": "std.file",
58 "isFile": "std.file",
59 "join": "std.array",
60 "joiner": "std.algorithm",
61 "malloc": "core.stdc.stdlib",
62 "map": "std.algorithm",
63 "max": "std.algorithm",
64 "min": "std.algorithm",
65 "mkdir": "std.file",
66 "popFront": "std.range",
67 "printf": "core.stdc.stdio",
68 "realloc": "core.stdc.stdlib",
69 "replace": "std.array",
70 "rmdir": "std.file",
71 "sin": "std.math",
72 "sort": "std.algorithm",
73 "split": "std.array",
74 "sqrt": "std.math",
75 "startsWith": "std.algorithm",
76 "take": "std.range",
77 "text": "std.conv",
78 "to": "std.conv",
79 "writefln": "std.stdio",
80 "writeln": "std.stdio",
81 "__va_argsave_t": "core.stdc.stdarg",
82 "__va_list_tag": "core.stdc.stdarg",
83 ];
84 }
85
86 unittest
87 {
88 assert(importHint("printf") !is null);
89 assert(importHint("fabs") !is null);
90 assert(importHint("xxxxx") is null);
91 }
92