-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathclangutils.go
More file actions
250 lines (227 loc) · 6.02 KB
/
clangutils.go
File metadata and controls
250 lines (227 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/goplus/lib/c"
"github.com/goplus/lib/c/clang"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
)
func main() {
TestClangUtil()
TestComposeIncludes()
TestPreprocess()
TestComment()
}
func TestClangUtil() {
testCases := []struct {
name string
content string
isTemp bool
isCpp bool
}{
{
name: "C Header File",
content: `
int test_function(int a, int b);
void another_function(void);
`,
isTemp: false,
isCpp: false,
},
{
name: "C++ Temp File",
content: `
class TestClass {
public:
void test_method();
static int static_method(float f);
};
namespace TestNamespace {
void namespaced_function();
}
`,
isTemp: true,
isCpp: true,
},
}
for _, tc := range testCases {
fmt.Printf("=== Test Case: %s ===\n", tc.name)
var filePath string
var tempFile *os.File
if tc.isTemp {
filePath = tc.content
} else {
var err error
tempFile, err = os.CreateTemp("", "test_*.h")
if err != nil {
fmt.Printf("Failed to create temporary file: %v\n", err)
continue
}
_, err = tempFile.Write([]byte(tc.content))
if err != nil {
fmt.Printf("Failed to write to temporary file: %v\n", err)
tempFile.Close()
os.Remove(tempFile.Name())
continue
}
tempFile.Close()
filePath = tempFile.Name()
}
config := &clangutils.Config{
File: filePath,
Temp: tc.isTemp,
IsCpp: tc.isCpp,
}
outputInfoFromTranslationUnit(config, func(cursor, parent clang.Cursor) clang.ChildVisitResult {
switch cursor.Kind {
case clang.CursorFunctionDecl, clang.CursorCXXMethod:
funcName := cursor.String()
fmt.Printf("Function/Method: %s\n", c.GoString(funcName.CStr()))
parts := clangutils.BuildScopingParts(cursor)
fmt.Printf("Scoping parts: %v\n", parts)
funcName.Dispose()
case clang.CursorClassDecl:
className := cursor.String()
fmt.Printf("Class: %s\n", c.GoString(className.CStr()))
className.Dispose()
return clang.ChildVisit_Recurse
case clang.CursorNamespace:
namespaceName := cursor.String()
fmt.Printf("Namespace: %s\n", c.GoString(namespaceName.CStr()))
namespaceName.Dispose()
return clang.ChildVisit_Recurse
}
return clang.ChildVisit_Continue
})
if !tc.isTemp && tempFile != nil {
os.Remove(tempFile.Name())
}
fmt.Println()
}
}
func TestComposeIncludes() {
fmt.Println("=== Test ComposeIncludes ===")
testCases := []struct {
name string
files []string
}{
{
name: "One file",
files: []string{"file1.h"},
},
{
name: "Two files",
files: []string{"file1.h", "file2.h"},
},
{
name: "Empty files",
files: []string{},
},
}
for _, tc := range testCases {
outfile, err := os.CreateTemp("", "compose_*.h")
if err != nil {
panic(err)
}
err = clangutils.ComposeIncludes(tc.files, outfile.Name())
if err != nil {
panic(err)
}
content, err := os.ReadFile(outfile.Name())
if err != nil {
panic(err)
}
fmt.Println(string(content))
outfile.Close()
os.Remove(outfile.Name())
}
}
func TestPreprocess() {
fmt.Println("=== TestPreprocess ===")
outfile, err := os.CreateTemp("", "compose_*.h")
if err != nil {
panic(err)
}
absPath, err := filepath.Abs("./hfile")
if err != nil {
panic(err)
}
clangutils.ComposeIncludes([]string{"main.h", "compat.h"}, outfile.Name())
efile, err := os.CreateTemp("", "temp_*.i")
if err != nil {
panic(err)
}
defer os.Remove(efile.Name())
cfg := &clangutils.PreprocessConfig{
File: outfile.Name(),
IsCpp: true,
Args: []string{"-I" + absPath},
OutFile: efile.Name(),
}
err = clangutils.Preprocess(cfg)
if err != nil {
panic(err)
}
config := &clangutils.Config{
File: efile.Name(),
Temp: false,
IsCpp: false,
}
outputInfoFromTranslationUnit(config, func(cursor, parent clang.Cursor) clang.ChildVisitResult {
switch cursor.Kind {
case clang.CursorEnumDecl, clang.CursorStructDecl, clang.CursorUnionDecl, clang.CursorTypedefDecl:
declName := cursor.String()
var filename clang.String
var line, column c.Uint
cursor.Location().PresumedLocation(&filename, &line, &column)
fmt.Printf("TypeKind: %d Name: %s\n", cursor.Kind, c.GoString(declName.CStr()))
fmt.Printf("Location: %s:%d:%d\n", path.Base(c.GoString(filename.CStr())), line, column)
declName.Dispose()
}
return clang.ChildVisit_Continue
})
outfile.Close()
os.Remove(outfile.Name())
}
func TestComment() {
fmt.Println("=== TestComment ===")
config := &clangutils.Config{
File: `
#include <comment.h>
`,
Temp: true,
IsCpp: false,
Args: []string{"-I./hfile", "-E", "-fparse-all-comments"},
}
index, unit, err := clangutils.CreateTranslationUnit(config)
if err != nil {
fmt.Printf("CreateTranslationUnit failed: %v\n", err)
}
cursor := unit.Cursor()
clangutils.VisitChildren(cursor, func(cursor, parent clang.Cursor) clang.ChildVisitResult {
if cursor.Kind != clang.CursorMacroDefinition && cursor.Kind != clang.CursorInclusionDirective {
fmt.Println("cursor", clang.GoString(cursor.String()), "rawComment:", clang.GoString(cursor.RawCommentText()))
commentRange := cursor.CommentRange()
cursorRange := cursor.Extent()
fmt.Printf("commentRange %d:%d -> %d:%d\n", commentRange.RangeStart().Line(), commentRange.RangeStart().Column(), commentRange.RangeEnd().Line(), commentRange.RangeEnd().Column())
fmt.Printf("cursorRange %d:%d -> %d:%d\n", cursorRange.RangeStart().Line(), cursorRange.RangeStart().Column(), cursorRange.RangeEnd().Line(), cursorRange.RangeEnd().Column())
fmt.Println("--------------------------------")
}
return clang.ChildVisit_Recurse
})
index.Dispose()
unit.Dispose()
}
func outputInfoFromTranslationUnit(config *clangutils.Config, visitFunc func(cursor, parent clang.Cursor) clang.ChildVisitResult) {
index, unit, err := clangutils.CreateTranslationUnit(config)
if err != nil {
panic(err)
}
fmt.Println("CreateTranslationUnit succeeded")
cursor := unit.Cursor()
clangutils.VisitChildren(cursor, visitFunc)
index.Dispose()
unit.Dispose()
}