Skip to content

Commit c4f527c

Browse files
committed
feat: support custom queryRoot(defaults to std, i.e. projectroot/qlsrc)
1 parent a8237bd commit c4f527c

27 files changed

Lines changed: 31 additions & 38 deletions

ci/malloc_test.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ buildGrps:
2525
queryconfig:
2626
resultRoot: codeqlResult # resultRoot only for -collect csv. Bqrs/csv per db: <dbPath>/results/lslightly/qlstat/<queryPathNoExt>.<fmt>
2727
parallelCore: 20 # parallel cores to run query
28-
queryRoot: qlsrc # query root. There should be codeql-pack.yaml in queryRoot directory
2928
queryGrps:
3029
- queryDBs:
3130
- batchmalloc

cmd/batch_clone_build/clone_test.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ repositories:
99

1010
queryconfig:
1111
resultRoot: codeqlResult
12-
queryRoot: qlsrc

cmd/codeql_qdriver/main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func main() {
5858
}
5959
for grpi, grp := range cfg.QueryGrps {
6060
fmt.Printf("Grp %d: Executing queries\n", grpi)
61-
queriesExec(cfg, grp, targetDecodeFmt)
61+
queriesExec(cfg, grp, grp.ResolvedQueryRoot(), targetDecodeFmt)
6262
}
6363
} else {
6464
fmt.Println("Decoding results")
@@ -103,13 +103,13 @@ func buildGroupExternalArgs(queries []config.Query, extroot string) []string {
103103
//
104104
// Results are placed at <dbPath>/results/<queryPack>/<queryPathNoExt>.bqrs.
105105
// Decoded output goes to <dbPath>/results/<queryPack>/<queryPathNoExt>.<tgtFmt>.
106-
func runQueriesOnDB(cfg *config.Artifact, db config.DB, queries []config.Query, tgtFmt string) {
106+
func runQueriesOnDB(cfg *config.Artifact, db config.DB, queryRoot string, queries []config.Query, tgtFmt string) {
107107
// --- Phase 1: run-queries ---
108108
extArgs := buildGroupExternalArgs(queries, db.ExtDir())
109109

110110
queryPaths := make([]string, len(queries))
111111
for i, q := range queries {
112-
queryPaths[i] = q.QueryPath(cfg.QueryRoot)
112+
queryPaths[i] = q.QueryPath(queryRoot)
113113
}
114114

115115
logDir := filepath.Join(cfg.PassLogDir("query"), db.Name)
@@ -164,13 +164,13 @@ first remove all content in ${config.OutResultRoot}/${qScriptNameNoExt}
164164
dump error log for ${db} in ${config.OutResultRoot}/${qScriptNameNoExt}/error.log
165165
dump stdout/stderr for ${db} in ${config.OutResultRoot}/${qScriptNameNoExt}/log/${db}.log
166166
*/
167-
func queriesExec(cfg *config.Artifact, grp config.QueryGroup, tgtFmt string) {
167+
func queriesExec(cfg *config.Artifact, grp config.QueryGroup, queryRoot string, tgtFmt string) {
168168
dbs := cfg.ConvStrSliceToDBSlice(grp.QueryDBs)
169169

170170
// Create Query objects for all queries in the group
171171
queries := make([]config.Query, len(grp.Queries))
172172
for i, qScript := range grp.Queries {
173-
queries[i] = config.CreateQuery(qScript, grp.Externals, grp.ExternalFiles)
173+
queries[i] = config.CreateQuery(qScript, grp.Externals, grp.ExternalFiles, queryRoot)
174174
}
175175

176176
bar := progressbar.Default(int64(len(dbs)))
@@ -180,7 +180,7 @@ func queriesExec(cfg *config.Artifact, grp config.QueryGroup, tgtFmt string) {
180180
wg.Add(1)
181181
go func(db config.DB) {
182182
defer wg.Done()
183-
runQueriesOnDB(cfg, db, queries, tgtFmt)
183+
runQueriesOnDB(cfg, db, queryRoot, queries, tgtFmt)
184184
bar.Describe(fmt.Sprintf("db: %-20s %d queries", db.Name, len(queries)))
185185
bar.Add(1)
186186
}(db)
@@ -207,12 +207,13 @@ func decodeResults(cfg *config.Artifact, tgtFmt string) {
207207
checkDecodeTargetFmt(tgtFmt)
208208

209209
for grpi, grp := range cfg.QueryGrps {
210+
queryRoot := grp.ResolvedQueryRoot()
210211
dbs := cfg.ConvStrSliceToDBSlice(grp.QueryDBs)
211212
queries := grp.Queries
212213

213214
bar := progressbar.Default(int64(len(queries) * len(dbs)))
214215
for _, qScript := range queries {
215-
query := config.CreateQuery(qScript, grp.Externals, grp.ExternalFiles)
216+
query := config.CreateQuery(qScript, grp.Externals, grp.ExternalFiles, queryRoot)
216217
for _, db := range dbs {
217218
bqrsPath := filepath.Join(db.Path(), "results/lslightly/qlstat", query.PathNoExt()+".bqrs")
218219
csvPath := filepath.Join(db.Path(), "results/lslightly/qlstat", query.PathNoExt()+"."+tgtFmt)
@@ -244,9 +245,10 @@ func decodeResults(cfg *config.Artifact, tgtFmt string) {
244245

245246
func collectCSVs(cfg *config.Artifact) {
246247
for grpi, grp := range cfg.QueryGrps {
248+
queryRoot := grp.ResolvedQueryRoot()
247249
bar := progressbar.Default(int64(len(grp.Queries)))
248250
for _, qScript := range grp.Queries {
249-
query := config.CreateQuery(qScript, grp.Externals, grp.ExternalFiles)
251+
query := config.CreateQuery(qScript, grp.Externals, grp.ExternalFiles, queryRoot)
250252
bar.Describe(fmt.Sprintf("Grp %d: Collecting for "+query.PathNoExt(), grpi))
251253
collectCSVsForQuery(cfg, query, grp)
252254
bar.Add(1)

cmd/escape_adapter/movedtoheap_test.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ buildGrps:
2020
# query
2121
queryconfig:
2222
resultRoot: codeqlResult # resultRoot only for -collect csv. Bqrs/csv per db: <dbPath>/results/lslightly/qlstat/<queryPathNoExt>.<fmt>
23-
queryRoot: qlsrc # query root. There should be codeql-pack.yaml in queryRoot directory
2423
parallelCore: 20 # parallel cores to run query
2524
queryGrps:
2625
- queryDBs: # "*" means all repositories. "-" means repositories defined in the "repositories" section. Otherwise, use fullnames. Note that repositories with same fullname in different source will be queried.

config/artifact.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ type Artifact struct {
2424

2525
type QueryConfig struct {
2626
ResultRoot string `yaml:"resultRoot"`
27-
QueryRoot string `yaml:"queryRoot"`
2827
ParallelCore int `yaml:"parallelCore"`
2928
QueryGrps []QueryGroup `yaml:"queryGrps"`
3029
}
@@ -37,12 +36,23 @@ type BuildGroup struct {
3736
}
3837

3938
type QueryGroup struct {
39+
QueryRoot string `yaml:"queryRoot"`
4040
QueryDBs []string `yaml:"queryDBs"`
4141
Queries []string `yaml:"queries"`
4242
Externals []string `yaml:"externals"`
4343
ExternalFiles []string `yaml:"externalFiles"`
4444
}
4545

46+
// ResolvedQueryRoot returns the absolute path for this group's query root.
47+
// "std" or empty → <projectRoot>/qlsrc (built-in queries).
48+
// Other values are returned as-is (absolute path or CWD-relative).
49+
func (g *QueryGroup) ResolvedQueryRoot() string {
50+
if g.QueryRoot == "" || g.QueryRoot == "std" {
51+
return filepath.Join(utils.ProjectRoot(), "qlsrc")
52+
}
53+
return g.QueryRoot
54+
}
55+
4656
func UnmarshalArtifact(filename string) *Artifact {
4757
data, err := os.ReadFile(filename)
4858
if err != nil {
@@ -68,10 +78,6 @@ func UnmarshalArtifact(filename string) *Artifact {
6878
cfg.ResultRoot = filepath.Join(utils.ProjectRoot(), "codeqlResult")
6979
log.Println("\033[33mWARNING: resultRoot is empty, set to default value:", cfg.ResultRoot, "\033[0m")
7080
}
71-
if cfg.QueryRoot == "" {
72-
cfg.QueryRoot = filepath.Join(utils.ProjectRoot(), "qlsrc")
73-
log.Println("\033[33mWARNING: queryRoot is empty, set to default value:", cfg.QueryRoot, "\033[0m")
74-
}
7581
return cfg
7682
}
7783

config/query.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ type Query struct {
1414
cacheEntriesForFiles []string // cache external entries for externalFiles
1515
}
1616

17-
func CreateQuery(path string, externals []string, externalFiles []string) Query {
17+
func CreateQuery(path string, externals []string, externalFiles []string, queryRoot string) Query {
1818
if filepath.Ext(path) != ".ql" {
1919
log.Fatalf("Suffix of query source %s is not .ql.", path)
2020
}
21-
cache := make([]string, len(externalFiles)*5)
21+
cache := make([]string, 0, len(externalFiles)*5)
2222
for _, extfile := range externalFiles {
23-
exts, err := ReadExtsFromFile(extfile)
23+
resolvedPath := extfile
24+
if !filepath.IsAbs(extfile) {
25+
resolvedPath = filepath.Join(queryRoot, extfile)
26+
}
27+
exts, err := ReadExtsFromFile(resolvedPath)
2428
if err != nil {
2529
log.Fatalf("Failed to read external file %s: %v", extfile, err)
2630
}

demo.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ buildGrps:
6262
# "-" — all databases defined in buildGrps
6363
queryconfig:
6464
resultRoot: codeqlResult
65-
queryRoot: qlsrc
6665
parallelCore: 20
6766
queryGrps:
6867
- queryDBs:

doc/adapters/escape_analysis.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ queryconfig:
3838
```
3939
4040
In this configuration:
41+
- `queryRoot` is optional per query group. If omitted, it defaults to `std`, which resolves to the built-in query library at `<projectRoot>/qlsrc`. Custom paths can be specified instead to use queries outside the built-in library.
4142
- `extgenScript: goescape` instructs the system to compile repositories with escape analysis enabled
4243
- Note that if you use custom script, 4 environment variables will be set. You can use them in your custom script.
4344
- `REPO_DIR`: The directory path of the repository

example.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ buildGrps:
4949
# query
5050
queryconfig:
5151
resultRoot: codeqlResult # resultRoot only for -collect csv. Bqrs/csv per db: <dbPath>/results/lslightly/qlstat/<queryPathNoExt>.<fmt>
52-
queryRoot: qlsrc # query root. There should be codeql-pack.yaml in queryRoot directory
5352
parallelCore: 20 # parallel cores to run query
5453
queryGrps:
5554
- queryDBs: # "*" means all repositories. "-" means repositories defined in the "repositories" section. Otherwise, use fullnames. Note that repositories with same fullname in different source will be queried.

qlsrc/pprof_ext/heapprofile_unittests/heapprofile_api_tests.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ buildGrps:
3434
queryconfig:
3535
resultRoot: codeqlResult # resultRoot only for -collect csv. Bqrs/csv per db: <dbPath>/results/lslightly/qlstat/<queryPathNoExt>.<fmt>
3636
parallelCore: 20 # parallel cores to run query
37-
queryRoot: qlsrc # query root. There should be codeql-pack.yaml in queryRoot directory
3837
queryGrps:
3938
- queryDBs:
4039
- malloc_test

0 commit comments

Comments
 (0)