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
data/src/core/intern.c ADDED
@@ -0,0 +1,94 @@
1
+ /* String interner: every name is stored once and known by a dense uint32_t id. */
2
+
3
+ #include "core/intern.h"
4
+
5
+ interner_t I;
6
+ static char *pool_cur;
7
+
8
+ static size_t pool_left;
9
+
10
+ static uint64_t fnv1a(const char *s, size_t n) {
11
+ uint64_t h = 1469598103934665603ULL;
12
+ for (size_t i = 0; i < n; i++) h = (h ^ (uint8_t) s[i]) * 1099511628211ULL;
13
+ return h;
14
+ }
15
+
16
+ static const char *pool_copy(const char *s, size_t n) {
17
+ if (n + 1 > pool_left) {
18
+ size_t sz = n + 1 > (1u << 20) ? n + 1 : (1u << 20);
19
+ pool_cur = xrealloc(NULL, sz);
20
+ pool_left = sz;
21
+ }
22
+ char *d = pool_cur;
23
+ memcpy(d, s, n);
24
+ d[n] = 0;
25
+ pool_cur += n + 1;
26
+ pool_left -= n + 1;
27
+ return d;
28
+ }
29
+
30
+ static void intern_grow(void) {
31
+ size_t nc = I.cap ? I.cap * 2 : 1024;
32
+ uint32_t *ns = xcalloc(nc, sizeof *ns);
33
+ for (size_t id = 0; id < I.n; id++) {
34
+ size_t i = I.hash[id] & (nc - 1);
35
+ while (ns[i]) i = (i + 1) & (nc - 1);
36
+ ns[i] = (uint32_t) id + 1;
37
+ }
38
+ free(I.slots);
39
+ I.slots = ns;
40
+ I.cap = nc;
41
+ }
42
+
43
+ uint32_t intern(const char *s, size_t n) {
44
+ if ((I.n + 1) * 2 > I.cap) intern_grow();
45
+ uint64_t h = fnv1a(s, n);
46
+ size_t i = h & (I.cap - 1);
47
+ while (I.slots[i]) {
48
+ uint32_t id = I.slots[i] - 1;
49
+ if (I.hash[id] == h && I.len[id] == n && !memcmp(I.str[id], s, n)) return id;
50
+ i = (i + 1) & (I.cap - 1);
51
+ }
52
+ if (I.n == I.scap) {
53
+ I.scap = I.scap ? I.scap * 2 : 1024;
54
+ I.str = xrealloc(I.str, I.scap * sizeof *I.str);
55
+ I.len = xrealloc(I.len, I.scap * sizeof *I.len);
56
+ I.hash = xrealloc(I.hash, I.scap * sizeof *I.hash);
57
+ }
58
+ uint32_t id = (uint32_t) I.n++;
59
+ I.str[id] = pool_copy(s, n);
60
+ I.len[id] = (uint32_t) n;
61
+ I.hash[id] = h;
62
+ I.slots[i] = id + 1;
63
+ return id;
64
+ }
65
+
66
+ uint32_t intern_cstr(const char *s) { return intern(s, strlen(s)); }
67
+
68
+ int64_t find_name(const char *s, size_t n) {
69
+ if (!I.cap) return -1;
70
+ uint64_t h = fnv1a(s, n);
71
+ size_t i = h & (I.cap - 1);
72
+ while (I.slots[i]) {
73
+ uint32_t id = I.slots[i] - 1;
74
+ if (I.hash[id] == h && I.len[id] == n && !memcmp(I.str[id], s, n)) return id;
75
+ i = (i + 1) & (I.cap - 1);
76
+ }
77
+ return -1;
78
+ }
79
+
80
+ /* Words the visitor compares against, interned first so their ids are
81
+ * 0..W_N-1 and a call's name is classified with an integer compare. */
82
+ static const char *const WORDS[] = {
83
+ "private", "protected", "public", "private_class_method", "public_class_method",
84
+ "module_function", "include", "prepend", "extend",
85
+ "send", "__send__", "public_send", "try", "try!", "method", "public_method",
86
+ "instance_method", "public_instance_method", "respond_to?", "alias_method",
87
+ "define_method",
88
+ "const_get", "constantize", "safe_constantize", "const_defined?",
89
+ "class_eval", "module_eval", "instance_eval", "eval", "runner", "require", NULL};
90
+
91
+ /* Interns WORDS first, so their ids are 0..W_N-1 (see the W_ enum). */
92
+ void intern_words(void) {
93
+ for (int k = 0; WORDS[k]; k++) intern_cstr(WORDS[k]);
94
+ }
data/src/core/intern.h ADDED
@@ -0,0 +1,40 @@
1
+ /* String interner: every name is stored once and known by a dense uint32_t id. */
2
+ #ifndef BACKBURN_CORE_INTERN_H
3
+ #define BACKBURN_CORE_INTERN_H
4
+
5
+ #include "core/util.h"
6
+
7
+ /* FNV-1a 64, open addressing, linear probing, doubled at 50% load.
8
+ * intern() inserts; find_name() only looks, so analysis never grows the
9
+ * table with failed lookups. */
10
+ typedef struct {
11
+ uint32_t *slots; /* id + 1, 0 = empty */
12
+ size_t cap;
13
+ const char **str;
14
+ uint32_t *len;
15
+ uint64_t *hash;
16
+ size_t n, scap;
17
+ } interner_t;
18
+ extern interner_t I;
19
+
20
+ uint32_t intern(const char *s, size_t n);
21
+ uint32_t intern_cstr(const char *s);
22
+ int64_t find_name(const char *s, size_t n); /* -1 when absent */
23
+ void intern_words(void);
24
+
25
+ #define S(id) (I.str[id])
26
+ #define SLEN(id) (I.len[id])
27
+
28
+ /* Words the visitor compares call names against; intern_words() gives
29
+ * them these ids, so a call's name is classified with an integer compare. */
30
+ enum {
31
+ W_PRIVATE, W_PROTECTED, W_PUBLIC, W_PRIVATE_CLASS_METHOD, W_PUBLIC_CLASS_METHOD,
32
+ W_MODULE_FUNCTION, W_INCLUDE, W_PREPEND, W_EXTEND,
33
+ W_SEND, W___SEND__, W_PUBLIC_SEND, W_TRY, W_TRY_BANG, W_METHOD, W_PUBLIC_METHOD,
34
+ W_INSTANCE_METHOD, W_PUBLIC_INSTANCE_METHOD, W_RESPOND_TO, W_ALIAS_METHOD,
35
+ W_DEFINE_METHOD,
36
+ W_CONST_GET, W_CONSTANTIZE, W_SAFE_CONSTANTIZE, W_CONST_DEFINED,
37
+ W_CLASS_EVAL, W_MODULE_EVAL, W_INSTANCE_EVAL, W_EVAL, W_RUNNER, W_REQUIRE, W_N
38
+ };
39
+
40
+ #endif
data/src/core/json.c ADDED
@@ -0,0 +1,17 @@
1
+ /* JSON string output. */
2
+
3
+ #include "core/json.h"
4
+
5
+ void json_str(FILE *f, const char *s) {
6
+ fputc('"', f);
7
+ for (; *s; s++) {
8
+ unsigned char c = (unsigned char) *s;
9
+ if (c == '"' || c == '\\') fprintf(f, "\\%c", c);
10
+ else if (c == '\n') fputs("\\n", f);
11
+ else if (c == '\t') fputs("\\t", f);
12
+ else if (c == '\r') fputs("\\r", f);
13
+ else if (c < 0x20) fprintf(f, "\\u%04x", c);
14
+ else fputc(c, f);
15
+ }
16
+ fputc('"', f);
17
+ }
data/src/core/json.h ADDED
@@ -0,0 +1,9 @@
1
+ /* JSON string output. */
2
+ #ifndef BACKBURN_CORE_JSON_H
3
+ #define BACKBURN_CORE_JSON_H
4
+
5
+ #include <stdio.h>
6
+
7
+ void json_str(FILE *f, const char *s);
8
+
9
+ #endif
data/src/core/model.c ADDED
@@ -0,0 +1,25 @@
1
+ /* The index: definitions, their references by name, and the side tables the visitor fills. */
2
+
3
+ #include "core/model.h"
4
+
5
+ def_vec D;
6
+
7
+ strvec FILES;
8
+
9
+ u8vec FTEST, FTEMPLATE;
10
+
11
+ tref_vec TR;
12
+
13
+ incl_vec INC;
14
+
15
+ srecv_vec SRECV;
16
+
17
+ dyn_vec DYN;
18
+
19
+ pref_vec PREF;
20
+
21
+ req_vec REQ;
22
+
23
+ perr_vec PERR;
24
+
25
+ uint32_t TEMPLATE_ERRORS;
data/src/core/model.h ADDED
@@ -0,0 +1,85 @@
1
+ /* The index: definitions, their references by name, and the side tables the visitor fills. */
2
+ #ifndef BACKBURN_CORE_MODEL_H
3
+ #define BACKBURN_CORE_MODEL_H
4
+
5
+ #include "core/util.h"
6
+
7
+ enum { K_LOAD, K_METHOD, K_SMETHOD, K_CLASS, K_MODULE, K_CONST };
8
+ enum { V_PUBLIC, V_PRIVATE, V_PROTECTED };
9
+ enum { R_LIVE, R_NEVER, R_DEAD_ONLY, R_CYCLE, R_UNCERTAIN };
10
+ enum { VIA_CALLS, VIA_HOLDS, VIA_SEND };
11
+ /* def_t.live: 0 dead, 1 live, LIVE_MAYBE when a computed send in its own class may
12
+ * call it, or only such code reaches it. Truthy, so it is never a candidate. */
13
+ #define LIVE_MAYBE 2
14
+ #define NOT_ROOT INT32_MIN
15
+ enum { RR_LOAD = -2, RR_SUPER = -3, RR_OPERATOR = -4, RR_HOOK = -5, RR_PUBLIC_API = -6 };
16
+
17
+ /* An outgoing reference: a name, and whether it names a constant. */
18
+ typedef struct { uint32_t name, file, line; uint8_t is_const; } ref_t;
19
+
20
+ /* A definition. Index 0 is the synthetic <load time> definition that owns
21
+ * every reference made outside a method body. */
22
+ typedef struct {
23
+ uint8_t kind, vis, overrides, live, test_ref, reason, nodoc, via;
24
+ uint8_t super_top; /* superclass written with a leading ::, resolved from the top level */
25
+ uint32_t name, qual, file, line, end_line;
26
+ int32_t super_, outer; /* classes: superclass as written, Module.nesting outside the class (-1 at top level) */
27
+ VEC(ref_t) refs;
28
+ uint32_t in_live, in_dead;
29
+ int32_t parent, root_rule, batch;
30
+ uint32_t pfile, pline; /* where the parent names it, for explain */
31
+ } def_t;
32
+ typedef VEC(def_t) def_vec;
33
+
34
+ typedef struct { uint32_t name, file; uint8_t is_const; } tref_t; /* test_refs + test_ref_files */
35
+ /* include / prepend / extend; nest is Module.nesting inside the class, innermost first, newline-separated */
36
+ typedef struct { uint32_t cls, mod; int32_t nest, res; } incl_t;
37
+ /* `def Name.x` written inside a namespace: which class Name is can only be
38
+ * known once every file is indexed. nest is Module.nesting where the def is written. */
39
+ typedef struct { uint32_t def, recv; int32_t nest; } srecv_t;
40
+ /* Reflective calls we can't read. A send-like call on self (no receiver, or
41
+ * `self.`) inside a class or module may call any method of that class. */
42
+ typedef struct {
43
+ uint32_t file, line;
44
+ const char *what;
45
+ int32_t self_ns; /* qualified name of the class it sends to, or -1 */
46
+ uint32_t owner; /* def whose body makes the call */
47
+ uint8_t public_only; /* public_send, try, try!, public_method */
48
+ uint8_t self_kind; /* K_METHOD or K_SMETHOD, by the def it is written in; K_LOAD outside any def (class body) */
49
+ } dyn_t;
50
+ typedef struct { int32_t owner; uint32_t pat, file, line; uint8_t is_const; } pref_t; /* computed names */
51
+ typedef struct { uint32_t file; char *req; } req_t; /* require "x", for gems */
52
+ typedef struct { uint32_t file, line; char *msg; } perr_t; /* files Prism rejected */
53
+ typedef VEC(tref_t) tref_vec;
54
+ typedef VEC(incl_t) incl_vec;
55
+ typedef VEC(srecv_t) srecv_vec;
56
+ typedef VEC(dyn_t) dyn_vec;
57
+ typedef VEC(pref_t) pref_vec;
58
+ typedef VEC(req_t) req_vec;
59
+ typedef VEC(perr_t) perr_vec;
60
+
61
+ extern def_vec D; /* every definition */
62
+ extern strvec FILES; /* relative paths, in walk order */
63
+ extern u8vec FTEST, FTEMPLATE; /* per file: a test file; converted from ERB */
64
+ extern tref_vec TR; /* references made from test files: they never mark anything */
65
+ extern incl_vec INC; /* include / prepend / extend with a constant argument */
66
+ extern srecv_vec SRECV; /* def Name.x inside a namespace, placed after indexing */
67
+ extern dyn_vec DYN; /* reflective calls whose target can't be read */
68
+ extern pref_vec PREF; /* computed names (:"visit_#{x}"), expanded after indexing */
69
+ extern req_vec REQ; /* require "x", for the gems command */
70
+ extern perr_vec PERR; /* Ruby files Prism rejected */
71
+ extern uint32_t TEMPLATE_ERRORS;
72
+
73
+ static inline int is_meth(int k) { return k == K_METHOD || k == K_SMETHOD; }
74
+ static inline int is_constk(int k) { return k == K_CLASS || k == K_MODULE || k == K_CONST; }
75
+ static inline const char *kind_word(int k) {
76
+ switch (k) {
77
+ case K_METHOD: case K_SMETHOD: return "method";
78
+ case K_CLASS: return "class";
79
+ case K_MODULE: return "module";
80
+ case K_CONST: return "constant";
81
+ default: return "load";
82
+ }
83
+ }
84
+
85
+ #endif
data/src/core/util.c ADDED
@@ -0,0 +1,116 @@
1
+ /* Memory, growable vectors, string buffers and small file helpers. */
2
+ #include <ctype.h>
3
+ #include <time.h>
4
+
5
+ #include "core/util.h"
6
+
7
+ static void die_oom(void) {
8
+ fputs("backburn: out of memory\n", stderr);
9
+ exit(70);
10
+ }
11
+
12
+ void *xrealloc(void *p, size_t n) {
13
+ void *q = realloc(p, n ? n : 1);
14
+ if (!q) die_oom();
15
+ return q;
16
+ }
17
+
18
+ void *xcalloc(size_t n, size_t s) {
19
+ void *q = calloc(n ? n : 1, s ? s : 1);
20
+ if (!q) die_oom();
21
+ return q;
22
+ }
23
+
24
+ char *xstrndup(const char *s, size_t n) {
25
+ char *d = xrealloc(NULL, n + 1);
26
+ memcpy(d, s, n);
27
+ d[n] = 0;
28
+ return d;
29
+ }
30
+
31
+ char *xstrdup(const char *s) { return xstrndup(s, strlen(s)); }
32
+
33
+ static void sb_grow(sbuf *b, size_t extra) {
34
+ if (b->n + extra + 1 > b->cap) {
35
+ size_t c = b->cap ? b->cap : 64;
36
+ while (c < b->n + extra + 1) c *= 2;
37
+ b->s = xrealloc(b->s, c);
38
+ b->cap = c;
39
+ }
40
+ }
41
+
42
+ void sb_put(sbuf *b, const char *s, size_t n) {
43
+ sb_grow(b, n);
44
+ if (n) memcpy(b->s + b->n, s, n);
45
+ b->n += n;
46
+ b->s[b->n] = 0;
47
+ }
48
+
49
+ void sb_puts(sbuf *b, const char *s) { sb_put(b, s, strlen(s)); }
50
+
51
+ void sb_putc(sbuf *b, char c) { sb_put(b, &c, 1); }
52
+
53
+ void sb_trunc(sbuf *b, size_t n) {
54
+ sb_grow(b, 0);
55
+ b->n = n;
56
+ b->s[n] = 0;
57
+ }
58
+
59
+ void sb_printf(sbuf *b, const char *fmt, ...) {
60
+ va_list ap, ap2;
61
+ va_start(ap, fmt);
62
+ va_copy(ap2, ap);
63
+ int k = vsnprintf(NULL, 0, fmt, ap);
64
+ va_end(ap);
65
+ if (k > 0) {
66
+ sb_grow(b, (size_t) k);
67
+ vsnprintf(b->s + b->n, (size_t) k + 1, fmt, ap2);
68
+ b->n += (size_t) k;
69
+ }
70
+ va_end(ap2);
71
+ }
72
+
73
+ int has_suffix(const char *s, const char *suf) {
74
+ size_t a = strlen(s), b = strlen(suf);
75
+ return a >= b && !memcmp(s + a - b, suf, b);
76
+ }
77
+
78
+ int cmp_cstr(const void *a, const void *b) {
79
+ return strcmp(*(char *const *) a, *(char *const *) b);
80
+ }
81
+
82
+ int u32_cmp(const void *a, const void *b) {
83
+ uint32_t x = *(const uint32_t *) a, y = *(const uint32_t *) b;
84
+ return x < y ? -1 : x > y;
85
+ }
86
+
87
+ int is_ident_char(int c) { return isalnum(c) || c == '_'; }
88
+
89
+ double now_ms(void) {
90
+ struct timespec ts;
91
+ clock_gettime(CLOCK_MONOTONIC, &ts);
92
+ return ts.tv_sec * 1000.0 + ts.tv_nsec / 1e6;
93
+ }
94
+
95
+ char *read_file(const char *path, size_t *len) {
96
+ FILE *f = fopen(path, "rb");
97
+ if (!f) return NULL;
98
+ sbuf b = {0};
99
+ char tmp[65536];
100
+ size_t k;
101
+ while ((k = fread(tmp, 1, sizeof tmp, f)) > 0) sb_put(&b, tmp, k);
102
+ fclose(f);
103
+ if (!b.s) sb_grow(&b, 0), b.s[0] = 0;
104
+ *len = b.n;
105
+ return b.s;
106
+ }
107
+
108
+ /* Single-quote a string for /bin/sh. */
109
+ void sh_quote(sbuf *b, const char *s) {
110
+ sb_putc(b, '\'');
111
+ for (; *s; s++) {
112
+ if (*s == '\'') sb_puts(b, "'\\''");
113
+ else sb_putc(b, *s);
114
+ }
115
+ sb_putc(b, '\'');
116
+ }
data/src/core/util.h ADDED
@@ -0,0 +1,48 @@
1
+ /* Memory, growable vectors, string buffers and small file helpers. */
2
+ #ifndef BACKBURN_CORE_UTIL_H
3
+ #define BACKBURN_CORE_UTIL_H
4
+
5
+ #include <stdarg.h>
6
+ #include <stddef.h>
7
+ #include <stdint.h>
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+
12
+ void *xrealloc(void *p, size_t n);
13
+ void *xcalloc(size_t n, size_t s);
14
+ char *xstrndup(const char *s, size_t n);
15
+ char *xstrdup(const char *s);
16
+
17
+ /* A growable array: VEC(T) v = {0}; PUSH(v, x); v.a[i], v.n. */
18
+ #define VEC(T) struct { T *a; size_t n, cap; }
19
+ #define PUSH(v, ...) \
20
+ do { \
21
+ if ((v).n == (v).cap) { \
22
+ (v).cap = (v).cap ? (v).cap * 2 : 8; \
23
+ (v).a = xrealloc((v).a, (v).cap * sizeof *(v).a); \
24
+ } \
25
+ (v).a[(v).n++] = (__VA_ARGS__); \
26
+ } while (0)
27
+
28
+ typedef VEC(uint8_t) u8vec;
29
+ typedef VEC(uint32_t) u32vec;
30
+ typedef VEC(char *) strvec;
31
+
32
+ /* A NUL-terminated growable string. */
33
+ typedef struct { char *s; size_t n, cap; } sbuf;
34
+ void sb_put(sbuf *b, const char *s, size_t n);
35
+ void sb_puts(sbuf *b, const char *s);
36
+ void sb_putc(sbuf *b, char c);
37
+ void sb_trunc(sbuf *b, size_t n);
38
+ void sb_printf(sbuf *b, const char *fmt, ...);
39
+
40
+ int has_suffix(const char *s, const char *suf);
41
+ int cmp_cstr(const void *a, const void *b); /* qsort comparator for char * */
42
+ int u32_cmp(const void *a, const void *b); /* qsort comparator for uint32_t */
43
+ int is_ident_char(int c);
44
+ double now_ms(void);
45
+ char *read_file(const char *path, size_t *len); /* whole file, NUL-terminated; NULL on error */
46
+ void sh_quote(sbuf *b, const char *s); /* single-quote for /bin/sh */
47
+
48
+ #endif
data/src/index/erb.c ADDED
@@ -0,0 +1,40 @@
1
+ /* ERB to Ruby, in place, keeping every newline so line numbers still match. */
2
+
3
+ #include "index/erb.h"
4
+
5
+ /* In place, keeping every newline: text outside tags becomes spaces, tag
6
+ * openers become spaces, %> becomes " ;" so tags stay separate statements,
7
+ * and <%# comments %> vanish. */
8
+ void erb_to_ruby(char *b, size_t n) {
9
+ size_t i = 0;
10
+ while (i < n) {
11
+ if (b[i] == '<' && i + 1 < n && b[i + 1] == '%') {
12
+ if (i + 2 < n && b[i + 2] == '%') { /* <%% is a literal <% */
13
+ b[i] = b[i + 1] = b[i + 2] = ' ';
14
+ i += 3;
15
+ continue;
16
+ }
17
+ int comment = i + 2 < n && b[i + 2] == '#';
18
+ b[i] = b[i + 1] = ' ';
19
+ i += 2;
20
+ while (!comment && i < n && (b[i] == '=' || b[i] == '-')) b[i++] = ' ';
21
+ while (i < n) {
22
+ if (b[i] == '%' && i + 1 < n && b[i + 1] == '>') {
23
+ if (comment) b[i] = b[i + 1] = ' ';
24
+ else {
25
+ if (i > 0 && b[i - 1] == '-') b[i - 1] = ' ';
26
+ b[i] = ' ';
27
+ b[i + 1] = ';';
28
+ }
29
+ i += 2;
30
+ break;
31
+ }
32
+ if (comment && b[i] != '\n') b[i] = ' ';
33
+ i++;
34
+ }
35
+ } else {
36
+ if (b[i] != '\n') b[i] = ' ';
37
+ i++;
38
+ }
39
+ }
40
+ }
data/src/index/erb.h ADDED
@@ -0,0 +1,9 @@
1
+ /* ERB to Ruby, in place, keeping every newline so line numbers still match. */
2
+ #ifndef BACKBURN_INDEX_ERB_H
3
+ #define BACKBURN_INDEX_ERB_H
4
+
5
+ #include <stddef.h>
6
+
7
+ void erb_to_ruby(char *b, size_t n);
8
+
9
+ #endif