Skip to content

Turn globals.argv0 into a DArray#8575

Merged
dlang-bot merged 14 commits intodlang:masterfrom
Geod24:argv0-slice
Sep 6, 2018
Merged

Turn globals.argv0 into a DArray#8575
dlang-bot merged 14 commits intodlang:masterfrom
Geod24:argv0-slice

Conversation

@Geod24
Copy link
Member

@Geod24 Geod24 commented Aug 18, 2018

This is a step towards converting globals to use D array.

There is currently no way to call a function accepting a D string from C++ (thanks to ABI issues on i386 / Win64) but it does not matter for globals AFAICS.
So I resorted to providing trivial C++ wrapper to the D functions, and gradually removing the wrapper as they become unused, as can be seen in filename.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @Geod24! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#8575"

src/dmd/mars.d Outdated
extern(C) void printGlobalConfigs(FILE* stream)
{
stream.fprintf("binary %s\n", global.params.argv0);
stream.fprintf("binary %*s\n", global.params.argv0.length, global.params.argv0.ptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%.*s - if memory serves me right.

@Geod24 Geod24 force-pushed the argv0-slice branch 2 times, most recently from da80538 to 71e023f Compare August 18, 2018 06:53
Copy link
Member

@ibuclaw ibuclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is github loosing my reviews?

@ibuclaw
Copy link
Member

ibuclaw commented Aug 18, 2018

Is github loosing my reviews?

Answer, yes. This is twice now I've written something and it's gone.

@Geod24 Geod24 force-pushed the argv0-slice branch 3 times, most recently from eef083e to dcd7960 Compare August 18, 2018 07:14
@JinShil
Copy link
Contributor

JinShil commented Aug 18, 2018

Is github loosing my reviews?

I experienced that yesterday as well; started questioning my sanity.

import std.string : fromStringz, toStringz;

auto f = findConfFile(dmdFilePath.toStringz, "dmd.conf");
auto f = findConfFile(dmdFilePath.toStringz[0 .. dmdFilePath.length], "dmd.conf");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is converted to a C string then back to a D string. Am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a leftover. findConfFile was still dependent on \0 termination, as one of the function was not converted to const(char)[]. I converted it and now \0 termination isn't required anymore.

@Geod24 Geod24 force-pushed the argv0-slice branch 6 times, most recently from 15a277c to 13a29aa Compare August 20, 2018 00:16
@Geod24 Geod24 removed the Review:WIP Work In Progress - not ready for review or pulling label Sep 5, 2018
@Geod24
Copy link
Member Author

Geod24 commented Sep 5, 2018

This finally passes and is ready for review

return null;

return f.fromStringz.idup;
return f.idup;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can idup a string that is null, which will save you the if statement.

Copy link
Member Author

@Geod24 Geod24 Sep 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff --git a/src/dmd/frontend.d b/src/dmd/frontend.d
index 93c5a8a94..9ae64a51a 100644
--- a/src/dmd/frontend.d
+++ b/src/dmd/frontend.d
@@ -79,11 +79,7 @@ string findDMDConfig(const(char)[] dmdFilePath)
     import dmd.dinifile : findConfFile;
     import std.string : fromStringz, toStringz;

-    auto f = findConfFile(dmdFilePath, "dmd.conf");
-    if (f is null)
-        return null;
-
-    return f.idup;
+    return findConfFile(dmdFilePath, "dmd.conf").idup;
 }

 /**

Updated thanks

Strings* paths = FileName.splitPath(getenv("PATH"));
if (auto p = FileName.searchPath(paths, "link.exe", false))
return p;
if (auto p = FileName.searchPath(paths, "link.exe"[], false))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this slice needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise the call is ambiguous, since we have a const(char)* and a const(char)[] overload, and manifest constant convert to both.
In a later PR (because this is already quite big) I'll remove the const(char)* overload

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok.

This is more of a proof of concept for future conversion.

Here's a few principles used to do the conversion from `T*` to `T[]`:
- If it's a string, the character at `s[$]` must be `'\0'`.
  It allows code to freely go back and forth between `char*` and `char[]`.
- Move the implementation to extern(D) and slices, and provide C++ wrapper.
- Stay on topic: those changes only focus on one parameter / module / function,
  because it's very easy to end up with a giant change that doesn't work.
- If `'\0'` termination is required (e.g. calling a C function),
  changing from `char*` to `char[]` should can be done using `toCStringThen`.
}

/// Ditto
extern (D) static int exists(const(char)[] name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably return an enum instead of an int, but that might be something for a separate PR.

{
import core.sys.posix.stdlib: realpath, free;
auto absPath = realpath(fileName, null /* realpath allocates */);
char* absPath = fileName.toCStringThen!((fn) => realpath(&fn[0], null /* realpath allocates */));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is freed later, so I'd have to cast away the const.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my bad.

@dlang-bot dlang-bot merged commit 61fe641 into dlang:master Sep 6, 2018
@Geod24 Geod24 deleted the argv0-slice branch September 6, 2018 22:30
@ghost
Copy link

ghost commented Dec 25, 2018

This causes a regression : https://issues.dlang.org/show_bug.cgi?id=19510.

@Geod24
Copy link
Member Author

Geod24 commented Dec 25, 2018

I'll look at it

EDIT: Or not, since you submitted #9142

@ghost
Copy link

ghost commented Dec 25, 2018

BTW, sorry since i discovered that your not responsible for the regression. It's more a Windows API call that was not used correctly... the problem was only revealed by your changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants