test: skip some tests on noopt builder

Adds a new build tag "gcflags_noopt" that can be used in test/*.go
tests.

Fixes #27833

Change-Id: I4ea0ccd9e9e58c4639de18645fec81eb24a3a929
Reviewed-on: https://go-review.googlesource.com/136898
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
diff --git a/test/checkbce.go b/test/checkbce.go
index 770c4c2..ef4e584 100644
--- a/test/checkbce.go
+++ b/test/checkbce.go
@@ -1,4 +1,4 @@
-// +build amd64
+// +build amd64,!gcflags_noopt
 // errorcheck -0 -d=ssa/check_bce/debug=3
 
 // Copyright 2016 The Go Authors. All rights reserved.
diff --git a/test/fixedbugs/issue7921.go b/test/fixedbugs/issue7921.go
index e30e556..ac2b494 100644
--- a/test/fixedbugs/issue7921.go
+++ b/test/fixedbugs/issue7921.go
@@ -1,3 +1,4 @@
+// +build !gcflags_noopt
 // errorcheck -0 -m
 
 // Copyright 2018 The Go Authors. All rights reserved.
diff --git a/test/nosplit.go b/test/nosplit.go
index b821d23..1855c01 100644
--- a/test/nosplit.go
+++ b/test/nosplit.go
@@ -1,4 +1,4 @@
-// +build !nacl,!js
+// +build !nacl,!js,!gcflags_noopt
 // run
 
 // Copyright 2014 The Go Authors. All rights reserved.
diff --git a/test/run.go b/test/run.go
index 24a4d4f..d0dccb4 100644
--- a/test/run.go
+++ b/test/run.go
@@ -354,8 +354,9 @@
 }
 
 type context struct {
-	GOOS   string
-	GOARCH string
+	GOOS     string
+	GOARCH   string
+	noOptEnv bool
 }
 
 // shouldTest looks for build tags in a source file and returns
@@ -375,10 +376,13 @@
 		if len(line) == 0 || line[0] != '+' {
 			continue
 		}
+		gcFlags := os.Getenv("GO_GCFLAGS")
 		ctxt := &context{
-			GOOS:   goos,
-			GOARCH: goarch,
+			GOOS:     goos,
+			GOARCH:   goarch,
+			noOptEnv: strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
 		}
+
 		words := strings.Fields(line)
 		if words[0] == "+build" {
 			ok := false
@@ -425,6 +429,10 @@
 		return true
 	}
 
+	if ctxt.noOptEnv && name == "gcflags_noopt" {
+		return true
+	}
+
 	if name == "test_run" {
 		return true
 	}