From 9572e7fb4967bce13c6bb91b6a54ffed00f43ae3 Mon Sep 17 00:00:00 2001 From: tsingbx Date: Fri, 10 Jan 2025 12:47:07 +0800 Subject: [PATCH] remove RunMode flag & add WithExpand flag --- cmd/llcppcfg/llcppcfg.go | 8 +------- cmd/llcppcfg/llcppgcfg/cfg.go | 15 ++++----------- cmd/llcppcfg/llcppgcfg/cfg_test.go | 24 ++++++++---------------- 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/cmd/llcppcfg/llcppcfg.go b/cmd/llcppcfg/llcppcfg.go index 48976906..e853cf2e 100644 --- a/cmd/llcppcfg/llcppcfg.go +++ b/cmd/llcppcfg/llcppcfg.go @@ -36,12 +36,6 @@ func main() { name = flag.Arg(0) } - fnToCfgExpandMode := func() llcppgcfg.RunMode { - if expand { - return llcppgcfg.ExpandMode - } - return llcppgcfg.NormalMode - } exts := strings.Fields(extsString) excludeSubdirs := []string{} if len(excludes) > 0 { @@ -54,7 +48,7 @@ func main() { if sortByDep { flag |= llcppgcfg.WithSort } - buf, err := llcppgcfg.GenCfg(name, flag, fnToCfgExpandMode(), exts, excludeSubdirs) + buf, err := llcppgcfg.GenCfg(name, flag, exts, excludeSubdirs) if err != nil { log.Fatal(err) } diff --git a/cmd/llcppcfg/llcppgcfg/cfg.go b/cmd/llcppcfg/llcppgcfg/cfg.go index 25ad18c3..2ed8aa50 100644 --- a/cmd/llcppcfg/llcppgcfg/cfg.go +++ b/cmd/llcppcfg/llcppgcfg/cfg.go @@ -15,19 +15,13 @@ import ( "github.com/goplus/llcppg/types" ) -type RunMode int - type FlagMode int -const ( - NormalMode RunMode = iota - ExpandMode -) - const ( WithSort FlagMode = 1 << iota WithNoTab WithCpp + WithExpand ) type emptyStringError struct { @@ -298,13 +292,12 @@ func NormalizePackageName(name string) string { return strings.Join(fields, "_") } -func GenCfg(name string, flag FlagMode, expand RunMode, exts []string, excludeSubdirs []string) (*bytes.Buffer, error) { +func GenCfg(name string, flag FlagMode, exts []string, excludeSubdirs []string) (*bytes.Buffer, error) { if len(name) == 0 { return nil, newEmptyStringError("name") } cfg := NewLLCppConfig(name, flag) - switch expand { - case ExpandMode: + if flag&WithExpand != 0 { if flag&WithSort != 0 { cfg.CFlags, _ = ExpandName(name, "", "cflags") cfg.Libs, _ = ExpandName(name, "", "libs") @@ -312,7 +305,7 @@ func GenCfg(name string, flag FlagMode, expand RunMode, exts []string, excludeSu } else { expandCFlagsAndLibs(name, cfg, "", exts, excludeSubdirs) } - case NormalMode: + } else { if flag&WithSort != 0 { expandCFlags, _ := ExpandName(name, "", "cflags") sortIncludes(expandCFlags, cfg, exts, excludeSubdirs) diff --git a/cmd/llcppcfg/llcppgcfg/cfg_test.go b/cmd/llcppcfg/llcppgcfg/cfg_test.go index 6b627606..bf9d594f 100644 --- a/cmd/llcppcfg/llcppgcfg/cfg_test.go +++ b/cmd/llcppcfg/llcppgcfg/cfg_test.go @@ -828,7 +828,6 @@ func TestGenCfg(t *testing.T) { type args struct { name string flag FlagMode - expand RunMode exts []string excludeSubdirs []string } @@ -843,7 +842,6 @@ func TestGenCfg(t *testing.T) { args{ "libcjson", WithSort, - NormalMode, []string{".h"}, []string{}, }, @@ -855,7 +853,6 @@ func TestGenCfg(t *testing.T) { args{ "bdw-gc", WithSort, - NormalMode, []string{".h"}, []string{}, }, @@ -867,7 +864,6 @@ func TestGenCfg(t *testing.T) { args{ "libffi", WithSort, - NormalMode, []string{".h"}, []string{}, }, @@ -879,7 +875,6 @@ func TestGenCfg(t *testing.T) { args{ "", WithSort, - NormalMode, []string{".h"}, []string{}, }, @@ -890,8 +885,7 @@ func TestGenCfg(t *testing.T) { "expand", args{ "libcjson", - WithSort, - ExpandMode, + WithSort | WithExpand, []string{".h"}, []string{}, }, @@ -902,8 +896,7 @@ func TestGenCfg(t *testing.T) { "expand_not_sort", args{ "libcjson", - 0, - ExpandMode, + WithExpand, []string{".h"}, []string{}, }, @@ -915,7 +908,6 @@ func TestGenCfg(t *testing.T) { args{ "libcjson", 0, - NormalMode, []string{".h"}, []string{}, }, @@ -925,19 +917,19 @@ func TestGenCfg(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GenCfg(tt.args.name, tt.args.flag, tt.args.expand, tt.args.exts, tt.args.excludeSubdirs) + got, err := GenCfg(tt.args.name, tt.args.flag, tt.args.exts, tt.args.excludeSubdirs) if (err != nil) != tt.wantErr { t.Errorf("GenCfg() error = %v, wantErr %v", err, tt.wantErr) return } - if tt.args.expand == NormalMode && tt.args.flag&WithSort != 0 { - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GenCfg() = %v, want %v", got, tt.want) - } - } else { + if tt.args.flag&WithExpand != 0 { if got.Len() <= 0 { t.Errorf("GenCfg() = %v, want expaned", got) } + } else { + if tt.args.flag&WithSort != 0 && !reflect.DeepEqual(got, tt.want) { + t.Errorf("GenCfg() = %v, want %v", got, tt.want) + } } }) }