diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2025-11-17 11:17:23 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2025-11-20 10:10:05 +0000 |
| commit | 280ea308c321115445df610f1a75b05bbadca5f3 (patch) | |
| tree | c195c76723c4a08986d74edbfc9e15a4f07fa6c1 /pkg/codesearch/database.go | |
| parent | 94d1e3f8b1838e8a04074464a957e979a5c5e36b (diff) | |
pkg/codesearch: add skeleton for code searching tool
Add a clang tool that is used for code indexing (tools/clang/codesearch/).
It follows conventions and build procedure of the declextract tool.
Add pkg/codesearch package that aggregates the info exposed by the clang tools,
and allows doing simple queries:
- show source code of an entity (function, struct, etc)
- show entity comment
- show all entities defined in a source file
Add tools/syz-codesearch wrapper tool that allows to create index for a kernel build,
and then run code queries on it.
Diffstat (limited to 'pkg/codesearch/database.go')
| -rw-r--r-- | pkg/codesearch/database.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/pkg/codesearch/database.go b/pkg/codesearch/database.go new file mode 100644 index 000000000..4757935e9 --- /dev/null +++ b/pkg/codesearch/database.go @@ -0,0 +1,56 @@ +// Copyright 2025 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package codesearch + +import ( + "strings" + + "github.com/google/syzkaller/pkg/clangtool" +) + +type Database struct { + Definitions []*Definition `json:"definitions,omitempty"` +} + +type Definition struct { + Kind string `json:"kind,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` + IsStatic bool `json:"is_static,omitempty"` + Body LineRange `json:"body,omitempty"` + Comment LineRange `json:"comment,omitempty"` +} + +type LineRange struct { + File string `json:"file,omitempty"` + StartLine int `json:"start_line,omitempty"` + EndLine int `json:"end_line,omitempty"` +} + +func (db *Database) Merge(other *Database) { + db.Definitions = append(db.Definitions, other.Definitions...) +} + +func (db *Database) Finalize(v *clangtool.Verifier) { + db.Definitions = clangtool.SortAndDedupSlice(db.Definitions) + + for _, def := range db.Definitions { + v.LineRange(def.Body.File, def.Body.StartLine, def.Body.EndLine) + if def.Comment.File != "" { + v.LineRange(def.Comment.File, def.Comment.StartLine, def.Comment.EndLine) + } + } +} + +// SetSoureFile attaches the source file to the entities that need it. +// The clang tool could do it, but it looks easier to do it here. +func (db *Database) SetSourceFile(file string, updatePath func(string) string) { + for _, def := range db.Definitions { + def.Body.File = updatePath(def.Body.File) + def.Comment.File = updatePath(def.Comment.File) + if strings.HasSuffix(def.Body.File, ".c") && def.Body.File != file { + def.IsStatic = false + } + } +} |
