Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/dmd/globals.d
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,44 @@ struct Global
_version = (import("VERSION") ~ '\0').ptr;
compiler.vendor = "Digital Mars D";
}

/**
Returns: the version as the number that would be returned for __VERSION__
*/
extern(C++) uint versionNumber()
Copy link
Member

Choose a reason for hiding this comment

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

This code duplicates code in the lexer https://github.com/dlang/dmd/blob/master/src/dmd/lexer.d#L518

In fact, it's a cut&paste of it. It should take an argument instead of relying on a global, and moved to another module (lexer.d?).

Copy link
Contributor Author

@marler8997 marler8997 Feb 20, 2018

Choose a reason for hiding this comment

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

As you can see later on in this PR, the code was removed from lexer.d and replaced with a call to this function. I could move this function to lexer.d and have json import lexer (instead of having json and lexer share the implementation from global). Would you prefer that? Although, I'm not sure if json importing lexer would cause a cyclic dependency issue...

Copy link
Contributor Author

@marler8997 marler8997 Feb 20, 2018

Choose a reason for hiding this comment

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

Yes it could take an argument, but then the value won't be cached. This current implementation allows the value to be cached in case __VERSION__ is used multiple times along with outputing the __VERSION__ json value. Plus, there's currently no other caller who would pass something other than global.params._version.

Copy link
Member

Choose a reason for hiding this comment

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

I see you are correct. I missed that. I with draw both suggestions.

{
import core.stdc.ctype;
__gshared static uint cached = 0;
if (cached == 0)
{
//
// parse _version
//
uint major = 0;
uint minor = 0;
bool point = false;
for (const(char)* p = _version + 1;; p++)
{
const c = *p;
if (isdigit(cast(char)c))
{
minor = minor * 10 + c - '0';
}
else if (c == '.')
{
if (point)
break; // ignore everything after second '.'
point = true;
major = minor;
minor = 0;
}
else
break;
}
cached = major * 1000 + minor;
}
return cached;
}
}

// Because int64_t and friends may be any integral type of the
Expand Down
5 changes: 5 additions & 0 deletions src/dmd/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ struct Global
void increaseErrorCount();

void _init();

/**
Returns: the version as the number that would be returned for __VERSION__
*/
unsigned versionNumber();
};

extern Global global;
Expand Down
149 changes: 137 additions & 12 deletions src/dmd/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ public:
buf.writestring(" : ");
}

/**
Write the given string object property only if `s` is not null.

Params:
name = the name of the object property
s = the string value of the object property
*/
void property(const(char)* name, const(char)* s)
{
if (s is null)
Expand All @@ -237,6 +244,23 @@ public:
comma();
}

/**
Write the given string object property.

Params:
name = the name of the object property
s = the string value of the object property
*/
void requiredProperty(const(char)* name, const(char)* s)
{
propertyStart(name);
if (s is null)
buf.writestring("null");
else
value(s);
comma();
}

void property(const(char)* name, int i)
{
propertyStart(name);
Expand Down Expand Up @@ -826,8 +850,61 @@ public:
private void generateCompilerInfo()
{
objectStart();
property("binary", global.params.argv0);
property("version", global._version);
requiredProperty("vendor", global.compiler.vendor);
requiredProperty("version", global._version);
property("__VERSION__", global.versionNumber());
requiredProperty("interface", determineCompilerInterface());
property("size_t", size_t.sizeof);
propertyStart("platforms");
arrayStart();
if (global.params.isWindows)
{
item("windows");
}
else
{
item("posix");
if (global.params.isLinux)
item("linux");
else if (global.params.isOSX)
item("osx");
else if (global.params.isFreeBSD)
{
item("freebsd");
item("bsd");
}
else if (global.params.isOpenBSD)
{
item("openbsd");
item("bsd");
}
else if (global.params.isSolaris)
{
item("solaris");
item("bsd");
}
}
arrayEnd();

propertyStart("architectures");
arrayStart();
if (global.params.is64bit)
item("x86_64");
else
version(X86) item("x86");
arrayEnd();

propertyStart("predefinedVersions");
arrayStart();
if (global.versionids)
{
foreach (const versionid; *global.versionids)
{
item(versionid.toChars());
}
}
arrayEnd();

propertyBool("supportsIncludeImports", true);
objectEnd();
}
Expand All @@ -839,18 +916,50 @@ public:
private void generateBuildInfo()
{
objectStart();
property("cwd", getcwd(null, 0));
property("config", global.inifilename ? global.inifilename : null);
if (global.params.lib) {
property("library", global.params.libname);
}
requiredProperty("cwd", getcwd(null, 0));
requiredProperty("argv0", global.params.argv0);
requiredProperty("config", global.inifilename);
requiredProperty("libName", global.params.libname);

propertyStart("importPaths");
arrayStart();
foreach (importPath; *global.params.imppath)
if (global.params.imppath)
{
item(importPath);
foreach (importPath; *global.params.imppath)
{
item(importPath);
}
}
arrayEnd();

propertyStart("objectFiles");
arrayStart();
foreach (objfile; global.params.objfiles)
{
item(objfile);
}
arrayEnd();

propertyStart("libraryFiles");
arrayStart();
foreach (lib; global.params.libfiles)
{
item(lib);
}
arrayEnd();

propertyStart("ddocFiles");
arrayStart();
foreach (ddocFile; global.params.ddocfiles)
{
item(ddocFile);
}
arrayEnd();

requiredProperty("mapFile", global.params.mapfile);
requiredProperty("resourceFile", global.params.resfile);
requiredProperty("defFile", global.params.deffile);

objectEnd();
}

Expand All @@ -867,9 +976,8 @@ public:
foreach (m; Module.amodules)
{
objectStart();
if(m.md)
property("name", m.md.toChars());
property("file", m.srcfile.toChars());
requiredProperty("name", m.md ? m.md.toChars() : null);
requiredProperty("file", m.srcfile.toChars());
propertyBool("isRoot", m.isRoot());
if(m.contentImportedFiles.dim > 0)
{
Expand Down Expand Up @@ -968,3 +1076,20 @@ JsonFieldFlags tryParseJsonField(const(char)* fieldName)
}
return JsonFieldFlags.none;
}

/**
Determines and returns the compiler interface which is one of `dmd`, `ldc`,
`gdc` or `sdc`. Returns `null` if no interface can be determined.
*/
private const(char)* determineCompilerInterface()
{
if (!strcmp(global.compiler.vendor, "Digital Mars D"))
return "dmd";
if (!strcmp(global.compiler.vendor, "LDC"))
return "ldc";
if (!strcmp(global.compiler.vendor, "GNU"))
return "gdc";
if (!strcmp(global.compiler.vendor, "SDC"))
return "sdc";
return null;
}
21 changes: 1 addition & 20 deletions src/dmd/lexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -517,27 +517,8 @@ class Lexer
}
else if (id == Id.VERSIONX)
{
uint major = 0;
uint minor = 0;
bool point = false;
for (const(char)* p = global._version + 1; 1; p++)
{
const c = *p;
if (isdigit(cast(char)c))
minor = minor * 10 + c - '0';
else if (c == '.')
{
if (point)
break; // ignore everything after second '.'
point = true;
major = minor;
minor = 0;
}
else
break;
}
t.value = TOK.int64Literal;
t.unsvalue = major * 1000 + minor;
t.unsvalue = global.versionNumber();
}
else if (id == Id.EOFX)
{
Expand Down
31 changes: 28 additions & 3 deletions test/compilable/extra-files/json2.out
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
{
"buildInfo": {
"argv0": "VALUE_REMOVED_FOR_TEST",
"config": "VALUE_REMOVED_FOR_TEST",
"cwd": "VALUE_REMOVED_FOR_TEST",
"ddocFiles": [],
"defFile": null,
"importPaths": [
"compilable",
"../../druntime/import",
"../../phobos"
]
],
"libName": "VALUE_REMOVED_FOR_TEST",
"libraryFiles": [
"VALUES_REMOVED_FOR_TEST"
],
"mapFile": "VALUE_REMOVED_FOR_TEST",
"objectFiles": [
"VALUES_REMOVED_FOR_TEST"
],
"resourceFile": null
},
"compilerInfo": {
"binary": "VALUE_REMOVED_FOR_TEST",
"__VERSION__": 0,
"architectures": [
"VALUES_REMOVED_FOR_TEST"
],
"interface": "dmd",
"platforms": [
"VALUES_REMOVED_FOR_TEST"
],
"predefinedVersions": [
"VALUES_REMOVED_FOR_TEST"
],
"size_t": 0,
"supportsIncludeImports": true,
"vendor": "VALUE_REMOVED_FOR_TEST",
"version": "VALUE_REMOVED_FOR_TEST"
},
"modules": [
Expand All @@ -32,7 +56,8 @@
"modules": [
{
"file": "compilable/json2.d",
"isRoot": true
"isRoot": true,
"name": null
},
{
"file": "compilable/json.d",
Expand Down
14 changes: 13 additions & 1 deletion test/compilable/extra-files/jsonCompilerInfo.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
{
"compilerInfo": {
"binary": "VALUE_REMOVED_FOR_TEST",
"__VERSION__": 0,
"architectures": [
"VALUES_REMOVED_FOR_TEST"
],
"interface": "dmd",
"platforms": [
"VALUES_REMOVED_FOR_TEST"
],
"predefinedVersions": [
"VALUES_REMOVED_FOR_TEST"
],
"size_t": 0,
"supportsIncludeImports": true,
"vendor": "VALUE_REMOVED_FOR_TEST",
"version": "VALUE_REMOVED_FOR_TEST"
}
}
14 changes: 13 additions & 1 deletion test/compilable/extra-files/json_nosource.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
{
"compilerInfo": {
"binary": "VALUE_REMOVED_FOR_TEST",
"__VERSION__": 0,
"architectures": [
"VALUES_REMOVED_FOR_TEST"
],
"interface": "dmd",
"platforms": [
"VALUES_REMOVED_FOR_TEST"
],
"predefinedVersions": [
"VALUES_REMOVED_FOR_TEST"
],
"size_t": 0,
"supportsIncludeImports": true,
"vendor": "VALUE_REMOVED_FOR_TEST",
"version": "VALUE_REMOVED_FOR_TEST"
}
}
Loading