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
# Extract a string literal from a SymbolNode or StringNode arg. Returns # "" if the arg is not a literal we recognize. # Source-file directory, analyze-side (mirrors codegen's # source_file_dirname). Used to fold __dir__ in FFI decl args. def ffi_source_dirname path = "" if @source_file_path != nil path = @source_file_path end if path == "" i = 0 while i < @nd_type.length if @nd_type[i] == "SourceFileNode" path = @nd_content[i] i = @nd_type.length else i = i + 1 end end end if path == "" return "." end last_slash = -1 j = 0 while j < path.length if path[j] == "/" last_slash = j end j = j + 1 end if last_slash < 0 return "." end if last_slash == 0 return "/" end path[0, last_slash] end # Resolve `rel` against `base` like File.expand_path: absolute rel # wins; otherwise join and collapse `.` / `..` segments. Enough for # the FFI-decl path-folding use (Issue #1011); not a full realpath # (no `~` expansion, no symlink resolution). def ffi_expand_path(rel, base) joined = "" if rel.length > 0 && rel[0] == "/" joined = rel elsif base.length > 0 && base[0] == "/" joined = base + "/" + rel else joined = base + "/" + rel end parts = joined.split("/", -1) stack = "".split(",", -1) pi = 0 while pi < parts.length seg = parts[pi] if seg == "" || seg == "." # skip empty (from leading / or //) and current-dir elsif seg == ".." if stack.length > 0 stack.pop end else stack.push(seg) end pi = pi + 1 end "/" + stack.join("/") end # Fold an FFI decl string argument: literals, plus the compile-time # forms __dir__ / __FILE__ / File.expand_path(rel, base) / String#+ # over foldable strings. Returns "" when the arg isn't foldable. # Issue #1011. def ffi_arg_str(nid) if nid < 0 return "" if t == "SymbolNode" || t == "StringNode" return @nd_content[nid] end # __FILE__ lowers to SourceFileNode (its content is the path). if t == "SourceFileNode" return @nd_content[nid] end if t == "CallNode" mn = @nd_name[nid] recv = @nd_receiver[nid] # __dir__ -- compile-time source directory. if mn == "__dir__" && recv < 0 return ffi_source_dirname end # File.expand_path(rel, base=__dir__) -- fold when rel folds. if mn == "expand_path" && recv >= 0 && @nd_type[recv] == "ConstantReadNode" && @nd_name[recv] == "File" ea = get_args(@nd_arguments[nid]) if ea.length >= 1 rel = ffi_arg_str(ea[0]) if rel != "" base = ea.length >= 2 ? ffi_arg_str(ea[1]) : ffi_source_dirname if base == "" base = ffi_source_dirname end return ffi_expand_path(rel, base) end end end # String#+ over two foldable strings. if mn == "+" && recv >= 0 lhs = ffi_arg_str(recv) if lhs != "" pa = get_args(@nd_arguments[nid]) if pa.length >= 1 rhs = ffi_arg_str(pa[0]) if rhs != "" return lhs + rhs end end end end end "" endtest/i1011.rbaddedView file
Loading diff…
test/i1011.rb.expectedaddedView file
Loading diff…