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 30, 2018
1 parent 2efef2b commit 41aaf97
Show file tree
Hide file tree
Showing 8 changed files with 1,212 additions and 940 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 @@ -4282,6 +4282,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
65 changes: 65 additions & 0 deletions src/dmd/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ import dmd.mtype;
import dmd.root.outbuffer;
import dmd.visitor;

version(Windows) {
extern (C) char* getcwd(char* buffer, size_t maxlen);
} else {
import core.sys.posix.unistd : getcwd;
}

private extern (C++) final class ToJsonVisitor : Visitor
{
alias visit = Visitor.visit;
Expand Down Expand Up @@ -789,19 +795,78 @@ public:
jsonProperties(d);
objectEnd();
}

private void generateCompilerInfo()
{
objectStart();
property("kind", "compilerInfo");
property("binary", global.params.argv0);
property("version", global._version);
propertyBool("supportsIncludeImports", true);
objectEnd();
}
private void generateBuildInfo()
{
objectStart();
property("kind", "buildInfo");
property("cwd", getcwd(null, 0));
property("config", global.inifilename ? global.inifilename : null);
if (global.params.lib) {
property("library", global.params.libname);
}
propertyStart("importPaths");
arrayStart();
foreach (importPath; *global.params.imppath)
{
item(importPath);
}
arrayEnd();
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)
{
item(file);
}
arrayEnd();
}
objectEnd();
}
arrayEnd();
objectEnd();
}
}

extern (C++) void json_generate(OutBuffer* buf, Modules* modules)
{
scope ToJsonVisitor json = new ToJsonVisitor(buf);
json.arrayStart();
json.generateCompilerInfo();
json.generateBuildInfo();
for (size_t i = 0; i < modules.dim; i++)
{
Module m = (*modules)[i];
if (global.params.verbose)
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
27 changes: 18 additions & 9 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,26 @@ fail_compilation_test_results=$(addsuffix .out,$(addprefix $(RESULTS_DIR)/,$(fai

all: run_tests

$(RESULTS_DIR)/runnable/%.d.out: runnable/%.d $(RESULTS_DIR)/.created $(RESULTS_DIR)/d_do_test$(EXE) $(DMD)
test_tools: $(RESULTS_DIR)/d_do_test$(EXE) $(RESULTS_DIR)/sanitize_json$(EXE)

$(RESULTS_DIR)/runnable/%.d.out: runnable/%.d $(RESULTS_DIR)/.created test_tools $(DMD)
$(QUIET) $(RESULTS_DIR)/d_do_test $(<D) $* d

$(RESULTS_DIR)/runnable/%.sh.out: runnable/%.sh $(RESULTS_DIR)/.created $(RESULTS_DIR)/d_do_test$(EXE) $(DMD)
$(RESULTS_DIR)/runnable/%.sh.out: runnable/%.sh $(RESULTS_DIR)/.created test_tools $(DMD)
$(QUIET) echo " ... $(<D)/$*.sh"
$(QUIET) ./$(<D)/$*.sh

$(RESULTS_DIR)/compilable/%.d.out: compilable/%.d $(RESULTS_DIR)/.created $(RESULTS_DIR)/d_do_test$(EXE) $(DMD)
$(RESULTS_DIR)/compilable/%.d.out: compilable/%.d $(RESULTS_DIR)/.created test_tools $(DMD)
$(QUIET) $(RESULTS_DIR)/d_do_test $(<D) $* d

$(RESULTS_DIR)/compilable/%.sh.out: compilable/%.sh $(RESULTS_DIR)/.created $(RESULTS_DIR)/d_do_test$(EXE) $(DMD)
$(RESULTS_DIR)/compilable/%.sh.out: compilable/%.sh $(RESULTS_DIR)/.created test_tools $(DMD)
$(QUIET) echo " ... $(<D)/$*.sh"
$(QUIET) ./$(<D)/$*.sh

$(RESULTS_DIR)/fail_compilation/%.d.out: fail_compilation/%.d $(RESULTS_DIR)/.created $(RESULTS_DIR)/d_do_test$(EXE) $(DMD)
$(RESULTS_DIR)/fail_compilation/%.d.out: fail_compilation/%.d $(RESULTS_DIR)/.created test_tools $(DMD)
$(QUIET) $(RESULTS_DIR)/d_do_test $(<D) $* d

$(RESULTS_DIR)/fail_compilation/%.html.out: fail_compilation/%.html $(RESULTS_DIR)/.created $(RESULTS_DIR)/d_do_test$(EXE) $(DMD)
$(RESULTS_DIR)/fail_compilation/%.html.out: fail_compilation/%.html $(RESULTS_DIR)/.created test_tools $(DMD)
$(QUIET) $(RESULTS_DIR)/d_do_test $(<D) $* html

quick:
Expand All @@ -242,19 +244,19 @@ run_tests: start_runnable_tests start_compilable_tests start_fail_compilation_te

run_runnable_tests: $(runnable_test_results)

start_runnable_tests: $(RESULTS_DIR)/.created $(RESULTS_DIR)/d_do_test$(EXE)
start_runnable_tests: $(RESULTS_DIR)/.created test_tools
@echo "Running runnable tests"
$(QUIET)$(MAKE) --no-print-directory run_runnable_tests

run_compilable_tests: $(compilable_test_results)

start_compilable_tests: $(RESULTS_DIR)/.created $(RESULTS_DIR)/d_do_test$(EXE)
start_compilable_tests: $(RESULTS_DIR)/.created test_tools
@echo "Running compilable tests"
$(QUIET)$(MAKE) --no-print-directory run_compilable_tests

run_fail_compilation_tests: $(fail_compilation_test_results)

start_fail_compilation_tests: $(RESULTS_DIR)/.created $(RESULTS_DIR)/d_do_test$(EXE)
start_fail_compilation_tests: $(RESULTS_DIR)/.created test_tools
@echo "Running fail compilation tests"
$(QUIET)$(MAKE) --no-print-directory run_fail_compilation_tests

Expand All @@ -266,3 +268,10 @@ $(RESULTS_DIR)/d_do_test$(EXE): d_do_test.d $(RESULTS_DIR)/.created
$(DMD) -conf= $(MODEL_FLAG) $(DEBUG_FLAGS) -unittest -run d_do_test.d -unittest
$(DMD) -conf= $(MODEL_FLAG) $(DEBUG_FLAGS) -od$(RESULTS_DIR) -of$(RESULTS_DIR)$(DSEP)d_do_test$(EXE) d_do_test.d

$(RESULTS_DIR)/sanitize_json$(EXE): sanitize_json.d $(RESULTS_DIR)/.created
@echo "Building sanitize_json tool"
@echo "OS: '$(OS)'"
@echo "MODEL: '$(MODEL)'"
@echo "PIC: '$(PIC_FLAG)'"
$(DMD) -conf= $(MODEL_FLAG) $(DEBUG_FLAGS) -od$(RESULTS_DIR) -of$(RESULTS_DIR)$(DSEP)sanitize_json$(EXE) -i sanitize_json.d

11 changes: 7 additions & 4 deletions test/compilable/extra-files/json-postscript.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env bash

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
echo SANITIZING JSON...
${RESULTS_DIR}/sanitize_json ${RESULTS_DIR}/compilable/json.out > ${RESULTS_DIR}/compilable/json.out.sanitized
if [ $? -ne 0 ]; then
exit 1;
fi

diff --strip-trailing-cr ${RESULTS_DIR}/compilable/json.out.2 ${RESULTS_DIR}/compilable/json.out.3
diff --strip-trailing-cr compilable/extra-files/json.out ${RESULTS_DIR}/compilable/json.out.sanitized
if [ $? -ne 0 ]; then
exit 1;
fi

rm ${RESULTS_DIR}/compilable/json.out{.2,.3}
rm ${RESULTS_DIR}/compilable/json.out.sanitized
Loading

0 comments on commit 41aaf97

Please sign in to comment.