...
Run Format

Source file src/cmd/vet/buildtag.go

Documentation: cmd/vet

     1  // Copyright 2013 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  	"bytes"
     9  	"fmt"
    10  	"os"
    11  	"strings"
    12  	"unicode"
    13  )
    14  
    15  var (
    16  	nl         = []byte("\n")
    17  	slashSlash = []byte("//")
    18  	plusBuild  = []byte("+build")
    19  )
    20  
    21  func badfLine(f *File, line int, format string, args ...interface{}) {
    22  	msg := fmt.Sprintf(format, args...)
    23  	fmt.Fprintf(os.Stderr, "%s:%d: %s\n", f.name, line, msg)
    24  	setExit(1)
    25  }
    26  
    27  // checkBuildTag checks that build tags are in the correct location and well-formed.
    28  func checkBuildTag(f *File) {
    29  	if !vet("buildtags") {
    30  		return
    31  	}
    32  
    33  	// we must look at the raw lines, as build tags may appear in non-Go
    34  	// files such as assembly files.
    35  	lines := bytes.SplitAfter(f.content, nl)
    36  
    37  	// lineWithComment reports whether a line corresponds to a comment in
    38  	// the source file. If the source file wasn't Go, the function always
    39  	// returns true.
    40  	lineWithComment := func(line int) bool {
    41  		if f.file == nil {
    42  			// Current source file is not Go, so be conservative.
    43  			return true
    44  		}
    45  		for _, group := range f.file.Comments {
    46  			startLine := f.fset.Position(group.Pos()).Line
    47  			endLine := f.fset.Position(group.End()).Line
    48  			if startLine <= line && line <= endLine {
    49  				return true
    50  			}
    51  		}
    52  		return false
    53  	}
    54  
    55  	// Determine cutpoint where +build comments are no longer valid.
    56  	// They are valid in leading // comments in the file followed by
    57  	// a blank line.
    58  	var cutoff int
    59  	for i, line := range lines {
    60  		line = bytes.TrimSpace(line)
    61  		if len(line) == 0 {
    62  			cutoff = i
    63  			continue
    64  		}
    65  		if bytes.HasPrefix(line, slashSlash) {
    66  			continue
    67  		}
    68  		break
    69  	}
    70  
    71  	for i, line := range lines {
    72  		line = bytes.TrimSpace(line)
    73  		if !bytes.HasPrefix(line, slashSlash) {
    74  			continue
    75  		}
    76  		if !bytes.Contains(line, plusBuild) {
    77  			// Check that the comment contains "+build" early, to
    78  			// avoid unnecessary lineWithComment calls that may
    79  			// incur linear searches.
    80  			continue
    81  		}
    82  		if !lineWithComment(i + 1) {
    83  			// This is a line in a Go source file that looks like a
    84  			// comment, but actually isn't - such as part of a raw
    85  			// string.
    86  			continue
    87  		}
    88  
    89  		text := bytes.TrimSpace(line[2:])
    90  		if bytes.HasPrefix(text, plusBuild) {
    91  			fields := bytes.Fields(text)
    92  			if !bytes.Equal(fields[0], plusBuild) {
    93  				// Comment is something like +buildasdf not +build.
    94  				badfLine(f, i+1, "possible malformed +build comment")
    95  				continue
    96  			}
    97  			if i >= cutoff {
    98  				badfLine(f, i+1, "+build comment must appear before package clause and be followed by a blank line")
    99  				continue
   100  			}
   101  			// Check arguments.
   102  		Args:
   103  			for _, arg := range fields[1:] {
   104  				for _, elem := range strings.Split(string(arg), ",") {
   105  					if strings.HasPrefix(elem, "!!") {
   106  						badfLine(f, i+1, "invalid double negative in build constraint: %s", arg)
   107  						break Args
   108  					}
   109  					elem = strings.TrimPrefix(elem, "!")
   110  					for _, c := range elem {
   111  						if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
   112  							badfLine(f, i+1, "invalid non-alphanumeric build constraint: %s", arg)
   113  							break Args
   114  						}
   115  					}
   116  				}
   117  			}
   118  			continue
   119  		}
   120  		// Comment with +build but not at beginning.
   121  		if i < cutoff {
   122  			badfLine(f, i+1, "possible malformed +build comment")
   123  			continue
   124  		}
   125  	}
   126  }
   127  

View as plain text