Skip to content

Commit e49596f

Browse files
committed
feat: parser cd
1 parent 3370c40 commit e49596f

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

.tasks

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
command=go run ./cmd/compiledb/main.go -v -c --full-path < ./tests/build.log
44
cwd=$(VIM_ROOT)
55
output=terminal
6-
close=1
6+
close=0
77

88
[release]
99
command=go install ./cmd/compiledb && GOOS=windows go install ./cmd/compiledb && mv ~/go/bin/windows_amd64/compiledb.exe ~/workspace/go/bin

internal/parser.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var RegexCompile string = `^.*-?(gcc|clang|cc|g\+\+|c\+\+|clang\+\+)-?.*(\.exe)?
2020
var RegexFile string = `^.*\s-c.*\s(.*\.(c|cpp|cc|cxx|c\+\+|s|m|mm|cu))(\s.*$|$)`
2121

2222
// Internal variables used to parse build log entries
23+
var cd_regex = regexp.MustCompile(`^.*cd\s(.*?)\s(;|&|\|)`)
2324
var sh_regex = regexp.MustCompile(`^.*(;|&&|&|\|)`)
2425
var compile_regex *regexp.Regexp
2526
var file_regex *regexp.Regexp
@@ -71,11 +72,13 @@ func commandProcess(line string, workingDir string) ([]string, string) {
7172

7273
func Parse(buildLog []string) {
7374
var (
74-
err error
75-
workingDir string
76-
exclude_regex *regexp.Regexp
77-
cmdCnt = 0
78-
result []Command
75+
err error
76+
workingDir = ""
77+
backupWorkingDir = ""
78+
exclude_regex *regexp.Regexp
79+
cmdCnt = 0
80+
result []Command
81+
matchGroup []string
7982
)
8083

8184
// check workingDir
@@ -94,7 +97,7 @@ func Parse(buildLog []string) {
9497

9598
dirStack := []string{workingDir}
9699

97-
//init regex
100+
// init regex
98101
if ParseConfig.Exclude != "" {
99102
exclude_regex, err = regexp.Compile(ParseConfig.Exclude)
100103
if err != nil {
@@ -117,6 +120,13 @@ func Parse(buildLog []string) {
117120
line = strings.TrimSpace(line)
118121
log.Debug("New command:", line)
119122

123+
// Restore workingDir
124+
if backupWorkingDir != "" {
125+
workingDir = backupWorkingDir
126+
backupWorkingDir = ""
127+
log.Infof("Restore workingDir: %s", workingDir)
128+
}
129+
120130
// Parse directory that make entering/leaving
121131
if make_enter_dir.MatchString(line) {
122132
group := make_enter_dir.FindStringSubmatch(line)
@@ -138,6 +148,19 @@ func Parse(buildLog []string) {
138148
continue
139149
}
140150

151+
// Parse cd xx
152+
matchGroup = cd_regex.FindStringSubmatch(line)
153+
if matchGroup != nil {
154+
backupWorkingDir = workingDir
155+
cdPath := matchGroup[1]
156+
if IsAbsPath(cdPath) == false {
157+
workingDir = workingDir + "/" + cdPath
158+
} else {
159+
workingDir = cdPath
160+
}
161+
log.Infof("Temporarily change workingDir: %s", workingDir)
162+
}
163+
141164
if checking_make.MatchString(line) {
142165
continue
143166
}

tests/build.log

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ make: Leaving directory '/opt/compiledb_test/src'
2222
make[1]: Entering directory `/opt/compiledb_test/src2"
2323

2424
gcc -c ../fullpath.c
25+
26+
cd ../ && gcc -c fullpath.c
27+
cd /opt/compiledb_test/ && gcc -c fullpath.c

0 commit comments

Comments
 (0)