github.com/bir3/gocompiler@v0.9.2202/src/cmd/compile/internal/ir/reassign_consistency_check.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ir
     6  
     7  import (
     8  	"github.com/bir3/gocompiler/src/cmd/compile/internal/base"
     9  	"github.com/bir3/gocompiler/src/cmd/internal/src"
    10  	"fmt"
    11  	"path/filepath"
    12  	"strings"
    13  )
    14  
    15  // checkStaticValueResult compares the result from ReassignOracle.StaticValue
    16  // with the corresponding result from ir.StaticValue to make sure they agree.
    17  // This method is called only when turned on via build tag.
    18  func checkStaticValueResult(n Node, newres Node) {
    19  	oldres := StaticValue(n)
    20  	if oldres != newres {
    21  		base.Fatalf("%s: new/old static value disagreement on %v:\nnew=%v\nold=%v", fmtFullPos(n.Pos()), n, newres, oldres)
    22  	}
    23  }
    24  
    25  // checkStaticValueResult compares the result from ReassignOracle.Reassigned
    26  // with the corresponding result from ir.Reassigned to make sure they agree.
    27  // This method is called only when turned on via build tag.
    28  func checkReassignedResult(n *Name, newres bool) {
    29  	origres := Reassigned(n)
    30  	if newres != origres {
    31  		base.Fatalf("%s: new/old reassigned disagreement on %v (class %s) newres=%v oldres=%v", fmtFullPos(n.Pos()), n, n.Class.String(), newres, origres)
    32  	}
    33  }
    34  
    35  // fmtFullPos returns a verbose dump for pos p, including inlines.
    36  func fmtFullPos(p src.XPos) string {
    37  	var sb strings.Builder
    38  	sep := ""
    39  	base.Ctxt.AllPos(p, func(pos src.Pos) {
    40  		fmt.Fprintf(&sb, sep)
    41  		sep = "|"
    42  		file := filepath.Base(pos.Filename())
    43  		fmt.Fprintf(&sb, "%s:%d:%d", file, pos.Line(), pos.Col())
    44  	})
    45  	return sb.String()
    46  }