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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/Makefile +50 -0
- data/README.md +520 -0
- data/docs/cli-and-outputs.md +226 -0
- data/docs/technical-spec.md +656 -0
- data/exe/backburn +8 -0
- data/ext/backburn/extconf.rb +31 -0
- data/ext/backburn/prism/COMMIT +1 -0
- data/ext/backburn/prism/LICENSE.md +7 -0
- data/ext/backburn/prism/Makefile +213 -0
- data/ext/backburn/prism/include/prism/arena.h +45 -0
- data/ext/backburn/prism/include/prism/arena.hpp +42 -0
- data/ext/backburn/prism/include/prism/ast.h +10395 -0
- data/ext/backburn/prism/include/prism/ast.hpp +641 -0
- data/ext/backburn/prism/include/prism/buffer.h +60 -0
- data/ext/backburn/prism/include/prism/buffer.hpp +55 -0
- data/ext/backburn/prism/include/prism/comments.h +72 -0
- data/ext/backburn/prism/include/prism/compiler/accel.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/align.h +36 -0
- data/ext/backburn/prism/include/prism/compiler/assume.h +25 -0
- data/ext/backburn/prism/include/prism/compiler/branch.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/bswap.h +46 -0
- data/ext/backburn/prism/include/prism/compiler/cplusplus.hpp +16 -0
- data/ext/backburn/prism/include/prism/compiler/endianness.h +34 -0
- data/ext/backburn/prism/include/prism/compiler/exceptions.hpp +17 -0
- data/ext/backburn/prism/include/prism/compiler/exported.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/fallthrough.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/filesystem.h +32 -0
- data/ext/backburn/prism/include/prism/compiler/flex_array.h +19 -0
- data/ext/backburn/prism/include/prism/compiler/force_inline.h +21 -0
- data/ext/backburn/prism/include/prism/compiler/format.h +27 -0
- data/ext/backburn/prism/include/prism/compiler/inline.h +17 -0
- data/ext/backburn/prism/include/prism/compiler/lifetimebound.hpp +25 -0
- data/ext/backburn/prism/include/prism/compiler/nodiscard.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/nonnull.h +18 -0
- data/ext/backburn/prism/include/prism/compiler/noreturn.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/stack.h +60 -0
- data/ext/backburn/prism/include/prism/compiler/unused.h +18 -0
- data/ext/backburn/prism/include/prism/constant_pool.h +112 -0
- data/ext/backburn/prism/include/prism/diagnostic.h +121 -0
- data/ext/backburn/prism/include/prism/errors_format.h +63 -0
- data/ext/backburn/prism/include/prism/excludes.h +29 -0
- data/ext/backburn/prism/include/prism/integer.h +79 -0
- data/ext/backburn/prism/include/prism/internal/allocator.h +68 -0
- data/ext/backburn/prism/include/prism/internal/allocator_debug.h +88 -0
- data/ext/backburn/prism/include/prism/internal/arena.h +223 -0
- data/ext/backburn/prism/include/prism/internal/assert.h +14 -0
- data/ext/backburn/prism/include/prism/internal/bignum.h +239 -0
- data/ext/backburn/prism/include/prism/internal/bit.h +66 -0
- data/ext/backburn/prism/include/prism/internal/buffer.h +104 -0
- data/ext/backburn/prism/include/prism/internal/char.h +145 -0
- data/ext/backburn/prism/include/prism/internal/comments.h +54 -0
- data/ext/backburn/prism/include/prism/internal/constant_pool.h +155 -0
- data/ext/backburn/prism/include/prism/internal/diagnostic.h +392 -0
- data/ext/backburn/prism/include/prism/internal/double.h +19 -0
- data/ext/backburn/prism/include/prism/internal/encoding.h +326 -0
- data/ext/backburn/prism/include/prism/internal/integer.h +66 -0
- data/ext/backburn/prism/include/prism/internal/isinf.h +16 -0
- data/ext/backburn/prism/include/prism/internal/line_offset_list.h +50 -0
- data/ext/backburn/prism/include/prism/internal/list.h +62 -0
- data/ext/backburn/prism/include/prism/internal/magic_comments.h +23 -0
- data/ext/backburn/prism/include/prism/internal/memchr.h +15 -0
- data/ext/backburn/prism/include/prism/internal/node.h +197 -0
- data/ext/backburn/prism/include/prism/internal/oom.h +16 -0
- data/ext/backburn/prism/include/prism/internal/options.h +248 -0
- data/ext/backburn/prism/include/prism/internal/parser.h +1170 -0
- data/ext/backburn/prism/include/prism/internal/regexp.h +70 -0
- data/ext/backburn/prism/include/prism/internal/serialize.h +39 -0
- data/ext/backburn/prism/include/prism/internal/source.h +72 -0
- data/ext/backburn/prism/include/prism/internal/static_literals.h +60 -0
- data/ext/backburn/prism/include/prism/internal/stringy.h +37 -0
- data/ext/backburn/prism/include/prism/internal/strncasecmp.h +18 -0
- data/ext/backburn/prism/include/prism/internal/strpbrk.h +61 -0
- data/ext/backburn/prism/include/prism/internal/tokens.h +11 -0
- data/ext/backburn/prism/include/prism/internal/xxh3.h +16 -0
- data/ext/backburn/prism/include/prism/json.h +40 -0
- data/ext/backburn/prism/include/prism/line_offset_list.h +69 -0
- data/ext/backburn/prism/include/prism/magic_comments.h +53 -0
- data/ext/backburn/prism/include/prism/node.h +139 -0
- data/ext/backburn/prism/include/prism/node.hpp +272 -0
- data/ext/backburn/prism/include/prism/node_type.hpp +38 -0
- data/ext/backburn/prism/include/prism/options.h +393 -0
- data/ext/backburn/prism/include/prism/options.hpp +43 -0
- data/ext/backburn/prism/include/prism/parser.h +433 -0
- data/ext/backburn/prism/include/prism/parser.hpp +274 -0
- data/ext/backburn/prism/include/prism/prettyprint.h +39 -0
- data/ext/backburn/prism/include/prism/serialize.h +123 -0
- data/ext/backburn/prism/include/prism/source.h +163 -0
- data/ext/backburn/prism/include/prism/stream.h +36 -0
- data/ext/backburn/prism/include/prism/string_query.h +97 -0
- data/ext/backburn/prism/include/prism/stringy.h +61 -0
- data/ext/backburn/prism/include/prism/stringy.hpp +31 -0
- data/ext/backburn/prism/include/prism/version.h +46 -0
- data/ext/backburn/prism/include/prism.h +123 -0
- data/ext/backburn/prism/include/prism.hpp +24 -0
- data/ext/backburn/prism/src/arena.c +200 -0
- data/ext/backburn/prism/src/bignum.c +399 -0
- data/ext/backburn/prism/src/buffer.c +405 -0
- data/ext/backburn/prism/src/char.c +289 -0
- data/ext/backburn/prism/src/comments.c +455 -0
- data/ext/backburn/prism/src/constant_pool.c +410 -0
- data/ext/backburn/prism/src/diagnostic.c +918 -0
- data/ext/backburn/prism/src/double.c +394 -0
- data/ext/backburn/prism/src/encoding.c +1701 -0
- data/ext/backburn/prism/src/errors_format.c +650 -0
- data/ext/backburn/prism/src/integer.c +675 -0
- data/ext/backburn/prism/src/json.c +5745 -0
- data/ext/backburn/prism/src/line_offset_list.c +99 -0
- data/ext/backburn/prism/src/list.c +24 -0
- data/ext/backburn/prism/src/memchr.c +37 -0
- data/ext/backburn/prism/src/node.c +4464 -0
- data/ext/backburn/prism/src/options.c +475 -0
- data/ext/backburn/prism/src/parser.c +371 -0
- data/ext/backburn/prism/src/prettyprint.c +9038 -0
- data/ext/backburn/prism/src/prism.c +23615 -0
- data/ext/backburn/prism/src/regexp.c +3833 -0
- data/ext/backburn/prism/src/serialize.c +2656 -0
- data/ext/backburn/prism/src/source.c +525 -0
- data/ext/backburn/prism/src/static_literals.c +562 -0
- data/ext/backburn/prism/src/string_query.c +195 -0
- data/ext/backburn/prism/src/stringy.c +77 -0
- data/ext/backburn/prism/src/strncasecmp.c +37 -0
- data/ext/backburn/prism/src/strpbrk.c +465 -0
- data/ext/backburn/prism/src/tokens.c +724 -0
- data/ext/backburn/prism/src/xxh3.c +292 -0
- data/lib/backburn/version.rb +7 -0
- data/presets/devise.roots +11 -0
- data/presets/fast-mcp.roots +5 -0
- data/presets/graphql.roots +6 -0
- data/presets/phlex.roots +7 -0
- data/presets/pundit.roots +3 -0
- data/presets/rails-framework.roots +10 -0
- data/presets/rails.roots +83 -0
- data/presets/sidekiq.roots +4 -0
- data/presets/thor.roots +3 -0
- data/rails.roots +99 -0
- data/scripts/embed-presets.sh +13 -0
- data/scripts/version-h.sh +7 -0
- data/src/analysis/analysis.c +95 -0
- data/src/analysis/analysis.h +24 -0
- data/src/analysis/batches.c +127 -0
- data/src/analysis/batches.h +17 -0
- data/src/analysis/graph.c +238 -0
- data/src/analysis/graph.h +34 -0
- data/src/analysis/mark.c +300 -0
- data/src/analysis/mark.h +21 -0
- data/src/analysis/roots.c +229 -0
- data/src/analysis/roots.h +29 -0
- data/src/cli/cli.c +237 -0
- data/src/cli/cli.h +9 -0
- data/src/cli/config.c +112 -0
- data/src/cli/config.h +14 -0
- data/src/cli/options.c +17 -0
- data/src/cli/options.h +35 -0
- data/src/commands/commands.h +11 -0
- data/src/commands/explain.c +255 -0
- data/src/commands/gemfile.c +126 -0
- data/src/commands/gemfile.h +27 -0
- data/src/commands/gems.c +408 -0
- data/src/commands/init.c +92 -0
- data/src/commands/roots.c +82 -0
- data/src/commands/scan.c +91 -0
- data/src/core/intern.c +94 -0
- data/src/core/intern.h +40 -0
- data/src/core/json.c +17 -0
- data/src/core/json.h +9 -0
- data/src/core/model.c +25 -0
- data/src/core/model.h +85 -0
- data/src/core/util.c +116 -0
- data/src/core/util.h +48 -0
- data/src/index/erb.c +40 -0
- data/src/index/erb.h +9 -0
- data/src/index/haml.c +334 -0
- data/src/index/haml.h +10 -0
- data/src/index/indexer.c +702 -0
- data/src/index/indexer.h +19 -0
- data/src/index/walk.c +144 -0
- data/src/index/walk.h +14 -0
- data/src/main.c +81 -0
- data/src/output/baseline.c +101 -0
- data/src/output/baseline.h +15 -0
- data/src/output/report.c +440 -0
- data/src/output/report.h +28 -0
- metadata +231 -0
data/src/index/indexer.h
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/* The Prism visitor: turns one parsed file into definitions and named references. */
|
|
2
|
+
#ifndef BACKBURN_INDEX_INDEXER_H
|
|
3
|
+
#define BACKBURN_INDEX_INDEXER_H
|
|
4
|
+
|
|
5
|
+
#include "prism.h"
|
|
6
|
+
|
|
7
|
+
enum { FK_NONE, FK_RUBY, FK_TEMPLATE, FK_HAML, FK_SLIM };
|
|
8
|
+
|
|
9
|
+
extern pm_options_t *OPTS;
|
|
10
|
+
|
|
11
|
+
/* Index one file: kind is FK_RUBY, or a view converted to Ruby first
|
|
12
|
+
* (FK_TEMPLATE for ERB and Thor .tt, FK_HAML, FK_SLIM). */
|
|
13
|
+
void index_file(const char *abs, const char *rel, int kind);
|
|
14
|
+
|
|
15
|
+
/* Prism helpers shared with the gems command. */
|
|
16
|
+
const pm_constant_t *safe_constant(pm_parser_t *p, pm_constant_id_t id);
|
|
17
|
+
const char *str_of(const pm_string_t *s, size_t *n);
|
|
18
|
+
|
|
19
|
+
#endif
|
data/src/index/walk.c
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/* The file walk: which files are read, and the path globs that choose them. */
|
|
2
|
+
#include <dirent.h>
|
|
3
|
+
#include <fnmatch.h>
|
|
4
|
+
#include <sys/stat.h>
|
|
5
|
+
|
|
6
|
+
#include "index/walk.h"
|
|
7
|
+
#include "core/intern.h"
|
|
8
|
+
#include "core/model.h"
|
|
9
|
+
#include "cli/options.h"
|
|
10
|
+
#include "index/indexer.h"
|
|
11
|
+
|
|
12
|
+
/* A glob matches a relative path at any depth unless it starts with /.
|
|
13
|
+
* fnmatch runs without FNM_PATHNAME, so * also crosses /, and a leading
|
|
14
|
+
* double-star-slash may match nothing. */
|
|
15
|
+
static int glob_match_at(const char *g, const char *path) {
|
|
16
|
+
if (!fnmatch(g, path, 0)) return 1;
|
|
17
|
+
if (!strncmp(g, "**/", 3) && !fnmatch(g + 3, path, 0)) return 1;
|
|
18
|
+
return 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
int path_glob(const char *g, const char *path) {
|
|
22
|
+
if (g[0] == '/') return glob_match_at(g + 1, path);
|
|
23
|
+
if (glob_match_at(g, path)) return 1;
|
|
24
|
+
for (const char *p = path; *p; p++)
|
|
25
|
+
if (*p == '/' && p[1] && glob_match_at(g, p + 1)) return 1;
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
int any_glob(const strvec *gs, const char *path) {
|
|
30
|
+
for (size_t i = 0; i < gs->n; i++)
|
|
31
|
+
if (path_glob(gs->a[i], path)) return 1;
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static int file_kind(const char *abs, const char *name) {
|
|
36
|
+
if (has_suffix(name, ".rb") || has_suffix(name, ".rake") || !strcmp(name, "Rakefile")) return FK_RUBY;
|
|
37
|
+
if (has_suffix(name, ".erb") || has_suffix(name, ".tt")) return FK_TEMPLATE;
|
|
38
|
+
if (has_suffix(name, ".haml")) return FK_HAML;
|
|
39
|
+
if (has_suffix(name, ".slim")) return FK_SLIM;
|
|
40
|
+
if (!strchr(name, '.')) {
|
|
41
|
+
FILE *f = fopen(abs, "rb");
|
|
42
|
+
if (!f) return FK_NONE;
|
|
43
|
+
char line[256];
|
|
44
|
+
int ok = fgets(line, sizeof line, f) && line[0] == '#' && line[1] == '!' && (strstr(line, "ruby") || strstr(line, "rails"));
|
|
45
|
+
fclose(f);
|
|
46
|
+
return ok ? FK_RUBY : FK_NONE;
|
|
47
|
+
}
|
|
48
|
+
return FK_NONE;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/* (dev, inode) set so symlinked directories are walked once. */
|
|
52
|
+
typedef struct { uint64_t dev, ino; uint8_t used; } dirkey_t;
|
|
53
|
+
static struct { dirkey_t *k; size_t n, cap; } DIRSET;
|
|
54
|
+
|
|
55
|
+
static size_t dir_slot(uint64_t dev, uint64_t ino, size_t cap) {
|
|
56
|
+
return (size_t) (((dev * 0x9E3779B97F4A7C15ULL) ^ ino) * 0x9E3779B97F4A7C15ULL >> 20) & (cap - 1);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* Returns 0 when the directory (dev, inode) was already walked. */
|
|
60
|
+
static int dirset_add(dev_t dev, ino_t ino) {
|
|
61
|
+
if ((DIRSET.n + 1) * 2 > DIRSET.cap) {
|
|
62
|
+
size_t nc = DIRSET.cap ? DIRSET.cap * 2 : 256;
|
|
63
|
+
dirkey_t *nk = xcalloc(nc, sizeof *nk);
|
|
64
|
+
for (size_t i = 0; i < DIRSET.cap; i++)
|
|
65
|
+
if (DIRSET.k[i].used) {
|
|
66
|
+
size_t j = dir_slot(DIRSET.k[i].dev, DIRSET.k[i].ino, nc);
|
|
67
|
+
while (nk[j].used) j = (j + 1) & (nc - 1);
|
|
68
|
+
nk[j] = DIRSET.k[i];
|
|
69
|
+
}
|
|
70
|
+
free(DIRSET.k);
|
|
71
|
+
DIRSET.k = nk;
|
|
72
|
+
DIRSET.cap = nc;
|
|
73
|
+
}
|
|
74
|
+
size_t j = dir_slot((uint64_t) dev, (uint64_t) ino, DIRSET.cap);
|
|
75
|
+
while (DIRSET.k[j].used) {
|
|
76
|
+
if (DIRSET.k[j].dev == (uint64_t) dev && DIRSET.k[j].ino == (uint64_t) ino) return 0;
|
|
77
|
+
j = (j + 1) & (DIRSET.cap - 1);
|
|
78
|
+
}
|
|
79
|
+
DIRSET.k[j] = (dirkey_t){(uint64_t) dev, (uint64_t) ino, 1};
|
|
80
|
+
DIRSET.n++;
|
|
81
|
+
return 1;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static void walk(sbuf *abs, sbuf *rel) {
|
|
85
|
+
DIR *d = opendir(abs->s);
|
|
86
|
+
if (!d) return;
|
|
87
|
+
strvec names = {0};
|
|
88
|
+
struct dirent *e;
|
|
89
|
+
while ((e = readdir(d)))
|
|
90
|
+
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) PUSH(names, xstrdup(e->d_name));
|
|
91
|
+
closedir(d);
|
|
92
|
+
qsort(names.a, names.n, sizeof *names.a, cmp_cstr);
|
|
93
|
+
size_t an = abs->n, rn = rel->n;
|
|
94
|
+
for (size_t i = 0; i < names.n; i++) {
|
|
95
|
+
const char *nm = names.a[i];
|
|
96
|
+
sb_trunc(abs, an);
|
|
97
|
+
sb_putc(abs, '/');
|
|
98
|
+
sb_puts(abs, nm);
|
|
99
|
+
sb_trunc(rel, rn);
|
|
100
|
+
if (rn) sb_putc(rel, '/');
|
|
101
|
+
sb_puts(rel, nm);
|
|
102
|
+
struct stat st;
|
|
103
|
+
if (stat(abs->s, &st)) continue;
|
|
104
|
+
if (S_ISDIR(st.st_mode)) {
|
|
105
|
+
if (!strcmp(nm, ".git") || !strcmp(nm, ".bundle") || !strcmp(nm, ".backburn")) continue;
|
|
106
|
+
sb_putc(rel, '/');
|
|
107
|
+
int skip = any_glob(&O.exclude, rel->s);
|
|
108
|
+
sb_trunc(rel, rel->n - 1);
|
|
109
|
+
if (skip || !dirset_add(st.st_dev, st.st_ino)) continue;
|
|
110
|
+
walk(abs, rel);
|
|
111
|
+
} else if (S_ISREG(st.st_mode)) {
|
|
112
|
+
if (any_glob(&O.exclude, rel->s)) continue;
|
|
113
|
+
int k = file_kind(abs->s, nm);
|
|
114
|
+
if (k) index_file(abs->s, rel->s, k);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
sb_trunc(abs, an);
|
|
118
|
+
sb_trunc(rel, rn);
|
|
119
|
+
for (size_t i = 0; i < names.n; i++) free(names.a[i]);
|
|
120
|
+
free(names.a);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
void index_tree(const char *root) {
|
|
124
|
+
OPTS = pm_options_new();
|
|
125
|
+
intern_words();
|
|
126
|
+
def_t load;
|
|
127
|
+
memset(&load, 0, sizeof load);
|
|
128
|
+
load.kind = K_LOAD;
|
|
129
|
+
load.name = load.qual = intern_cstr("<load time>");
|
|
130
|
+
load.super_ = load.outer = -1;
|
|
131
|
+
load.parent = -1;
|
|
132
|
+
load.root_rule = NOT_ROOT;
|
|
133
|
+
load.batch = -1;
|
|
134
|
+
PUSH(D, load);
|
|
135
|
+
sbuf abs = {0}, rel = {0};
|
|
136
|
+
sb_puts(&abs, root);
|
|
137
|
+
while (abs.n > 1 && abs.s[abs.n - 1] == '/') sb_trunc(&abs, abs.n - 1);
|
|
138
|
+
sb_trunc(&rel, 0);
|
|
139
|
+
struct stat st;
|
|
140
|
+
if (!stat(abs.s, &st)) dirset_add(st.st_dev, st.st_ino);
|
|
141
|
+
walk(&abs, &rel);
|
|
142
|
+
free(abs.s);
|
|
143
|
+
free(rel.s);
|
|
144
|
+
}
|
data/src/index/walk.h
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* The file walk: which files are read, and the path globs that choose them. */
|
|
2
|
+
#ifndef BACKBURN_INDEX_WALK_H
|
|
3
|
+
#define BACKBURN_INDEX_WALK_H
|
|
4
|
+
|
|
5
|
+
#include "core/util.h"
|
|
6
|
+
|
|
7
|
+
/* A glob matches a relative path at any depth unless it starts with /. */
|
|
8
|
+
int path_glob(const char *glob, const char *path);
|
|
9
|
+
int any_glob(const strvec *globs, const char *path);
|
|
10
|
+
|
|
11
|
+
/* Walk root and index every Ruby, ERB and Ruby-script file in it. */
|
|
12
|
+
void index_tree(const char *root);
|
|
13
|
+
|
|
14
|
+
#endif
|
data/src/main.c
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* backburn: reports Ruby definitions that no production code can reach.
|
|
3
|
+
*
|
|
4
|
+
* It parses a codebase with Prism, builds a name-resolved reference graph,
|
|
5
|
+
* marks everything reachable from a set of entry points (Trémaux's maze rule:
|
|
6
|
+
* chalk each passage the first time, never walk a chalked one twice), and
|
|
7
|
+
* lists what stays unmarked. Framework knowledge lives only in roots files
|
|
8
|
+
* (presets compiled in, or project files), never in this code.
|
|
9
|
+
*
|
|
10
|
+
* core/ helpers, the string interner, the definition tables, JSON
|
|
11
|
+
* index/ the file walk, ERB conversion and the Prism visitor
|
|
12
|
+
* analysis/ the reference graph, roots, marking, classification, batches
|
|
13
|
+
* cli/ options, the command line and the config file
|
|
14
|
+
* output/ reports and baselines
|
|
15
|
+
* commands/ scan, batches, explain, roots, gems, init
|
|
16
|
+
*/
|
|
17
|
+
#include <errno.h>
|
|
18
|
+
#include <stdlib.h>
|
|
19
|
+
#include <sys/stat.h>
|
|
20
|
+
#include <unistd.h>
|
|
21
|
+
|
|
22
|
+
#include "analysis/analysis.h"
|
|
23
|
+
#include "analysis/roots.h"
|
|
24
|
+
#include "cli/cli.h"
|
|
25
|
+
#include "cli/options.h"
|
|
26
|
+
#include "commands/commands.h"
|
|
27
|
+
#include "index/indexer.h"
|
|
28
|
+
|
|
29
|
+
static void default_globs(void) {
|
|
30
|
+
static const char *EXCLUDE[] = {"vendor/**", "node_modules/**", "tmp/**", "log/**", "coverage/**", NULL};
|
|
31
|
+
static const char *TESTS[] = {"test/**", "spec/**", "**/*_test.rb", "**/*_spec.rb", NULL};
|
|
32
|
+
if (!O.exclude.n)
|
|
33
|
+
for (int k = 0; EXCLUDE[k]; k++) PUSH(O.exclude, (char *) EXCLUDE[k]);
|
|
34
|
+
if (!O.tests.n)
|
|
35
|
+
for (int k = 0; TESTS[k]; k++) PUSH(O.tests, (char *) TESTS[k]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/* Opened only once the options and roots are known good, so a usage error
|
|
39
|
+
* never truncates an existing report. */
|
|
40
|
+
static int open_output(void) {
|
|
41
|
+
OUT = stdout;
|
|
42
|
+
if (O.output && !(OUT = fopen(O.output, "w"))) {
|
|
43
|
+
fprintf(stderr, "backburn: cannot write %s: %s\n", O.output, strerror(errno));
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
COLOR = !strcmp(O.color_when, "always") ||
|
|
47
|
+
(!strcmp(O.color_when, "auto") && isatty(fileno(OUT)) && !getenv("NO_COLOR") && O.format == F_TEXT);
|
|
48
|
+
return 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static int run(void) {
|
|
52
|
+
switch (O.cmd) {
|
|
53
|
+
case CMD_GEMS: return cmd_gems();
|
|
54
|
+
case CMD_EXPLAIN: return cmd_explain();
|
|
55
|
+
case CMD_ROOTS: return cmd_roots();
|
|
56
|
+
default: return cmd_scan();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
int main(int argc, char **argv) {
|
|
61
|
+
parse_args(argc, argv);
|
|
62
|
+
if (O.cmd == CMD_INIT) {
|
|
63
|
+
if (!open_output()) return 2;
|
|
64
|
+
OPTS = pm_options_new();
|
|
65
|
+
return cmd_init();
|
|
66
|
+
}
|
|
67
|
+
struct stat st;
|
|
68
|
+
if (stat(O.root, &st) || !S_ISDIR(st.st_mode)) usage_error("%s is not a directory", O.root);
|
|
69
|
+
apply_config();
|
|
70
|
+
default_globs();
|
|
71
|
+
load_all_rules();
|
|
72
|
+
if (!open_output()) return 2;
|
|
73
|
+
char *abs = realpath(O.root, NULL);
|
|
74
|
+
ROOT_ABS = abs ? abs : xstrdup(O.root);
|
|
75
|
+
|
|
76
|
+
analyse();
|
|
77
|
+
compute_ids();
|
|
78
|
+
int code = run();
|
|
79
|
+
if (OUT != stdout) fclose(OUT);
|
|
80
|
+
return code;
|
|
81
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/* Baselines: accept today's dead code so CI only fails on new dead code. */
|
|
2
|
+
#include <errno.h>
|
|
3
|
+
|
|
4
|
+
#include "output/baseline.h"
|
|
5
|
+
#include "core/model.h"
|
|
6
|
+
#include "core/json.h"
|
|
7
|
+
#include "cli/options.h"
|
|
8
|
+
#include "analysis/analysis.h"
|
|
9
|
+
|
|
10
|
+
strvec BASELINE;
|
|
11
|
+
|
|
12
|
+
int HAVE_BASELINE;
|
|
13
|
+
|
|
14
|
+
char *git_head(void) {
|
|
15
|
+
static char *cached;
|
|
16
|
+
static int done;
|
|
17
|
+
if (done) return cached;
|
|
18
|
+
done = 1;
|
|
19
|
+
sbuf cmd = {0};
|
|
20
|
+
sb_puts(&cmd, "git -C ");
|
|
21
|
+
sh_quote(&cmd, O.root);
|
|
22
|
+
sb_puts(&cmd, " rev-parse --short HEAD 2>/dev/null");
|
|
23
|
+
FILE *p = popen(cmd.s, "r");
|
|
24
|
+
free(cmd.s);
|
|
25
|
+
if (!p) return NULL;
|
|
26
|
+
char buf[64] = {0};
|
|
27
|
+
if (fgets(buf, sizeof buf, p)) {
|
|
28
|
+
buf[strcspn(buf, "\r\n")] = 0;
|
|
29
|
+
if (*buf) cached = xstrdup(buf);
|
|
30
|
+
}
|
|
31
|
+
pclose(p);
|
|
32
|
+
return cached;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/* Reads the "ids" array of a baseline file: just the JSON strings in it. */
|
|
36
|
+
int read_baseline(const char *path) {
|
|
37
|
+
size_t n;
|
|
38
|
+
char *t = read_file(path, &n);
|
|
39
|
+
if (!t) return 0;
|
|
40
|
+
char *p = strstr(t, "\"ids\"");
|
|
41
|
+
p = p ? strchr(p, '[') : NULL;
|
|
42
|
+
while (p && *p && *p != ']') {
|
|
43
|
+
if (*p == '"') {
|
|
44
|
+
sbuf s = {0};
|
|
45
|
+
p++;
|
|
46
|
+
while (*p && *p != '"') {
|
|
47
|
+
if (*p == '\\' && p[1]) {
|
|
48
|
+
p++;
|
|
49
|
+
char c = *p == 'n' ? '\n' : *p == 't' ? '\t' : *p;
|
|
50
|
+
sb_putc(&s, c);
|
|
51
|
+
} else {
|
|
52
|
+
sb_putc(&s, *p);
|
|
53
|
+
}
|
|
54
|
+
p++;
|
|
55
|
+
}
|
|
56
|
+
PUSH(BASELINE, s.s ? s.s : xstrdup(""));
|
|
57
|
+
}
|
|
58
|
+
if (*p) p++;
|
|
59
|
+
}
|
|
60
|
+
free(t);
|
|
61
|
+
qsort(BASELINE.a, BASELINE.n, sizeof *BASELINE.a, cmp_cstr);
|
|
62
|
+
return 1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
int in_baseline(const char *id) {
|
|
66
|
+
return bsearch(&id, BASELINE.a, BASELINE.n, sizeof *BASELINE.a, cmp_cstr) != NULL;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
int write_baseline_file(void) {
|
|
70
|
+
const char *path = O.baseline ? O.baseline : root_path(".backburn-baseline.json");
|
|
71
|
+
strvec ids = {0};
|
|
72
|
+
for (uint32_t i = 1; i < D.n; i++)
|
|
73
|
+
if (!D.a[i].live) {
|
|
74
|
+
sbuf b = {0};
|
|
75
|
+
def_id(&b, i);
|
|
76
|
+
PUSH(ids, b.s);
|
|
77
|
+
}
|
|
78
|
+
qsort(ids.a, ids.n, sizeof *ids.a, cmp_cstr);
|
|
79
|
+
FILE *f = fopen(path, "w");
|
|
80
|
+
if (!f) {
|
|
81
|
+
fprintf(stderr, "backburn: cannot write %s: %s\n", path, strerror(errno));
|
|
82
|
+
return 2;
|
|
83
|
+
}
|
|
84
|
+
fputs("{\n \"schema\": 1,\n \"commit\": ", f);
|
|
85
|
+
char *g = git_head();
|
|
86
|
+
if (g) json_str(f, g);
|
|
87
|
+
else fputs("null", f);
|
|
88
|
+
fputs(",\n \"ids\": [", f);
|
|
89
|
+
size_t w = 0;
|
|
90
|
+
for (size_t k = 0; k < ids.n; k++) {
|
|
91
|
+
if (k && !strcmp(ids.a[k], ids.a[k - 1])) continue;
|
|
92
|
+
fputs(w++ ? ",\n " : "\n ", f);
|
|
93
|
+
json_str(f, ids.a[k]);
|
|
94
|
+
}
|
|
95
|
+
fputs(w ? "\n ]\n}\n" : "]\n}\n", f);
|
|
96
|
+
fclose(f);
|
|
97
|
+
fprintf(stderr, "backburn: wrote %s (%zu ids)\n", path, w);
|
|
98
|
+
for (size_t k = 0; k < ids.n; k++) free(ids.a[k]);
|
|
99
|
+
free(ids.a);
|
|
100
|
+
return 0;
|
|
101
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/* Baselines: accept today's dead code so CI only fails on new dead code. */
|
|
2
|
+
#ifndef BACKBURN_OUTPUT_BASELINE_H
|
|
3
|
+
#define BACKBURN_OUTPUT_BASELINE_H
|
|
4
|
+
|
|
5
|
+
#include "core/util.h"
|
|
6
|
+
|
|
7
|
+
extern strvec BASELINE; /* sorted ids */
|
|
8
|
+
extern int HAVE_BASELINE;
|
|
9
|
+
|
|
10
|
+
int read_baseline(const char *path);
|
|
11
|
+
int in_baseline(const char *id);
|
|
12
|
+
int write_baseline_file(void);
|
|
13
|
+
char *git_head(void); /* short commit of the project, or NULL */
|
|
14
|
+
|
|
15
|
+
#endif
|