Skip to content

Commit 7ea3148

Browse files
authored
Merge pull request #1834 from cpunion/test/directive-comments
test: cover LLGo directive comments
2 parents 9b0889e + 5442f6a commit 7ea3148

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//go:build llgo && directive_skipall_fixture
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 _ "unsafe"
22+
23+
// This file is a spelling fixture for //llgo:skipall. It is intentionally behind
24+
// an explicit build tag because enabling it in the normal test package skips
25+
// package codegen, including the package init symbol expected by the test main.
26+
// Manual exercise: go run ./cmd/llgo test -tags directive_skipall_fixture ./test/go.
27+
// The command is expected to fail during link if included in the ordinary test
28+
// package; source-patch skipall behavior is covered by internal/build tests.
29+
//
30+
//llgo:skipall
31+
type directiveSkipAllAnchor struct{}
32+
33+
//go:linkname directiveSkipAllMissing C.llgo_missing_directive_skipall
34+
func directiveSkipAllMissing()
35+
36+
func directiveSkipAllBad() {
37+
directiveSkipAllMissing()
38+
}

test/go/directives_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)