Merge pull request #868 from morluto/fix/swift-demangle-arguments

fix(native): pass Swift symbols to swift-demangle as operands
This commit is contained in:
Kaoru0822
2026-10-06 23:00:45 -07:00
committed by GitHub
11 changed files with 175 additions and 16 deletions
+2 -4
View File
@@ -13,6 +13,7 @@ import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { PRODUCT_IDENTITY } from "../identity.js";
import { logCliCommand } from "../cliLogging.js";
import { CLI_COMMANDS } from "../cliCommandNames.js";
import { swiftSymbolsSchema } from "../contracts/nativeToolContracts.js";
import type { Logger } from "../logger.js";
import { isReferenceSourceImportCliFailure } from "./referenceSourceImportStatus.js";
import type { CliInstance } from "./types.js";
@@ -152,10 +153,7 @@ const registerNativeCommands = (cli: CliInstance, logger: Logger): void => {
description: "Demangle Swift symbols without Hopper",
args: z.object({
path: z.string().describe("Artifact path used for evidence identity"),
symbols: z
.array(z.string().min(1))
.min(1)
.describe("Swift mangled symbols to demangle"),
symbols: swiftSymbolsSchema.describe("Swift mangled symbols to demangle"),
}),
run: ({ args }) =>
logCliCommand(logger, "demangle-swift", () =>
+12 -2
View File
@@ -48,6 +48,16 @@ const native = <Name extends string, Schema extends z.ZodObject>(
} satisfies ToolContract<Name, Schema, typeof outputSchema>;
};
/** Ordered single-line symbols; `swift-demangle` ends each result with LF. */
export const swiftSymbolsSchema = z
.array(
z
.string()
.min(1)
.regex(/^[^\n]*$/u, "Each Swift symbol must be one line."),
)
.min(1);
/** Provider-neutral semantic operations backed initially by macOS utilities. */
export const NATIVE_TOOL_CONTRACTS = [
native(
@@ -84,9 +94,9 @@ export const NATIVE_TOOL_CONTRACTS = [
),
native(
"demangle_swift",
"Demangle an ordered list of Swift symbols without requiring Hopper. Each input returns demangled, unchanged, or invalid status.",
"Demangle an ordered list of single-line Swift symbols without requiring Hopper. Each input returns demangled, unchanged, or invalid status.",
z.object({
symbols: z.array(z.string().min(1)).min(1),
symbols: swiftSymbolsSchema,
}),
),
] as const satisfies readonly ToolContract[];
File diff suppressed because one or more lines are too long
+2 -1
View File
@@ -270,7 +270,8 @@ class NativeMacOSClient implements AnalysisClient {
const capture = await this.#run(
"demangle_swift",
"swift-demangle",
["--compact", ...symbols.data],
// `--` keeps symbols that begin with `-` from selecting demangler options.
["--compact", "--", ...symbols.data],
{ signal },
);
if (!capture.ok) return capture;
+10 -1
View File
@@ -10,11 +10,20 @@ it.each(["ordinary name ", "ordinary\t", " "])(
},
);
it.each(["carriage\r", "\r", "mid\rdle"])(
"keeps carriage returns that belong to the symbol: %j",
(input) => {
expect(parseDemangledSymbols([input], `${input}\n`)).toEqual([
{ input, output: input, status: "unchanged" },
]);
},
);
it("removes only the line terminator and preserves mixed output ordering", () => {
expect(
parseDemangledSymbols(
["$s4main5helloyyF", "plain "],
"main.hello() -> ()\r\nplain \r\n",
"main.hello() -> ()\nplain \n",
),
).toEqual([
{
+6 -2
View File
@@ -6,12 +6,16 @@ const resultSchema = z.object({
status: z.enum(["demangled", "unchanged", "invalid"]),
});
/** Preserve input ordering while parsing `swift-demangle --compact` lines. */
/**
* Preserve input ordering while parsing `swift-demangle --compact` lines.
* The demangler ends each record with LF, so a carriage return belongs to the
* symbol text.
*/
export const parseDemangledSymbols = (
inputs: readonly string[],
output: string,
) => {
const lines = output.replace(/\r?\n$/u, "").split(/\r?\n/u);
const lines = output.replace(/\n$/u, "").split("\n");
if (lines.length !== inputs.length)
throw new TypeError("swift-demangle output count does not match input");
return inputs.map((input, index) => {