go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/model/compile_failure_signal.go (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package model
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/bisection/util"
    21  )
    22  
    23  // Compile Failure Signal represents signal extracted from compile failure log.
    24  type CompileFailureSignal struct {
    25  	Nodes []string
    26  	Edges []*CompileFailureEdge
    27  	// A map of {<file_path>:[lines]} represents failure positions in source file
    28  	Files map[string][]int
    29  	// A map of {<dependency_file_name>:[<list of dependencies>]}. Used to improve
    30  	// the speed when we do dependency analysis
    31  	DependencyMap map[string][]string
    32  }
    33  
    34  // CompileFailureEdge represents a failed edge in ninja failure log
    35  type CompileFailureEdge struct {
    36  	Rule         string // Rule is like CXX, CC...
    37  	OutputNodes  []string
    38  	Dependencies []string
    39  }
    40  
    41  func (c *CompileFailureSignal) AddLine(filePath string, line int) {
    42  	c.AddFilePath(filePath)
    43  	for _, l := range c.Files[filePath] {
    44  		if l == line {
    45  			return
    46  		}
    47  	}
    48  	c.Files[filePath] = append(c.Files[filePath], line)
    49  }
    50  
    51  func (c *CompileFailureSignal) AddFilePath(filePath string) {
    52  	if c.Files == nil {
    53  		c.Files = map[string][]int{}
    54  	}
    55  	_, exist := c.Files[filePath]
    56  	if !exist {
    57  		c.Files[filePath] = []int{}
    58  	}
    59  }
    60  
    61  // Put all the dependencies in a map with the form
    62  // {<dependency_file_name>:[<list of dependencies>]}
    63  func (cfs *CompileFailureSignal) CalculateDependencyMap(c context.Context) {
    64  	cfs.DependencyMap = map[string][]string{}
    65  	for _, edge := range cfs.Edges {
    66  		for _, dependency := range edge.Dependencies {
    67  			fileName := util.GetCanonicalFileName(dependency)
    68  			_, ok := cfs.DependencyMap[fileName]
    69  			if !ok {
    70  				cfs.DependencyMap[fileName] = []string{}
    71  			}
    72  			// Check if the dependency already exists
    73  			// Do a for loop, the length is short. So it should be ok.
    74  			exist := false
    75  			for _, d := range cfs.DependencyMap[fileName] {
    76  				if d == dependency {
    77  					exist = true
    78  					break
    79  				}
    80  			}
    81  			if !exist {
    82  				cfs.DependencyMap[fileName] = append(cfs.DependencyMap[fileName], dependency)
    83  			}
    84  		}
    85  	}
    86  }