Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add -fdump-analysis to dump type information to json #3372

Merged
merged 2 commits into from
Oct 3, 2019
Merged

Conversation

andrewrk
Copy link
Member

@andrewrk andrewrk commented Oct 3, 2019

This commit adds -fdump-analysis which creates
a $NAME-analysis.json file with all of the finished
semantic analysis that the stage1 compiler produced.
It contains types, packages, declarations, and files.

This is an initial implementation; some data will be
missing. However it's easy to improve the implementation,
which is in src/dump_analysis.cpp.

The next step for #21 will be to create Zig code which parses
this json file and creates user-facing HTML documentation.

This feature has other uses, however; for example, it could
be used for IDE integration features until the self-hosted
compiler is available.

This commit adds -fdump-analysis which creates
a `$NAME-analysis.json` file with all of the finished
semantic analysis that the stage1 compiler produced.
It contains types, packages, declarations, and files.

This is an initial implementation; some data will be
missing. However it's easy to improve the implementation,
which is in `src/dump_analysis.cpp`.

The next step for #21 will be to create Zig code which parses
this json file and creates user-facing HTML documentation.

This feature has other uses, however; for example, it could
be used for IDE integration features until the self-hosted
compiler is available.
@andrewrk
Copy link
Member Author

andrewrk commented Oct 3, 2019

Here's the simplest example I could produce, if you want to see what the JSON looks like:

export fn entry() void {}

const builtin = @import("builtin");
pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
    while (true) {}
}
./zig build-obj test.zig -fdump-analysis
cat test-analysis.json
{
 "typeKinds": [
  "Type",
  "Void",
  "Bool",
  "NoReturn",
  "Int",
  "Float",
  "Pointer",
  "Array",
  "Struct",
  "ComptimeFloat",
  "ComptimeInt",
  "Undefined",
  "Null",
  "Optional",
  "ErrorUnion",
  "ErrorSet",
  "Enum",
  "Union",
  "Fn",
  "BoundFn",
  "ArgTuple",
  "Opaque",
  "Frame",
  "AnyFrame",
  "Vector",
  "EnumLiteral"
 ],
 "params": {
  "zigId": "7qNQjBOgnEoYwB2CHOOrV7SvVwTsUtbVBO1AYpgcSpIsdFryNMtbLkIxZYgzZuks",
  "zigVersion": "0.5.0+7640bec8",
  "target": "x86_64-linux-gnu"
 },
 "rootPkg": 0,
 "packages": [
  {
   "name": "",
   "file": 0,
   "main": 0,
   "table": {
    "builtin": 1,
    "std": 2,
    "root": 0
   }
  },
  {
   "name": "builtin",
   "file": 1,
   "main": 1,
   "table": {}
  }
 ],
 "types": [
  {
   "name": "",
   "kind": 8,
   "decls": [
    0,
    1,
    2
   ],
   "file": 0
  },
  {
   "name": "builtin",
   "kind": 8,
   "decls": [
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12,
    13,
    14,
    15,
    16,
    17,
    18,
    19,
    20,
    21,
    22,
    23,
    24,
    25,
    26,
    27,
    28,
    29,
    30,
    31,
    32
   ],
   "file": 1
  },
  {
   "name": "type",
   "kind": 0
  },
  {
   "name": "extern fn() void",
   "kind": 18
  },
  {
   "name": "fn([]const u8, ?*builtin.StackTrace) noreturn",
   "kind": 18
  },
  {
   "name": "builtin.StackTrace",
   "kind": 8,
   "decls": []
  }
 ],
 "decls": [
  {
   "import": 0,
   "line": 2,
   "col": 0,
   "name": "builtin",
   "kind": "const",
   "type": 2,
   "value": 1
  },
  {
   "import": 0,
   "line": 0,
   "col": 0,
   "name": "entry",
   "kind": "const",
   "type": 3
  },
  {
   "import": 0,
   "line": 3,
   "col": 4,
   "name": "panic",
   "kind": "const",
   "type": 4
  },
  {
   "import": 1,
   "line": 193,
   "col": 4,
   "name": "AtomicRmwOp"
  },
  {
   "import": 1,
   "line": 461,
   "col": 4,
   "name": "position_independent_code"
  },
  {
   "import": 1,
   "line": 458,
   "col": 4,
   "name": "link_libc"
  },
  {
   "import": 1,
   "line": 456,
   "col": 4,
   "name": "object_format"
  },
  {
   "import": 1,
   "line": 453,
   "col": 4,
   "name": "arch"
  },
  {
   "import": 1,
   "line": 422,
   "col": 4,
   "name": "FloatMode"
  },
  {
   "import": 1,
   "line": 449,
   "col": 4,
   "name": "endian"
  },
  {
   "import": 1,
   "line": 457,
   "col": 4,
   "name": "mode"
  },
  {
   "import": 1,
   "line": 452,
   "col": 4,
   "name": "os"
  },
  {
   "import": 1,
   "line": 212,
   "col": 4,
   "name": "TypeId"
  },
  {
   "import": 1,
   "line": 184,
   "col": 4,
   "name": "AtomicOrder"
  },
  {
   "import": 1,
   "line": 169,
   "col": 4,
   "name": "ObjectFormat"
  },
  {
   "import": 1,
   "line": 454,
   "col": 4,
   "name": "abi"
  },
  {
   "import": 1,
   "line": 0,
   "col": 4,
   "name": "StackTrace",
   "kind": "const",
   "type": 2,
   "value": 5
  },
  {
   "import": 1,
   "line": 450,
   "col": 4,
   "name": "is_test"
  },
  {
   "import": 1,
   "line": 5,
   "col": 4,
   "name": "PanicFn",
   "kind": "const",
   "type": 2,
   "value": 4
  },
  {
   "import": 1,
   "line": 47,
   "col": 4,
   "name": "Arch"
  },
  {
   "import": 1,
   "line": 455,
   "col": 4,
   "name": "glibc_version"
  },
  {
   "import": 1,
   "line": 460,
   "col": 4,
   "name": "valgrind_support"
  },
  {
   "import": 1,
   "line": 438,
   "col": 4,
   "name": "SubSystem"
  },
  {
   "import": 1,
   "line": 205,
   "col": 4,
   "name": "Mode"
  },
  {
   "import": 1,
   "line": 427,
   "col": 4,
   "name": "Endian"
  },
  {
   "import": 1,
   "line": 462,
   "col": 4,
   "name": "strip_debug_info"
  },
  {
   "import": 1,
   "line": 7,
   "col": 4,
   "name": "Os"
  },
  {
   "import": 1,
   "line": 432,
   "col": 4,
   "name": "Version"
  },
  {
   "import": 1,
   "line": 451,
   "col": 4,
   "name": "single_threaded"
  },
  {
   "import": 1,
   "line": 177,
   "col": 4,
   "name": "GlobalLinkage"
  },
  {
   "import": 1,
   "line": 144,
   "col": 4,
   "name": "Abi"
  },
  {
   "import": 1,
   "line": 459,
   "col": 4,
   "name": "have_error_return_tracing"
  },
  {
   "import": 1,
   "line": 241,
   "col": 4,
   "name": "TypeInfo"
  }
 ],
 "files": [
  "/home/andy/dev/zig/build/test.zig",
  "/home/andy/.local/share/zig/stage1/builtin/Mb2TI_lUZey68IlsL1U9nGQ-DfP4mc41hhGstc4Tfls8HIKfojclFH2NTajGhj_8/builtin.zig"
 ]
}

I think this is already enough to start to make progress on #21.

@andrewrk
Copy link
Member Author

andrewrk commented Oct 3, 2019

My eventual hope is that the self-hosted code could merge multiple of these json files in a sort of "multibuilds" fashion (#3028) but even just processing 1 of them into HTML docs would be huge.

@andrewrk
Copy link
Member Author

andrewrk commented Oct 3, 2019

Tests pass locally.

@andrewrk andrewrk merged commit eca2aa6 into master Oct 3, 2019
@andrewrk andrewrk deleted the dump-analysis branch October 3, 2019 22:37
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant