github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/codesearch/database.go (about)

     1  // Copyright 2025 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package codesearch
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/google/syzkaller/pkg/clangtool"
    10  )
    11  
    12  type Database struct {
    13  	Definitions []*Definition `json:"definitions,omitempty"`
    14  }
    15  
    16  type Definition struct {
    17  	Kind     string    `json:"kind,omitempty"`
    18  	Name     string    `json:"name,omitempty"`
    19  	Type     string    `json:"type,omitempty"`
    20  	IsStatic bool      `json:"is_static,omitempty"`
    21  	Body     LineRange `json:"body,omitempty"`
    22  	Comment  LineRange `json:"comment,omitempty"`
    23  }
    24  
    25  type LineRange struct {
    26  	File      string `json:"file,omitempty"`
    27  	StartLine int    `json:"start_line,omitempty"`
    28  	EndLine   int    `json:"end_line,omitempty"`
    29  }
    30  
    31  func (db *Database) Merge(other *Database) {
    32  	db.Definitions = append(db.Definitions, other.Definitions...)
    33  }
    34  
    35  func (db *Database) Finalize(v *clangtool.Verifier) {
    36  	db.Definitions = clangtool.SortAndDedupSlice(db.Definitions)
    37  
    38  	for _, def := range db.Definitions {
    39  		v.LineRange(def.Body.File, def.Body.StartLine, def.Body.EndLine)
    40  		if def.Comment.File != "" {
    41  			v.LineRange(def.Comment.File, def.Comment.StartLine, def.Comment.EndLine)
    42  		}
    43  	}
    44  }
    45  
    46  // SetSoureFile attaches the source file to the entities that need it.
    47  // The clang tool could do it, but it looks easier to do it here.
    48  func (db *Database) SetSourceFile(file string, updatePath func(string) string) {
    49  	for _, def := range db.Definitions {
    50  		def.Body.File = updatePath(def.Body.File)
    51  		def.Comment.File = updatePath(def.Comment.File)
    52  		if strings.HasSuffix(def.Body.File, ".c") && def.Body.File != file {
    53  			def.IsStatic = false
    54  		}
    55  	}
    56  }