| 1 | # spinel-doctor: one health report for a spinel program. Each leg is
|
| 2 | # independent; the behavior leg is skipped cleanly when CRuby is absent.
|
| 3 | #
|
| 4 | # Usage: spinel-doctor [--only a,b] [--skip a,b] [--behavior] [--quiet] app.rb
|
| 5 | #
|
| 6 | # Legs:
|
| 7 | # build compile to a binary; report compile / codegen / C-build failure
|
| 8 | # unsupported codegen gaps that degrade to a stub (stderr "unsupported ...")
|
| 9 | # unresolved calls that silently degrade to nil/0 (SPINEL_WARN_UNRESOLVED)
|
| 10 | # inference methods spinel widened to untyped (the boxed poly slow path)
|
| 11 | # requires non-relative `require`s spinel treats as native / no-op
|
| 12 | # behavior (opt) compiled output vs CRuby; needs `ruby` on PATH
|
| 13 |
|
| 14 | require_relative "tool_common"
|
| 15 |
|
| 16 | # Does this leg run, given --only / --skip?
|
| 17 | def leg_on(name, only, skip)
|
| 18 | return false if skip.include?(name)
|
| 19 | return true if only.length == 0
|
| 20 | only.include?(name)
|
| 21 | end
|
| 22 |
|
| 23 | # Print a leg's header and its finding lines. Returns the finding count.
|
| 24 | def report(name, sev, lines, quiet)
|
| 25 | if lines.length == 0
|
| 26 | puts " [ok] " + name if !quiet
|
| 27 | return 0
|
| 28 | end
|
| 29 | mark = "[warn]"
|
| 30 | mark = "[ERR] " if sev == "error"
|
| 31 | mark = "[info]" if sev == "info"
|
| 32 | puts mark + " " + name + " (" + lines.length.to_s + ")"
|
| 33 | lines.each { |l| puts " " + l }
|
| 34 | lines.length
|
| 35 | end
|
| 36 |
|
| 37 | # Keep only the interesting diagnostic lines from a compile run.
|
| 38 | def grep_kind(out, needle)
|
| 39 | hits = []
|
| 40 | out.split("\n").each { |l|
|
| 41 | hits.push(l.strip) if l.include?(needle)
|
| 42 | }
|
| 43 | hits
|
| 44 | end
|
| 45 |
|
| 46 | def main
|
| 47 | only = []
|
| 48 | skip = []
|
| 49 | want_behavior = false
|
| 50 | quiet = false
|
| 51 | src = ""
|
| 52 | i = 0
|
| 53 | while i < ARGV.length
|
| 54 | a = ARGV[i]
|
| 55 | if a == "--only"
|
| 56 | i = i + 1
|
| 57 | only = ARGV[i].split(",") if i < ARGV.length
|
| 58 | elsif a == "--skip"
|
| 59 | i = i + 1
|
| 60 | skip = ARGV[i].split(",") if i < ARGV.length
|
| 61 | elsif a == "--behavior"
|
| 62 | want_behavior = true
|
| 63 | elsif a == "--quiet"
|
| 64 | quiet = true
|
| 65 | elsif a == "-h" || a == "--help"
|
| 66 | puts "usage: spinel-doctor [--only a,b] [--skip a,b] [--behavior] [--quiet] app.rb"
|
| 67 | puts " legs: build unsupported unresolved inference requires behavior"
|
| 68 | exit(0)
|
| 69 | else
|
| 70 | src = a
|
| 71 | end
|
| 72 | i = i + 1
|
| 73 | end
|
| 74 | die("usage: spinel-doctor [options] app.rb", 2) if src.length == 0
|
| 75 | die("spinel-doctor: no such file: " + src, 2) if !File.exist?(src)
|
| 76 |
|
| 77 | sp = find_spinel
|
| 78 | cobj = tmp_path("doctor", src, ".c")
|
| 79 | bin = tmp_path("doctor", src, ".bin")
|
| 80 | errs = 0
|
| 81 |
|
| 82 | puts "spinel-doctor: " + src
|
| 83 |
|
| 84 | # One codegen-only run with unresolved warnings on feeds three legs.
|
| 85 | diag = sh("SPINEL_WARN_UNRESOLVED=1 " + sp + " " + src + " -c -o " + cobj)
|
| 86 | cg_ok = $sh_status == 0
|
| 87 |
|
| 88 | if leg_on("unsupported", only, skip)
|
| 89 | errs = errs + report("unsupported", "error", grep_kind(diag, "unsupported "), quiet)
|
| 90 | end
|
| 91 | if leg_on("unresolved", only, skip)
|
| 92 | errs = errs + report("unresolved", "warn", grep_kind(diag, "warning: unresolved"), quiet)
|
| 93 | end
|
| 94 |
|
| 95 | if leg_on("inference", only, skip)
|
| 96 | # Methods whose signature spinel widened to `untyped` -- the boxed poly
|
| 97 | # slow path. Each such site is a "# spinel: widened to untyped (slow path)"
|
| 98 | # comment in --emit-rbs. Not a defect (the program is correct), so it is
|
| 99 | # info severity and does not affect the exit code; it surfaces where
|
| 100 | # inference could not pin a concrete type (a perf cost, and sometimes the
|
| 101 | # first sign of a latent type gap worth a closer look).
|
| 102 | rbs = tmp_path("doctor", src, ".rbs")
|
| 103 | sh(sp + " " + src + " --emit-rbs -o " + rbs)
|
| 104 | widened = []
|
| 105 | if File.exist?(rbs)
|
| 106 | File.read(rbs).split("\n").each { |l|
|
| 107 | widened.push(l.strip) if l.include?("# spinel: widened")
|
| 108 | }
|
| 109 | end
|
| 110 | report("inference", "info", widened, quiet)
|
| 111 | end
|
| 112 |
|
| 113 | if leg_on("requires", only, skip)
|
| 114 | reqs = []
|
| 115 | File.read(src).split("\n").each { |line|
|
| 116 | s = line.strip
|
| 117 | if s.start_with?("require ") && !s.start_with?("require_relative")
|
| 118 | reqs.push(s)
|
| 119 | end
|
| 120 | }
|
| 121 | report("requires", "info", reqs, quiet)
|
| 122 | end
|
| 123 |
|
| 124 | if leg_on("build", only, skip)
|
| 125 | out = sh(sp + " " + src + " -o " + bin + " --line-map")
|
| 126 | if $sh_status == 0
|
| 127 | report("build", "error", [], quiet)
|
| 128 | else
|
| 129 | lines = []
|
| 130 | out.split("\n").each { |l| lines.push(l.strip) if l.strip.length > 0 }
|
| 131 | errs = errs + report("build", "error", lines, quiet)
|
| 132 | end
|
| 133 | end
|
| 134 |
|
| 135 | if leg_on("behavior", only, skip) && (want_behavior || have_cmd("ruby"))
|
| 136 | if !have_cmd("ruby")
|
| 137 | puts " [skip] behavior (needs ruby)" if !quiet
|
| 138 | elsif !File.exist?(bin)
|
| 139 | puts " [skip] behavior (build produced no binary)" if !quiet
|
| 140 | else
|
| 141 | ref = sh("ruby " + src)
|
| 142 | got = sh(bin)
|
| 143 | if ref == got
|
| 144 | report("behavior", "error", [], quiet)
|
| 145 | else
|
| 146 | errs = errs + report("behavior", "error",
|
| 147 | ["compiled output differs from CRuby (diff the two runs to localize)"], quiet)
|
| 148 | end
|
| 149 | end
|
| 150 | end
|
| 151 |
|
| 152 | puts ""
|
| 153 | if errs == 0
|
| 154 | puts "spinel-doctor: clean"
|
| 155 | exit(0)
|
| 156 | end
|
| 157 | puts "spinel-doctor: " + errs.to_s + " finding(s) at error severity"
|
| 158 | exit(1)
|
| 159 | end
|
| 160 |
|
| 161 | main
|