From 0935d10edf6e292645547092d386ca8c25aac3ed Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 27 Aug 2025 19:36:46 +0800 Subject: [PATCH] xtool/safesplit:properly handle consecutive short flags --- internal/clang/clang_test.go | 6 ++++++ xtool/safesplit/safesplit.go | 7 +++++++ xtool/safesplit/safesplit_test.go | 12 +++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/internal/clang/clang_test.go b/internal/clang/clang_test.go index 390bcb1b..fecaf256 100644 --- a/internal/clang/clang_test.go +++ b/internal/clang/clang_test.go @@ -446,6 +446,12 @@ func TestFlagMergingScenarios(t *testing.T) { expectComp: []string{"-O3", "-fPIC", "-Wall", "-Wextra", "-std=c11"}, expectLink: []string{"-O3", "-lm", "-lpthread", "-static"}, }, + { + // case from https://github.com/goplus/llgo/issues/1244 + name: "issue 1244", + envCFlags: "-w -pipe -mmacosx-version-min=15 -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk", + expectComp: []string{"-w", "-pipe", "-mmacosx-version-min=15", "-isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk"}, + }, } // Save original environment diff --git a/xtool/safesplit/safesplit.go b/xtool/safesplit/safesplit.go index 58f2c242..71783bcd 100644 --- a/xtool/safesplit/safesplit.go +++ b/xtool/safesplit/safesplit.go @@ -49,6 +49,13 @@ func SplitPkgConfigFlags(s string) []string { for i < len(s) && (s[i] == ' ' || s[i] == '\t') { i++ } + + // Check if next character is another flag (short flag with no argument) + if i < len(s) && s[i] == '-' { + // This is a short flag with no argument, finish current flag + continue + } + // Read content until next space for i < len(s) { if s[i] == '\\' && i+1 < len(s) && (s[i+1] == ' ' || s[i+1] == '\t') { diff --git a/xtool/safesplit/safesplit_test.go b/xtool/safesplit/safesplit_test.go index cbb8c9cc..ccc60318 100644 --- a/xtool/safesplit/safesplit_test.go +++ b/xtool/safesplit/safesplit_test.go @@ -50,8 +50,8 @@ func TestSplitPkgConfigFlags(t *testing.T) { }) t.Run("consecutive_flags", func(t *testing.T) { - ftest("-I -L", `["-I-L"]`) - ftest("-I -L /usr/lib", `["-I-L /usr/lib"]`) + ftest("-I -L", `["-I" "-L"]`) + ftest("-I -L /usr/lib", `["-I" "-L/usr/lib"]`) }) t.Run("edge_cases", func(t *testing.T) { @@ -59,7 +59,7 @@ func TestSplitPkgConfigFlags(t *testing.T) { ftest(" ", "[]") ftest("-", `["-"]`) ftest("-I", `["-I"]`) - ftest("-I -", `["-I-"]`) + ftest("-I -", `["-I" "-"]`) }) t.Run("escaped_spaces", func(t *testing.T) { @@ -77,6 +77,12 @@ func TestSplitPkgConfigFlags(t *testing.T) { ftest("-DVERSION=2.1 -DDEBUG=1", `["-DVERSION=2.1" "-DDEBUG=1"]`) ftest("-D VERSION=2.1 -D DEBUG=1", `["-DVERSION=2.1" "-DDEBUG=1"]`) }) + + // case for https://github.com/goplus/llgo/issues/1244 + t.Run("w_pipe", func(t *testing.T) { + ftest("-w -pipe", `["-w" "-pipe"]`) + ftest("-Os -w -pipe", `["-Os" "-w" "-pipe"]`) + }) } func toString(ss []string) string {