Skip to content

Commit

Permalink
Extend JSON format to include relevant information for tools like rdmd.
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Jan 20, 2018
1 parent 1b805ae commit 9a297d6
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ extern (C++) final class Module : Package
uint numlines; // number of lines in source file
int isDocFile; // if it is a documentation input file, not D source
bool isPackageFile; // if it is a package.d
Strings contentImportedFiles; // array of files whose content was imported
int needmoduleinfo;
/**
How many unit tests have been seen so far in this module. Makes it so the
Expand Down
1 change: 1 addition & 0 deletions src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -4275,6 +4275,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
return setError();
}

sc._module.contentImportedFiles.push(name);
if (global.params.verbose)
fprintf(global.stdmsg, "file %.*s\t(%s)\n", cast(int)se.len, se.string, name);
if (global.params.moduleDeps !is null)
Expand Down
30 changes: 30 additions & 0 deletions src/dmd/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,35 @@ public:
jsonProperties(d);
objectEnd();
}

private void generateSemantics()
{
objectStart();
property("kind", "semantics");
propertyStart("modules");
arrayStart();
foreach (m; Module.amodules)
{
objectStart();
if(m.md)
property("name", m.md.toChars());
property("file", m.srcfile.toChars());
propertyBool("isRoot", m.isRoot());
if(m.contentImportedFiles.dim > 0)
{
propertyStart("contentImports");
arrayStart();
foreach (file; m.contentImportedFiles)
{
value(file);
}
arrayEnd();
}
objectEnd();
}
arrayEnd();
objectEnd();
}
}

extern (C++) void json_generate(OutBuffer* buf, Modules* modules)
Expand All @@ -806,6 +835,7 @@ extern (C++) void json_generate(OutBuffer* buf, Modules* modules)
fprintf(global.stdmsg, "json gen %s\n", m.toChars());
m.accept(json);
}
json.generateSemantics();
json.arrayEnd();
json.removeComma();
}
1 change: 1 addition & 0 deletions src/dmd/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Module : public Package
unsigned numlines; // number of lines in source file
int isDocFile; // if it is a documentation input file, not D source
bool isPackageFile; // if it is a package.d
Strings contentImportedFiles; // array of files whose content was imported
int needmoduleinfo;
/**
How many unit tests have been seen so far in this module. Makes it so the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[
import std.stdio : write;
void main()
{
write(
`[
{
"name" : "json",
"kind" : "module",
Expand Down Expand Up @@ -926,5 +930,51 @@
"deco" : "YZi"
}
]
},
{
"kind" : "semantics",
"modules" : [
{
"name" : "json",
"file" : "compilable/json.d",
"isRoot" : true
},
{
"name" : "imports.jsonimport1",
"file" : "compilable/imports/jsonimport1.d",
"isRoot" : false
},
{
"name" : "object",
"file" : "../../druntime/import/object.d",
"isRoot" : false
},`);
version(OSX) version(X86_64)
{
write(`
{
"name" : "core.attribute",
"file" : "../../druntime/import/core/attribute.d",
"isRoot" : false
},`);
}
write(`
{
"name" : "imports.jsonimport2",
"file" : "compilable/imports/jsonimport2.d",
"isRoot" : false
},
{
"name" : "imports.jsonimport3",
"file" : "compilable/imports/jsonimport3.d",
"isRoot" : false
},
{
"name" : "imports.jsonimport4",
"file" : "compilable/imports/jsonimport4.d",
"isRoot" : false
}
]
}
]
]`);
}
11 changes: 11 additions & 0 deletions test/compilable/extra-files/json-postscript.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#!/usr/bin/env bash


echo GENERATING compilable/extra-files/json.out
set +x
${DMD} -m${MODEL} -run compilable/extra-files/gen_json_out.d > compilable/extra-files/json.out
if [ $? -ne 0 ]; then
cat compilable/extra-files/json.out
exit 1;
fi


grep -v "\"file\" : " ${RESULTS_DIR}/compilable/json.out | grep -v "\"offset\" : " | grep -v "\"deco\" : " > ${RESULTS_DIR}/compilable/json.out.2
grep -v "\"file\" : " compilable/extra-files/json.out | grep -v "\"offset\" : " | grep -v "\"deco\" : " > ${RESULTS_DIR}/compilable/json.out.3

Expand All @@ -8,4 +18,5 @@ if [ $? -ne 0 ]; then
exit 1;
fi

rm compilable/extra-files/json.out
rm ${RESULTS_DIR}/compilable/json.out{.2,.3}

0 comments on commit 9a297d6

Please sign in to comment.