|
| 1 | +//go:build llgo |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package gotest |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + _ "unsafe" |
| 24 | + |
| 25 | + "github.com/goplus/lib/c" |
| 26 | +) |
| 27 | + |
| 28 | +//go:linkname directiveSqrt C.sqrt |
| 29 | +func directiveSqrt(x c.Double) c.Double |
| 30 | + |
| 31 | +//llgo:link directiveAbs C.abs |
| 32 | +func directiveAbs(x c.Int) c.Int { return 0 } |
| 33 | + |
| 34 | +// The spaced form is an LLGo extension and intentionally differs from Go's |
| 35 | +// compiler directive spelling. |
| 36 | +// llgo:link directiveAbsSpaced C.abs |
| 37 | +func directiveAbsSpaced(x c.Int) c.Int { return 0 } |
| 38 | + |
| 39 | +// The anchor type keeps the named //llgo:skip directive attached to a declaration |
| 40 | +// where LLGo collects skip comments, while the skipped symbol is named explicitly. |
| 41 | +// |
| 42 | +//llgo:skip DirectiveSkippedBad |
| 43 | +type directiveSkipAnchor struct{} |
| 44 | + |
| 45 | +//go:linkname directiveSkippedMissing C.llgo_missing_directive_skip |
| 46 | +func directiveSkippedMissing() |
| 47 | + |
| 48 | +func DirectiveSkippedBad() { |
| 49 | + directiveSkippedMissing() |
| 50 | +} |
| 51 | + |
| 52 | +func TestLLGoDirectiveLinknameForms(t *testing.T) { |
| 53 | + if got := float64(directiveSqrt(9)); got != 3 { |
| 54 | + t.Fatalf("go:linkname sqrt = %v, want 3", got) |
| 55 | + } |
| 56 | + if got := int(directiveAbs(-4)); got != 4 { |
| 57 | + t.Fatalf("llgo:link abs = %d, want 4", got) |
| 58 | + } |
| 59 | + if got := int(directiveAbsSpaced(-5)); got != 5 { |
| 60 | + t.Fatalf("spaced llgo:link abs = %d, want 5", got) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestLLGoDirectiveSkip(t *testing.T) { |
| 65 | + _ = t |
| 66 | + // This is a link-time regression check. If //llgo:skip above is ignored, |
| 67 | + // DirectiveSkippedBad is compiled and the package fails to link because |
| 68 | + // C.llgo_missing_directive_skip does not exist. |
| 69 | +} |
0 commit comments