diff --git a/compiler/src/dmd/main.d b/compiler/src/dmd/main.d index 84497773df0e..7abb06c80eea 100644 --- a/compiler/src/dmd/main.d +++ b/compiler/src/dmd/main.d @@ -414,15 +414,23 @@ private int tryMain(size_t argc, const(char)** argv, out Param params) foreach (entry; imppath) { - int sink(const(char)* p) nothrow + struct SinkContext { - ImportPathInfo temp = entry; + ImportPathInfo* entry; + Array!ImportPathInfo* array; + } + + static int sink(const(char)* p, void* ctx) nothrow + { + auto c = cast(SinkContext*)ctx; + ImportPathInfo temp = *c.entry; temp.path = p; - array.push(temp); + c.array.push(temp); return 0; } - FileName.splitPath(&sink, entry.path); + SinkContext context = SinkContext(&entry, &array); + FileName.splitPath(&sink, entry.path, &context); FileName.appendSplitPath(entry.path, pathsOnlyArray); } diff --git a/compiler/src/dmd/root/filename.d b/compiler/src/dmd/root/filename.d index 5ad07750b3b0..6da2cd48612f 100644 --- a/compiler/src/dmd/root/filename.d +++ b/compiler/src/dmd/root/filename.d @@ -471,14 +471,16 @@ nothrow: // Split a path and append the results to `array` extern (C++) static void appendSplitPath(const(char)* path, ref Strings array) { - int sink(const(char)* p) nothrow + static int sink(const(char)* p, void* ctx) nothrow { - array.push(p); + auto arr = cast(Strings*)ctx; + arr.push(p); return 0; } - splitPath(&sink, path); + splitPath(&sink, path, &array); } + /**** * Split path (such as that returned by `getenv("PATH")`) into pieces, each piece is mem.xmalloc'd * Handle double quotes and ~. @@ -486,8 +488,9 @@ nothrow: * Params: * sink = send the path pieces here, end when sink() returns !=0 * path = the path to split up. + * ctx = pointer to context passed to sink */ - static void splitPath(int delegate(const(char)*) nothrow sink, const(char)* path) + static void splitPath(int function(const(char)*, void*) nothrow sink, const(char)* path, void* ctx) { if (!path) return; @@ -561,8 +564,8 @@ nothrow: } if (buf.length) // if path is not empty { - if (sink(buf.extractChars())) - break; + if (sink(buf.extractChars(), ctx)) + break; } } while (c); } @@ -712,31 +715,35 @@ nothrow: extern (D) static const(char)[] searchPath(const char* path, const char[] name, bool cwd) { if (absolute(name)) - { return exists(name) ? name : null; - } if (cwd) - { if (exists(name)) return name; - } if (path && *path) { const(char)[] result; - int sink(const(char)* p) nothrow + struct SinkContext + { + const(char)[] name; + const(char)[]* resultSlot; + } + SinkContext ctx = { name, &result }; + + static int sink(const(char)* p, void* vctx) nothrow { - auto n = combine(p.toDString, name); + auto ctx = cast(SinkContext*)vctx; + auto n = combine(p.toDString, ctx.name); mem.xfree(cast(void*)p); if (exists(n)) { - result = n; - return 1; // done with splitPath() call + *ctx.resultSlot = n; + return 1; } return 0; } - splitPath(&sink, path); + splitPath(&sink, path, &ctx); return result; } return null;