-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgomodextractor.go
202 lines (166 loc) · 5.27 KB
/
gomodextractor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package extractor
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/mod/modfile"
"github.com/github/codeql-go/extractor/dbscheme"
"github.com/github/codeql-go/extractor/srcarchive"
"github.com/github/codeql-go/extractor/trap"
)
func (extraction *Extraction) extractGoMod(path string) error {
if normPath, err := filepath.EvalSymlinks(path); err == nil {
path = normPath
}
extraction.Lock.Lock()
if extraction.SeenGoMods[path] {
extraction.Lock.Unlock()
return nil
}
extraction.SeenGoMods[path] = true
extraction.Lock.Unlock()
tw, err := trap.NewWriter(path, nil)
if err != nil {
return err
}
defer tw.Close()
err = srcarchive.Add(path)
if err != nil {
return err
}
extraction.extractFileInfo(tw, path, false)
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open go.mod file %s: %s", path, err.Error())
}
data, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("failed to read go.mod file %s: %s", path, err.Error())
}
modfile, err := modfile.Parse(path, data, nil)
if err != nil {
return fmt.Errorf("failed to parse go.mod file %s: %s", path, err.Error())
}
extractGoModFile(tw, modfile.Syntax)
return nil
}
type commentGroupIdxAllocator struct {
nextIdx int
}
func (cgIdxAlloc *commentGroupIdxAllocator) nextCgIdx() int {
ret := cgIdxAlloc.nextIdx
cgIdxAlloc.nextIdx++
return ret
}
func extractGoModFile(tw *trap.Writer, file *modfile.FileSyntax) {
cgIdxAlloc := commentGroupIdxAllocator{0}
for idx, stmt := range file.Stmt {
extractGoModExpr(tw, stmt, tw.Labeler.FileLabel(), idx, &cgIdxAlloc)
}
extractGoModComments(tw, file, tw.Labeler.FileLabel(), &cgIdxAlloc)
}
func extractGoModExpr(tw *trap.Writer, expr modfile.Expr, parent trap.Label, idx int, cgIdxAlloc *commentGroupIdxAllocator) {
lbl := tw.Labeler.LocalID(expr)
var kind int
switch expr := expr.(type) {
case *modfile.CommentBlock:
kind = dbscheme.ModCommentBlockType.Index()
case *modfile.LParen:
kind = dbscheme.ModLParenType.Index()
case *modfile.RParen:
kind = dbscheme.ModRParenType.Index()
case *modfile.Line:
kind = dbscheme.ModLineType.Index()
for idx, tok := range expr.Token {
dbscheme.ModTokensTable.Emit(tw, tok, lbl, idx)
}
case *modfile.LineBlock:
kind = dbscheme.ModLineBlockType.Index()
for idx, tok := range expr.Token {
dbscheme.ModTokensTable.Emit(tw, tok, lbl, idx)
}
extractGoModExpr(tw, &expr.LParen, lbl, 0, cgIdxAlloc)
for idx, line := range expr.Line {
extractGoModExpr(tw, line, lbl, idx+1, cgIdxAlloc)
}
extractGoModExpr(tw, &expr.RParen, lbl, len(expr.Line)+1, cgIdxAlloc)
default:
log.Fatalf("unknown go.mod expression of type %T", expr)
}
dbscheme.ModExprsTable.Emit(tw, lbl, kind, parent, idx)
extractGoModComments(tw, expr, lbl, cgIdxAlloc)
start, end := expr.Span()
extractLocation(tw, lbl, start.Line, start.LineRune, end.Line, end.LineRune)
}
type GoModExprCommentWrapper struct {
expr modfile.Expr
}
func minInt(a int, b int) int {
if a < b {
return a
}
return b
}
func maxInt(a int, b int) int {
if a > b {
return a
}
return b
}
func lexMin(a1 int, a2 int, b1 int, b2 int) (int, int) {
if a1 < b1 {
return a1, a2
} else if a1 > b1 {
return b1, b2
} else {
return a1, minInt(b1, b2)
}
}
func lexMax(a1 int, a2 int, b1 int, b2 int) (int, int) {
if a1 < b1 {
return b1, b2
} else if a1 > b1 {
return a1, a2
} else {
return a1, maxInt(b1, b2)
}
}
func extractGoModComments(tw *trap.Writer, expr modfile.Expr, exprlbl trap.Label, cgIdxAlloc *commentGroupIdxAllocator) {
comments := expr.Comment()
if len(comments.Before) == 0 && len(comments.Suffix) == 0 && len(comments.After) == 0 {
return
}
// extract a pseudo `@commentgroup` for each expr that contains their associated comments
grouplbl := tw.Labeler.LocalID(GoModExprCommentWrapper{expr})
dbscheme.CommentGroupsTable.Emit(tw, grouplbl, tw.Labeler.FileLabel(), cgIdxAlloc.nextCgIdx())
dbscheme.DocCommentsTable.Emit(tw, exprlbl, grouplbl)
var allComments []modfile.Comment
allComments = append(allComments, comments.Before...)
allComments = append(allComments, comments.Suffix...)
allComments = append(allComments, comments.After...)
var startLine, startCol, endLine, endCol int = 0, 0, 0, 0
var first bool = true
idx := 0
for _, comment := range allComments {
commentToken := strings.TrimSuffix(strings.TrimSuffix(comment.Token, "\n"), "\r")
extractGoModComment(tw, comment, commentToken, grouplbl, idx)
idx++
commentEndCol := comment.Start.LineRune + (len(commentToken) - 1)
if first {
startLine, startCol, endLine, endCol = comment.Start.Line, comment.Start.LineRune, comment.Start.Line, commentEndCol
first = false
} else {
startLine, startCol = lexMin(comment.Start.Line, comment.Start.LineRune, startLine, startCol)
endLine, endCol = lexMax(comment.Start.Line, commentEndCol, endLine, endCol)
}
}
extractLocation(tw, grouplbl, startLine, startCol, endLine, endCol)
}
func extractGoModComment(tw *trap.Writer, comment modfile.Comment, commentToken string, grouplbl trap.Label, idx int) {
lbl := tw.Labeler.LocalID(comment)
dbscheme.CommentsTable.Emit(tw, lbl, dbscheme.SlashSlashComment.Index(), grouplbl, idx, commentToken)
extractLocation(tw, lbl, comment.Start.Line, comment.Start.LineRune, comment.Start.Line, comment.Start.LineRune+(len(commentToken)-1))
}