Commit 9f0bdd93

9f0bdd93efc7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

analyze: fold dir/File.expand_path/String#+ in FFI decl args

ffi_cflags File.expand_path("x.o", __dir__) errored ("argument must be a string literal") — only String/Symbol literals folded, so gems shipping a C artifact had to template the path at build time.

ffi_arg_str now also folds the compile-time string forms: dir (source directory), FILE (SourceFileNode), File.expand_path(rel, base=dir) with ./.. segment collapsing, and String#+ over two foldable strings. Applies to ffi_lib and ffi_cflags (both route through ffi_arg_str). Not a full realpath (no ~ / symlink resolution) — enough to make a gem's FFI path references self-contained from source position.

Fixes #1011

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
9f0bdd93efc7
Parent
77a68413b2c6

spinel_analyze.rbmodifiedView file

@@ -12404,6 +12404,80 @@
1240412404
1240512405 # Extract a string literal from a SymbolNode or StringNode arg. Returns
1240612406 # "" if the arg is not a literal we recognize.
12407+ # Source-file directory, analyze-side (mirrors codegen's
12408+ # source_file_dirname). Used to fold __dir__ in FFI decl args.
12409+ def ffi_source_dirname
12410+ path = ""
12411+ if @source_file_path != nil
12412+ path = @source_file_path
12413+ end
12414+ if path == ""
12415+ i = 0
12416+ while i < @nd_type.length
12417+ if @nd_type[i] == "SourceFileNode"
12418+ path = @nd_content[i]
12419+ i = @nd_type.length
12420+ else
12421+ i = i + 1
12422+ end
12423+ end
12424+ end
12425+ if path == ""
12426+ return "."
12427+ end
12428+ last_slash = -1
12429+ j = 0
12430+ while j < path.length
12431+ if path[j] == "/"
12432+ last_slash = j
12433+ end
12434+ j = j + 1
12435+ end
12436+ if last_slash < 0
12437+ return "."
12438+ end
12439+ if last_slash == 0
12440+ return "/"
12441+ end
12442+ path[0, last_slash]
12443+ end
12444+
12445+ # Resolve `rel` against `base` like File.expand_path: absolute rel
12446+ # wins; otherwise join and collapse `.` / `..` segments. Enough for
12447+ # the FFI-decl path-folding use (Issue #1011); not a full realpath
12448+ # (no `~` expansion, no symlink resolution).
12449+ def ffi_expand_path(rel, base)
12450+ joined = ""
12451+ if rel.length > 0 && rel[0] == "/"
12452+ joined = rel
12453+ elsif base.length > 0 && base[0] == "/"
12454+ joined = base + "/" + rel
12455+ else
12456+ joined = base + "/" + rel
12457+ end
12458+ parts = joined.split("/", -1)
12459+ stack = "".split(",", -1)
12460+ pi = 0
12461+ while pi < parts.length
12462+ seg = parts[pi]
12463+ if seg == "" || seg == "."
12464+ # skip empty (from leading / or //) and current-dir
12465+ elsif seg == ".."
12466+ if stack.length > 0
12467+ stack.pop
12468+ end
12469+ else
12470+ stack.push(seg)
12471+ end
12472+ pi = pi + 1
12473+ end
12474+ "/" + stack.join("/")
12475+ end
12476+
12477+ # Fold an FFI decl string argument: literals, plus the compile-time
12478+ # forms __dir__ / __FILE__ / File.expand_path(rel, base) / String#+
12479+ # over foldable strings. Returns "" when the arg isn't foldable.
12480+ # Issue #1011.
1240712481 def ffi_arg_str(nid)
1240812482 if nid < 0
1240912483 return ""
@@ -12412,6 +12486,45 @@
1241212486 if t == "SymbolNode" || t == "StringNode"
1241312487 return @nd_content[nid]
1241412488 end
12489+ # __FILE__ lowers to SourceFileNode (its content is the path).
12490+ if t == "SourceFileNode"
12491+ return @nd_content[nid]
12492+ end
12493+ if t == "CallNode"
12494+ mn = @nd_name[nid]
12495+ recv = @nd_receiver[nid]
12496+ # __dir__ -- compile-time source directory.
12497+ if mn == "__dir__" && recv < 0
12498+ return ffi_source_dirname
12499+ end
12500+ # File.expand_path(rel, base=__dir__) -- fold when rel folds.
12501+ if mn == "expand_path" && recv >= 0 && @nd_type[recv] == "ConstantReadNode" && @nd_name[recv] == "File"
12502+ ea = get_args(@nd_arguments[nid])
12503+ if ea.length >= 1
12504+ rel = ffi_arg_str(ea[0])
12505+ if rel != ""
12506+ base = ea.length >= 2 ? ffi_arg_str(ea[1]) : ffi_source_dirname
12507+ if base == ""
12508+ base = ffi_source_dirname
12509+ end
12510+ return ffi_expand_path(rel, base)
12511+ end
12512+ end
12513+ end
12514+ # String#+ over two foldable strings.
12515+ if mn == "+" && recv >= 0
12516+ lhs = ffi_arg_str(recv)
12517+ if lhs != ""
12518+ pa = get_args(@nd_arguments[nid])
12519+ if pa.length >= 1
12520+ rhs = ffi_arg_str(pa[0])
12521+ if rhs != ""
12522+ return lhs + rhs
12523+ end
12524+ end
12525+ end
12526+ end
12527+ end
1241512528 ""
1241612529 end
1241712530