From 0d36999a0fe3fd63c9c845faf3c9cc8be32c4b58 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 6 Apr 2017 15:30:53 -0700 Subject: [PATCH] cmd/compile: make TestAssembly resilient to output ordering To preserve reproducible builds, the text entries during compilation will be sorted before being printed. TestAssembly currently assumes that function init comes after all user-defined functions. Remove that assumption. Instead of looking for "TEXT" to tell you where a function ends--which may now yield lots of non-function-code junk--look for a line beginning with non-whitespace. Updates #15756 Change-Id: Ibc82dba6143d769ef4c391afc360e523b1a51348 Reviewed-on: https://go-review.googlesource.com/39853 Run-TryBot: Josh Bleecher Snyder Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/gc/asm_test.go | 27 ++++++++++++++----------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/cmd/compile/internal/gc/asm_test.go b/src/cmd/compile/internal/gc/asm_test.go index b340d07942..a36287865b 100644 --- a/src/cmd/compile/internal/gc/asm_test.go +++ b/src/cmd/compile/internal/gc/asm_test.go @@ -42,23 +42,32 @@ func TestAssembly(t *testing.T) { asm := ats.compileToAsm(tt, dir) for _, at := range ats.tests { - funcName := nameRegexp.FindString(at.function)[5:] - fa := funcAsm(asm, funcName) - at.verifyAsm(tt, fa) + funcName := nameRegexp.FindString(at.function)[len("func "):] + fa := funcAsm(tt, asm, funcName) + if fa != "" { + at.verifyAsm(tt, fa) + } } }) } }) } +var nextTextRegexp = regexp.MustCompile(`\n\S`) + // funcAsm returns the assembly listing for the given function name. -func funcAsm(asm string, funcName string) string { +func funcAsm(t *testing.T, asm string, funcName string) string { if i := strings.Index(asm, fmt.Sprintf("TEXT\t\"\".%s(SB)", funcName)); i >= 0 { asm = asm[i:] + } else { + t.Errorf("could not find assembly for function %v", funcName) + return "" } - if i := strings.Index(asm[1:], "TEXT\t\"\"."); i >= 0 { - asm = asm[:i+1] + // Find the next line that doesn't begin with whitespace. + loc := nextTextRegexp.FindStringIndex(asm) + if loc != nil { + asm = asm[:loc[0]] } return asm @@ -130,12 +139,6 @@ func (ats *asmTests) compileToAsm(t *testing.T, dir string) string { // Now, compile the individual file for which we want to see the generated assembly. asm := ats.runGo(t, "tool", "compile", "-I", testDir, "-S", "-o", filepath.Join(testDir, "out.o"), src) - - // Get rid of code for "".init. Also gets rid of type algorithms & other junk. - if i := strings.Index(asm, "\n\"\".init "); i >= 0 { - asm = asm[:i+1] - } - return asm } -- 2.48.1