codesearch

package
v0.0.0-...-43249ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Commands = []Command{
	{"dir-index", 1, func(index *Index, args []string) (string, error) {
		subdirs, files, err := index.DirIndex(args[0])
		if err != nil {
			return "", err
		}
		b := new(strings.Builder)
		fmt.Fprintf(b, "directory %v subdirs:\n", args[0])
		for _, subdir := range subdirs {
			fmt.Fprintf(b, " - %v\n", subdir)
		}
		fmt.Fprintf(b, "\ndirectory %v files:\n", args[0])
		for _, file := range files {
			fmt.Fprintf(b, " - %v\n", file)
		}
		return b.String(), nil
	}},
	{"read-file", 1, func(index *Index, args []string) (string, error) {
		return index.ReadFile(args[0])
	}},
	{"file-index", 1, func(index *Index, args []string) (string, error) {
		entities, err := index.FileIndex(args[0])
		if err != nil {
			return "", err
		}
		b := new(strings.Builder)
		fmt.Fprintf(b, "file %v defines the following entities:\n\n", args[0])
		for _, ent := range entities {
			fmt.Fprintf(b, "%v %v\n", ent.Kind, ent.Name)
		}
		return b.String(), nil
	}},
	{"def-comment", 2, func(index *Index, args []string) (string, error) {
		info, err := index.DefinitionComment(args[0], args[1])
		if err != nil {
			return "", err
		}
		if info.Body == "" {
			return fmt.Sprintf("%v %v is defined in %v and is not commented\n",
				info.Kind, args[1], info.File), nil
		}
		return fmt.Sprintf("%v %v is defined in %v and commented as:\n\n%v",
			info.Kind, args[1], info.File, info.Body), nil
	}},
	{"def-source", 3, func(index *Index, args []string) (string, error) {
		info, err := index.DefinitionSource(args[0], args[1], args[2] == "yes")
		if err != nil {
			return "", err
		}
		return fmt.Sprintf("%v %v is defined in %v:\n\n%v", info.Kind, args[1], info.File, info.Body), nil
	}},
	{"find-references", 5, func(index *Index, args []string) (string, error) {
		contextLines, err := strconv.Atoi(args[3])
		if err != nil {
			return "", fmt.Errorf("failed to parse number of context lines %q: %w", args[3], err)
		}
		outputLimit, err := strconv.Atoi(args[4])
		if err != nil {
			return "", fmt.Errorf("failed to parse output limit %q: %w", args[4], err)
		}
		refs, totalCount, err := index.FindReferences(args[0], args[1], args[2], contextLines, outputLimit)
		if err != nil {
			return "", err
		}
		b := new(strings.Builder)
		fmt.Fprintf(b, "%v has %v references:\n\n", args[1], totalCount)
		for _, ref := range refs {
			fmt.Fprintf(b, "%v %v %v it at %v:%v\n%v\n\n",
				ref.ReferencingEntityKind, ref.ReferencingEntityName, ref.ReferenceKind,
				ref.SourceFile, ref.SourceLine, ref.SourceSnippet)
		}
		return b.String(), nil
	}},
	{"struct-layout", 0, func(index *Index, args []string) (string, error) {
		if len(args) != 2 && len(args) != 3 {
			return "", fmt.Errorf("codesearch command struct-layout requires 2 or 3 args, but %v provided",
				len(args))
		}
		var fieldOffset *uint
		if len(args) == 3 {
			val, err := strconv.ParseUint(args[2], 10, 64)
			if err != nil {
				return "", fmt.Errorf("bad offset: %w", err)
			}
			fieldOffset = new(uint)
			*fieldOffset = uint(val)
		}
		fields, err := index.GetStructLayout(args[0], args[1], fieldOffset)
		if err != nil {
			return "", err
		}
		b := new(strings.Builder)
		fmt.Fprintf(b, "struct %v has %v fields:\n", args[1], len(fields))
		for _, f := range fields {
			fmt.Fprintf(b, "[%v - %v] %v\n", f.OffsetBits, f.OffsetBits+f.SizeBits, f.Name)
		}
		return b.String(), nil
	}},
}

Commands are used to run unit tests and for the syz-codesearch tool.

View Source
var DatabaseFormatHash = func() string {
	// Semantic version should be bumped when the schema does not change,
	// but stored values changes.
	const semanticVersion = "4"
	schema, err := jsonschema.For[Database](nil)
	if err != nil {
		panic(err)
	}
	return hash.String(schema, semanticVersion)
}()

DatabaseFormatHash contains a hash uniquely identifying format of the database. In covers both structure and semantics of the data, and is supposed to be used for caching of the database files.

Functions

func IsSourceFile

func IsSourceFile(file string) bool

Types

type Command

type Command struct {
	Name  string
	NArgs int
	Func  func(*Index, []string) (string, error)
}

type Database

type Database struct {
	Definitions []*Definition `json:"definitions,omitempty"`
	// contains filtered or unexported fields
}

func (*Database) Finalize

func (db *Database) Finalize(v *clangtool.Verifier)

func (*Database) Merge

func (db *Database) Merge(other *Database, v *clangtool.Verifier)

func (*Database) SetSourceFile

func (db *Database) SetSourceFile(file string, updatePath func(string) string)

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.

type Definition

type Definition struct {
	Name     string      `json:"name,omitempty"`
	Type     string      `json:"type,omitempty"`
	Kind     EntityKind  `json:"kind,omitempty"`
	IsStatic bool        `json:"is_static,omitempty"`
	Body     LineRange   `json:"body,omitempty"`
	Comment  LineRange   `json:"comment,omitempty"`
	Refs     []Reference `json:"refs,omitempty"`
	Fields   []FieldInfo `json:"fields,omitempty"`
}

type Entity

type Entity struct {
	Kind string
	Name string
}

type EntityInfo

type EntityInfo struct {
	File string
	Kind string
	Body string
}

type EntityKind

type EntityKind uint8
const (
	EntityKindFunction EntityKind
	EntityKindStruct
	EntityKindUnion
	EntityKindGlobalVariable
	EntityKindMacro
	EntityKindEnum
	EntityKindTypedef
	EntityKindField
)

func (*EntityKind) MarshalJSON

func (v *EntityKind) MarshalJSON() ([]byte, error)

func (*EntityKind) String

func (v *EntityKind) String() string

func (*EntityKind) UnmarshalJSON

func (v *EntityKind) UnmarshalJSON(data []byte) error

type FieldInfo

type FieldInfo struct {
	Name       string `json:"name,omitempty"`
	OffsetBits uint64 `json:"offset"`
	SizeBits   uint64 `json:"size"`
}

type Index

type Index struct {
	// contains filtered or unexported fields
}

func NewIndex

func NewIndex(databaseFile string, srcDirs []string) (*Index, error)

func NewTestIndex

func NewTestIndex(t *testing.T, dir string) *Index

func (*Index) Command

func (index *Index) Command(cmd string, args []string) (string, error)

func (*Index) DefinitionComment

func (index *Index) DefinitionComment(contextFile, name string) (*EntityInfo, error)

func (*Index) DefinitionSource

func (index *Index) DefinitionSource(contextFile, name string, includeLines bool) (*EntityInfo, error)

func (*Index) DirIndex

func (index *Index) DirIndex(dir string) ([]string, []string, error)

func (*Index) FileIndex

func (index *Index) FileIndex(file string) ([]Entity, error)

func (*Index) FindReferences

func (index *Index) FindReferences(contextFile, name, srcPrefix string, contextLines, outputLimit int) (
	[]ReferenceInfo, int, error)

func (*Index) GetStructLayout

func (index *Index) GetStructLayout(contextFile, name string, fieldOffset *uint) ([]FieldInfo, error)

func (*Index) ReadFile

func (index *Index) ReadFile(file string) (string, error)

type LineRange

type LineRange struct {
	File      string `json:"file,omitempty"`
	StartLine uint32 `json:"start_line,omitempty"`
	EndLine   uint32 `json:"end_line,omitempty"`
}

type RefKind

type RefKind uint8
const (
	RefKindUses RefKind
	RefKindCall
	RefKindRead
	RefKindWrite
	RefKindTakesAddr
)

func (*RefKind) MarshalJSON

func (v *RefKind) MarshalJSON() ([]byte, error)

func (*RefKind) String

func (v *RefKind) String() string

func (*RefKind) UnmarshalJSON

func (v *RefKind) UnmarshalJSON(data []byte) error

type Reference

type Reference struct {
	Name       string     `json:"name,omitempty"`
	Kind       RefKind    `json:"kind,omitempty"`
	EntityKind EntityKind `json:"entity_kind,omitempty"`
	Line       uint32     `json:"line,omitempty"`
}

type ReferenceInfo

type ReferenceInfo struct {
	ReferencingEntityKind string `jsonschema:"Kind of the referencing entity (function, struct, etc)."`
	ReferencingEntityName string `jsonschema:"Name of the referencing entity."`
	ReferenceKind         string `jsonschema:"Kind of the reference (calls, takes-address, reads, writes-to, etc)."`
	SourceFile            string `jsonschema:"Source file of the reference."`
	SourceLine            int    `jsonschema:"Source line of the reference."`
	SourceSnippet         string `jsonschema:"Surrounding code snippet, if requested." json:",omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL