Skip to content

Commit

Permalink
output invlid header files
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingbx committed Mar 6, 2025
1 parent fd53f5b commit cefbc09
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/llcppcfg/llcppcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
if tab {
flag |= llcppgcfg.WithTab
}
buf, err := llcppgcfg.GenCfg(name, flag, exts, excludeSubdirs)
buf, err := llcppgcfg.GenCfg(llcppgcfg.NewGenConfig(name, flag, exts, excludeSubdirs))
if err != nil {
log.Fatal(err)
}
Expand Down
47 changes: 32 additions & 15 deletions cmd/llcppcfg/llcppgcfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package llcppgcfg
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
Expand All @@ -15,6 +16,17 @@ import (
"github.com/goplus/llcppg/llcppg"
)

type GenConfig struct {
name string
flag FlagMode
exts []string
excludeSubdirs []string
}

func NewGenConfig(name string, flag FlagMode, exts, excludeSubdirs []string) *GenConfig {
return &GenConfig{name: name, flag: flag, exts: exts, excludeSubdirs: excludeSubdirs}
}

type llcppCfgKey string

const (
Expand Down Expand Up @@ -104,9 +116,9 @@ func getClangArgs(cflags string, relpath string) []string {
return args
}

func parseFileEntry(cflags, trimCflag, path string, d fs.DirEntry, exts []string, excludeSubdirs []string) *ObjFile {
func parseFileEntry(cflags, trimCflag, path string, d fs.DirEntry, exts []string, excludeSubdirs []string) (*ObjFile, error) {
if d.IsDir() || strings.HasPrefix(d.Name(), ".") {
return nil
return nil, errors.New("invalid file entry")
}
idx := len(exts)
for i, ext := range exts {
Expand All @@ -116,28 +128,28 @@ func parseFileEntry(cflags, trimCflag, path string, d fs.DirEntry, exts []string
}
}
if idx == len(exts) {
return nil
return nil, errors.New("invalid file ext")
}
relPath, err := filepath.Rel(trimCflag, path)
if err != nil {
relPath = path
}
if isExcludeDir(relPath, excludeSubdirs) {
return nil
return nil, errors.New("file in excluded directory")
}
args := getClangArgs(cflags, relPath)
clangCmd := cmdout.NewExecCommand("clang", args...)
outString, err := cmdout.GetOut(clangCmd, trimCflag)
if err != nil {
log.Println(outString)
return nil
return NewObjFile(relPath, relPath), errors.New(outString)
}
outString = strings.ReplaceAll(outString, "\\\n", "\n")
fields := strings.Fields(outString)
lines, objFileStr := findDepSlice(fields)
objFile := NewObjFileString(objFileStr)
objFile.Deps = append(objFile.Deps, lines...)
return objFile
return objFile, nil
}

func parseCFlagsEntry(cflags, cflag string, exts []string, excludeSubdirs []string) *CflagEntry {
Expand All @@ -151,11 +163,18 @@ func parseCFlagsEntry(cflags, cflag string, exts []string, excludeSubdirs []stri
var cflagEntry CflagEntry
cflagEntry.Include = trimCflag
cflagEntry.ObjFiles = make([]*ObjFile, 0)
cflagEntry.InvalidObjFiles = make([]*ObjFile, 0)
err := filepath.WalkDir(trimCflag, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
pObjFile := parseFileEntry(cflags, trimCflag, path, d, exts, excludeSubdirs)
pObjFile, err := parseFileEntry(cflags, trimCflag, path, d, exts, excludeSubdirs)
if err != nil {
if pObjFile != nil {
cflagEntry.InvalidObjFiles = append(cflagEntry.InvalidObjFiles, pObjFile)
}
return nil
}
if pObjFile != nil {
cflagEntry.ObjFiles = append(cflagEntry.ObjFiles, pObjFile)
}
Expand Down Expand Up @@ -204,19 +223,17 @@ func NormalizePackageName(name string) string {
return strings.Join(fields, "_")
}

func GenCfg(name string, flag FlagMode, exts []string, excludeSubdirs []string) (*bytes.Buffer, error) {
if len(name) == 0 {
func GenCfg(genCfg *GenConfig) (*bytes.Buffer, error) {
if len(genCfg.name) == 0 {
return nil, newEmptyStringError("name")
}
cfg := NewLLCppgConfig(name, flag)
expandCFlags := ExpandName(name, "", cfgCflagsKey)
sortIncludes(expandCFlags, cfg, exts, excludeSubdirs)

cfg := NewLLCppgConfig(genCfg.name, genCfg.flag)
expandCFlags := ExpandName(genCfg.name, "", cfgCflagsKey)
sortIncludes(expandCFlags, cfg, genCfg.exts, genCfg.excludeSubdirs)
cfg.Name = NormalizePackageName(cfg.Name)

buf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(buf)
if flag&WithTab != 0 {
if genCfg.flag&WithTab != 0 {
jsonEncoder.SetIndent("", "\t")
}
err := jsonEncoder.Encode(cfg)
Expand Down
4 changes: 2 additions & 2 deletions cmd/llcppcfg/llcppgcfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func Test_parseFileEntry(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseFileEntry(tt.args.cflags, tt.args.trimStr, tt.args.path, tt.args.d, tt.args.exts, tt.args.excludeSubdirs)
got, _ := parseFileEntry(tt.args.cflags, tt.args.trimStr, tt.args.path, tt.args.d, tt.args.exts, tt.args.excludeSubdirs)
if tt.want != nil && got != nil && !got.IsEqual(tt.want) {
t.Errorf("parseFileEntry() = %v, want %v", got, tt.want)
}
Expand Down Expand Up @@ -643,7 +643,7 @@ 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.exts, tt.args.excludeSubdirs)
got, err := GenCfg(NewGenConfig(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
Expand Down
16 changes: 14 additions & 2 deletions cmd/llcppcfg/llcppgcfg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ func (p *IncludeList) AddCflagEntry(index int, entry *CflagEntry) {
p.AddIncludeForObjFile(objFile, index)
}
}
lenInvalidObjFiles := len(entry.InvalidObjFiles)
if lenInvalidObjFiles > 0 {
fmt.Println("Invlid header files:")
for idx, objFile := range entry.InvalidObjFiles {
if idx < lenInvalidObjFiles-1 {
fmt.Printf("\t\t%q,\n", objFile.HFile)
} else {
fmt.Printf("\t\t%q\n", objFile.HFile)
}
}
}
}

func (p *IncludeList) AddIncludeForObjFile(objFile *ObjFile, index int) {
Expand All @@ -88,8 +99,9 @@ func (p *IncludeList) AddIncludeForObjFile(objFile *ObjFile, index int) {
}

type CflagEntry struct {
Include string
ObjFiles []*ObjFile
Include string
ObjFiles []*ObjFile
InvalidObjFiles []*ObjFile
}

func (c *CflagEntry) IsEmpty() bool {
Expand Down

0 comments on commit cefbc09

Please sign in to comment.