Skip to content

Commit

Permalink
fix unexpected include list for libcjson
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingbx committed Jan 10, 2025
1 parent fe85cf1 commit dbdb87d
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 11 deletions.
15 changes: 4 additions & 11 deletions cmd/llcppcfg/llcppgcfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,12 @@ func parseCFlagsEntry(cflags, cflag string, exts []string, excludeSubdirs []stri

func sortIncludes(expandCflags string, cfg *LLCppConfig, exts []string, excludeSubdirs []string) {
list := strings.Fields(expandCflags)
cflagEntryList := make([]*CflagEntry, 0)
for _, cflag := range list {
includeList := NewIncludeList()
for i, cflag := range list {
pCflagEntry := parseCFlagsEntry(expandCflags, cflag, exts, excludeSubdirs)
if pCflagEntry != nil {
cflagEntryList = append(cflagEntryList, pCflagEntry)
}
}
cfg.Include = make([]string, 0)
for _, cflagEntry := range cflagEntryList {
for _, objFile := range cflagEntry.ObjFiles {
cfg.Include = append(cfg.Include, objFile.HFile)
}
includeList.AddCflagEntry(i, pCflagEntry)
}
cfg.Include = includeList.include
}

func NewLLCppConfig(name string, flag FlagMode) *LLCppConfig {
Expand Down
28 changes: 28 additions & 0 deletions cmd/llcppcfg/llcppgcfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,31 @@ func Test_getDir(t *testing.T) {
})
}
}

func Test_getClangArgs(t *testing.T) {
type args struct {
cflags string
relpath string
}
tests := []struct {
name string
args args
want []string
}{
{
"cjson",
args{
cflags: ExpandName("libcjson", "", cfgCflagsKey),
relpath: "cJSON.h",
},
[]string{"-I/usr/local/Cellar/cjson/1.7.18/include", "-I/usr/local/Cellar/cjson/1.7.18/include/cjson", "-MM", "cJSON.h"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getClangArgs(tt.args.cflags, tt.args.relpath); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getClangArgs() = %v, want %v", got, tt.want)
}
})
}
}
37 changes: 37 additions & 0 deletions cmd/llcppcfg/llcppgcfg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package llcppgcfg

import (
"fmt"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -49,7 +50,43 @@ func (p *ObjFile) String() string {
return fmt.Sprintf("{OFile:%s, HFile:%s, Deps:%v}", p.OFile, p.HFile, p.Deps)
}

type IncludeList struct {
include []string
absPathMap map[string]struct{}
relPathMap map[string]struct{}
}

func NewIncludeList() *IncludeList {
return &IncludeList{include: make([]string, 0), absPathMap: make(map[string]struct{}), relPathMap: make(map[string]struct{})}
}

func (p *IncludeList) AddCflagEntry(index int, entry *CflagEntry) {
if entry == nil {
return
}
entry.ID = index
for _, objFile := range entry.ObjFiles {
absPath := filepath.Join(entry.Include, objFile.HFile)
_, ok := p.absPathMap[absPath]
if !ok {
p.absPathMap[absPath] = struct{}{}
p.AddIncludeForObjFile(objFile, entry.ID)
}
}
}

func (p *IncludeList) AddIncludeForObjFile(objFile *ObjFile, entryID int) {
hFile := objFile.HFile
_, ok := p.relPathMap[objFile.HFile]
if ok {
hFile = fmt.Sprintf("%s@%d", objFile.HFile, entryID)
}
p.relPathMap[hFile] = struct{}{}
p.include = append(p.include, hFile)
}

type CflagEntry struct {
ID int // -I index
Include string
ObjFiles []*ObjFile
}
Expand Down
157 changes: 157 additions & 0 deletions cmd/llcppcfg/llcppgcfg/types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package llcppgcfg

import (
"reflect"
"sort"
"strings"
"testing"
)

Expand Down Expand Up @@ -232,3 +235,157 @@ func TestObjFile_IsEqual(t *testing.T) {
})
}
}

func TestNewIncludeList(t *testing.T) {
tests := []struct {
name string
want *IncludeList
}{
{
"new",
NewIncludeList(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewIncludeList()
if got == nil || tt.want == nil {
t.Errorf("NewIncludeList() returns nil, want not nil")
}
})
}
}

func TestIncludeList_AddCflagEntry(t *testing.T) {
cjsonExpandCflags := ExpandName("libcjson", "", cfgCflagsKey)
cflagsList := strings.Fields(cjsonExpandCflags)
trimCflags0 := strings.TrimPrefix(strings.TrimSpace(cflagsList[0]), "-I")
trimCflags1 := strings.TrimPrefix(strings.TrimSpace(cflagsList[1]), "-I")
type fields struct {
include []string
absPathMap map[string]struct{}
relPathMap map[string]struct{}
}
type args struct {
index int
entry *CflagEntry
}
tests := []struct {
name string
fields fields
args []args
want []string
}{
{
"cjson",
fields{
make([]string, 0),
make(map[string]struct{}),
make(map[string]struct{}),
},
[]args{
{
0,
&CflagEntry{
ID: 0,
Include: trimCflags0,
ObjFiles: []*ObjFile{
{OFile: "cJSON_Utils.o", HFile: "cjson/cJSON_Utils.h", Deps: []string{"cjson/cJSON.h"}},
{OFile: "cJSON.o", HFile: "cjson/cJSON.h", Deps: []string{}},
},
},
},
{
1,
&CflagEntry{
ID: 0,
Include: trimCflags1,
ObjFiles: []*ObjFile{
{OFile: "cJSON_Utils.o", HFile: "cJSON_Utils.h", Deps: []string{"cJSON.h"}},
{OFile: "cJSON.o", HFile: "cJSON.h", Deps: []string{}},
},
},
},
},
[]string{
"cjson/cJSON_Utils.h",
"cjson/cJSON.h",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &IncludeList{
include: tt.fields.include,
absPathMap: tt.fields.absPathMap,
relPathMap: tt.fields.relPathMap,
}
for _, arg := range tt.args {
p.AddCflagEntry(arg.index, arg.entry)
}
sort.Strings(tt.want)
sort.Strings(p.include)
if !reflect.DeepEqual(p.include, tt.want) {
t.Errorf("AddCflagEntry got %v, want %v", p.include, tt.want)
}
})
}
}

func TestIncludeList_AddIncludeForObjFile(t *testing.T) {
type fields struct {
include []string
absPathMap map[string]struct{}
relPathMap map[string]struct{}
}
type args struct {
objFile *ObjFile
entryID int
}
tests := []struct {
name string
fields fields
args []args
want []string
}{
{
"cjson",
fields{
make([]string, 0),
make(map[string]struct{}),
make(map[string]struct{}),
},
[]args{
{
&ObjFile{OFile: "cJSON_Utils.o", HFile: "cjson/cJSON_Utils.h", Deps: []string{"cjson/cJSON.h"}},
0,
},
{
&ObjFile{OFile: "cJSON.o", HFile: "cjson/cJSON.h", Deps: []string{}},
0,
},
},
[]string{
"cjson/cJSON_Utils.h",
"cjson/cJSON.h",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &IncludeList{
include: tt.fields.include,
absPathMap: tt.fields.absPathMap,
relPathMap: tt.fields.relPathMap,
}
for _, arg := range tt.args {
p.AddIncludeForObjFile(arg.objFile, arg.entryID)
}
sort.Strings(tt.want)
sort.Strings(p.include)
if !reflect.DeepEqual(p.include, tt.want) {
t.Errorf("AddIncludeForObjFile got %v, want %v", p.include, tt.want)
}
})
}
}

0 comments on commit dbdb87d

Please sign in to comment.