github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/go/analysis/passes/atomicalign/testdata/src/a/a.go (about)

     1  // Copyright 2019 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  // This file contains tests for the atomic alignment checker.
     6  
     7  // +build arm 386
     8  
     9  package testdata
    10  
    11  import (
    12  	"io"
    13  	"sync/atomic"
    14  )
    15  
    16  func intsAlignment() {
    17  	var s struct {
    18  		a bool
    19  		b uint8
    20  		c int8
    21  		d byte
    22  		f int16
    23  		g int16
    24  		h int64
    25  		i byte
    26  		j uint64
    27  	}
    28  	atomic.AddInt64(&s.h, 9)
    29  	atomic.AddUint64(&s.j, 0) // want "address of non 64-bit aligned field .j passed to atomic.AddUint64"
    30  }
    31  
    32  func floatAlignment() {
    33  	var s struct {
    34  		a float32
    35  		b int64
    36  		c float32
    37  		d float64
    38  		e uint64
    39  	}
    40  	atomic.LoadInt64(&s.b) // want "address of non 64-bit aligned field .b passed to atomic.LoadInt64"
    41  	atomic.LoadUint64(&s.e)
    42  }
    43  
    44  func uintptrAlignment() {
    45  	var s struct {
    46  		a uintptr
    47  		b int64
    48  		c int
    49  		d uint
    50  		e int32
    51  		f uint64
    52  	}
    53  	atomic.StoreInt64(&s.b, 0) // want "address of non 64-bit aligned field .b passed to atomic.StoreInt64"
    54  	atomic.StoreUint64(&s.f, 0)
    55  }
    56  
    57  func runeAlignment() {
    58  	var s struct {
    59  		a rune
    60  		b int64
    61  		_ rune
    62  		c uint64
    63  	}
    64  	atomic.SwapInt64(&s.b, 0) // want "address of non 64-bit aligned field .b passed to atomic.SwapInt64"
    65  	atomic.SwapUint64(&s.c, 0)
    66  }
    67  
    68  func complexAlignment() {
    69  	var s struct {
    70  		a complex64
    71  		b int64
    72  		c complex128
    73  		d uint64
    74  	}
    75  	atomic.CompareAndSwapInt64(&s.b, 0, 1)
    76  	atomic.CompareAndSwapUint64(&s.d, 0, 1)
    77  }
    78  
    79  // continuer ici avec les tests
    80  
    81  func channelAlignment() {
    82  	var a struct {
    83  		a chan struct{}
    84  		b int64
    85  		c <-chan struct{}
    86  		d uint64
    87  	}
    88  
    89  	atomic.AddInt64(&a.b, 0) // want "address of non 64-bit aligned field .b passed to atomic.AddInt64"
    90  	atomic.AddUint64(&a.d, 0)
    91  }
    92  
    93  func arrayAlignment() {
    94  	var a struct {
    95  		a [1]uint16
    96  		b int64
    97  		_ [2]uint16
    98  		c int64
    99  		d [1]uint16
   100  		e uint64
   101  	}
   102  
   103  	atomic.LoadInt64(&a.b) // want "address of non 64-bit aligned field .b passed to atomic.LoadInt64"
   104  	atomic.LoadInt64(&a.c)
   105  	atomic.LoadUint64(&a.e) // want "address of non 64-bit aligned field .e passed to atomic.LoadUint64"
   106  }
   107  
   108  func anonymousFieldAlignment() {
   109  	var f struct {
   110  		a, b int32
   111  		c, d int64
   112  		_    bool
   113  		e, f uint64
   114  	}
   115  
   116  	atomic.StoreInt64(&f.c, 12)
   117  	atomic.StoreInt64(&f.d, 27)
   118  	atomic.StoreUint64(&f.e, 6)  // want "address of non 64-bit aligned field .e passed to atomic.StoreUint64"
   119  	atomic.StoreUint64(&f.f, 79) // want "address of non 64-bit aligned field .f passed to atomic.StoreUint64"
   120  }
   121  
   122  type ts struct {
   123  	e  int64
   124  	e2 []int
   125  	f  uint64
   126  }
   127  
   128  func typedStructAlignment() {
   129  	var b ts
   130  	atomic.SwapInt64(&b.e, 9)
   131  	atomic.SwapUint64(&b.f, 9) // want "address of non 64-bit aligned field .f passed to atomic.SwapUint64"
   132  }
   133  
   134  func aliasAlignment() {
   135  	type (
   136  		mybytea uint8
   137  		mybyteb byte
   138  		mybytec = uint8
   139  		mybyted = byte
   140  	)
   141  
   142  	var e struct {
   143  		a    byte
   144  		b    mybytea
   145  		c    mybyteb
   146  		e    mybytec
   147  		f    int64
   148  		g, h uint16
   149  		i    uint64
   150  	}
   151  
   152  	atomic.CompareAndSwapInt64(&e.f, 0, 1) // want "address of non 64-bit aligned field .f passed to atomic.CompareAndSwapInt64"
   153  	atomic.CompareAndSwapUint64(&e.i, 1, 2)
   154  }
   155  
   156  func stringAlignment() {
   157  	var a struct {
   158  		a uint32
   159  		b string
   160  		c int64
   161  	}
   162  	atomic.AddInt64(&a.c, 10) // want "address of non 64-bit aligned field .c passed to atomic.AddInt64"
   163  }
   164  
   165  func sliceAlignment() {
   166  	var s struct {
   167  		a []int32
   168  		b int64
   169  		c uint32
   170  		d uint64
   171  	}
   172  
   173  	atomic.LoadInt64(&s.b) // want "address of non 64-bit aligned field .b passed to atomic.LoadInt64"
   174  	atomic.LoadUint64(&s.d)
   175  }
   176  
   177  func interfaceAlignment() {
   178  	var s struct {
   179  		a interface{}
   180  		b int64
   181  		c io.Writer
   182  		e int64
   183  		_ int32
   184  		f uint64
   185  	}
   186  
   187  	atomic.StoreInt64(&s.b, 9)
   188  	atomic.StoreInt64(&s.e, 9)
   189  	atomic.StoreUint64(&s.f, 9) // want "address of non 64-bit aligned field .f passed to atomic.StoreUint64"
   190  }
   191  
   192  func pointerAlignment() {
   193  	var s struct {
   194  		a, b *int
   195  		c    int64
   196  		d    *interface{}
   197  		e    uint64
   198  	}
   199  
   200  	atomic.SwapInt64(&s.c, 9)
   201  	atomic.SwapUint64(&s.e, 9) // want "address of non 64-bit aligned field .e passed to atomic.SwapUint64"
   202  }
   203  
   204  // non-struct fields are already 64-bits correctly aligned per Go spec
   205  func nonStructFields() {
   206  	var (
   207  		a *int64
   208  		b [2]uint64
   209  		c int64
   210  	)
   211  
   212  	atomic.CompareAndSwapInt64(a, 10, 11)
   213  	atomic.CompareAndSwapUint64(&b[0], 5, 23)
   214  	atomic.CompareAndSwapInt64(&c, -1, -15)
   215  }
   216  
   217  func embeddedStructFields() {
   218  	var s1 struct {
   219  		_ struct{ _ int32 }
   220  		a int64
   221  		_ struct{}
   222  		b uint64
   223  		_ struct{ _ [2]uint16 }
   224  		c int64
   225  	}
   226  
   227  	atomic.AddInt64(&s1.a, 9)  // want "address of non 64-bit aligned field .a passed to atomic.AddInt64"
   228  	atomic.AddUint64(&s1.b, 9) // want "address of non 64-bit aligned field .b passed to atomic.AddUint64"
   229  	atomic.AddInt64(&s1.c, 9)
   230  }