From 280ea308c321115445df610f1a75b05bbadca5f3 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 17 Nov 2025 11:17:23 +0100 Subject: 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. --- pkg/codesearch/database.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 pkg/codesearch/database.go (limited to 'pkg/codesearch/database.go') 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 + } + } +} -- cgit mrf-deployment