From c666cd6cd0d05e4ce809f38ebdef498019460d56 Mon Sep 17 00:00:00 2001 From: henderkes Date: Sun, 24 May 2026 21:38:22 +0700 Subject: [PATCH] deduplicate_flags: keep paired flag+value tokens together deduplicate_flags() split flags on whitespace then ran a per-token unique. For paired flags like `-Xclang -mllvm` or `-framework Cocoa`, where the value is a separate token, the value token could collide with an unrelated flag or value and get dropped, corrupting the command line. Group known paired flags (-Xclang, -Xpreprocessor, -Xlinker, -Xassembler, -framework, -arch, -target, -include, -imacros, -isystem, -isysroot, -iquote, -idirafter, -MT, -MF, -MQ) with their following token into a single atom before the unique pass. --- src/globals/functions.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/globals/functions.php b/src/globals/functions.php index 549ef498..c824bd3d 100644 --- a/src/globals/functions.php +++ b/src/globals/functions.php @@ -256,10 +256,30 @@ function clean_spaces(string $string): string */ function deduplicate_flags(string $flags): string { - $tokens = preg_split('/\s+/', trim($flags)); + // Flags that take their value as a separate token. + static $paired = [ + '-Xclang', '-Xpreprocessor', '-Xlinker', '-Xassembler', + '-framework', '-arch', '-target', + '-include', '-imacros', '-isystem', '-isysroot', '-iquote', '-idirafter', + '-MT', '-MF', '-MQ', + ]; + + $tokens = preg_split('/\s+/', trim($flags)) ?: []; + + // Group paired flag+value into a single atom before dedup. + $atoms = []; + $n = count($tokens); + for ($i = 0; $i < $n; ++$i) { + if (in_array($tokens[$i], $paired, true) && $i + 1 < $n) { + $atoms[] = $tokens[$i] . ' ' . $tokens[$i + 1]; + ++$i; + } else { + $atoms[] = $tokens[$i]; + } + } // Reverse, unique, reverse back - keeps last occurrence of duplicates - $deduplicated = array_reverse(array_unique(array_reverse($tokens))); + $deduplicated = array_reverse(array_unique(array_reverse($atoms))); return implode(' ', $deduplicated); }