github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/covermerger/file_line_merger.go (about)

     1  // Copyright 2024 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 covermerger
     5  
     6  func makeFileLineCoverMerger(
     7  	fileVersions map[RepoBranchCommit]string, base RepoBranchCommit) FileCoverageMerger {
     8  	baseFile := ""
     9  	baseFileExists := false
    10  	for rbc, fileContent := range fileVersions {
    11  		if rbc == base {
    12  			baseFile = fileContent
    13  			baseFileExists = true
    14  			break
    15  		}
    16  	}
    17  	if !baseFileExists {
    18  		return &DeletedFileLineMerger{}
    19  	}
    20  	a := &FileLineCoverMerger{
    21  		rbcToFile: fileVersions,
    22  		baseFile:  baseFile,
    23  		hitCounts: make(map[int]int),
    24  		matchers:  make(map[RepoBranchCommit]*LineToLineMatcher),
    25  	}
    26  	for rbc, fileVersion := range fileVersions {
    27  		a.matchers[rbc] = makeLineToLineMatcher(fileVersion, baseFile)
    28  	}
    29  	return a
    30  }
    31  
    32  type FileLineCoverMerger struct {
    33  	rbcToFile map[RepoBranchCommit]string
    34  	baseFile  string
    35  	hitCounts map[int]int
    36  	matchers  map[RepoBranchCommit]*LineToLineMatcher
    37  }
    38  
    39  func (a *FileLineCoverMerger) AddRecord(rbc RepoBranchCommit, arch string, f Frame, hitCount int) {
    40  	if targetLine := a.matchers[rbc].SameLinePos(f.StartLine); targetLine != -1 {
    41  		a.hitCounts[f.StartLine] += hitCount
    42  	}
    43  }
    44  
    45  func (a *FileLineCoverMerger) Result() *MergeResult {
    46  	return &MergeResult{
    47  		HitCounts:  a.hitCounts,
    48  		FileExists: true,
    49  	}
    50  }