Skip to content

Commit

Permalink
fix: update parser for get compile name, do escape \" in json.Marshal…
Browse files Browse the repository at this point in the history
…Indent
  • Loading branch information
fcying committed Aug 2, 2024
1 parent bb3f1e1 commit 9003748
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 23 deletions.
1 change: 1 addition & 0 deletions .nvim.lua
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Option.asyncrun_auto_close_qf = false
vim.g.asynctasks_term_pos = 'bottom'
2 changes: 1 addition & 1 deletion .tasks
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build]
# command=go run ./cmd/compiledb/main.go -v
command=go run ./cmd/compiledb/main.go -v -c -S --full-path -p ./tests/build.log
command=go run ./cmd/compiledb/main.go -v -c -S --full-path < ./tests/build.log
output=terminal

[release]
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Compilation Database Generator

rewrite [nickdiego/compiledb](https://github.com/nickdiego/compiledb) in Go for speed.
test using a build_log file over 2MB in size with over 200 valid entries
test using a build log over 2MB size and over 200 valid entries
```
# make -Bnkw > build.log
compiledb-go
# time ~/go/bin/compiledb -n make
# time ~/go/bin/compiledb -p build.log
~/go/bin/compiledb -n make 0.21s user 0.02s system 106% cpu 0.210 total
compiledb-python
# time ~/.local/bin/compiledb -n make
# time ~/.local/bin/compiledb -p build.log
~/.local/bin/compiledb -n make 5.21s user 0.01s system 100% cpu 5.179 total
```

Expand Down
2 changes: 1 addition & 1 deletion cmd/compiledb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/urfave/cli/v2"
)

var Version string = "v1.2.0"
var Version string = "v1.2.1"

func init() {
log.SetOutput(os.Stdout)
Expand Down
2 changes: 1 addition & 1 deletion internal/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func Generate() {
)
defer file.Close()

if ParseConfig.InputFile != "" {
if ParseConfig.InputFile != "stdin" {
file, err = os.OpenFile(ParseConfig.InputFile, os.O_RDONLY, 0444)
if err != nil {
log.Fatalf("open %v failed!", ParseConfig.InputFile)
Expand Down
37 changes: 26 additions & 11 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

// Internal variables used to parse build log entries
var cc_compile_regex = regexp.MustCompile(`^.*-?(gcc|clang)-?.*(\.exe)?$`)
var cpp_compile_regex = regexp.MustCompile(`^.*-?([gc]|clang)\+\+-?.*(\.exe)?$`)
var compile_regex = regexp.MustCompile(`^.*-?(gcc|clang|cc|g\+\+|c\+\+|clang\+\+)-?.*(\.exe)?`)
var sh_regex = regexp.MustCompile(`^.*(;|&&|&|\|)`)

var file_regex = regexp.MustCompile(`^.*-c\s(.*\.(c|cpp|cc|cxx|c\+\+|s|m|mm|cu))(\s.*$|$)`)
var file_regex = regexp.MustCompile(`^.*-c.*\s(.*\.(c|cpp|cc|cxx|c\+\+|s|m|mm|cu))(\s.*$|$)`)
var compiler_wrappers []string = []string{"ccache", "icecc", "sccache"}

// Leverage `make --print-directory` option
Expand All @@ -26,9 +26,24 @@ var checking_make = regexp.MustCompile(`^\s?checking whether .*(yes|no)$`)
func commandProcess(line string, workingDir string) ([]string, string) {
arguments := []string{}
filepath := ""
if cc_compile_regex.MatchString(line) ||
cpp_compile_regex.MatchString(line) {
if compile_regex.MatchString(line) {
// not escape \", json.MarshalIndent will do it
line = strings.ReplaceAll(line, `\"`, `"`)

arguments = strings.Fields(line)

// check compile word
for i, word := range arguments {
if compile_regex.MatchString(word) {
arguments = arguments[i:]
index := sh_regex.FindStringIndex(word)
if index != nil {
arguments[0] = word[index[1]:]
}
break
}
}

group := file_regex.FindStringSubmatch(line)
if group != nil {
filepath = group[1]
Expand Down Expand Up @@ -73,7 +88,7 @@ func Parse(buildLog []string) {
continue
}
line = strings.TrimSpace(line)
log.Println("New command:", line)
log.Debug("New command:", line)

// Parse directory that make entering/leaving
if make_enter_dir.MatchString(line) {
Expand Down Expand Up @@ -101,12 +116,12 @@ func Parse(buildLog []string) {

// Parse command
arguments, filepath := commandProcess(line, workingDir)
command := ""
compileFullPath := ""
if filepath != "" {
if ParseConfig.NoStrict == false {
if FileExist(filepath) == false {
log.Printf("file %s not exist", filepath)
fileFullPath := workingDir + "/" + filepath
if FileExist(fileFullPath) == false {
log.Printf("file %s not exist", fileFullPath)
continue
}
}
Expand All @@ -130,8 +145,8 @@ func Parse(buildLog []string) {
arguments = append(arguments, strings.Fields(ParseConfig.Macros)...)
}

command := strings.Join(arguments, " ")
if ParseConfig.CommandStyle {
command = strings.Join(arguments, " ")
data := struct {
Directory string `json:"directory"`
Command string `json:"command"`
Expand All @@ -154,7 +169,7 @@ func Parse(buildLog []string) {
}
ParseResult = append(ParseResult, data)
}
log.Printf("Adding command %d: %s", CommandCnt, line)
log.Printf("Adding command %d: %s", CommandCnt, command)
CommandCnt += 1
}
}
Expand Down
13 changes: 7 additions & 6 deletions tests/build.log
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
make: Entering directory 'out/test'

clang -target pi32v2 -mcpu=r3 -Wno-invalid-noreturn -Oz -mllvm -pi32v2-large-program=true -w -I../include -MM -MT objs/main1.c.o main1.c -o objs/main1.c.d
clang -target pi32v2 -mcpu=r3 -Wno-invalid-noreturn -Oz -I../include -MM -MT objs/main1.c.o main1.c -o objs/main1.c.d

clang -target pi32v2 -mcpu=r3 -Wno-invalid-noreturn -Oz -mllvm -pi32v2-large-program=true -w -I../include -c main1.c -o objs/main1.c.o
clang -target pi32v2 -mcpu=r3 -Wno-invalid-noreturn -Oz -I../include -c main1.c -o objs/main1.c.o

clang.exe -target pi32v2 -mcpu=r3 -Wno-invalid-noreturn -Oz -mllvm -pi32v2-large-program=true -w -I../include -c main2.cc -o objs/main2.cc.o
clang.exe -target pi32v2 -mcpu=r3 -Wno-invalid-noreturn -Oz -I../include -c main2.cc -o objs/main2.cc.o

gcc -target pi32v2 -mcpu=r3 -Wno-invalid-noreturn -Oz -mllvm -pi32v2-large-program=true -w -I../include -c main3.c -o objs/main3.c.o
gcc -target pi32v2 -mcpu=r3 -c -Wno-invalid-noreturn -Oz -I../include main3.c -o objs/main3.c.o

g++ -c test1.cpp
c++ -c test2.cpp
ccache-clang-11++ -c test3.cpp
ccache-clang-11++ -c test2.cpp

printf 't' 1>&2;cc -c -DINC=\"t.h\" test3.c

make: Leaving directory 'out/test'

0 comments on commit 9003748

Please sign in to comment.