Commit 55ac5f22

55ac5f222251Merged

Chris Hasiński <[email protected]> · 7 months ago · 4 files · +3 added · ~1 modified

Description

fix(parser): dedupe require_relative inlines via canonical-path tracking

If two files both require_relative 'lib/types', the parser inlined the same content twice, producing duplicate struct sp_X_s decls in the generated C and a "redefinition" compile error. Ruby's load-once semantics had no equivalent here.

Adds a small (256-entry) static array of canonical paths already inlined; subsequent require_relatives of the same file are replaced with a comment instead of the file content. Uses realpath() so relative-path-vs-symlink-vs-cwd variants resolve to the same key.

Adds test/bm_require_dedup/ with a main.rb that requires lib/types both directly and transitively through lib/data; demonstrates the fix without a redefinition error.

Details

Commit
55ac5f222251
Parent
a68f283157ff

spinel_parse.cmodifiedView file

@@ -761,8 +761,53 @@
761761 return buf;
762762 }
763763
764+/* Track files already inlined so duplicate requires/require_relatives in
765+ different files don't re-emit (and re-define structs/classes) the same
766+ content. Dynamic so we don't silently drop entries on large projects. */
767+static char **sp_included_paths = NULL;
768+static int sp_included_count = 0;
769+static int sp_included_cap = 0;
770+
771+/* Resolve a path to its canonical form for dedup. realpath() returns NULL
772+ on missing files; in that case fall back to the literal path. */
773+static char *sp_canonical_path(const char *path) {
774+ char *real = realpath(path, NULL);
775+ return real ? real : strdup(path);
776+}
777+
778+static int sp_path_already_included(const char *canonical) {
779+ for (int i = 0; i < sp_included_count; i++) {
780+ if (strcmp(sp_included_paths[i], canonical) == 0) return 1;
781+ }
782+ return 0;
783+}
784+
785+static void sp_mark_path_included(const char *canonical) {
786+ if (sp_included_count >= sp_included_cap) {
787+ sp_included_cap = sp_included_cap == 0 ? 16 : sp_included_cap * 2;
788+ sp_included_paths = (char **)realloc(sp_included_paths,
789+ sizeof(char *) * sp_included_cap);
790+ }
791+ sp_included_paths[sp_included_count++] = strdup(canonical);
792+}
793+
794+/* Free the included-paths table at end of run. The process is short-lived,
795+ so this matters mostly for tools (leak checkers, embedders) that
796+ scrutinise end-of-run state. */
797+static void sp_includes_free(void) {
798+ for (int i = 0; i < sp_included_count; i++) {
799+ free(sp_included_paths[i]);
800+ }
801+ free(sp_included_paths);
802+ sp_included_paths = NULL;
803+ sp_included_count = 0;
804+ sp_included_cap = 0;
805+}
806+
764807 /* Simple require_relative resolver: replace lines matching
765- require_relative "path" with the file content */
808+ require_relative "path" with the file content. Files that have
809+ already been included once are silently skipped on subsequent
810+ requires (matching Ruby's load-once semantics). */
766811 static char *resolve_requires(const char *source, const char *source_path) {
767812 /* Get base directory */
768813 char *path_copy = strdup(source_path);
@@ -815,14 +860,24 @@
815860 if (!strstr(full_path, ".rb"))
816861 strcat(full_path, ".rb");
817862
818- char *content = read_file(full_path);
819- if (!content) {
820- content = strdup("# require_relative not found");
863+ char *canonical = sp_canonical_path(full_path);
864+ char *content;
865+ if (sp_path_already_included(canonical)) {
866+ /* Already inlined once -- replace require with empty content */
867+ content = strdup("# require_relative skipped (already included)");
868+ free(canonical);
821869 } else {
822- /* Recursively resolve */
823- char *resolved = resolve_requires(content, full_path);
824- free(content);
825- content = resolved;
870+ sp_mark_path_included(canonical);
871+ content = read_file(full_path);
872+ if (!content) {
873+ content = strdup("# require_relative not found");
874+ } else {
875+ /* Recursively resolve */
876+ char *resolved = resolve_requires(content, full_path);
877+ free(content);
878+ content = resolved;
879+ }
880+ free(canonical);
826881 }
827882
828883 /* Replace the line */