Debian Codegraph @

src/cmd/fix/buildtag.go

raw
1 // Copyright 2020 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 main
6
7 import (
8 "go/ast"
9 "go/version"
10 "strings"
11 )
12
13 func init() {
14 register(buildtagFix)
15 }
16
17 const buildtagGoVersionCutoff = "go1.18"
18
19 var buildtagFix = fix{
20 name: "buildtag",
21 date: "2021-08-25",
22 f: buildtag,
23 desc: `Remove +build comments from modules using Go 1.18 or later`,
24 }
25
26 func buildtag(f *ast.File) bool {
27 if version.Compare(*goVersion, buildtagGoVersionCutoff) < 0 {
28 return false
29 }
30
31 // File is already gofmt-ed, so we know that if there are +build lines,
32 // they are in a comment group that starts with a //go:build line followed
33 // by a blank line. While we cannot delete comments from an AST and
34 // expect consistent output in general, this specific case - deleting only
35 // some lines from a comment block - does format correctly.
36 fixed := false
37 for _, g := range f.Comments {
38 sawGoBuild := false
39 for i, c := range g.List {
40 if strings.HasPrefix(c.Text, "//go:build ") {
41 sawGoBuild = true
42 }
43 if sawGoBuild && strings.HasPrefix(c.Text, "// +build ") {
44 g.List = g.List[:i]
45 fixed = true
46 break
47 }
48 }
49 }
50
51 return fixed
52 }