@@ -14,6 +14,7 @@ import (
14
14
"flag"
15
15
"fmt"
16
16
"go/build"
17
+ "go/build/constraint"
17
18
"hash/fnv"
18
19
"io"
19
20
"io/fs"
@@ -462,57 +463,31 @@ func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
462
463
return true , ""
463
464
}
464
465
for _ , line := range strings .Split (src , "\n " ) {
465
- line = strings .TrimSpace (line )
466
- if strings .HasPrefix (line , "//" ) {
467
- line = line [2 :]
468
- } else {
469
- continue
470
- }
471
- line = strings .TrimSpace (line )
472
- if len (line ) == 0 || line [0 ] != '+' {
473
- continue
466
+ if strings .HasPrefix (line , "package " ) {
467
+ break
474
468
}
475
- gcFlags := os .Getenv ("GO_GCFLAGS" )
476
- ctxt := & context {
477
- GOOS : goos ,
478
- GOARCH : goarch ,
479
- cgoEnabled : cgoEnabled ,
480
- noOptEnv : strings .Contains (gcFlags , "-N" ) || strings .Contains (gcFlags , "-l" ),
481
- }
482
-
483
- words := strings .Fields (line )
484
- if words [0 ] == "+build" {
485
- ok := false
486
- for _ , word := range words [1 :] {
487
- if ctxt .match (word ) {
488
- ok = true
489
- break
490
- }
469
+
470
+ if expr , err := constraint .Parse (line ); err == nil {
471
+ gcFlags := os .Getenv ("GO_GCFLAGS" )
472
+ ctxt := & context {
473
+ GOOS : goos ,
474
+ GOARCH : goarch ,
475
+ cgoEnabled : cgoEnabled ,
476
+ noOptEnv : strings .Contains (gcFlags , "-N" ) || strings .Contains (gcFlags , "-l" ),
491
477
}
492
- if ! ok {
493
- // no matching tag found.
478
+
479
+ if ! expr . Eval ( ctxt . match ) {
494
480
return false , line
495
481
}
496
482
}
497
483
}
498
- // no build tags
499
484
return true , ""
500
485
}
501
486
502
487
func (ctxt * context ) match (name string ) bool {
503
488
if name == "" {
504
489
return false
505
490
}
506
- if first , rest , ok := strings .Cut (name , "," ); ok {
507
- // comma-separated list
508
- return ctxt .match (first ) && ctxt .match (rest )
509
- }
510
- if strings .HasPrefix (name , "!!" ) { // bad syntax, reject always
511
- return false
512
- }
513
- if strings .HasPrefix (name , "!" ) { // negation
514
- return len (name ) > 1 && ! ctxt .match (name [1 :])
515
- }
516
491
517
492
// Tags must be letters, digits, underscores or dots.
518
493
// Unlike in Go identifiers, all digits are fine (e.g., "386").
0 commit comments