backburn 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/Makefile +50 -0
  4. data/README.md +520 -0
  5. data/docs/cli-and-outputs.md +226 -0
  6. data/docs/technical-spec.md +656 -0
  7. data/exe/backburn +8 -0
  8. data/ext/backburn/extconf.rb +31 -0
  9. data/ext/backburn/prism/COMMIT +1 -0
  10. data/ext/backburn/prism/LICENSE.md +7 -0
  11. data/ext/backburn/prism/Makefile +213 -0
  12. data/ext/backburn/prism/include/prism/arena.h +45 -0
  13. data/ext/backburn/prism/include/prism/arena.hpp +42 -0
  14. data/ext/backburn/prism/include/prism/ast.h +10395 -0
  15. data/ext/backburn/prism/include/prism/ast.hpp +641 -0
  16. data/ext/backburn/prism/include/prism/buffer.h +60 -0
  17. data/ext/backburn/prism/include/prism/buffer.hpp +55 -0
  18. data/ext/backburn/prism/include/prism/comments.h +72 -0
  19. data/ext/backburn/prism/include/prism/compiler/accel.h +26 -0
  20. data/ext/backburn/prism/include/prism/compiler/align.h +36 -0
  21. data/ext/backburn/prism/include/prism/compiler/assume.h +25 -0
  22. data/ext/backburn/prism/include/prism/compiler/branch.h +26 -0
  23. data/ext/backburn/prism/include/prism/compiler/bswap.h +46 -0
  24. data/ext/backburn/prism/include/prism/compiler/cplusplus.hpp +16 -0
  25. data/ext/backburn/prism/include/prism/compiler/endianness.h +34 -0
  26. data/ext/backburn/prism/include/prism/compiler/exceptions.hpp +17 -0
  27. data/ext/backburn/prism/include/prism/compiler/exported.h +24 -0
  28. data/ext/backburn/prism/include/prism/compiler/fallthrough.h +22 -0
  29. data/ext/backburn/prism/include/prism/compiler/filesystem.h +32 -0
  30. data/ext/backburn/prism/include/prism/compiler/flex_array.h +19 -0
  31. data/ext/backburn/prism/include/prism/compiler/force_inline.h +21 -0
  32. data/ext/backburn/prism/include/prism/compiler/format.h +27 -0
  33. data/ext/backburn/prism/include/prism/compiler/inline.h +17 -0
  34. data/ext/backburn/prism/include/prism/compiler/lifetimebound.hpp +25 -0
  35. data/ext/backburn/prism/include/prism/compiler/nodiscard.h +22 -0
  36. data/ext/backburn/prism/include/prism/compiler/nonnull.h +18 -0
  37. data/ext/backburn/prism/include/prism/compiler/noreturn.h +24 -0
  38. data/ext/backburn/prism/include/prism/compiler/stack.h +60 -0
  39. data/ext/backburn/prism/include/prism/compiler/unused.h +18 -0
  40. data/ext/backburn/prism/include/prism/constant_pool.h +112 -0
  41. data/ext/backburn/prism/include/prism/diagnostic.h +121 -0
  42. data/ext/backburn/prism/include/prism/errors_format.h +63 -0
  43. data/ext/backburn/prism/include/prism/excludes.h +29 -0
  44. data/ext/backburn/prism/include/prism/integer.h +79 -0
  45. data/ext/backburn/prism/include/prism/internal/allocator.h +68 -0
  46. data/ext/backburn/prism/include/prism/internal/allocator_debug.h +88 -0
  47. data/ext/backburn/prism/include/prism/internal/arena.h +223 -0
  48. data/ext/backburn/prism/include/prism/internal/assert.h +14 -0
  49. data/ext/backburn/prism/include/prism/internal/bignum.h +239 -0
  50. data/ext/backburn/prism/include/prism/internal/bit.h +66 -0
  51. data/ext/backburn/prism/include/prism/internal/buffer.h +104 -0
  52. data/ext/backburn/prism/include/prism/internal/char.h +145 -0
  53. data/ext/backburn/prism/include/prism/internal/comments.h +54 -0
  54. data/ext/backburn/prism/include/prism/internal/constant_pool.h +155 -0
  55. data/ext/backburn/prism/include/prism/internal/diagnostic.h +392 -0
  56. data/ext/backburn/prism/include/prism/internal/double.h +19 -0
  57. data/ext/backburn/prism/include/prism/internal/encoding.h +326 -0
  58. data/ext/backburn/prism/include/prism/internal/integer.h +66 -0
  59. data/ext/backburn/prism/include/prism/internal/isinf.h +16 -0
  60. data/ext/backburn/prism/include/prism/internal/line_offset_list.h +50 -0
  61. data/ext/backburn/prism/include/prism/internal/list.h +62 -0
  62. data/ext/backburn/prism/include/prism/internal/magic_comments.h +23 -0
  63. data/ext/backburn/prism/include/prism/internal/memchr.h +15 -0
  64. data/ext/backburn/prism/include/prism/internal/node.h +197 -0
  65. data/ext/backburn/prism/include/prism/internal/oom.h +16 -0
  66. data/ext/backburn/prism/include/prism/internal/options.h +248 -0
  67. data/ext/backburn/prism/include/prism/internal/parser.h +1170 -0
  68. data/ext/backburn/prism/include/prism/internal/regexp.h +70 -0
  69. data/ext/backburn/prism/include/prism/internal/serialize.h +39 -0
  70. data/ext/backburn/prism/include/prism/internal/source.h +72 -0
  71. data/ext/backburn/prism/include/prism/internal/static_literals.h +60 -0
  72. data/ext/backburn/prism/include/prism/internal/stringy.h +37 -0
  73. data/ext/backburn/prism/include/prism/internal/strncasecmp.h +18 -0
  74. data/ext/backburn/prism/include/prism/internal/strpbrk.h +61 -0
  75. data/ext/backburn/prism/include/prism/internal/tokens.h +11 -0
  76. data/ext/backburn/prism/include/prism/internal/xxh3.h +16 -0
  77. data/ext/backburn/prism/include/prism/json.h +40 -0
  78. data/ext/backburn/prism/include/prism/line_offset_list.h +69 -0
  79. data/ext/backburn/prism/include/prism/magic_comments.h +53 -0
  80. data/ext/backburn/prism/include/prism/node.h +139 -0
  81. data/ext/backburn/prism/include/prism/node.hpp +272 -0
  82. data/ext/backburn/prism/include/prism/node_type.hpp +38 -0
  83. data/ext/backburn/prism/include/prism/options.h +393 -0
  84. data/ext/backburn/prism/include/prism/options.hpp +43 -0
  85. data/ext/backburn/prism/include/prism/parser.h +433 -0
  86. data/ext/backburn/prism/include/prism/parser.hpp +274 -0
  87. data/ext/backburn/prism/include/prism/prettyprint.h +39 -0
  88. data/ext/backburn/prism/include/prism/serialize.h +123 -0
  89. data/ext/backburn/prism/include/prism/source.h +163 -0
  90. data/ext/backburn/prism/include/prism/stream.h +36 -0
  91. data/ext/backburn/prism/include/prism/string_query.h +97 -0
  92. data/ext/backburn/prism/include/prism/stringy.h +61 -0
  93. data/ext/backburn/prism/include/prism/stringy.hpp +31 -0
  94. data/ext/backburn/prism/include/prism/version.h +46 -0
  95. data/ext/backburn/prism/include/prism.h +123 -0
  96. data/ext/backburn/prism/include/prism.hpp +24 -0
  97. data/ext/backburn/prism/src/arena.c +200 -0
  98. data/ext/backburn/prism/src/bignum.c +399 -0
  99. data/ext/backburn/prism/src/buffer.c +405 -0
  100. data/ext/backburn/prism/src/char.c +289 -0
  101. data/ext/backburn/prism/src/comments.c +455 -0
  102. data/ext/backburn/prism/src/constant_pool.c +410 -0
  103. data/ext/backburn/prism/src/diagnostic.c +918 -0
  104. data/ext/backburn/prism/src/double.c +394 -0
  105. data/ext/backburn/prism/src/encoding.c +1701 -0
  106. data/ext/backburn/prism/src/errors_format.c +650 -0
  107. data/ext/backburn/prism/src/integer.c +675 -0
  108. data/ext/backburn/prism/src/json.c +5745 -0
  109. data/ext/backburn/prism/src/line_offset_list.c +99 -0
  110. data/ext/backburn/prism/src/list.c +24 -0
  111. data/ext/backburn/prism/src/memchr.c +37 -0
  112. data/ext/backburn/prism/src/node.c +4464 -0
  113. data/ext/backburn/prism/src/options.c +475 -0
  114. data/ext/backburn/prism/src/parser.c +371 -0
  115. data/ext/backburn/prism/src/prettyprint.c +9038 -0
  116. data/ext/backburn/prism/src/prism.c +23615 -0
  117. data/ext/backburn/prism/src/regexp.c +3833 -0
  118. data/ext/backburn/prism/src/serialize.c +2656 -0
  119. data/ext/backburn/prism/src/source.c +525 -0
  120. data/ext/backburn/prism/src/static_literals.c +562 -0
  121. data/ext/backburn/prism/src/string_query.c +195 -0
  122. data/ext/backburn/prism/src/stringy.c +77 -0
  123. data/ext/backburn/prism/src/strncasecmp.c +37 -0
  124. data/ext/backburn/prism/src/strpbrk.c +465 -0
  125. data/ext/backburn/prism/src/tokens.c +724 -0
  126. data/ext/backburn/prism/src/xxh3.c +292 -0
  127. data/lib/backburn/version.rb +7 -0
  128. data/presets/devise.roots +11 -0
  129. data/presets/fast-mcp.roots +5 -0
  130. data/presets/graphql.roots +6 -0
  131. data/presets/phlex.roots +7 -0
  132. data/presets/pundit.roots +3 -0
  133. data/presets/rails-framework.roots +10 -0
  134. data/presets/rails.roots +83 -0
  135. data/presets/sidekiq.roots +4 -0
  136. data/presets/thor.roots +3 -0
  137. data/rails.roots +99 -0
  138. data/scripts/embed-presets.sh +13 -0
  139. data/scripts/version-h.sh +7 -0
  140. data/src/analysis/analysis.c +95 -0
  141. data/src/analysis/analysis.h +24 -0
  142. data/src/analysis/batches.c +127 -0
  143. data/src/analysis/batches.h +17 -0
  144. data/src/analysis/graph.c +238 -0
  145. data/src/analysis/graph.h +34 -0
  146. data/src/analysis/mark.c +300 -0
  147. data/src/analysis/mark.h +21 -0
  148. data/src/analysis/roots.c +229 -0
  149. data/src/analysis/roots.h +29 -0
  150. data/src/cli/cli.c +237 -0
  151. data/src/cli/cli.h +9 -0
  152. data/src/cli/config.c +112 -0
  153. data/src/cli/config.h +14 -0
  154. data/src/cli/options.c +17 -0
  155. data/src/cli/options.h +35 -0
  156. data/src/commands/commands.h +11 -0
  157. data/src/commands/explain.c +255 -0
  158. data/src/commands/gemfile.c +126 -0
  159. data/src/commands/gemfile.h +27 -0
  160. data/src/commands/gems.c +408 -0
  161. data/src/commands/init.c +92 -0
  162. data/src/commands/roots.c +82 -0
  163. data/src/commands/scan.c +91 -0
  164. data/src/core/intern.c +94 -0
  165. data/src/core/intern.h +40 -0
  166. data/src/core/json.c +17 -0
  167. data/src/core/json.h +9 -0
  168. data/src/core/model.c +25 -0
  169. data/src/core/model.h +85 -0
  170. data/src/core/util.c +116 -0
  171. data/src/core/util.h +48 -0
  172. data/src/index/erb.c +40 -0
  173. data/src/index/erb.h +9 -0
  174. data/src/index/haml.c +334 -0
  175. data/src/index/haml.h +10 -0
  176. data/src/index/indexer.c +702 -0
  177. data/src/index/indexer.h +19 -0
  178. data/src/index/walk.c +144 -0
  179. data/src/index/walk.h +14 -0
  180. data/src/main.c +81 -0
  181. data/src/output/baseline.c +101 -0
  182. data/src/output/baseline.h +15 -0
  183. data/src/output/report.c +440 -0
  184. data/src/output/report.h +28 -0
  185. metadata +231 -0
@@ -0,0 +1,300 @@
1
+ /* Marking (Trémaux), namespace liveness, and classification of what stays dead. */
2
+
3
+ #include "analysis/mark.h"
4
+ #include "core/intern.h"
5
+ #include "analysis/graph.h"
6
+ #include "analysis/roots.h"
7
+
8
+ void mark(void) {
9
+ uint8_t *done = xcalloc((size_t) NN * 2 + 2, 1);
10
+ u32vec stack = {0};
11
+ for (uint32_t i = 0; i < D.n; i++) {
12
+ int32_t rr = root_for(i);
13
+ D.a[i].root_rule = rr;
14
+ if (rr != NOT_ROOT) {
15
+ D.a[i].live = 1;
16
+ PUSH(stack, i);
17
+ }
18
+ }
19
+ while (stack.n) {
20
+ uint32_t d = stack.a[--stack.n];
21
+ for (size_t k = 0; k < D.a[d].refs.n; k++) {
22
+ ref_t r = D.a[d].refs.a[k];
23
+ size_t key = (size_t) r.name * 2 + r.is_const;
24
+ if (r.name >= NN || done[key]) continue;
25
+ done[key] = 1;
26
+ CSR_FOR(*targets_of(r.is_const), r.name, t) {
27
+ if (D.a[t].live) continue;
28
+ D.a[t].live = 1;
29
+ D.a[t].parent = (int32_t) d;
30
+ D.a[t].pfile = r.file;
31
+ D.a[t].pline = r.line;
32
+ D.a[t].via = VIA_CALLS;
33
+ PUSH(stack, t);
34
+ }
35
+ }
36
+ }
37
+ free(stack.a);
38
+ free(done);
39
+ }
40
+
41
+ /* A class or module is live if a live class, module or constant is nested
42
+ * in it. Methods don't count: a method is live by name alone. */
43
+ void mark_namespaces(void) {
44
+ for (uint32_t i = 0; i < D.n; i++) {
45
+ if (!D.a[i].live || !is_constk(D.a[i].kind)) continue;
46
+ const char *q = S(D.a[i].qual);
47
+ size_t L = SLEN(D.a[i].qual);
48
+ uint32_t child = i;
49
+ for (;;) {
50
+ size_t k = L;
51
+ while (k >= 2 && !(q[k - 1] == ':' && q[k - 2] == ':')) k--;
52
+ if (k < 2) break;
53
+ L = k - 2;
54
+ int64_t id = find_name(q, L);
55
+ if (id < 0 || id >= NN) continue;
56
+ uint32_t next = child;
57
+ CSR_FOR(CLS_BY_QUAL_ALL, (uint32_t) id, j) {
58
+ if (!D.a[j].live) {
59
+ D.a[j].live = 1;
60
+ D.a[j].parent = (int32_t) child;
61
+ D.a[j].via = VIA_HOLDS;
62
+ D.a[j].pfile = D.a[child].file;
63
+ D.a[j].pline = D.a[child].line;
64
+ }
65
+ next = j;
66
+ }
67
+ child = next;
68
+ }
69
+ }
70
+ }
71
+
72
+ /* Dead methods keyed by the qualified name of their class or module. */
73
+ static uint32_t key_dead_meth_owner(size_t i, int *ok) {
74
+ *ok = 0;
75
+ const def_t *d = &D.a[i];
76
+ if (!is_meth(d->kind) || d->live) return 0;
77
+ const char *s;
78
+ size_t n;
79
+ owner_qual(d, &s, &n);
80
+ int64_t id = n ? find_name(s, n) : -1;
81
+ *ok = id >= 0 && id < NN;
82
+ return *ok ? (uint32_t) id : 0;
83
+ }
84
+
85
+ static void mark_maybe(uint32_t t, uint32_t parent, uint8_t via, uint32_t file, uint32_t line, u32vec *stack) {
86
+ def_t *d = &D.a[t];
87
+ d->live = LIVE_MAYBE;
88
+ d->parent = (int32_t) parent;
89
+ d->via = via;
90
+ d->pfile = file;
91
+ d->pline = line;
92
+ PUSH(*stack, t);
93
+ }
94
+
95
+ /* Whether a self-send may call method t of its class: public_send and friends only
96
+ * reach public methods; in an instance method self is an instance, in `def self.x`
97
+ * it is the class. */
98
+ static int send_reaches(const dyn_t *s, const def_t *t) {
99
+ return (!s->public_only || t->vis == V_PUBLIC) && (s->self_kind == K_LOAD || s->self_kind == t->kind);
100
+ }
101
+
102
+ /* A fully computed send on self (send(name), public_send(name), method(name),
103
+ * try(name)) may call any method of the class it is written in: instance methods
104
+ * from an instance method, class methods from `def self.x`, either from a class
105
+ * body or a block outside any def. When live code makes one, those dead methods
106
+ * are uncertain rather than dead, and so is dead code that only they reach.
107
+ * Sends in uncertain code count too; sends in dead code never run. Same walk as
108
+ * mark(), over what is still dead. */
109
+ void mark_uncertain(void) {
110
+ csr_t by_owner, sends;
111
+ csr_build(&by_owner, NN, D.n, key_dead_meth_owner, 0);
112
+ /* self-sends by the def that makes them */
113
+ sends.off = xcalloc(D.n + 2, sizeof *sends.off);
114
+ for (size_t k = 0; k < DYN.n; k++)
115
+ if (DYN.a[k].self_ns >= 0) sends.off[DYN.a[k].owner + 2]++;
116
+ for (size_t i = 0; i < D.n; i++) sends.off[i + 2] += sends.off[i + 1];
117
+ sends.items = xcalloc(sends.off[D.n + 1] + 1, sizeof *sends.items);
118
+ for (size_t k = 0; k < DYN.n; k++)
119
+ if (DYN.a[k].self_ns >= 0) sends.items[sends.off[DYN.a[k].owner + 1]++] = (uint32_t) k;
120
+
121
+ uint8_t *done = xcalloc((size_t) NN * 2 + 2, 1);
122
+ u32vec stack = {0};
123
+ for (uint32_t i = 0; i < D.n; i++)
124
+ if (D.a[i].live == 1 && sends.off[i + 1] > sends.off[i]) PUSH(stack, i);
125
+ while (stack.n) {
126
+ uint32_t d = stack.a[--stack.n];
127
+ CSR_FOR(sends, d, k) {
128
+ const dyn_t *s = &DYN.a[k];
129
+ uint32_t ns = (uint32_t) s->self_ns;
130
+ if (ns >= NN) continue;
131
+ CSR_FOR(by_owner, ns, t)
132
+ if (!D.a[t].live && send_reaches(s, &D.a[t]))
133
+ mark_maybe(t, d, VIA_SEND, s->file, s->line, &stack);
134
+ }
135
+ if (D.a[d].live != LIVE_MAYBE) continue; /* live code's references were walked by mark() */
136
+ for (size_t k = 0; k < D.a[d].refs.n; k++) {
137
+ ref_t r = D.a[d].refs.a[k];
138
+ size_t key = (size_t) r.name * 2 + r.is_const;
139
+ if (r.name >= NN || done[key]) continue;
140
+ done[key] = 1;
141
+ CSR_FOR(*targets_of(r.is_const), r.name, t)
142
+ if (!D.a[t].live) mark_maybe(t, d, VIA_CALLS, r.file, r.line, &stack);
143
+ }
144
+ }
145
+ /* as in mark_namespaces: a dead namespace holding uncertain constants is uncertain */
146
+ for (uint32_t i = 0; i < D.n; i++) {
147
+ if (D.a[i].live != LIVE_MAYBE || !is_constk(D.a[i].kind)) continue;
148
+ const char *q = S(D.a[i].qual);
149
+ size_t L = SLEN(D.a[i].qual);
150
+ uint32_t child = i;
151
+ for (;;) {
152
+ size_t k = L;
153
+ while (k >= 2 && !(q[k - 1] == ':' && q[k - 2] == ':')) k--;
154
+ if (k < 2) break;
155
+ L = k - 2;
156
+ int64_t id = find_name(q, L);
157
+ if (id < 0 || id >= NN) continue;
158
+ uint32_t next = child;
159
+ CSR_FOR(CLS_BY_QUAL_ALL, (uint32_t) id, j) {
160
+ if (!D.a[j].live) {
161
+ D.a[j].live = LIVE_MAYBE;
162
+ D.a[j].parent = (int32_t) child;
163
+ D.a[j].via = VIA_HOLDS;
164
+ D.a[j].pfile = D.a[child].file;
165
+ D.a[j].pline = D.a[child].line;
166
+ }
167
+ next = j;
168
+ }
169
+ child = next;
170
+ }
171
+ }
172
+ free(stack.a);
173
+ free(done);
174
+ free(by_owner.off);
175
+ free(by_owner.items);
176
+ free(sends.off);
177
+ free(sends.items);
178
+ }
179
+
180
+ /* The send that makes t uncertain: follow parents back to the first VIA_SEND. */
181
+ int32_t uncertain_site(uint32_t t) {
182
+ int32_t u = (int32_t) t;
183
+ for (size_t steps = 0; u >= 0 && D.a[u].live == LIVE_MAYBE && D.a[u].via != VIA_SEND && steps <= D.n; steps++)
184
+ u = D.a[u].parent;
185
+ if (u < 0 || D.a[u].live != LIVE_MAYBE || D.a[u].via != VIA_SEND) return -1;
186
+ const def_t *d = &D.a[u];
187
+ for (size_t k = 0; k < DYN.n; k++)
188
+ if (DYN.a[k].self_ns >= 0 && (int32_t) DYN.a[k].owner == d->parent && DYN.a[k].file == d->pfile &&
189
+ DYN.a[k].line == d->pline && send_reaches(&DYN.a[k], d))
190
+ return (int32_t) k;
191
+ return -1;
192
+ }
193
+
194
+ csr_t TR_BY_KEY, DEAD_CALLERS;
195
+
196
+ static uint32_t *STAMP, GEN;
197
+
198
+ size_t NCAND;
199
+
200
+ static uint32_t key_tr(size_t i, int *ok) { *ok = TR.a[i].name < NN; return *ok ? TR.a[i].name * 2 + TR.a[i].is_const : 0; }
201
+
202
+ /* Dead-callers index: for each (name, kind) key, the dead defs that name it. */
203
+ static void build_dead_callers(void) {
204
+ DEAD_CALLERS.off = xcalloc((size_t) NN * 2 + 2, sizeof(uint32_t));
205
+ for (size_t d = 1; d < D.n; d++) {
206
+ if (D.a[d].live) continue;
207
+ for (size_t k = 0; k < D.a[d].refs.n; k++) {
208
+ const ref_t *r = &D.a[d].refs.a[k];
209
+ if (r->name < NN) DEAD_CALLERS.off[r->name * 2 + r->is_const + 2]++;
210
+ }
211
+ }
212
+ for (size_t k = 0; k < (size_t) NN * 2; k++) DEAD_CALLERS.off[k + 2] += DEAD_CALLERS.off[k + 1];
213
+ DEAD_CALLERS.items = xcalloc(DEAD_CALLERS.off[(size_t) NN * 2 + 1] + 1, sizeof(uint32_t));
214
+ for (size_t d = 1; d < D.n; d++) {
215
+ if (D.a[d].live) continue;
216
+ for (size_t k = 0; k < D.a[d].refs.n; k++) {
217
+ const ref_t *r = &D.a[d].refs.a[k];
218
+ if (r->name < NN) DEAD_CALLERS.items[DEAD_CALLERS.off[r->name * 2 + r->is_const + 1]++] = (uint32_t) d;
219
+ }
220
+ }
221
+ }
222
+
223
+ static int in_dead_cycle(uint32_t t) {
224
+ GEN++;
225
+ u32vec st = {0};
226
+ PUSH(st, t);
227
+ int first = 1, found = 0;
228
+ while (st.n && !found) {
229
+ uint32_t u = st.a[--st.n];
230
+ if (!first && STAMP[u] == GEN) continue;
231
+ STAMP[u] = GEN;
232
+ first = 0;
233
+ for (size_t k = 0; k < D.a[u].refs.n && !found; k++) {
234
+ const ref_t *r = &D.a[u].refs.a[k];
235
+ if (r->name >= NN) continue;
236
+ CSR_FOR(*targets_of(r->is_const), r->name, v) {
237
+ if (D.a[v].live || v == u) continue;
238
+ if (v == t) {
239
+ found = 1;
240
+ break;
241
+ }
242
+ if (STAMP[v] != GEN) PUSH(st, v);
243
+ }
244
+ }
245
+ }
246
+ free(st.a);
247
+ return found;
248
+ }
249
+
250
+ void classify(void) {
251
+ uint32_t *lcnt = xcalloc((size_t) NN * 2 + 2, sizeof *lcnt), *dcnt = xcalloc((size_t) NN * 2 + 2, sizeof *dcnt);
252
+ for (size_t d = 0; d < D.n; d++)
253
+ for (size_t k = 0; k < D.a[d].refs.n; k++) {
254
+ const ref_t *r = &D.a[d].refs.a[k];
255
+ if (r->name >= NN) continue;
256
+ (D.a[d].live ? lcnt : dcnt)[r->name * 2 + r->is_const]++;
257
+ }
258
+ csr_build(&TR_BY_KEY, NN * 2, TR.n, key_tr, 0);
259
+ build_dead_callers();
260
+ STAMP = xcalloc(D.n + 1, sizeof *STAMP);
261
+ NCAND = 0;
262
+ for (size_t t = 1; t < D.n; t++) {
263
+ def_t *d = &D.a[t];
264
+ uint32_t key = def_key(d);
265
+ int self = find_ref(d, d->name, is_constk(d->kind)) != NULL;
266
+ d->in_live = lcnt[key] - (d->live && self);
267
+ d->in_dead = dcnt[key] - (!d->live && self);
268
+ d->test_ref = TR_BY_KEY.off[key + 1] > TR_BY_KEY.off[key];
269
+ if (d->live) {
270
+ d->reason = d->live == LIVE_MAYBE ? R_UNCERTAIN : R_LIVE;
271
+ continue;
272
+ }
273
+ NCAND++;
274
+ d->reason = d->in_dead == 0 ? R_NEVER : in_dead_cycle((uint32_t) t) ? R_CYCLE : R_DEAD_ONLY;
275
+ }
276
+ free(lcnt);
277
+ free(dcnt);
278
+ }
279
+
280
+ void dead_callers(uint32_t t, u32vec *out) {
281
+ const def_t *d = &D.a[t];
282
+ uint32_t key = def_key(d);
283
+ if (d->name >= NN) return;
284
+ for (uint32_t k = DEAD_CALLERS.off[key]; k < DEAD_CALLERS.off[key + 1]; k++) {
285
+ uint32_t c = DEAD_CALLERS.items[k];
286
+ if (c != t && (!out->n || out->a[out->n - 1] != c)) PUSH(*out, c);
287
+ }
288
+ }
289
+
290
+ void test_files_of(uint32_t t, u32vec *out) {
291
+ const def_t *d = &D.a[t];
292
+ uint32_t key = def_key(d);
293
+ if (d->name >= NN) return;
294
+ CSR_FOR(TR_BY_KEY, key, k) PUSH(*out, TR.a[k].file);
295
+ qsort(out->a, out->n, sizeof *out->a, u32_cmp);
296
+ size_t w = 0;
297
+ for (size_t i = 0; i < out->n; i++)
298
+ if (!w || out->a[i] != out->a[w - 1]) out->a[w++] = out->a[i];
299
+ out->n = w;
300
+ }
@@ -0,0 +1,21 @@
1
+ /* Marking (Trémaux), namespace liveness, and classification of what stays dead. */
2
+ #ifndef BACKBURN_ANALYSIS_MARK_H
3
+ #define BACKBURN_ANALYSIS_MARK_H
4
+
5
+ #include "analysis/graph.h"
6
+
7
+ extern csr_t TR_BY_KEY, DEAD_CALLERS; /* by reference key: test references; dead defs naming it */
8
+ extern size_t NCAND;
9
+
10
+ void mark(void); /* depth-first mark from <load time> and every root */
11
+ void mark_namespaces(void); /* a class is live when it holds a live class, module or constant */
12
+ void mark_uncertain(void); /* what a computed send on self may call: live == LIVE_MAYBE */
13
+ void classify(void); /* unreferenced, dead-chain or dead-cycle; uncertain for LIVE_MAYBE */
14
+
15
+ /* For an uncertain def: the DYN index of the send that reaches it, or -1. */
16
+ int32_t uncertain_site(uint32_t t);
17
+
18
+ void dead_callers(uint32_t t, u32vec *out);
19
+ void test_files_of(uint32_t t, u32vec *out);
20
+
21
+ #endif
@@ -0,0 +1,229 @@
1
+ /* Root rules: the entrances to the maze (presets, roots files, hooks, --public-api). */
2
+ #include <ctype.h>
3
+ #include <errno.h>
4
+ #include <fnmatch.h>
5
+
6
+ #include "analysis/roots.h"
7
+ #include "core/intern.h"
8
+ #include "analysis/graph.h"
9
+ #include "cli/options.h"
10
+ #include "presets.h"
11
+
12
+ rule_vec RULES;
13
+
14
+ strvec ROOT_SOURCES; /* for the header: "rails", "rails.roots" … */
15
+
16
+ static const char *const HOOKS[] = {
17
+ "initialize", "initialize_copy", "initialize_dup", "initialize_clone", "method_missing",
18
+ "respond_to_missing?", "respond_to?", "to_s", "to_str", "inspect", "to_proc", "to_a", "to_ary",
19
+ "to_h", "to_hash", "to_i", "to_int", "to_f", "to_r", "to_c", "to_sym", "to_json", "as_json",
20
+ "to_param", "call", "each", "hash", "eql?", "coerce", "inherited", "included", "extended",
21
+ "prepended", "method_added", "singleton_method_added", "const_missing", "marshal_dump",
22
+ "marshal_load", "_dump", "_load", "encode_with", "init_with", "pretty_print", "freeze", "dup",
23
+ "clone", "===", "==", "<=>", NULL};
24
+
25
+ uint8_t *IS_HOOK;
26
+
27
+ int32_t *FILE_PATH_RULE;
28
+
29
+ static void load_rules(const char *text, const char *label) {
30
+ int lineno = 0;
31
+ const char *p = text;
32
+ PUSH(ROOT_SOURCES, xstrdup(label));
33
+ while (*p) {
34
+ const char *e = strchr(p, '\n');
35
+ size_t n = e ? (size_t) (e - p) : strlen(p);
36
+ lineno++;
37
+ const char *s = p;
38
+ while (n && isspace((unsigned char) *s)) s++, n--;
39
+ while (n && isspace((unsigned char) s[n - 1])) n--;
40
+ p = e ? e + 1 : p + strlen(p);
41
+ if (!n || s[0] == '#') continue;
42
+ rule_t r;
43
+ memset(&r, 0, sizeof r);
44
+ r.text = xstrndup(s, n);
45
+ sbuf src = {0};
46
+ sb_printf(&src, "%s:%d", label, lineno);
47
+ r.source = src.s;
48
+ const char *q = r.text;
49
+ if (!strncmp(q, "public ", 7)) {
50
+ r.pub = 1;
51
+ q += 7;
52
+ while (*q == ' ') q++;
53
+ }
54
+ if (q[0] == '<' && (q[1] == ' ' || q[1] == '\t')) {
55
+ r.type = RULE_KIND;
56
+ q += 1;
57
+ while (*q == ' ' || *q == '\t') q++;
58
+ if (!strncmp(q, "::", 2)) q += 2;
59
+ } else if (!strncmp(q, "path:", 5)) {
60
+ r.type = RULE_PATH;
61
+ q += 5;
62
+ } else {
63
+ r.type = RULE_PAT;
64
+ }
65
+ r.pat = xstrdup(q);
66
+ PUSH(RULES, r);
67
+ }
68
+ }
69
+
70
+ /* Is a module matching pat mixed into class cls itself? */
71
+ static int mixes_in(const char *pat, uint32_t cls) {
72
+ CSR_FOR(INC_BY_CLS, cls, k) {
73
+ const incl_t *in = &INC.a[k];
74
+ if (!fnmatch(pat, S(in->mod), 0)) return 1;
75
+ if (in->res >= 0 && !fnmatch(pat, S(D.a[in->res].qual), 0)) return 1;
76
+ }
77
+ return 0;
78
+ }
79
+
80
+ static int kind_direct(const char *pat, const char *owner, int64_t oid) {
81
+ if (!fnmatch(pat, owner, 0)) return 1;
82
+ if (oid < 0 || oid >= NN) return 0;
83
+ if (mixes_in(pat, (uint32_t) oid)) return 1;
84
+ int32_t cd = CLS_BY_QUAL[oid];
85
+ for (int step = 0; cd >= 0 && step < 32; step++) {
86
+ const def_t *d = &D.a[cd];
87
+ if (d->kind != K_CLASS || d->super_ < 0) break;
88
+ if (!fnmatch(pat, S(d->super_), 0)) return 1;
89
+ int32_t r = SUPER_DEF[cd];
90
+ if (r < 0) break;
91
+ if (!fnmatch(pat, S(D.a[r].qual), 0)) return 1;
92
+ if (mixes_in(pat, D.a[r].qual)) return 1;
93
+ cd = CLS_BY_QUAL[D.a[r].qual];
94
+ }
95
+ return 0;
96
+ }
97
+
98
+ static int kind_of(rule_t *r, const char *owner, int64_t oid) {
99
+ if (oid >= 0 && oid < NN) {
100
+ if (!r->memo) r->memo = xcalloc(NN, 1);
101
+ if (r->memo[oid]) return r->memo[oid] == 1;
102
+ }
103
+ int res = kind_direct(r->pat, owner, oid);
104
+ if (!res && oid >= 0 && oid < NN) {
105
+ CSR_FOR(INC_BY_RES, (uint32_t) oid, k) {
106
+ uint32_t cls = INC.a[k].cls;
107
+ if (kind_direct(r->pat, S(cls), cls)) {
108
+ res = 1;
109
+ break;
110
+ }
111
+ }
112
+ }
113
+ if (oid >= 0 && oid < NN) r->memo[oid] = res ? 1 : 2;
114
+ return res;
115
+ }
116
+
117
+ /* Does a non-path rule match definition i? (public only restricts methods) */
118
+ int rule_matches(rule_t *r, uint32_t i) {
119
+ const def_t *d = &D.a[i];
120
+ int meth = is_meth(d->kind);
121
+ if (r->pub && meth && d->vis != V_PUBLIC) return 0;
122
+ const char *q = S(d->qual);
123
+ if (r->type == RULE_PAT) return !fnmatch(r->pat, q, 0);
124
+ if (r->type != RULE_KIND) return 0;
125
+ size_t on = SLEN(d->qual);
126
+ if (meth) {
127
+ const char *cut = strrchr(q, '#');
128
+ if (!cut) cut = strrchr(q, '.');
129
+ if (cut) on = (size_t) (cut - q);
130
+ } else if (d->kind == K_CONST) {
131
+ /* a constant belongs to its namespace: cut at the last `::` */
132
+ for (size_t j = on; j >= 2; j--) {
133
+ if (q[j - 1] == ':' && q[j - 2] == ':') {
134
+ on = j - 2;
135
+ break;
136
+ }
137
+ }
138
+ }
139
+ char stackbuf[512];
140
+ char *owner = on < sizeof stackbuf ? stackbuf : xrealloc(NULL, on + 1);
141
+ memcpy(owner, q, on);
142
+ owner[on] = 0;
143
+ int res = kind_of(r, owner, find_name(owner, on));
144
+ if (owner != stackbuf) free(owner);
145
+ return res;
146
+ }
147
+
148
+ int is_operator(uint32_t name) {
149
+ unsigned char c0 = (unsigned char) S(name)[0];
150
+ return !(isalpha(c0) || c0 == '_' || c0 >= 0x80);
151
+ }
152
+
153
+ /* Built-in root checks, in the spec's order. Returns a rule code. */
154
+ int32_t root_for(uint32_t i) {
155
+ const def_t *d = &D.a[i];
156
+ if (d->kind == K_LOAD) return RR_LOAD;
157
+ if (FILE_PATH_RULE[d->file] >= 0) return FILE_PATH_RULE[d->file];
158
+ if (is_meth(d->kind) && d->overrides) return RR_SUPER;
159
+ if (is_constk(d->kind)) {
160
+ if (O.public_api && !d->nodoc) return RR_PUBLIC_API;
161
+ for (size_t r = 0; r < RULES.n; r++)
162
+ if (RULES.a[r].type != RULE_PATH && rule_matches(&RULES.a[r], i)) return (int32_t) r;
163
+ return NOT_ROOT;
164
+ }
165
+ if (is_operator(d->name)) return RR_OPERATOR;
166
+ if (d->name < NN && IS_HOOK[d->name]) return RR_HOOK;
167
+ if (O.public_api && d->vis == V_PUBLIC && !d->nodoc) return RR_PUBLIC_API;
168
+ for (size_t r = 0; r < RULES.n; r++)
169
+ if (RULES.a[r].type != RULE_PATH && rule_matches(&RULES.a[r], i)) return (int32_t) r;
170
+ return NOT_ROOT;
171
+ }
172
+
173
+ void prepare_roots(void) {
174
+ IS_HOOK = xcalloc(NN + 1, 1);
175
+ for (int k = 0; HOOKS[k]; k++) {
176
+ int64_t id = find_name(HOOKS[k], strlen(HOOKS[k]));
177
+ if (id >= 0 && id < NN) IS_HOOK[id] = 1;
178
+ }
179
+ FILE_PATH_RULE = xcalloc(FILES.n + 1, sizeof *FILE_PATH_RULE);
180
+ for (size_t f = 0; f < FILES.n; f++) {
181
+ FILE_PATH_RULE[f] = -1;
182
+ for (size_t r = 0; r < RULES.n; r++)
183
+ if (RULES.a[r].type == RULE_PATH && !fnmatch(RULES.a[r].pat, FILES.a[f], 0)) {
184
+ FILE_PATH_RULE[f] = (int32_t) r;
185
+ break;
186
+ }
187
+ }
188
+ }
189
+
190
+ void rule_desc(int32_t rr, sbuf *b) {
191
+ switch (rr) {
192
+ case RR_LOAD: sb_puts(b, "code outside any method runs when its file loads"); break;
193
+ case RR_SUPER: sb_puts(b, "calls super, so whoever calls the parent method reaches it"); break;
194
+ case RR_OPERATOR: sb_puts(b, "operator method"); break;
195
+ case RR_HOOK: sb_puts(b, "Ruby calls this hook by convention"); break;
196
+ case RR_PUBLIC_API: sb_puts(b, "public API (--public-api)"); break;
197
+ default:
198
+ if (rr >= 0 && (size_t) rr < RULES.n) sb_printf(b, "%s (%s)", RULES.a[rr].text, RULES.a[rr].source);
199
+ }
200
+ }
201
+
202
+ const char *preset_text(const char *name) {
203
+ for (int k = 0; PRESETS[k].name; k++)
204
+ if (!strcmp(PRESETS[k].name, name)) return PRESETS[k].text;
205
+ return NULL;
206
+ }
207
+
208
+ void load_all_rules(void) {
209
+ for (size_t k = 0; k < O.presets.n; k++) {
210
+ const char *t = preset_text(O.presets.a[k]);
211
+ if (!t) {
212
+ fprintf(stderr, "backburn: unknown preset %s (available:", O.presets.a[k]);
213
+ for (int p = 0; PRESETS[p].name; p++) fprintf(stderr, " %s", PRESETS[p].name);
214
+ fputs(")\n", stderr);
215
+ exit(2);
216
+ }
217
+ load_rules(t, O.presets.a[k]);
218
+ }
219
+ for (size_t k = 0; k < O.roots.n; k++) {
220
+ size_t n;
221
+ char *t = read_file(O.roots.a[k], &n);
222
+ if (!t) {
223
+ fprintf(stderr, "backburn: cannot read roots file %s: %s\n", O.roots.a[k], strerror(errno));
224
+ exit(2);
225
+ }
226
+ load_rules(t, O.roots.a[k]);
227
+ free(t);
228
+ }
229
+ }
@@ -0,0 +1,29 @@
1
+ /* Root rules: the entrances to the maze (presets, roots files, hooks, --public-api). */
2
+ #ifndef BACKBURN_ANALYSIS_ROOTS_H
3
+ #define BACKBURN_ANALYSIS_ROOTS_H
4
+
5
+ #include "core/model.h"
6
+
7
+ enum { RULE_PAT, RULE_KIND, RULE_PATH };
8
+ typedef struct {
9
+ int type, pub;
10
+ char *pat, *text, *source; /* glob, the line as written, preset:line or file:line */
11
+ uint32_t matched;
12
+ uint8_t *memo; /* RULE_KIND: per owner name id, 0 unknown, 1 yes, 2 no */
13
+ } rule_t;
14
+ typedef VEC(rule_t) rule_vec;
15
+
16
+ extern rule_vec RULES;
17
+ extern strvec ROOT_SOURCES; /* labels of the loaded presets and files */
18
+ extern uint8_t *IS_HOOK; /* per name id: a Ruby hook */
19
+ extern int32_t *FILE_PATH_RULE; /* per file: first matching path: rule, or -1 */
20
+
21
+ void load_all_rules(void); /* presets, then roots files; exits 2 on error */
22
+ const char *preset_text(const char *name);
23
+ void prepare_roots(void);
24
+ int32_t root_for(uint32_t i); /* rule code, or NOT_ROOT */
25
+ int rule_matches(rule_t *r, uint32_t i);
26
+ int is_operator(uint32_t name);
27
+ void rule_desc(int32_t rule, sbuf *b);
28
+
29
+ #endif