diff --git a/.github/workflows/llgo.yml b/.github/workflows/llgo.yml index fd5c32715..926cb25ee 100644 --- a/.github/workflows/llgo.yml +++ b/.github/workflows/llgo.yml @@ -23,8 +23,8 @@ jobs: os: - macos-latest - ubuntu-latest - llvm: [19] - llgo: [v1.0.1] + llvm: [22] + llgo: [v1.0.2] go: [1.27] fail-fast: false runs-on: ${{matrix.os}} diff --git a/_xtool/internal/clang/clang.go b/_xtool/internal/clang/clang.go deleted file mode 100644 index 667d3a9b5..000000000 --- a/_xtool/internal/clang/clang.go +++ /dev/null @@ -1,144 +0,0 @@ -package clang - -import ( - "errors" - "path/filepath" - "unsafe" - - "github.com/goplus/lib/c" - "github.com/goplus/llcppg/_xtool/internal/clangtool" - clang "github.com/goplus/llcppg/_xtool/internal/libclang" -) - -type Config struct { - File string - Temp bool - Args []string - IsCpp bool - Index *clang.Index - Options c.Uint -} - -type Visitor func(cursor, parent clang.Cursor) clang.ChildVisitResult - -type InclusionVisitor func(included_file clang.File, inclusions []clang.SourceLocation) - -const TEMP_FILE = "temp.h" - -func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) { - // default use the C/C++ standard of clang; c:gnu17 C++:gnu++17 - // https://clang.llvm.org/docs/CommandGuide/clang.html - allArgs := clangtool.WithSysRoot(append(defaultArgs(config.IsCpp), config.Args...)) - - cArgs := make([]*c.Char, len(allArgs)) - for i, arg := range allArgs { - cArgs[i] = c.AllocaCStr(arg) - } - - var index *clang.Index - if config.Index != nil { - index = config.Index - } else { - index = clang.CreateIndex(0, 0) - } - - var unit *clang.TranslationUnit - - if config.Temp { - content := c.AllocaCStr(config.File) - tempFile := &clang.UnsavedFile{ - Filename: c.Str(TEMP_FILE), - Contents: content, - Length: c.Ulong(c.Strlen(content)), - } - - unit = index.ParseTranslationUnit( - tempFile.Filename, - unsafe.SliceData(cArgs), c.Int(len(cArgs)), - tempFile, 1, - config.Options, - ) - - } else { - cFile := c.AllocaCStr(config.File) - unit = index.ParseTranslationUnit( - cFile, - unsafe.SliceData(cArgs), c.Int(len(cArgs)), - nil, 0, - config.Options, - ) - } - - if unit == nil { - return nil, nil, errors.New("failed to parse translation unit") - } - - return index, unit, nil -} - -func GetLocation(loc clang.SourceLocation) (file clang.File, line c.Uint, column c.Uint, offset c.Uint) { - loc.SpellingLocation(&file, &line, &column, &offset) - return -} - -func GetPresumedLocation(loc clang.SourceLocation) (fileGo string, line c.Uint, column c.Uint) { - var file clang.String - loc.PresumedLocation(&file, &line, &column) - fileGo = filepath.Clean(clang.GoString(file)) - return -} - -// Traverse up the semantic parents -func BuildScopingParts(cursor clang.Cursor) []string { - var parts []string - for cursor.IsNull() != 1 && cursor.Kind != clang.CursorTranslationUnit { - name := cursor.String() - qualified := c.GoString(name.CStr()) - parts = append([]string{qualified}, parts...) - cursor = cursor.SemanticParent() - name.Dispose() - } - return parts -} - -func HasParent(cursor clang.Cursor) bool { - semanticParentsNum := 0 - node := cursor - for node.IsNull() != 1 && node.Kind != clang.CursorTranslationUnit { - semanticParentsNum++ - node = node.SemanticParent() - } - if semanticParentsNum > 1 { - return true - } - node = cursor - lexicalParentsNum := 0 - for node.IsNull() != 1 && node.Kind != clang.CursorTranslationUnit { - lexicalParentsNum++ - node = node.LexicalParent() - } - return lexicalParentsNum > 1 -} - -func VisitChildren(cursor clang.Cursor, fn Visitor) c.Uint { - return clang.VisitChildren(cursor, func(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { - cfn := *(*Visitor)(clientData) - return cfn(cursor, parent) - }, unsafe.Pointer(&fn)) -} - -func GetInclusions(unit *clang.TranslationUnit, visitor InclusionVisitor) { - clang.GetInclusions(unit, func(inced clang.File, incin *clang.SourceLocation, incilen c.Uint, data c.Pointer) { - ics := unsafe.Slice(incin, incilen) - cfn := *(*InclusionVisitor)(data) - cfn(inced, ics) - }, unsafe.Pointer(&visitor)) -} - -func defaultArgs(isCpp bool) []string { - args := []string{"-x", "c"} - if isCpp { - args = []string{"-x", "c++"} - } - return args -} diff --git a/_xtool/internal/clang/clang_test.go b/_xtool/internal/clang/clang_test.go deleted file mode 100644 index 6008db66b..000000000 --- a/_xtool/internal/clang/clang_test.go +++ /dev/null @@ -1,195 +0,0 @@ -package clang_test - -import ( - "fmt" - "os" - "strings" - "testing" - - clangutils "github.com/goplus/llcppg/_xtool/internal/clang" - clang "github.com/goplus/llcppg/_xtool/internal/libclang" -) - -func TestClangUtil(t *testing.T) { - testCases := []struct { - name string - content string - isTemp bool - isCpp bool - expect string - }{ - { - name: "C Header File", - content: ` - int test_function(int a, int b); - void another_function(void); - `, - isTemp: false, - isCpp: false, - expect: ` -Function/Method: test_function -Scoping parts: test_function -Function/Method: another_function -Scoping parts: another_function -`, - }, - { - 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, - expect: ` -Class: TestClass -Function/Method: test_method -Scoping parts: TestClass,test_method -Function/Method: static_method -Scoping parts: TestClass,static_method -Namespace: TestNamespace -Function/Method: namespaced_function -Scoping parts: TestNamespace,namespaced_function - `, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - 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 { - t.Fatalf("Failed to create temporary file: %w\n", err) - return - } - defer tempFile.Close() - - _, err = tempFile.Write([]byte(tc.content)) - if err != nil { - t.Fatalf("Failed to write to temporary file: %w\n", err) - } - defer os.Remove(tempFile.Name()) - filePath = tempFile.Name() - } - - config := &clangutils.Config{ - File: filePath, - Temp: tc.isTemp, - IsCpp: tc.isCpp, - } - - var str strings.Builder - - visit(config, func(cursor, parent clang.Cursor) clang.ChildVisitResult { - switch cursor.Kind { - case clang.CursorFunctionDecl, clang.CursorCXXMethod: - str.WriteString("Function/Method: ") - str.WriteString(clang.GoString(cursor.String())) - str.WriteString("\n") - parts := clangutils.BuildScopingParts(cursor) - str.WriteString("Scoping parts: ") - str.WriteString(strings.Join(parts, ",")) - str.WriteString("\n") - case clang.CursorClassDecl: - str.WriteString("Class: ") - str.WriteString(clang.GoString(cursor.String())) - str.WriteString("\n") - return clang.ChildVisit_Recurse - case clang.CursorNamespace: - str.WriteString("Namespace: ") - str.WriteString(clang.GoString(cursor.String())) - str.WriteString("\n") - return clang.ChildVisit_Recurse - } - return clang.ChildVisit_Continue - }) - compareOutput(t, tc.expect, str.String()) - }) - } -} - -func TestComment(t *testing.T) { - config := &clangutils.Config{ - File: ` - #include - `, - Temp: true, - IsCpp: false, - Args: []string{"-I./testdata/hfile", "-E", "-fparse-all-comments"}, - } - - var str strings.Builder - - visit(config, func(cursor, parent clang.Cursor) clang.ChildVisitResult { - if cursor.Kind != clang.CursorMacroDefinition && cursor.Kind != clang.CursorInclusionDirective { - str.WriteString("cursor ") - str.WriteString(clang.GoString(cursor.String())) - str.WriteString("rawComment: ") - str.WriteString(clang.GoString(cursor.RawCommentText())) - str.WriteString("\n") - commentRange := cursor.CommentRange() - cursorRange := cursor.Extent() - str.WriteString("commentRange ") - str.WriteString(fmt.Sprintf("%d:%d -> %d:%d\n", commentRange.RangeStart().Line(), commentRange.RangeStart().Column(), commentRange.RangeEnd().Line(), commentRange.RangeEnd().Column())) - str.WriteString("cursorRange ") - str.WriteString(fmt.Sprintf("%d:%d -> %d:%d\n", cursorRange.RangeStart().Line(), cursorRange.RangeStart().Column(), cursorRange.RangeEnd().Line(), cursorRange.RangeEnd().Column())) - str.WriteString("--------------------------------\n") - } - return clang.ChildVisit_Recurse - }) - - expect := ` -cursor FoorawComment: // doc -commentRange 1:1 -> 1:7 -cursorRange 2:1 -> 8:2 --------------------------------- -cursor xrawComment: // doc -commentRange 3:5 -> 3:11 -cursorRange 4:5 -> 4:10 --------------------------------- -cursor yrawComment: // comment -commentRange 5:12 -> 5:22 -cursorRange 5:5 -> 5:10 --------------------------------- -cursor zrawComment: // comment -commentRange 7:12 -> 7:22 -cursorRange 7:5 -> 7:10 --------------------------------- -cursor foorawComment: // doc -commentRange 10:1 -> 10:7 -cursorRange 11:1 -> 11:11 ---------------------------------` - - compareOutput(t, expect, str.String()) -} - -func visit(config *clangutils.Config, visitFunc func(cursor, parent clang.Cursor) clang.ChildVisitResult) { - index, unit, err := clangutils.CreateTranslationUnit(config) - if err != nil { - panic(err) - } - cursor := unit.Cursor() - clangutils.VisitChildren(cursor, visitFunc) - index.Dispose() - unit.Dispose() -} - -func compareOutput(t *testing.T, expected, actual string) { - expected = strings.TrimSpace(expected) - actual = strings.TrimSpace(actual) - if expected != actual { - t.Fatalf("Test failed: expected \n%s \ngot \n%s", expected, actual) - } -} diff --git a/_xtool/internal/clang/testdata/hfile/comment.h b/_xtool/internal/clang/testdata/hfile/comment.h deleted file mode 100644 index 1ec502458..000000000 --- a/_xtool/internal/clang/testdata/hfile/comment.h +++ /dev/null @@ -1,12 +0,0 @@ -// doc -struct Foo { - // doc - int x; - int y; // comment - // doc field doc (parse ignore with comment in same cursor) - int z; // comment -};// comment - -// doc -void foo(); // comment - diff --git a/_xtool/internal/clangtool/clangtool.go b/_xtool/internal/clangtool/clangtool.go deleted file mode 100644 index 64ac8c547..000000000 --- a/_xtool/internal/clangtool/clangtool.go +++ /dev/null @@ -1,109 +0,0 @@ -package clangtool - -import ( - "bytes" - "fmt" - "os" - "os/exec" - "path/filepath" - "regexp" - "strings" - "sync" -) - -var _sysRootDirOnce = sync.OnceValues(sysRoot) - -var _matchISysrootRegex = regexp.MustCompile(`-(resource-dir|internal-isystem|isysroot|internal-externc-isystem)\s(\S+)`) - -// ComposeIncludes create Include list -// #include -// #include -func ComposeIncludes(files []string, outfile string) error { - var str string - for _, file := range files { - str += ("#include <" + file + ">\n") - } - return os.WriteFile(outfile, []byte(str), 0644) -} - -func GetIncludePaths(isCpp bool) []string { - args := []string{"-E", "-v"} - args = append(args, defaultArgs(isCpp)...) - args = append(args, "/dev/null") - cmd := exec.Command("clang", args...) - output, err := cmd.CombinedOutput() - if err != nil { - panic(err) - } - return ParseClangIncOutput(string(output)) -} - -func ParseClangIncOutput(output string) []string { - var paths []string - start := strings.Index(output, "#include <...> search starts here:") - end := strings.Index(output, "End of search list.") - if start == -1 || end == -1 { - return paths - } - content := output[start:end] - lines := strings.Split(content, "\n") - for _, line := range lines[1:] { - for _, item := range strings.Fields(line) { - if path := strings.TrimSpace(item); filepath.IsAbs(path) { - paths = append(paths, path) - } - } - } - return paths -} - -func WithSysRoot(args []string) []string { - _defaultSysRootDir, _ := _sysRootDirOnce() - return append(args, _defaultSysRootDir...) -} - -func defaultArgs(isCpp bool) []string { - args := []string{"-x", "c"} - if isCpp { - args = []string{"-x", "c++"} - } - return args -} - -// sysRoot retrieves isysroot from clang preprocessor -func sysRoot() ([]string, error) { - var output bytes.Buffer - - // -x dones't matter, we don't care, just get the isysroot - cmd := exec.Command("clang", "-E", "-v", "-x", "c", "/dev/null") - cmd.Stderr = &output - - cmd.Run() - - return ParseSystemPath(output.String()) -} - -func ParseSystemPath(output string) ([]string, error) { - sysRootResults := _matchISysrootRegex.FindAllStringSubmatch(output, -1) - - var result []string - - for _, sysRootResult := range sysRootResults { - if len(sysRootResult) == 3 { - if sysRootResult[1] == "resource-dir" { - // the format of resource-dir must be -resource-dir=/xxx - result = append(result, fmt.Sprintf("-%s=%s", sysRootResult[1], sysRootResult[2])) - // append its header path also - result = append(result, fmt.Sprintf("-I%s", filepath.Join(sysRootResult[2], "include"))) - continue - } - result = append(result, fmt.Sprintf("-%s%s", sysRootResult[1], sysRootResult[2])) - } - } - - if len(result) == 0 { - return nil, fmt.Errorf("failed to find any sysRoot path") - } - - return result, nil -} diff --git a/_xtool/internal/clangtool/clangtool_test.go b/_xtool/internal/clangtool/clangtool_test.go deleted file mode 100644 index 9037fe6ee..000000000 --- a/_xtool/internal/clangtool/clangtool_test.go +++ /dev/null @@ -1,155 +0,0 @@ -package clangtool_test - -import ( - "os" - "reflect" - "testing" - - "github.com/goplus/llcppg/_xtool/internal/clangtool" -) - -func TestComposeIncludes(t *testing.T) { - testCases := []struct { - name string - files []string - expect string - }{ - { - name: "One file", - files: []string{"file1.h"}, - expect: `#include -`, - }, - { - name: "Two files", - files: []string{"file1.h", "file2.h"}, - expect: `#include -#include -`, - }, - { - name: "Empty files", - files: []string{}, - expect: "", - }, - } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - outfile, err := os.CreateTemp("", "compose_*.h") - if err != nil { - t.Fatal(err) - } - - err = clangtool.ComposeIncludes(tc.files, outfile.Name()) - if err != nil { - t.Fatal(err) - } - content, err := os.ReadFile(outfile.Name()) - if err != nil { - t.Fatal(err) - } - if string(content) != tc.expect { - t.Fatalf("expect %s, but got %s", tc.expect, string(content)) - } - outfile.Close() - os.Remove(outfile.Name()) - }) - } -} - -func TestClangIncOutput(t *testing.T) { - res := clangtool.ParseClangIncOutput( - `Ubuntu clang version 18.1.3 (1ubuntu1) -Target: aarch64-unknown-linux-gnu -Thread model: posix -InstalledDir: /usr/bin -Found candidate GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/13 -Selected GCC installation: /usr/bin/../lib/gcc/aarch64-linux-gnu/13 -Candidate multilib: .;@m64 -Selected multilib: .;@m64 - (in-process) - "/usr/lib/llvm-18/bin/clang" -cc1 -triple aarch64-unknown-linux-gnu -E -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name null -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=non-leaf -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu generic -target-feature +v8a -target-feature +fp-armv8 -target-feature +neon -target-abi aapcs -debugger-tuning=gdb -fdebug-compilation-dir=/root/llcppg -v -fcoverage-compilation-dir=/root/llcppg -resource-dir /usr/lib/llvm-18/lib/clang/18 -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/aarch64-linux-gnu/13/../../../../aarch64-linux-gnu/include -internal-externc-isystem /usr/include/aarch64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fno-signed-char -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -target-feature +outline-atomics -target-feature -fmv -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o - -x c /dev/null -clang -cc1 version 18.1.3 based upon LLVM 18.1.3 default target aarch64-unknown-linux-gnu -ignoring nonexistent directory "/usr/bin/../lib/gcc/aarch64-linux-gnu/13/../../../../aarch64-linux-gnu/include" -ignoring nonexistent directory "/include" -#include "..." search starts here: -#include <...> search starts here: - /usr/lib/llvm-18/lib/clang/18/include - /usr/local/include - /usr/include/aarch64-linux-gnu - /usr/include -End of search list. -# 1 "/dev/null" -# 1 "" 1 -# 1 "" 3 -# 399 "" 3 -# 1 "" 1 -# 1 "" 2 -# 1 "/dev/null" 2 -`) - expect := []string{ - "/usr/lib/llvm-18/lib/clang/18/include", - "/usr/local/include", - "/usr/include/aarch64-linux-gnu", - "/usr/include", - } - if !reflect.DeepEqual(res, expect) { - t.Fatalf("expect %v, but got %v", expect, res) - } -} - -func TestSysRoot(t *testing.T) { - testCases := []struct { - name string - input string - expect []string - }{ - { - name: "macos-sysroot", - input: `Homebrew clang version 19.1.7 -Target: arm64-apple-darwin23.6.0 -Thread model: posix -InstalledDir: /opt/homebrew/Cellar/llvm@19/19.1.7/bin -Configuration file: /opt/homebrew/etc/clang/arm64-apple-darwin23.cfg -System configuration file directory: /opt/homebrew/etc/clang - (in-process) - "/opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang-19" -cc1 -triple arm64-apple-macosx14.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name null -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -funwind-tables=1 -target-sdk-version=14.4 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -fbuiltin-headers-in-system-modules -fdefine-target-os-macros -target-cpu apple-m1 -target-feature +zcm -target-feature +zcz -target-feature +v8.4a -target-feature +aes -target-feature +altnzcv -target-feature +ccdp -target-feature +complxnum -target-feature +crc -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +fptoint -target-feature +fullfp16 -target-feature +jsconv -target-feature +lse -target-feature +neon -target-feature +pauth -target-feature +perfmon -target-feature +predres -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sb -target-feature +sha2 -target-feature +sha3 -target-feature +specrestrict -target-feature +ssbs -target-abi darwinpcs -debugger-tuning=lldb -target-linker-version 1115.7.3 -v -resource-dir /opt/homebrew/Cellar/llvm@19/19.1.7/lib/clang/19 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/local/include -internal-isystem /opt/homebrew/Cellar/llvm@19/19.1.7/lib/clang/19/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/include -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fmax-type-align=16 -fcolor-diagnostics -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o - -x c /dev/null -clang -cc1 version 19.1.7 based upon LLVM 19.1.7 default target arm64-apple-darwin23.6.0 -ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/local/include" -ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/Library/Frameworks" -#include "..." search starts here: -#include <...> search starts here: - /opt/homebrew/Cellar/llvm@19/19.1.7/lib/clang/19/include - /Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/include - /Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/System/Library/Frameworks (framework directory) -End of search list. -# 1 "/dev/null" -# 1 "" 1 -# 1 "" 3 -# 455 "" 3 -# 1 "" 1 -# 1 "" 2 -# 1 "/dev/null" 2 -`, - expect: []string{ - "-resource-dir=/opt/homebrew/Cellar/llvm@19/19.1.7/lib/clang/19", - "-I/opt/homebrew/Cellar/llvm@19/19.1.7/lib/clang/19/include", - "-isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk", - "-internal-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/local/include", - "-internal-isystem/opt/homebrew/Cellar/llvm@19/19.1.7/lib/clang/19/include", - "-internal-externc-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/include", - }, - }, - } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - output, err := clangtool.ParseSystemPath(tc.input) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(output, tc.expect) { - t.Fatalf("parse sysroot failed: want: %v got %v", tc.expect, output) - } - }) - } -} diff --git a/_xtool/internal/clangtool/inclusion.go b/_xtool/internal/clangtool/inclusion.go deleted file mode 100644 index 5dee081b5..000000000 --- a/_xtool/internal/clangtool/inclusion.go +++ /dev/null @@ -1,70 +0,0 @@ -package clangtool - -import ( - "bufio" - "bytes" - "errors" - "fmt" - "os" - "os/exec" - "path/filepath" - "strings" -) - -type Config struct { - HeaderFileName string - ComposedHeaderFile string - CompileArgs []string - IsCpp bool -} - -func GetInclusions(conf *Config, fn func(fileName string, depth int)) error { - if conf.HeaderFileName == "" && conf.ComposedHeaderFile == "" { - return errors.New("failed to get inclusion: no header file") - } - file := conf.ComposedHeaderFile - if file == "" { - tmpFile, err := os.CreateTemp("", "inclusion") - if err != nil { - return err - } - tmpName := tmpFile.Name() - if err := tmpFile.Close(); err != nil { - _ = os.Remove(tmpName) - return err - } - defer os.Remove(tmpName) - - inc := fmt.Sprintf("#include <%s>", conf.HeaderFileName) - if err := os.WriteFile(tmpName, []byte(inc), 0600); err != nil { - return err - } - - file = tmpName - } - - args := defaultArgs(conf.IsCpp) - args = append(args, "-H", "-E") - args = append(args, conf.CompileArgs...) - args = append(args, file) - - var buf bytes.Buffer - - cmd := exec.Command("clang", args...) - cmd.Stderr = &buf - err := cmd.Run() - if err != nil { - return errors.New(buf.String()) - } - - br := bufio.NewScanner(&buf) - - for br.Scan() { - strs := strings.Split(br.Text(), " ") - if len(strs) == 2 { - fn(filepath.Clean(strs[1]), strings.Count(strs[0], ".")) - } - } - - return nil -} diff --git a/_xtool/internal/header/header.go b/_xtool/internal/header/header.go deleted file mode 100644 index 12ade1758..000000000 --- a/_xtool/internal/header/header.go +++ /dev/null @@ -1,139 +0,0 @@ -package header - -import ( - "os" - "path/filepath" - "strings" - - "github.com/goplus/llcppg/_xtool/internal/clangtool" -) - -type PkgHfilesInfo struct { - Inters []string // From types.Config.Include - Impls []string // From same root of types.Config.Include - Thirds []string // Not Current Pkg's Files - Plats []string // Platform Difference Files -} - -func (p *PkgHfilesInfo) CurPkgFiles() []string { - return append(p.Inters, p.Impls...) -} - -type Config struct { - // Includes specifies the header file include paths to be processed. - // These are the paths used in #include directives, such as: - // - "zlib.h" - // - "openssl/ssl.h" - Includes []string - // PlatDiff specifies header file include paths that differ between platforms, these are include paths in Includes. - PlatDiff []string - Args []string - Mix bool -} - -// PkgHfileInfo analyzes header files dependencies and categorizes them into three groups: -// 1. Inters: Direct includes from types.Config.Include -// 2. Impls: Header files from the same root directory as Inters -// 3. Thirds: Header files from external sources -// -// The function works by: -// 1. Creating a temporary header file that includes all headers from conf.Include -// 2. Using clang to parse the translation unit and analyze includes -// 3. Categorizing includes based on their inclusion level and path relationship -func PkgHfileInfo(conf *Config) *PkgHfilesInfo { - info := &PkgHfilesInfo{ - Inters: []string{}, - Impls: []string{}, - Thirds: []string{}, - } - outfile, err := os.CreateTemp("", "compose_*.h") - if err != nil { - panic(err) - } - outfileName := outfile.Name() - if err := outfile.Close(); err != nil { - panic(err) - } - defer os.Remove(outfileName) - - inters := make(map[string]struct{}) - others := []string{} // impl & third - - retrieveInterfaceFn := func(filename string, depth int) { - if depth == 1 { - info.Inters = append(info.Inters, filename) - inters[filename] = struct{}{} - } - } - - retrieveComposedHeadersFn := func(filename string, depth int) { - // not in the first level include maybe impl or third hfile - _, inter := inters[filename] - if depth > 1 && !inter { - others = append(others, filename) - } - } - - for _, f := range conf.Includes { - err := clangtool.GetInclusions(&clangtool.Config{ - HeaderFileName: f, - CompileArgs: conf.Args, - }, retrieveInterfaceFn) - if err != nil { - panic(err) - } - } - - clangtool.ComposeIncludes(conf.Includes, outfileName) - err = clangtool.GetInclusions(&clangtool.Config{ - ComposedHeaderFile: outfileName, - CompileArgs: conf.Args, - }, retrieveComposedHeadersFn) - if err != nil { - panic(err) - } - - if conf.Mix { - info.Thirds = others - return info - } - - root, err := filepath.Abs(commonParentDir(info.Inters)) - if err != nil { - panic(err) - } - for _, f := range others { - file, err := filepath.Abs(f) - if err != nil { - panic(err) - } - if strings.HasPrefix(file, root) { - info.Impls = append(info.Impls, f) - } else { - info.Thirds = append(info.Thirds, f) - } - } - return info -} - -// commonParentDir finds the longest common parent directory path for a given slice of paths. -// For example, given paths ["/a/b/c/d", "/a/b/e/f"], it returns "/a/b". -func commonParentDir(paths []string) string { - if len(paths) == 0 { - return "" - } - - parts := make([][]string, len(paths)) - for i, path := range paths { - parts[i] = strings.Split(filepath.Dir(path), string(filepath.Separator)) - } - - for i := 0; i < len(parts[0]); i++ { - for j := 1; j < len(parts); j++ { - if i == len(parts[j]) || parts[j][i] != parts[0][i] { - return filepath.Join(parts[0][:i]...) - } - } - } - return filepath.Dir(paths[0]) -} diff --git a/_xtool/internal/header/header_test.go b/_xtool/internal/header/header_test.go deleted file mode 100644 index 293ddcd21..000000000 --- a/_xtool/internal/header/header_test.go +++ /dev/null @@ -1,79 +0,0 @@ -package header_test - -import ( - "fmt" - "path/filepath" - "reflect" - "strings" - "testing" - - "github.com/goplus/llcppg/_xtool/internal/header" - llconfig "github.com/goplus/llcppg/config" -) - -func TestPkgHfileInfo(t *testing.T) { - cases := []struct { - conf *llconfig.Config - want *header.PkgHfilesInfo - }{ - { - conf: &llconfig.Config{ - CFlags: "-I./testdata/hfile -I ./testdata/thirdhfile", - Include: []string{"temp1.h", "temp2.h"}, - }, - want: &header.PkgHfilesInfo{ - Inters: []string{"testdata/hfile/temp1.h", "testdata/hfile/temp2.h"}, - Impls: []string{"testdata/hfile/tempimpl.h"}, - }, - }, - { - conf: &llconfig.Config{ - CFlags: "-I./testdata/hfile -I ./testdata/thirdhfile", - Include: []string{"temp1.h", "temp2.h"}, - Mix: true, - }, - want: &header.PkgHfilesInfo{ - Inters: []string{"testdata/hfile/temp1.h", "testdata/hfile/temp2.h"}, - Impls: []string{}, - }, - }, - } - - for i, tc := range cases { - t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { - info := header.PkgHfileInfo(&header.Config{ - Includes: tc.conf.Include, - Args: strings.Fields(tc.conf.CFlags), - Mix: tc.conf.Mix, - }) - if !reflect.DeepEqual(info.Inters, tc.want.Inters) { - t.Fatalf("inter expected %v, but got %v", tc.want.Inters, info.Inters) - } - if !reflect.DeepEqual(info.Impls, tc.want.Impls) { - t.Fatalf("impl expected %v, but got %v", tc.want.Impls, info.Impls) - } - - thirdhfile, err := filepath.Abs("./testdata/thirdhfile/third.h") - if err != nil { - t.Fatalf("failed to get abs path: %w", err) - } - tfileFound := false - stdioFound := false - for _, tfile := range info.Thirds { - absTfile, err := filepath.Abs(tfile) - if err != nil { - t.Fatalf("failed to get abs path: %w", err) - } - if absTfile == thirdhfile { - tfileFound = true - } - if strings.HasSuffix(absTfile, "stdio.h") { - stdioFound = true - } - } - if !tfileFound || !stdioFound { - t.Fatalf("third hfile or std hfile not found") - } - }) - } -} diff --git a/_xtool/internal/header/testdata/hfile/temp1.h b/_xtool/internal/header/testdata/hfile/temp1.h deleted file mode 100644 index 704cd55ea..000000000 --- a/_xtool/internal/header/testdata/hfile/temp1.h +++ /dev/null @@ -1,2 +0,0 @@ -#include "tempimpl.h" -#include \ No newline at end of file diff --git a/_xtool/internal/header/testdata/hfile/temp2.h b/_xtool/internal/header/testdata/hfile/temp2.h deleted file mode 100644 index 6724ae374..000000000 --- a/_xtool/internal/header/testdata/hfile/temp2.h +++ /dev/null @@ -1 +0,0 @@ -#include \ No newline at end of file diff --git a/_xtool/internal/header/testdata/hfile/tempimpl.h b/_xtool/internal/header/testdata/hfile/tempimpl.h deleted file mode 100644 index e69de29bb..000000000 diff --git a/_xtool/internal/header/testdata/thirdhfile/third.h b/_xtool/internal/header/testdata/thirdhfile/third.h deleted file mode 100644 index e69de29bb..000000000 diff --git a/_xtool/internal/ld/ld.go b/_xtool/internal/ld/ld.go deleted file mode 100644 index 09f77aaa3..000000000 --- a/_xtool/internal/ld/ld.go +++ /dev/null @@ -1,34 +0,0 @@ -package ld - -import ( - "os/exec" - "regexp" - "runtime" -) - -// GetLibSearchPaths returns the library paths from the ld command. -// With linux, it will use ld --verbose to get the library paths. -func GetLibSearchPaths() []string { - var paths []string - if runtime.GOOS == "linux" { - //resolution from https://github.com/goplus/llcppg/commit/02307485db9269481297a4dc5e8449fffaa4f562 - cmd := exec.Command("ld", "--verbose") - output, err := cmd.Output() - if err != nil { - panic(err) - } - return ParseOutput(string(output)) - } - return paths -} - -// ParseOutput parses the output of the ld command. -// It returns the search library paths from the ld command. -func ParseOutput(output string) []string { - var paths []string - matches := regexp.MustCompile(`SEARCH_DIR\("=([^"]+)"\)`).FindAllStringSubmatch(output, -1) - for _, match := range matches { - paths = append(paths, match[1]) - } - return paths -} diff --git a/_xtool/internal/ld/ld_test.go b/_xtool/internal/ld/ld_test.go deleted file mode 100644 index 58786ef58..000000000 --- a/_xtool/internal/ld/ld_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package ld_test - -import ( - "reflect" - "testing" - - "github.com/goplus/llcppg/_xtool/internal/ld" -) - -func TestLdOutput(t *testing.T) { - res := ld.ParseOutput( - `GNU ld (GNU Binutils for Ubuntu) 2.42 - Supported emulations: - aarch64linux - aarch64elf - aarch64elf32 - aarch64elf32b - aarch64elfb - armelf - armelfb - aarch64linuxb - aarch64linux32 - aarch64linux32b - armelfb_linux_eabi - armelf_linux_eabi - using internal linker script: - ================================================== - /* Script for -z combreloc */ - /* Copyright (C) 2014-2024 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ - OUTPUT_FORMAT("elf64-littleaarch64", "elf64-bigaarch64", - "elf64-littleaarch64") - OUTPUT_ARCH(aarch64) - ENTRY(_start) - SEARCH_DIR("=/usr/local/lib/aarch64-linux-gnu"); SEARCH_DIR("=/lib/aarch64-linux-gnu"); SEARCH_DIR("=/usr/lib/aarch64-linux-gnu"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/aarch64-linux-gnu/lib"); - SECTIONS - { - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - `) - expect := []string{ - "/usr/local/lib/aarch64-linux-gnu", - "/lib/aarch64-linux-gnu", - "/usr/lib/aarch64-linux-gnu", - "/usr/local/lib", - "/lib", - "/usr/lib", - "/usr/aarch64-linux-gnu/lib", - } - if !reflect.DeepEqual(res, expect) { - t.Fatalf("expect %v, but got %v", expect, res) - } -} diff --git a/_xtool/internal/parser/_testdata/hfile/compat.h b/_xtool/internal/parser/_testdata/hfile/compat.h deleted file mode 100644 index 027814afe..000000000 --- a/_xtool/internal/parser/_testdata/hfile/compat.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef COMPAT_H -#define COMPAT_H -typedef A B; -#endif \ No newline at end of file diff --git a/_xtool/internal/parser/_testdata/hfile/main.h b/_xtool/internal/parser/_testdata/hfile/main.h deleted file mode 100644 index 521b6e9f6..000000000 --- a/_xtool/internal/parser/_testdata/hfile/main.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef MAIN_H -#define MAIN_H -typedef struct A { - int a; - int b; -} A; -#include "compat.h" -typedef B C; -#endif \ No newline at end of file diff --git a/_xtool/internal/parser/marshal.go b/_xtool/internal/parser/marshal.go deleted file mode 100644 index 5c773e3d1..000000000 --- a/_xtool/internal/parser/marshal.go +++ /dev/null @@ -1,251 +0,0 @@ -package parser - -import ( - "github.com/goplus/llcppg/ast" -) - -func XMarshalDeclList(list []ast.Decl) []map[string]any { - var root []map[string]any - for _, item := range list { - root = append(root, XMarshalASTDecl(item)) - } - return root -} - -func XMarshalFieldList(list []*ast.Field) []map[string]any { - if list == nil { - return nil - } - var root []map[string]any - - for _, item := range list { - root = append(root, XMarshalASTExpr(item)) - } - return root -} - -func XMarshalIncludeList(list []*ast.Include) []map[string]any { - var root []map[string]any - for _, item := range list { - root = append(root, map[string]any{ - "_Type": "Include", - "Path": item.Path, - }) - } - return root -} - -func XMarshalMacroList(list []*ast.Macro) []map[string]any { - var root []map[string]any - - for _, item := range list { - root = append(root, map[string]any{ - "_Type": "Macro", - "Loc": XMarshalLocation(item.Loc), - "Name": item.Name, - "Tokens": XMarshalTokenList(item.Tokens), - }) - } - return root -} - -func XMarshalTokenList(list []*ast.Token) []map[string]any { - if list == nil { - return nil - } - var root []map[string]any - for _, item := range list { - root = append(root, XMarshalToken(item)) - } - return root -} - -func XMarshalIdentList(list []*ast.Ident) []map[string]any { - if list == nil { - return nil - } - var root []map[string]any - - for _, item := range list { - root = append(root, XMarshalASTExpr(item)) - } - return root -} - -func XMarshalASTFile(file *ast.File) map[string]any { - return map[string]any{ - "_Type": "File", - "decls": XMarshalDeclList(file.Decls), - "includes": XMarshalIncludeList(file.Includes), - "macros": XMarshalMacroList(file.Macros), - } -} - -func XMarshalToken(tok *ast.Token) map[string]any { - return map[string]any{ - "_Type": "Token", - "Token": uint(tok.Token), - "Lit": tok.Lit, - } -} - -func XMarshalASTDecl(decl ast.Decl) map[string]any { - if decl == nil { - return nil - } - root := make(map[string]any) - - switch d := decl.(type) { - case *ast.EnumTypeDecl: - root["_Type"] = "EnumTypeDecl" - XMarshalObject(d.Object, root) - root["Type"] = XMarshalASTExpr(d.Type) - case *ast.TypedefDecl: - root["_Type"] = "TypedefDecl" - XMarshalObject(d.Object, root) - root["Type"] = XMarshalASTExpr(d.Type) - case *ast.FuncDecl: - root["_Type"] = "FuncDecl" - XMarshalObject(d.Object, root) - root["MangledName"] = d.MangledName - root["Type"] = XMarshalASTExpr(d.Type) - root["IsInline"] = d.IsInline - root["IsStatic"] = d.IsStatic - root["IsConst"] = d.IsConst - root["IsExplicit"] = d.IsExplicit - root["IsConstructor"] = d.IsConstructor - root["IsDestructor"] = d.IsDestructor - root["IsVirtual"] = d.IsVirtual - root["IsOverride"] = d.IsOverride - case *ast.TypeDecl: - root["_Type"] = "TypeDecl" - XMarshalObject(d.Object, root) - root["Type"] = XMarshalASTExpr(d.Type) - } - return root -} - -func XMarshalObject(decl ast.Object, root map[string]any) { - root["Loc"] = XMarshalLocation(decl.Loc) - root["Doc"] = XMarshalASTExpr(decl.Doc) - root["Parent"] = XMarshalASTExpr(decl.Parent) - root["Name"] = XMarshalASTExpr(decl.Name) -} - -func XMarshalLocation(loc *ast.Location) map[string]any { - if loc == nil { - return nil - } - root := make(map[string]any) - root["_Type"] = "Location" - root["File"] = loc.File - return root -} - -func XMarshalASTExpr(t ast.Expr) map[string]any { - if t == nil { - return nil - } - - root := make(map[string]any) - - switch d := t.(type) { - case *ast.EnumType: - root["_Type"] = "EnumType" - var items []map[string]any - for _, e := range d.Items { - items = append(items, XMarshalASTExpr(e)) - } - root["Items"] = items - case *ast.EnumItem: - root["_Type"] = "EnumItem" - root["Name"] = XMarshalASTExpr(d.Name) - root["Value"] = XMarshalASTExpr(d.Value) - case *ast.RecordType: - root["_Type"] = "RecordType" - root["Tag"] = uint(d.Tag) - root["Fields"] = XMarshalASTExpr(d.Fields) - var methods []map[string]any - for _, m := range d.Methods { - methods = append(methods, XMarshalASTDecl(m)) - } - root["Methods"] = methods - case *ast.FuncType: - root["_Type"] = "FuncType" - root["Params"] = XMarshalASTExpr(d.Params) - root["Ret"] = XMarshalASTExpr(d.Ret) - case *ast.FieldList: - root["_Type"] = "FieldList" - if d == nil { - return nil - } - root["List"] = XMarshalFieldList(d.List) - case *ast.Field: - root["_Type"] = "Field" - root["Type"] = XMarshalASTExpr(d.Type) - root["Doc"] = XMarshalASTExpr(d.Doc) - root["Comment"] = XMarshalASTExpr(d.Comment) - root["IsStatic"] = d.IsStatic - root["Access"] = uint(d.Access) - root["Names"] = XMarshalIdentList(d.Names) - case *ast.Variadic: - root["_Type"] = "Variadic" - case *ast.Ident: - root["_Type"] = "Ident" - if d == nil { - return nil - } - root["Name"] = d.Name - case *ast.TagExpr: - root["_Type"] = "TagExpr" - root["Name"] = XMarshalASTExpr(d.Name) - root["Tag"] = uint(d.Tag) - case *ast.BasicLit: - root["_Type"] = "BasicLit" - root["Kind"] = uint(d.Kind) - root["Value"] = d.Value - case *ast.LvalueRefType: - root["_Type"] = "LvalueRefType" - root["X"] = XMarshalASTExpr(d.X) - case *ast.RvalueRefType: - root["_Type"] = "RvalueRefType" - root["X"] = XMarshalASTExpr(d.X) - case *ast.PointerType: - root["_Type"] = "PointerType" - root["X"] = XMarshalASTExpr(d.X) - case *ast.BlockPointerType: - root["_Type"] = "BlockPointerType" - root["X"] = XMarshalASTExpr(d.X) - case *ast.ArrayType: - root["_Type"] = "ArrayType" - root["Elt"] = XMarshalASTExpr(d.Elt) - root["Len"] = XMarshalASTExpr(d.Len) - case *ast.BuiltinType: - root["_Type"] = "BuiltinType" - root["Kind"] = uint(d.Kind) - root["Flags"] = uint(d.Flags) - case *ast.Comment: - root["_Type"] = "Comment" - if d == nil { - return nil - } - root["Text"] = d.Text - case *ast.CommentGroup: - root["_Type"] = "CommentGroup" - if d == nil { - return nil - } - var list []map[string]any - for _, c := range d.List { - list = append(list, XMarshalASTExpr(c)) - } - root["List"] = list - case *ast.ScopingExpr: - root["_Type"] = "ScopingExpr" - root["X"] = XMarshalASTExpr(d.X) - root["Parent"] = XMarshalASTExpr(d.Parent) - default: - return nil - } - return root -} diff --git a/_xtool/internal/parser/parser.go b/_xtool/internal/parser/parser.go deleted file mode 100644 index 6617eda74..000000000 --- a/_xtool/internal/parser/parser.go +++ /dev/null @@ -1,1101 +0,0 @@ -package parser - -import ( - "fmt" - "os" - "path/filepath" - "runtime" - "strings" - "unsafe" - - "github.com/goplus/lib/c" - clangutils "github.com/goplus/llcppg/_xtool/internal/clang" - clang "github.com/goplus/llcppg/_xtool/internal/libclang" - "github.com/goplus/llcppg/ast" - "github.com/goplus/llcppg/token" -) - -type dbgFlags = int - -var debugParse bool - -const ( - DbgParse dbgFlags = 1 << iota - DbgFlagAll = DbgParse -) - -func SetDebug(dbgFlags dbgFlags) { - debugParse = (dbgFlags & DbgParse) != 0 -} - -type Converter struct { - file *ast.File - index *clang.Index - unit *clang.TranslationUnit - indent int // for verbose debug -} - -var tagMap = map[string]ast.Tag{ - "struct": ast.Struct, - "union": ast.Union, - "enum": ast.Enum, - "class": ast.Class, -} - -type ConverterConfig struct { - File string - Args []string - IsCpp bool -} - -func Do(config *ConverterConfig) (*ast.File, error) { - converter, err := NewConverter(config) - if err != nil { - return nil, err - } - return converter.Convert() -} - -func NewConverter(config *ConverterConfig) (*Converter, error) { - if debugParse { - fmt.Fprintln(os.Stderr, "NewConverter: config") - fmt.Fprintln(os.Stderr, "config.File", config.File) - } - - index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{ - File: config.File, - Temp: false, - Args: config.Args, - IsCpp: config.IsCpp, - Options: clang.DetailedPreprocessingRecord, - }) - if err != nil { - return nil, err - } - - return &Converter{ - index: index, - unit: unit, - file: &ast.File{}, - }, nil -} - -func (ct *Converter) Dispose() { - ct.logln("Dispose") - ct.index.Dispose() - ct.unit.Dispose() -} - -func (ct *Converter) GetTokens(cursor clang.Cursor) []*ast.Token { - ran := cursor.Extent() - var numTokens c.Uint - var tokens *clang.Token - ct.unit.Tokenize(ran, &tokens, &numTokens) - defer ct.unit.DisposeTokens(tokens, numTokens) - - tokensSlice := unsafe.Slice(tokens, int(numTokens)) - - result := make([]*ast.Token, 0, int(numTokens)) - for _, tok := range tokensSlice { - tokStr := ct.unit.Token(tok) - result = append(result, &ast.Token{ - Token: toToken(tok), - Lit: c.GoString(tokStr.CStr()), - }) - tokStr.Dispose() - } - return result -} - -func (ct *Converter) logBase() string { - return strings.Repeat(" ", ct.indent) -} - -func (ct *Converter) incIndent() { - ct.indent++ -} - -func (ct *Converter) decIndent() { - if ct.indent > 0 { - ct.indent-- - } -} - -func (ct *Converter) logf(format string, args ...interface{}) { - if debugParse { - fmt.Fprintf(os.Stderr, ct.logBase()+format, args...) - } -} -func (ct *Converter) logln(args ...interface{}) { - if debugParse { - if len(args) > 0 { - firstArg := fmt.Sprintf("%s%v", ct.logBase(), args[0]) - fmt.Fprintln(os.Stderr, append([]interface{}{firstArg}, args[1:]...)...) - } else { - fmt.Fprintln(os.Stderr, ct.logBase()) - } - } -} - -func (ct *Converter) InFile(cursor clang.Cursor) bool { - loc := cursor.Location() - filePath, _, _ := clangutils.GetPresumedLocation(loc) - ct.logf("GetCurFile: PresumedLocation %s cursor.Location() %s\n", filePath, clang.GoString(loc.File().FileName())) - if filePath == "" || filePath == "" { - //todo(zzy): For some built-in macros, there is no file. - ct.logln("GetCurFile: NO FILE") - return false - } - return true -} - -func (ct *Converter) CreateObject(cursor clang.Cursor, name *ast.Ident) ast.Object { - base := ast.Object{ - Loc: createLoc(cursor), - Parent: ct.BuildScopingExpr(cursor.SemanticParent()), - Name: name, - } - commentGroup, isDoc := ct.ParseCommentGroup(cursor) - if isDoc { - base.Doc = commentGroup - } - return base -} - -func createLoc(cursor clang.Cursor) *ast.Location { - filename, _, _ := clangutils.GetPresumedLocation(cursor.Location()) - return &ast.Location{ - File: filename, - } -} - -// extracts and parses comments associated with a given Clang cursor, -// distinguishing between documentation comments and line comments. -// -// The function determines whether a comment is a documentation comment or a line comment by -// comparing the range of the comment node with the range of the declaration node in the AST. -// -// Note: In cases where both documentation comments and line comments conceptually exist, -// only the line comment will be preserved. -func (ct *Converter) ParseCommentGroup(cursor clang.Cursor) (comentGroup *ast.CommentGroup, isDoc bool) { - rawComment := toStr(cursor.RawCommentText()) - commentGroup := &ast.CommentGroup{} - if rawComment != "" { - commentRange := cursor.CommentRange() - cursorRange := cursor.Extent() - isDoc := getOffset(commentRange.RangeStart()) < getOffset(cursorRange.RangeStart()) - commentGroup = ct.ParseComment(rawComment) - if len(commentGroup.List) > 0 { - return commentGroup, isDoc - } - } - return nil, false -} - -func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup { - return &ast.CommentGroup{ - List: []*ast.Comment{ - {Text: rawComment}, - }, - } -} - -// visit top decls (struct, class, function, enum & macro, include) -func (ct *Converter) visitTop(cursor, parent clang.Cursor) clang.ChildVisitResult { - ct.incIndent() - defer ct.decIndent() - - inFile := ct.InFile(cursor) - - name := toStr(cursor.String()) - ct.logf("visitTop: Cursor: %s\n", name) - - if !inFile { - return clang.ChildVisit_Continue - } - - switch cursor.Kind { - case clang.CursorInclusionDirective: - include, err := ct.ProcessInclude(cursor) - if err != nil { - ct.logln(err) - return clang.ChildVisit_Continue - } - ct.file.Includes = append(ct.file.Includes, include) - ct.logln("visitTop: ProcessInclude END ", include.Path) - case clang.CursorMacroDefinition: - macro := ct.ProcessMacro(cursor) - if cursor.IsMacroBuiltin() == 0 { - ct.file.Macros = append(ct.file.Macros, macro) - } - ct.logln("visitTop: ProcessMacro END ", macro.Name, "Tokens Length:", len(macro.Tokens)) - case clang.CursorEnumDecl: - enum := ct.ProcessEnumDecl(cursor) - ct.file.Decls = append(ct.file.Decls, enum) - ct.logf("visitTop: ProcessEnumDecl END") - if enum.Name != nil { - ct.logln(enum.Name.Name) - } else { - ct.logln("ANONY") - } - - case clang.CursorClassDecl: - classDecl := ct.ProcessClassDecl(cursor) - // todo(zzy):class need consider nested struct situation - ct.file.Decls = append(ct.file.Decls, classDecl) - // class havent anonymous situation - ct.logln("visitTop: ProcessClassDecl END", classDecl.Name.Name) - case clang.CursorStructDecl: - decls := ct.ProcessStructDecl(cursor) - ct.file.Decls = append(ct.file.Decls, decls...) - ct.logf("visitTop: ProcessStructDecl END") - case clang.CursorUnionDecl: - decls := ct.ProcessUnionDecl(cursor) - ct.file.Decls = append(ct.file.Decls, decls...) - ct.logf("visitTop: ProcessUnionDecl END") - case clang.CursorFunctionDecl, clang.CursorCXXMethod, clang.CursorConstructor, clang.CursorDestructor: - // Handle functions and class methods (including out-of-class method) - // Example: void MyClass::myMethod() { ... } out-of-class method - funcDecl := ct.ProcessFuncDecl(cursor) - ct.file.Decls = append(ct.file.Decls, funcDecl) - ct.logln("visitTop: ProcessFuncDecl END", funcDecl.Name.Name, funcDecl.MangledName, "isStatic:", funcDecl.IsStatic, "isInline:", funcDecl.IsInline) - case clang.CursorTypedefDecl: - typedefDecl := ct.ProcessTypeDefDecl(cursor) - if typedefDecl == nil { - return clang.ChildVisit_Continue - } - ct.file.Decls = append(ct.file.Decls, typedefDecl) - ct.logln("visitTop: ProcessTypeDefDecl END", typedefDecl.Name.Name) - case clang.CursorNamespace: - clangutils.VisitChildren(cursor, ct.visitTop) - } - return clang.ChildVisit_Continue -} - -// for flatten ast, keep type order -// input is clang -E 's result -func (ct *Converter) Convert() (*ast.File, error) { - cursor := ct.unit.Cursor() - clangutils.VisitChildren(cursor, ct.visitTop) - return ct.file, nil -} - -func (ct *Converter) ProcessType(t clang.Type) ast.Expr { - ct.incIndent() - defer ct.decIndent() - - typeName, typeKind := getTypeDesc(t) - ct.logln("ProcessType: TypeName:", typeName, "TypeKind:", typeKind) - - if t.Kind == clang.TypeUnexposed { - // https://github.com/goplus/llcppg/issues/497 - return ct.ProcessType(t.CanonicalType()) - } - - if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin { - return ct.ProcessBuiltinType(t) - } - - if t.Kind == clang.TypeElaborated { - return ct.ProcessElaboratedType(t) - } - - if t.Kind == clang.TypeTypedef { - return ct.ProcessTypeDefType(t) - } - - var expr ast.Expr - switch t.Kind { - case clang.TypePointer: - name, kind := getTypeDesc(t.PointeeType()) - ct.logln("ProcessType: PointerType Pointee TypeName:", name, "TypeKind:", kind) - expr = &ast.PointerType{X: ct.ProcessType(t.PointeeType())} - case clang.TypeBlockPointer: - name, kind := getTypeDesc(t) - ct.logln("ProcessType: BlockPointerType TypeName:", name, "TypeKind:", kind) - typ := ct.ProcessType(t.PointeeType()) - fnType, ok := typ.(*ast.FuncType) - if !ok { - panic("BlockPointerType: not FuncType") - } - expr = &ast.BlockPointerType{X: fnType} - case clang.TypeLValueReference: - name, kind := getTypeDesc(t.NonReferenceType()) - ct.logln("ProcessType: LvalueRefType NonReference TypeName:", name, "TypeKind:", kind) - expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())} - case clang.TypeRValueReference: - name, kind := getTypeDesc(t.NonReferenceType()) - ct.logln("ProcessType: RvalueRefType NonReference TypeName:", name, "TypeKind:", kind) - expr = &ast.RvalueRefType{X: ct.ProcessType(t.NonReferenceType())} - case clang.TypeFunctionProto, clang.TypeFunctionNoProto: - // treating TypeFunctionNoProto as a general function without parameters - // function type will only collect return type, params will be collected in ProcessFuncDecl - name, kind := getTypeDesc(t) - ct.logln("ProcessType: FunctionType TypeName:", name, "TypeKind:", kind) - expr = ct.ProcessFunctionType(t) - case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: - if t.Kind == clang.TypeConstantArray { - len := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20)) - c.Sprintf(len, c.Str("%lld"), t.ArraySize()) - defer c.Free(unsafe.Pointer(len)) - expr = &ast.ArrayType{ - Elt: ct.ProcessType(t.ArrayElementType()), - Len: &ast.BasicLit{Kind: ast.IntLit, Value: c.GoString(len)}, - } - } else if t.Kind == clang.TypeIncompleteArray { - // incomplete array havent len expr - expr = &ast.ArrayType{ - Elt: ct.ProcessType(t.ArrayElementType()), - } - } - default: - name, kind := getTypeDesc(t) - ct.logln("ProcessType: Unknown Type TypeName:", name, "TypeKind:", kind) - } - return expr -} - -// For function types, we can only obtain the parameter types, but not the parameter names. -// This is because we cannot reverse-lookup the corresponding declaration node from a function type. -// Note: For function declarations, parameter names are collected in the ProcessFuncDecl method. -func (ct *Converter) ProcessFunctionType(t clang.Type) *ast.FuncType { - ct.incIndent() - defer ct.decIndent() - typeName, typeKind := getTypeDesc(t) - ct.logln("ProcessFunctionType: TypeName:", typeName, "TypeKind:", typeKind) - // Note: Attempting to get the type declaration for a function type will result in CursorNoDeclFound - // cursor := t.TypeDeclaration() - // This would return CursorNoDeclFound - resType := t.ResultType() - - name, kind := getTypeDesc(resType) - ct.logln("ProcessFunctionType: ResultType TypeName:", name, "TypeKind:", kind) - - ret := ct.ProcessType(resType) - params := &ast.FieldList{} - numArgs := t.NumArgTypes() - for i := 0; i < int(numArgs); i++ { - argType := t.ArgType(c.Uint(i)) - params.List = append(params.List, &ast.Field{ - Type: ct.ProcessType(argType), - }) - } - if t.IsFunctionTypeVariadic() != 0 { - params.List = append(params.List, &ast.Field{ - Type: &ast.Variadic{}, - }) - } - - return &ast.FuncType{ - Ret: ret, - Params: params, - } -} - -func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { - ct.incIndent() - defer ct.decIndent() - name, kind := getCursorDesc(cursor) - ct.logln("ProcessTypeDefDecl: CursorName:", name, "CursorKind:", kind, "CursorTypeKind:", toStr(cursor.Type().Kind.String())) - - typ := ct.ProcessUnderlyingType(cursor) - // For cases like: typedef struct { int x; } Name; - // libclang incorrectly reports the anonymous structure as a named structure - // with the same name as the typedef. Since the anonymous structure definition - // has already been collected when processing its declaration cursor, - // we skip this redundant typedef declaration by returning nil. - if typ == nil { - return nil - } - - decl := &ast.TypedefDecl{ - Object: ct.CreateObject(cursor, &ast.Ident{Name: name}), - Type: typ, - } - return decl -} - -func (ct *Converter) ProcessUnderlyingType(cursor clang.Cursor) ast.Expr { - underlyingTyp := cursor.TypedefDeclUnderlyingType() - - if underlyingTyp.Kind != clang.TypeElaborated { - ct.logln("ProcessUnderlyingType: not elaborated") - return ct.ProcessType(underlyingTyp) - } - - defName := toStr(cursor.String()) - // Using getActualTypeCursor to recursively find the actual declaration of the underlying type, - // handles cases with multi-level typedef chains - underName := toStr(ct.getActualTypeCursor(underlyingTyp.TypeDeclaration()).String()) - ct.logln("ProcessUnderlyingType: defName:", defName, "underName:", underName) - - // For a typedef like "typedef struct xxx xxx;", the underlying type declaration - // can appear in two locations: - // 1. Inside the typedef itself when the struct is defined inline - // 2. At the implementation location when there's a separate struct xxx definition - // in the source file - // Therefore, we shouldn't use declaration location to determine whether to remove - // extra typedef nodes - // - // Note: This handles both direct self-references (e.g., typedef struct Foo Foo;) and - // multi-level typedef chains that refer back to the original declaration (e.g., typedef enum algorithm {...} algorithm_t; typedef algorithm_t algorithm;) - if defName == underName { - ct.logln("ProcessUnderlyingType: is self reference") - return nil - } - - return ct.ProcessElaboratedType(underlyingTyp) -} - -// getActualType gets the actual type by handling only the outer Elaborated and Typedef types. -// Note: We don't use CanonicalType() because it recursively resolves all types, including parameter types -// in function signatures, which would cause typedef types within function signatures to be desugared. -// For example: -// -// typedef struct OSSL_CORE_HANDLE OSSL_CORE_HANDLE; -// typedef struct OSSL_DISPATCH OSSL_DISPATCH; -// typedef int (OSSL_provider_init_fn)(const OSSL_CORE_HANDLE *handle, -// const OSSL_DISPATCH *in, -// const OSSL_DISPATCH **out, -// void **provctx); -// OSSL_provider_init_fn OSSL_provider_init; -// -// Using CanonicalType() would desugar OSSL_CORE_HANDLE and OSSL_DISPATCH in the function signature -// to their underlying struct types, which is not what we want. -func (ct *Converter) getActualType(t clang.Type) clang.Type { - ct.incIndent() - defer ct.decIndent() - typName, typKind := getTypeDesc(t) - ct.logln("getActualType: TypeName:", typName, "TypeKind:", typKind) - switch t.Kind { - case clang.TypeElaborated: - ct.logln("getActualType: TypeElaborated") - return ct.getActualType(t.NamedType()) - case clang.TypeTypedef: - ct.logln("getActualType: TypeTypedef") - return ct.getActualType(t.TypeDeclaration().TypedefDeclUnderlyingType()) - default: - return t - } -} - -// getActualTypeCursor recursively gets the actual underlying cursor of a type declaration. -// For multi-level nested typedef chains, it continues recursion until finding the original non-typedef type. -// Example: -// - typedef enum Foo {...} Foo_t; -// - typedef Foo_t Foo; -// When processing Foo, it recursively finds the declaration cursor of enum Foo -func (ct *Converter) getActualTypeCursor(cursor clang.Cursor) clang.Cursor { - ct.incIndent() - defer ct.decIndent() - typName, typKind := getCursorDesc(cursor) - ct.logln("getActualTypeCursor: TypeName:", typName, "TypeKind:", typKind) - switch cursor.Kind { - case clang.CursorTypedefDecl: - return ct.getActualTypeCursor(cursor.TypedefDeclUnderlyingType().TypeDeclaration()) - default: - return cursor - } -} - -// converts functions, methods, constructors, destructors (including out-of-class decl) to ast.FuncDecl nodes. -func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { - ct.incIndent() - defer ct.decIndent() - name, kind := getCursorDesc(cursor) - mangledName := toStr(cursor.Mangling()) - ct.logln("ProcessFuncDecl: CursorName:", name, "CursorKind:", kind, "mangledName:", mangledName) - - // function type will only collect return type - // ProcessType can't get the field names, will collect in follows - fnType := cursor.Type() - typName, typKind := getTypeDesc(fnType) - ct.logln("ProcessFuncDecl: TypeName:", typName, "TypeKind:", typKind) - - typeToProcess := fnType - if fnType.Kind == clang.TypeElaborated { - typeToProcess = ct.getActualType(fnType) - actualTypeName, actualTypeKind := getTypeDesc(typeToProcess) - ct.logln("ProcessFuncDecl: ActualType TypeName:", actualTypeName, "TypeKind:", actualTypeKind) - } - funcType, ok := ct.ProcessType(typeToProcess).(*ast.FuncType) - if !ok { - ct.logln("ProcessFuncDecl: failed to process function type") - return nil - } - ct.logln("ProcessFuncDecl: ProcessFieldList") - - // For function type references (e.g. `typedef void (fntype)(); fntype foo;`), - // params are already processed in ProcessType via CanonicalType - if fnType.Kind != clang.TypeElaborated { - numArgs := cursor.NumArguments() - numFields := c.Int(len(funcType.Params.List)) - for i := c.Int(0); i < numArgs; i++ { - arg := cursor.Argument(c.Uint(i)) - name := clang.GoString(arg.DisplayName()) - if len(name) > 0 && i < numFields { - field := funcType.Params.List[i] - field.Names = []*ast.Ident{&ast.Ident{Name: name}} - } - } - } - - // Linux has one less leading underscore than macOS, so remove one leading underscore on macOS - if runtime.GOOS == "darwin" { - mangledName = strings.TrimPrefix(mangledName, "_") - } - - funcDecl := &ast.FuncDecl{ - Object: ct.CreateObject(cursor, &ast.Ident{Name: name}), - Type: funcType, - MangledName: mangledName, - } - - if cursor.IsFunctionInlined() != 0 { - funcDecl.IsInline = true - } - - if isMethod(cursor) { - ct.logln("ProcessFuncDecl: is method, ProcessMethodAttributes") - ct.ProcessMethodAttributes(cursor, funcDecl) - } else { - if cursor.StorageClass() == clang.SCStatic { - funcDecl.IsStatic = true - } - } - - return funcDecl -} - -// get Methods Attributes -func (ct *Converter) ProcessMethodAttributes(cursor clang.Cursor, fn *ast.FuncDecl) { - if parent := cursor.SemanticParent(); parent.Equal(cursor.LexicalParent()) != 1 { - fn.Parent = ct.BuildScopingExpr(cursor.SemanticParent()) - } - - switch cursor.Kind { - case clang.CursorDestructor: - fn.IsDestructor = true - case clang.CursorConstructor: - fn.IsConstructor = true - if cursor.IsExplicit() != 0 { - fn.IsExplicit = true - } - } - - if cursor.IsStatic() != 0 { - fn.IsStatic = true - } - if cursor.IsVirtual() != 0 || cursor.IsPureVirtual() != 0 { - fn.IsVirtual = true - } - if cursor.IsConst() != 0 { - fn.IsConst = true - } - - var numOverridden c.Uint - var overridden *clang.Cursor - cursor.OverriddenCursors(&overridden, &numOverridden) - if numOverridden > 0 { - fn.IsOverride = true - } - overridden.DisposeOverriddenCursors() -} - -func (ct *Converter) ProcessEnumType(cursor clang.Cursor) *ast.EnumType { - items := make([]*ast.EnumItem, 0) - - clangutils.VisitChildren(cursor, func(cursor, parent clang.Cursor) clang.ChildVisitResult { - if cursor.Kind == clang.CursorEnumConstantDecl { - name := cursor.String() - defer name.Dispose() - - val := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20)) - c.Sprintf(val, c.Str("%lld"), cursor.EnumConstantDeclValue()) - defer c.Free(unsafe.Pointer(val)) - - enum := &ast.EnumItem{ - Name: &ast.Ident{Name: c.GoString(name.CStr())}, - Value: &ast.BasicLit{ - Kind: ast.IntLit, - Value: c.GoString(val), - }, - } - items = append(items, enum) - } - return clang.ChildVisit_Continue - }) - - return &ast.EnumType{ - Items: items, - } -} - -func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl { - cursorName, cursorKind := getCursorDesc(cursor) - ct.logln("ProcessEnumDecl: CursorName:", cursorName, "CursorKind:", cursorKind) - - decl := &ast.EnumTypeDecl{ - Object: ct.CreateObject(cursor, nil), - Type: ct.ProcessEnumType(cursor), - } - - anony := cursor.IsAnonymous() - if anony == 0 { - decl.Name = &ast.Ident{Name: cursorName} - ct.logln("ProcessEnumDecl: has name", cursorName) - } else { - ct.logln("ProcessEnumDecl: is anonymous") - } - - return decl -} - -// current only collect macro which defined in file -func (ct *Converter) ProcessMacro(cursor clang.Cursor) *ast.Macro { - macro := &ast.Macro{ - Loc: createLoc(cursor), - Name: clang.GoString(cursor.String()), - Tokens: ct.GetTokens(cursor), - } - return macro -} - -func (ct *Converter) ProcessInclude(cursor clang.Cursor) (*ast.Include, error) { - name := toStr(cursor.String()) - includedFile := cursor.IncludedFile() - includedPath := toStr(includedFile.FileName()) - if includedPath == "" { - return nil, fmt.Errorf("%s: failed to get included file", name) - } - return &ast.Include{Path: filepath.Clean(includedPath)}, nil -} - -func (ct *Converter) createBaseField(cursor clang.Cursor) *ast.Field { - ct.incIndent() - defer ct.decIndent() - - fieldName := toStr(cursor.String()) - - typ := cursor.Type() - - typeName, typeKind := getTypeDesc(typ) - - ct.logf("createBaseField: ProcessType %s TypeKind: %s", typeName, typeKind) - - field := &ast.Field{ - Type: ct.ProcessType(typ), - } - - commentGroup, isDoc := ct.ParseCommentGroup(cursor) - if commentGroup != nil { - if isDoc { - field.Doc = commentGroup - } else { - field.Comment = commentGroup - } - } - // NOTE(MeteorsLiu): In non C++ mode, an anonymous field name may be `unname struct` instead of empty string - // so check it via IsAnonymous() - if cursor.IsAnonymous() == 0 { - field.Names = []*ast.Ident{{Name: fieldName}} - } - return field -} - -// For Record Type(struct, union ...)'s FieldList -func (ct *Converter) ProcessFieldList(cursor clang.Cursor) *ast.FieldList { - ct.incIndent() - defer ct.decIndent() - flds := &ast.FieldList{} - ct.logln("ProcessFieldList: VisitChildren") - clangutils.VisitChildren(cursor, func(subcsr, parent clang.Cursor) clang.ChildVisitResult { - switch subcsr.Kind { - case clang.CursorFieldDecl: - // In C language, parameter lists do not have similar parameter grouping in Go. - // func foo(a, b int) - - // For follows struct, it will also parse to two FieldDecl - // struct A { - // int a, b; - // }; - ct.logln("ProcessFieldList: CursorFieldDecl") - field := ct.createBaseField(subcsr) - field.Access = ast.AccessSpecifier(subcsr.CXXAccessSpecifier()) - flds.List = append(flds.List, field) - case clang.CursorVarDecl: - if subcsr.StorageClass() == clang.SCStatic { - // static member variable - field := ct.createBaseField(subcsr) - field.Access = ast.AccessSpecifier(subcsr.CXXAccessSpecifier()) - field.IsStatic = true - flds.List = append(flds.List, field) - } - } - return clang.ChildVisit_Continue - }) - return flds -} - -// Note:Public Method is considered -func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { - methods := make([]*ast.FuncDecl, 0) - clangutils.VisitChildren(cursor, func(subcsr, parent clang.Cursor) clang.ChildVisitResult { - if isMethod(subcsr) && subcsr.CXXAccessSpecifier() == clang.CXXPublic { - method := ct.ProcessFuncDecl(subcsr) - if method != nil { - methods = append(methods, method) - } - } - return clang.ChildVisit_Continue - }) - return methods -} - -func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor) []ast.Decl { - var decls []ast.Decl - ct.incIndent() - defer ct.decIndent() - cursorName, cursorKind := getCursorDesc(cursor) - ct.logln("ProcessRecordDecl: CursorName:", cursorName, "CursorKind:", cursorKind) - - childs := PostOrderVisitChildren(cursor, func(child, parent clang.Cursor) bool { - // if we found a nested enum, handle it like nested struct - if child.Kind == clang.CursorEnumDecl { - return true - } - return (child.Kind == clang.CursorStructDecl || child.Kind == clang.CursorUnionDecl) && child.IsAnonymous() == 0 - }) - - for _, child := range childs { - switch child.Kind { - case clang.CursorStructDecl, clang.CursorUnionDecl: - // note(zzy):use len(typ.Fields.List) to ensure it has fields not a forward declaration - // but maybe make the forward decl in to AST is also good. - childName := clang.GoString(child.String()) - ct.logln("ProcessRecordDecl: Found named nested struct:", childName) - // Check if this is a named nested struct/union - typ := ct.ProcessRecordType(child) - // note(zzy):use len(typ.Fields.List) to ensure it has fields not a forward declaration - // but maybe make the forward decl in to AST is also good. - if child.IsAnonymous() == 0 && typ.Fields != nil { - decls = append(decls, &ast.TypeDecl{ - Object: ct.CreateObject(child, &ast.Ident{Name: childName}), - Type: ct.ProcessRecordType(child), - }) - } - case clang.CursorEnumDecl: - childName := clang.GoString(child.String()) - - ct.logln("ProcessRecordDecl: Found named nested enum:", childName) - - ct.incIndent() - decls = append(decls, ct.ProcessEnumDecl(child)) - ct.decIndent() - } - } - ct.logln("ProcessRecordDecl: process record: ", cursorName) - - decl := &ast.TypeDecl{ - Object: ct.CreateObject(cursor, nil), - Type: ct.ProcessRecordType(cursor), - } - - // NOTE(MeteorsLiu): IsAnonymousRecordDecl may return fake results when we're in non Cpp mode - // to avoid that case, we have to check the IsAnonymous result - isAnonymousRecord := cursor.IsAnonymousRecordDecl() > 0 || cursor.IsAnonymous() > 0 - - if !isAnonymousRecord { - decl.Name = &ast.Ident{Name: cursorName} - ct.logln("ProcessRecordDecl: has name", cursorName) - } else { - ct.logln("ProcessRecordDecl: is anonymous") - } - - decls = append(decls, decl) - return decls -} - -func (ct *Converter) ProcessStructDecl(cursor clang.Cursor) []ast.Decl { - return ct.ProcessRecordDecl(cursor) -} - -func (ct *Converter) ProcessUnionDecl(cursor clang.Cursor) []ast.Decl { - return ct.ProcessRecordDecl(cursor) -} - -func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl { - cursorName, cursorKind := getCursorDesc(cursor) - ct.logln("ProcessClassDecl: CursorName:", cursorName, "CursorKind:", cursorKind) - - // Pushing class scope before processing its type and popping after - base := ct.CreateObject(cursor, &ast.Ident{Name: cursorName}) - typ := ct.ProcessRecordType(cursor) - - decl := &ast.TypeDecl{ - Object: base, - Type: typ, - } - - return decl -} - -func (ct *Converter) ProcessRecordType(cursor clang.Cursor) *ast.RecordType { - ct.incIndent() - defer ct.decIndent() - - typ := &ast.RecordType{} - - cursorName, cursorKind := getCursorDesc(cursor) - ct.logln("ProcessRecordType: CursorName:", cursorName, "CursorKind:", cursorKind) - - typ.Tag = toTag(cursor.Kind) - ct.logln("ProcessRecordType: toTag", typ.Tag) - - if cursor.IsCursorDefinition() == 0 { - ct.logln("ProcessRecordType: forward declaration, no definition") - return typ - } - - ct.logln("ProcessRecordType: ProcessFieldList") - typ.Fields = ct.ProcessFieldList(cursor) - - ct.logln("ProcessRecordType: ProcessMethods") - typ.Methods = ct.ProcessMethods(cursor) - - return typ -} - -// process ElaboratedType Reference -// -// 1. Named elaborated type references: -// - Examples: struct MyStruct, union MyUnion, class MyClass, enum MyEnum -// - Handling: Constructed as TagExpr or ScopingExpr references -// -// 2. Anonymous elaborated type references: -// - Examples: struct { int x; int y; }, union { int a; float b; } -// - Handling: Retrieve their corresponding concrete types -func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { - ct.incIndent() - defer ct.decIndent() - typeName, typeKind := getTypeDesc(t) - ct.logln("ProcessElaboratedType: TypeName:", typeName, "TypeKind:", typeKind) - - decl := t.TypeDeclaration() - isAnonymousDecl := decl.IsAnonymous() > 0 - - if isAnonymousDecl && decl.Kind != clang.CursorEnumDecl { - return ct.ProcessRecordType(decl) - } - parts := clangutils.BuildScopingParts(decl) - hasParent := clangutils.HasParent(decl) - // NOTE(MeteorsLiu): nested enum behaves different from nested struct, for example, we can find its semantic parent - // however, it will cause we misidentified it as a class method expr, so take it out - if isAnonymousDecl && decl.Kind == clang.CursorEnumDecl { - // case 1: anonymous enum, but not nested (anonymous enum decl variable case) - if !hasParent { - // this is not a nested enum, handle it normally - return ct.ProcessEnumType(decl) - } - // case 2: anonymous enum, nested (normal nested struct reference) - // by default, the type of an anonymous enum is int - // NOTE(MeteorsLiu): see disscussion https://github.com/goplus/llcppg/pull/530 - return &ast.BuiltinType{Kind: ast.Int} - - // case 3: named enum, nested, fallback to process as a ElaboratedType (nornaml nested struct) - // case 4: named enum, non-nested, fallback to process as a ElaboratedType normally. (typedef enum case) - } - - // for elaborated type, it could have a tag description - // like struct A, union B, class C, enum D - typeParts := strings.SplitN(typeName, " ", 2) - - if len(typeParts) == 2 { - if tagValue, ok := tagMap[typeParts[0]]; ok { - return &ast.TagExpr{ - Tag: tagValue, - Name: buildScopingFromParts(parts), - } - } - } - - return buildScopingFromParts(parts) -} - -func (ct *Converter) ProcessTypeDefType(t clang.Type) ast.Expr { - cursor := t.TypeDeclaration() - ct.logln("ProcessTypeDefType: Typedef TypeDeclaration", toStr(cursor.String()), toStr(t.String())) - if name := toStr(cursor.String()); name != "" { - return &ast.Ident{Name: name} - } - ct.logln("ProcessTypeDefType: typedef type have no name") - return nil -} - -func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { - ct.incIndent() - defer ct.decIndent() - typeName, typeKind := getTypeDesc(t) - ct.logln("ProcessBuiltinType: TypeName:", typeName, "TypeKind:", typeKind) - - kind := ast.Void - var flags ast.TypeFlag - - switch t.Kind { - case clang.TypeVoid: - kind = ast.Void - case clang.TypeBool: - kind = ast.Bool - case clang.TypeCharU, clang.TypeUChar, clang.TypeCharS, clang.TypeSChar: - kind = ast.Char - case clang.TypeChar16: - kind = ast.Char16 - case clang.TypeChar32: - kind = ast.Char32 - case clang.TypeWChar: - kind = ast.WChar - case clang.TypeShort, clang.TypeUShort: - kind = ast.Int - flags |= ast.Short - case clang.TypeInt, clang.TypeUInt: - kind = ast.Int - case clang.TypeLong, clang.TypeULong: - kind = ast.Int - flags |= ast.Long - case clang.TypeLongLong, clang.TypeULongLong: - kind = ast.Int - flags |= ast.LongLong - case clang.TypeInt128, clang.TypeUInt128: - kind = ast.Int128 - case clang.TypeFloat: - kind = ast.Float - case clang.TypeHalf, clang.TypeFloat16: - kind = ast.Float16 - case clang.TypeDouble: - kind = ast.Float - flags |= ast.Double - case clang.TypeLongDouble: - kind = ast.Float - flags |= ast.Long | ast.Double - case clang.TypeFloat128: - kind = ast.Float128 - case clang.TypeComplex: - kind = ast.Complex - complexKind := t.ElementType().Kind - if complexKind == clang.TypeLongDouble { - flags |= ast.Long | ast.Double - } else if complexKind == clang.TypeDouble { - flags |= ast.Double - } - // float complfex flag is not set - default: - // like IBM128,NullPtr,Accum - kindStr := toStr(t.Kind.String()) - fmt.Fprintln(os.Stderr, "todo: unknown builtin type:", kindStr) - } - - if IsExplicitSigned(t) { - flags |= ast.Signed - } else if IsExplicitUnsigned(t) { - flags |= ast.Unsigned - } - - return &ast.BuiltinType{ - Kind: kind, - Flags: flags, - } -} - -// Constructs a complete scoping expression by traversing the semantic parents, starting from the given clang.Cursor -// For anonymous decl of typedef references, use their anonymous name -func (ct *Converter) BuildScopingExpr(cursor clang.Cursor) ast.Expr { - parts := clangutils.BuildScopingParts(cursor) - return buildScopingFromParts(parts) -} - -func PostOrderVisitChildren(cursor clang.Cursor, collect func(c, p clang.Cursor) bool) []clang.Cursor { - var children []clang.Cursor - clangutils.VisitChildren(cursor, func(child, parent clang.Cursor) clang.ChildVisitResult { - if collect(child, parent) { - childs := PostOrderVisitChildren(child, collect) - children = append(children, childs[:]...) - children = append(children, child) - } - return clang.ChildVisit_Continue - }) - return children -} - -func IsExplicitSigned(t clang.Type) bool { - return t.Kind == clang.TypeCharS || t.Kind == clang.TypeSChar -} - -func IsExplicitUnsigned(t clang.Type) bool { - return t.Kind == clang.TypeCharU || t.Kind == clang.TypeUChar || - t.Kind == clang.TypeUShort || t.Kind == clang.TypeUInt || - t.Kind == clang.TypeULong || t.Kind == clang.TypeULongLong || - t.Kind == clang.TypeUInt128 -} - -func toTag(kind clang.CursorKind) ast.Tag { - switch kind { - case clang.CursorStructDecl: - return ast.Struct - case clang.CursorUnionDecl: - return ast.Union - case clang.CursorClassDecl: - return ast.Class - default: - panic(fmt.Sprintf("Unexpected cursor kind in toTag: %v", kind)) - } -} - -func toToken(tok clang.Token) token.Token { - if tok.Kind() < clang.Punctuation || tok.Kind() > clang.Comment { - return token.ILLEGAL - } else { - return token.Token(tok.Kind() + 1) - } -} -func isMethod(cursor clang.Cursor) bool { - return cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor -} - -func buildScopingFromParts(parts []string) ast.Expr { - if len(parts) == 0 { - return nil - } - var expr ast.Expr = &ast.Ident{Name: parts[0]} - for _, part := range parts[1:] { - expr = &ast.ScopingExpr{ - Parent: expr, - X: &ast.Ident{Name: part}, - } - } - return expr -} - -func getOffset(location clang.SourceLocation) c.Uint { - _, _, _, offset := clangutils.GetLocation(location) - return offset -} - -func toStr(clangStr clang.String) (str string) { - defer clangStr.Dispose() - if clangStr.CStr() != nil { - str = c.GoString(clangStr.CStr()) - } - return -} - -func getTypeDesc(t clang.Type) (name string, kind string) { - name = toStr(t.String()) - kind = toStr(t.Kind.String()) - return -} - -func getCursorDesc(cursor clang.Cursor) (name string, kind string) { - name = toStr(cursor.String()) - kind = toStr(cursor.Kind.String()) - return -} diff --git a/_xtool/internal/parser/parser_test.go b/_xtool/internal/parser/parser_test.go deleted file mode 100644 index 5cfc3ef98..000000000 --- a/_xtool/internal/parser/parser_test.go +++ /dev/null @@ -1,705 +0,0 @@ -package parser_test - -import ( - "encoding/json" - "fmt" - "os" - "path" - "path/filepath" - "reflect" - "strings" - "testing" - - "github.com/goplus/lib/c" - clangutils "github.com/goplus/llcppg/_xtool/internal/clang" - "github.com/goplus/llcppg/_xtool/internal/clangtool" - clang "github.com/goplus/llcppg/_xtool/internal/libclang" - "github.com/goplus/llcppg/_xtool/internal/parser" - "github.com/goplus/llcppg/ast" - "github.com/goplus/llgo/xtool/clang/preprocessor" -) - -func TestParserCppMode(t *testing.T) { - cases := []string{"class", "comment", "enum", "func", "scope", "struct", "typedef", "union", "macro", "forwarddecl1", "forwarddecl2", "include", "typeof", "forward_vs_empty", "nestedenum_cpp"} - // https://github.com/goplus/llgo/issues/1114 - // todo(zzy):use os.ReadDir - for _, folder := range cases { - t.Run(folder, func(t *testing.T) { - testFrom(t, filepath.Join("testdata", folder), "temp.h", true, false) - }) - } -} - -func TestParserCMode(t *testing.T) { - cases := []string{"enum", "struct", "union", "macro", "include", "typeof", "named_nested_struct", "forward_vs_empty", "nestedenum"} - for _, folder := range cases { - t.Run(folder, func(t *testing.T) { - testFrom(t, filepath.Join("testdata", folder), "temp.h", false, false) - }) - } -} - -func testFrom(t *testing.T, dir string, filename string, isCpp, gen bool) { - var expect string - var err error - if !gen { - json, err := os.ReadFile(filepath.Join(dir, "expect.json")) - if err != nil { - t.Fatal("ReadExpectFile failed:", err) - } - expect = string(json) - } - ast, err := parser.Do(&parser.ConverterConfig{ - File: filepath.Join(dir, filename), - IsCpp: isCpp, - Args: []string{"-fparse-all-comments"}, - }) - if err != nil { - t.Fatal("Do failed:", err) - } - // https://github.com/goplus/llgo/issues/1116 - // astJson, err := json.MarshalIndent(ast, "", " ") - // todo(zzy):use json.Marshal - if err != nil { - t.Fatal("MarshalIndent failed:", err) - } - js := parser.XMarshalASTFile(ast) - output, _ := json.MarshalIndent(&js, "", " ") - - if gen { - err = os.WriteFile(filepath.Join(dir, "expect.json"), output, os.ModePerm) - if err != nil { - t.Fatal("WriteFile failed:", err) - } - } else if expect != string(output) { - t.Fatalf("expect %s, got %s", expect, string(output)) - } -} - -func TestNonBuiltinTypes(t *testing.T) { - tests := []struct { - TypeCode string - ExpectTypeStr string - expr ast.Expr - }{ - { - TypeCode: "int*", - ExpectTypeStr: "int *", - expr: &ast.PointerType{ - X: &ast.BuiltinType{ - Kind: ast.Int, - }, - }, - }, - { - TypeCode: "int***", - ExpectTypeStr: "int ***", - expr: &ast.PointerType{ - X: &ast.PointerType{ - X: &ast.PointerType{ - X: &ast.BuiltinType{Kind: ast.Int}, - }, - }, - }, - }, - { - TypeCode: "int[]", - ExpectTypeStr: "int[]", - expr: &ast.ArrayType{ - Elt: &ast.BuiltinType{Kind: ast.Int}, - }, - }, - { - TypeCode: "int[10]", - ExpectTypeStr: "int[10]", - expr: &ast.ArrayType{ - Elt: &ast.BuiltinType{Kind: ast.Int}, - Len: &ast.BasicLit{ - Kind: ast.IntLit, - Value: "10", - }, - }, - }, - { - TypeCode: "int[3][4]", - ExpectTypeStr: "int[3][4]", - expr: &ast.ArrayType{ - Elt: &ast.ArrayType{ - Elt: &ast.BuiltinType{Kind: ast.Int}, - Len: &ast.BasicLit{ - Kind: ast.IntLit, - Value: "4", - }, - }, - Len: &ast.BasicLit{ - Kind: ast.IntLit, - Value: "3", - }, - }, - }, - { - TypeCode: "int&", - ExpectTypeStr: "int &", - expr: &ast.LvalueRefType{ - X: &ast.BuiltinType{Kind: ast.Int}, - }, - }, - { - TypeCode: "int&&", - ExpectTypeStr: "int &&", - expr: &ast.RvalueRefType{ - X: &ast.BuiltinType{Kind: ast.Int}, - }, - }, - { - TypeCode: `struct Foo {}; - Foo`, - ExpectTypeStr: "Foo", - expr: &ast.Ident{ - Name: "Foo", - }, - }, - { - TypeCode: `struct Foo {}; - struct Foo`, - ExpectTypeStr: "struct Foo", - expr: &ast.TagExpr{ - Tag: ast.Struct, - Name: &ast.Ident{ - Name: "Foo", - }, - }, - }, - { - TypeCode: `struct { - int x; - }`, - ExpectTypeStr: "struct (unnamed struct at temp.h:1:1)", - expr: &ast.RecordType{ - Tag: ast.Struct, - Fields: &ast.FieldList{ - List: []*ast.Field{ - { - Names: []*ast.Ident{ - {Name: "x"}, - }, - Type: &ast.BuiltinType{Kind: ast.Int}, - Access: ast.Public, - }, - }, - }, - Methods: []*ast.FuncDecl{}, - }, - }, - { - TypeCode: `union Foo {}; - Foo`, - ExpectTypeStr: "Foo", - expr: &ast.Ident{ - Name: "Foo", - }, - }, - { - TypeCode: `union Foo {}; - union Foo`, - ExpectTypeStr: "union Foo", - expr: &ast.TagExpr{ - Tag: ast.Union, - Name: &ast.Ident{ - Name: "Foo", - }, - }, - }, - { - TypeCode: `union { - int x; - }`, - ExpectTypeStr: "union (unnamed union at temp.h:1:1)", - expr: &ast.RecordType{ - Tag: ast.Union, - Fields: &ast.FieldList{ - List: []*ast.Field{ - { - Names: []*ast.Ident{ - {Name: "x"}, - }, - Access: ast.Public, - Type: &ast.BuiltinType{Kind: ast.Int}, - }, - }, - }, - Methods: []*ast.FuncDecl{}, - }, - }, - { - TypeCode: `enum Foo {}; - Foo`, - ExpectTypeStr: "Foo", - expr: &ast.Ident{ - Name: "Foo", - }, - }, - { - TypeCode: `enum Foo {}; - enum Foo`, - ExpectTypeStr: "enum Foo", - expr: &ast.TagExpr{ - Tag: ast.Enum, - Name: &ast.Ident{ - Name: "Foo", - }, - }, - }, - { - TypeCode: `struct Foo { enum Bar {} k; }; - enum Bar`, - ExpectTypeStr: "enum Bar", - expr: &ast.TagExpr{ - Tag: ast.Enum, - Name: &ast.Ident{Name: "Bar"}, - }, - }, - { - TypeCode: `enum { x = 42 }`, - ExpectTypeStr: "enum (unnamed enum at temp.h:1:1)", - expr: &ast.EnumType{ - Items: []*ast.EnumItem{ - { - Name: &ast.Ident{ - Name: "x", - }, - Value: &ast.BasicLit{ - Kind: ast.IntLit, - Value: "42", - }, - }, - }, - }, - }, - { - TypeCode: `class Foo {}; - Foo`, - ExpectTypeStr: "Foo", - expr: &ast.Ident{ - Name: "Foo", - }, - }, - { - TypeCode: `class Foo {}; - class Foo`, - ExpectTypeStr: "class Foo", - expr: &ast.TagExpr{ - Tag: ast.Class, - Name: &ast.Ident{ - Name: "Foo", - }, - }, - }, - { - TypeCode: `class { - int x; - }`, - ExpectTypeStr: "class (unnamed class at temp.h:1:1)", - expr: &ast.RecordType{ - Tag: ast.Class, - Fields: &ast.FieldList{ - List: []*ast.Field{ - { - Names: []*ast.Ident{ - {Name: "x"}, - }, - Access: ast.Private, - Type: &ast.BuiltinType{Kind: ast.Int}, - }, - }, - }, - Methods: []*ast.FuncDecl{}, - }, - }, - { - TypeCode: `namespace a { - namespace b { - class c { - }; - } - } - a::b::c`, - ExpectTypeStr: "a::b::c", - expr: &ast.ScopingExpr{ - Parent: &ast.ScopingExpr{ - Parent: &ast.Ident{ - Name: "a", - }, - X: &ast.Ident{ - Name: "b", - }, - }, - X: &ast.Ident{ - Name: "c", - }, - }, - }, - { - TypeCode: `namespace a { - namespace b { - class c { - }; - } - } - class a::b::c`, - ExpectTypeStr: "class a::b::c", - expr: &ast.TagExpr{ - Tag: ast.Class, - Name: &ast.ScopingExpr{ - Parent: &ast.ScopingExpr{ - Parent: &ast.Ident{ - Name: "a", - }, - X: &ast.Ident{ - Name: "b", - }, - }, - X: &ast.Ident{ - Name: "c", - }, - }, - }, - }, - { - TypeCode: `int (*p)(int, int);`, - ExpectTypeStr: "int (*)(int, int)", - expr: &ast.PointerType{ - X: &ast.FuncType{ - Params: &ast.FieldList{ - List: []*ast.Field{ - { - Type: &ast.BuiltinType{Kind: ast.Int}, - }, - { - Type: &ast.BuiltinType{Kind: ast.Int}, - }, - }, - }, - Ret: &ast.BuiltinType{Kind: ast.Int}, - }, - }, - }, - } - - for _, tc := range tests { - t.Run(tc.ExpectTypeStr, func(t *testing.T) { - typ, index, unit := GetType(&GetTypeOptions{ - TypeCode: tc.TypeCode, - IsCpp: true, - }) - converter := &parser.Converter{} - expr := converter.ProcessType(typ) - typstr := typ.String() - if typGoStr := c.GoString(typstr.CStr()); typGoStr != tc.ExpectTypeStr { - t.Fatalf("expect %s , got %s", tc.ExpectTypeStr, typGoStr) - } - if !reflect.DeepEqual(expr, tc.expr) { - t.Fatalf("%s expect %#v, got %#v", tc.ExpectTypeStr, tc.expr, expr) - } - - typstr.Dispose() - - index.Dispose() - unit.Dispose() - }) - } -} - -func TestBuiltinType(t *testing.T) { - tests := []struct { - name string - typ clang.Type - expected ast.BuiltinType - }{ - {"Void", btType(clang.TypeVoid), ast.BuiltinType{Kind: ast.Void}}, - {"Bool", btType(clang.TypeBool), ast.BuiltinType{Kind: ast.Bool}}, - {"Char_S", btType(clang.TypeCharS), ast.BuiltinType{Kind: ast.Char, Flags: ast.Signed}}, - {"Char_U", btType(clang.TypeCharU), ast.BuiltinType{Kind: ast.Char, Flags: ast.Unsigned}}, - {"Char16", btType(clang.TypeChar16), ast.BuiltinType{Kind: ast.Char16}}, - {"Char32", btType(clang.TypeChar32), ast.BuiltinType{Kind: ast.Char32}}, - {"WChar", btType(clang.TypeWChar), ast.BuiltinType{Kind: ast.WChar}}, - {"Short", btType(clang.TypeShort), ast.BuiltinType{Kind: ast.Int, Flags: ast.Short}}, - {"UShort", btType(clang.TypeUShort), ast.BuiltinType{Kind: ast.Int, Flags: ast.Short | ast.Unsigned}}, - {"Int", btType(clang.TypeInt), ast.BuiltinType{Kind: ast.Int}}, - {"UInt", btType(clang.TypeUInt), ast.BuiltinType{Kind: ast.Int, Flags: ast.Unsigned}}, - {"Long", btType(clang.TypeLong), ast.BuiltinType{Kind: ast.Int, Flags: ast.Long}}, - {"ULong", btType(clang.TypeULong), ast.BuiltinType{Kind: ast.Int, Flags: ast.Long | ast.Unsigned}}, - {"LongLong", btType(clang.TypeLongLong), ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong}}, - {"ULongLong", btType(clang.TypeULongLong), ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong | ast.Unsigned}}, - {"Int128", btType(clang.TypeInt128), ast.BuiltinType{Kind: ast.Int128}}, - {"UInt128", btType(clang.TypeUInt128), ast.BuiltinType{Kind: ast.Int128, Flags: ast.Unsigned}}, - {"Float", btType(clang.TypeFloat), ast.BuiltinType{Kind: ast.Float}}, - {"Half", btType(clang.TypeHalf), ast.BuiltinType{Kind: ast.Float16}}, - {"Float16", btType(clang.TypeFloat16), ast.BuiltinType{Kind: ast.Float16}}, - {"Double", btType(clang.TypeDouble), ast.BuiltinType{Kind: ast.Float, Flags: ast.Double}}, - {"LongDouble", btType(clang.TypeLongDouble), ast.BuiltinType{Kind: ast.Float, Flags: ast.Long | ast.Double}}, - {"Float128", btType(clang.TypeFloat128), ast.BuiltinType{Kind: ast.Float128}}, - {"Complex", getComplexType(0), ast.BuiltinType{Kind: ast.Complex}}, - {"Complex", getComplexType(ast.Double), ast.BuiltinType{Flags: ast.Double, Kind: ast.Complex}}, - {"Complex", getComplexType(ast.Long | ast.Double), ast.BuiltinType{Flags: ast.Long | ast.Double, Kind: ast.Complex}}, - {"Unknown", btType(clang.TypeIbm128), ast.BuiltinType{Kind: ast.Void}}, - } - - converter := &parser.Converter{} - converter.Convert() - for _, bt := range tests { - res := converter.ProcessBuiltinType(bt.typ) - if res.Kind != bt.expected.Kind { - t.Fatalf("%s Kind mismatch:got %d want %d, \n", bt.name, res.Kind, bt.expected.Kind) - } - if res.Flags != bt.expected.Flags { - t.Fatalf("%s Flags mismatch:got %d,want %d\n", bt.name, res.Flags, bt.expected.Flags) - } - } -} - -// Char's Default Type in macos is signed char & in linux is unsigned char -// So we only confirm the char's kind is char & flags is unsigned or signed -func TestChar(t *testing.T) { - typ, index, transunit := GetType(&GetTypeOptions{ - TypeCode: "char", - IsCpp: false, - }) - converter := &parser.Converter{} - expr := converter.ProcessType(typ) - if btType, ok := expr.(*ast.BuiltinType); ok { - if btType.Kind == ast.Char { - if btType.Flags != ast.Signed && btType.Flags != ast.Unsigned { - t.Fatal("Char's flags is not signed or unsigned") - } - } - } else { - t.Fatal("Char's expr is not a builtin type") - } - index.Dispose() - transunit.Dispose() -} - -type GetTypeOptions struct { - TypeCode string // e.g. "char*", "char**" - - // ExpectTypeKind specifies the expected type kind (optional) - // Use clang.Type_Invalid to accept any type (default behavior) - // *For complex types (when is included), specifying this is crucial - // to filter out the correct type, as there will be multiple VarDecl fields present - ExpectTypeKind clang.TypeKind - - // Args contains additional compilation arguments passed to Clang (optional) - // These are appended after the default language-specific arguments - // Example: []string{"-std=c++11"} - Args []string - - // IsCpp indicates whether the code should be treated as C++ (true) or C (false) - // This affects the default language arguments passed to Clang: - // - For C++: []string{"-x", "c++"} - // - For C: []string{"-x", "c"} - // *For complex C types, C Must be specified - IsCpp bool -} - -// GetType returns the clang.Type of the given type code -// Need to dispose the index and unit after using -// e.g. index.Dispose(), unit.Dispose() -func GetType(option *GetTypeOptions) (clang.Type, *clang.Index, *clang.TranslationUnit) { - code := fmt.Sprintf("%s placeholder;", option.TypeCode) - index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{ - File: code, - Temp: true, - Args: option.Args, - IsCpp: option.IsCpp, - }) - if err != nil { - panic(err) - } - cursor := unit.Cursor() - var typ clang.Type - clangutils.VisitChildren(cursor, func(child, parent clang.Cursor) clang.ChildVisitResult { - if child.Kind == clang.CursorVarDecl && (option.ExpectTypeKind == clang.TypeInvalid || option.ExpectTypeKind == child.Type().Kind) { - typ = child.Type() - return clang.ChildVisit_Break - } - return clang.ChildVisit_Continue - }) - return typ, index, unit -} - -func btType(kind clang.TypeKind) clang.Type { - return clang.Type{Kind: kind} -} - -// get complex type from source code parsed -func getComplexType(flag ast.TypeFlag) clang.Type { - var typeStr string - if flag&(ast.Long|ast.Double) == (ast.Long | ast.Double) { - typeStr = "long double" - } else if flag&ast.Double != 0 { - typeStr = "double" - } else { - typeStr = "float" - } - - code := fmt.Sprintf("#include \n%s complex", typeStr) - - // todo(zzy):free index and unit after test - typ, _, _ := GetType(&GetTypeOptions{ - TypeCode: code, - ExpectTypeKind: clang.TypeComplex, - IsCpp: false, - }) - - return typ -} - -func TestPreprocess(t *testing.T) { - combinedFile, err := os.CreateTemp("./", "compose_*.h") - if err != nil { - panic(err) - } - defer os.Remove(combinedFile.Name()) - - clangtool.ComposeIncludes([]string{"main.h", "compat.h"}, combinedFile.Name()) - - efile, err := os.CreateTemp("", "temp_*.i") - if err != nil { - panic(err) - } - defer os.Remove(efile.Name()) - - ppconf := &preprocessor.Config{ - Compiler: "clang", - Flags: []string{"-I./_testdata/hfile"}, - } - err = preprocessor.Do(combinedFile.Name(), efile.Name(), ppconf) - if err != nil { - t.Fatal(err) - } - - config := &clangutils.Config{ - File: efile.Name(), - Temp: false, - IsCpp: false, - } - - var str strings.Builder - - visit(config, func(cursor, parent clang.Cursor) clang.ChildVisitResult { - switch cursor.Kind { - case clang.CursorEnumDecl, clang.CursorStructDecl, clang.CursorUnionDecl, clang.CursorTypedefDecl: - var filename clang.String - var line, column c.Uint - cursor.Location().PresumedLocation(&filename, &line, &column) - str.WriteString("TypeKind: ") - str.WriteString(clang.GoString(cursor.Kind.String())) - str.WriteString(" Name: ") - str.WriteString(clang.GoString(cursor.String())) - str.WriteString("\n") - str.WriteString("Location: ") - str.WriteString(fmt.Sprintf("%s:%d:%d\n", path.Base(c.GoString(filename.CStr())), line, column)) - } - return clang.ChildVisit_Continue - }) - - expect := ` -TypeKind: StructDecl Name: A -Location: main.h:3:16 -TypeKind: TypedefDecl Name: A -Location: main.h:6:3 -TypeKind: TypedefDecl Name: B -Location: compat.h:3:11 -TypeKind: TypedefDecl Name: C -Location: main.h:8:11 -` - - compareOutput(t, expect, str.String()) -} - -func visit(config *clangutils.Config, visitFunc func(cursor, parent clang.Cursor) clang.ChildVisitResult) { - index, unit, err := clangutils.CreateTranslationUnit(config) - if err != nil { - panic(err) - } - cursor := unit.Cursor() - clangutils.VisitChildren(cursor, visitFunc) - index.Dispose() - unit.Dispose() -} - -func compareOutput(t *testing.T, expected, actual string) { - expected = strings.TrimSpace(expected) - actual = strings.TrimSpace(actual) - if expected != actual { - t.Fatalf("Test failed: expected \n%s \ngot \n%s", expected, actual) - } -} - -func TestPostOrderVisitChildren(t *testing.T) { - config := &clangutils.Config{ - File: "./testdata/named_nested_struct/temp.h", - Temp: false, - IsCpp: false, - } - - name := make(map[string]bool) - visit(config, func(cursor, parent clang.Cursor) clang.ChildVisitResult { - if cursor.Kind == clang.CursorStructDecl { - if !name[clang.GoString(cursor.String())] { - name[clang.GoString(cursor.String())] = true - file, line, column := clangutils.GetPresumedLocation(cursor.Location()) - fmt.Println("StructDecl Name:", clang.GoString(cursor.String()), file, line, column) - } - } - return clang.ChildVisit_Recurse - }) - - index, unit, err := clangutils.CreateTranslationUnit(config) - if err != nil { - panic(err) - } - defer index.Dispose() - defer unit.Dispose() - - childStr := make([]string, 6) - childs := parser.PostOrderVisitChildren(unit.Cursor(), func(child, parent clang.Cursor) bool { - return child.Kind == clang.CursorStructDecl - }) - for i, child := range childs { - childStr[i] = clang.GoString(child.String()) - } - expect := []string{"c", "d", "b", "f", "e", "a"} - if !reflect.DeepEqual(expect, childStr) { - fmt.Println("Unexpected child order:", childStr) - } -} - -func TestEmptyDeclVsForwardDecl(t *testing.T) { - config := &clangutils.Config{ - File: "./testdata/forward_vs_empty/temp.h", - Temp: false, - IsCpp: false, - } - - type isDefinition = bool - var decl map[string]isDefinition = map[string]isDefinition{ - "ForwardOnly": false, - "EmptyStruct": true, - } - - visit(config, func(cursor, parent clang.Cursor) clang.ChildVisitResult { - if cursor.Kind == clang.CursorStructDecl { - sdecl := clang.GoString(cursor.String()) - if _, ok := decl[sdecl]; ok { - isDefine := cursor.IsCursorDefinition() != 0 - if isDefine != decl[sdecl] { - t.Fatalf("StructDecl %s isDefinition expect %v, got %v", sdecl, decl[sdecl], isDefine) - } - } - fmt.Println("StructDecl Name:", clang.GoString(cursor.String()), "isDefinition:", cursor.IsCursorDefinition() != 0) - } - return clang.ChildVisit_Recurse - }) -} diff --git a/_xtool/internal/parser/testdata/class/expect.json b/_xtool/internal/parser/testdata/class/expect.json deleted file mode 100644 index 1dbfe94a6..000000000 --- a/_xtool/internal/parser/testdata/class/expect.json +++ /dev/null @@ -1,731 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "A", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "B", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": true, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": [ - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN1B3fooEid", - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "B", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 16, - "Kind": 8, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 8, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN1B5vafooEiz", - "Name": { - "Name": "vafoo", - "_Type": "Ident" - }, - "Parent": { - "Name": "B", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "_Type": "Variadic" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - } - ], - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "C", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": [ - { - "Doc": null, - "IsConst": false, - "IsConstructor": true, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN1CC1Ev", - "Name": { - "Name": "C", - "_Type": "Ident" - }, - "Parent": { - "Name": "C", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": true, - "IsDestructor": false, - "IsExplicit": true, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN1CC1Ev", - "Name": { - "Name": "C", - "_Type": "Ident" - }, - "Parent": { - "Name": "C", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": true, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN1CD1Ev", - "Name": { - "Name": "~C", - "_Type": "Ident" - }, - "Parent": { - "Name": "C", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": true, - "IsOverride": false, - "IsStatic": true, - "IsVirtual": false, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN1C3fooEv", - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "C", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - } - ], - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Base", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": [ - { - "Doc": null, - "IsConst": false, - "IsConstructor": true, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN4BaseC1Ev", - "Name": { - "Name": "Base", - "_Type": "Ident" - }, - "Parent": { - "Name": "Base", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": true, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": true, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN4BaseD1Ev", - "Name": { - "Name": "~Base", - "_Type": "Ident" - }, - "Parent": { - "Name": "Base", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": true, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN4Base3fooEv", - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "Base", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - } - ], - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Derived", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": [ - { - "Doc": null, - "IsConst": false, - "IsConstructor": true, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN7DerivedC1Ev", - "Name": { - "Name": "Derived", - "_Type": "Ident" - }, - "Parent": { - "Name": "Derived", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": true, - "IsExplicit": false, - "IsInline": false, - "IsOverride": true, - "IsStatic": false, - "IsVirtual": true, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN7DerivedD1Ev", - "Name": { - "Name": "~Derived", - "_Type": "Ident" - }, - "Parent": { - "Name": "Derived", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": true, - "IsStatic": false, - "IsVirtual": true, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN7Derived3fooEv", - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "Derived", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - } - ], - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/class/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "NSA", - "_Type": "Ident" - }, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/class/temp.h b/_xtool/internal/parser/testdata/class/temp.h deleted file mode 100644 index 3a5b5b194..000000000 --- a/_xtool/internal/parser/testdata/class/temp.h +++ /dev/null @@ -1,44 +0,0 @@ -class A { - public: - int a; - int b; -}; - -class B { - public: - static int a; - int b; - float foo(int a, double b); - void vafoo(int a, ...); - - private: - static void bar(); - - protected: - void bar2(); -}; - -class C { - public: - C(); - explicit C(); - ~C(); - static inline void foo(); -}; - -class Base { - public: - Base(); - virtual ~Base(); - virtual void foo(); -}; -class Derived : public Base { - public: - Derived(); - ~Derived() override; - void foo() override; -}; - -namespace NSA { -class Foo {}; -} // namespace NSA diff --git a/_xtool/internal/parser/testdata/comment/expect.json b/_xtool/internal/parser/testdata/comment/expect.json deleted file mode 100755 index 77bfa3f6d..000000000 --- a/_xtool/internal/parser/testdata/comment/expect.json +++ /dev/null @@ -1,685 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": { - "List": [ - { - "Text": "// not read doc 1", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo1v", - "Name": { - "Name": "foo1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": { - "List": [ - { - "Text": "/* not read doc 2 */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo2v", - "Name": { - "Name": "foo2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": { - "List": [ - { - "Text": "/// doc", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo3v", - "Name": { - "Name": "foo3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": { - "List": [ - { - "Text": "/** doc */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo4v", - "Name": { - "Name": "foo4", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": { - "List": [ - { - "Text": "/*! doc */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo5v", - "Name": { - "Name": "foo5", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": { - "List": [ - { - "Text": "/// doc 1\n/// doc 2", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo6v", - "Name": { - "Name": "foo6", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": { - "List": [ - { - "Text": "/*! doc 1 */\n/*! doc 2 */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo7v", - "Name": { - "Name": "foo7", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": { - "List": [ - { - "Text": "/** doc 1 */\n/** doc 1 */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo8v", - "Name": { - "Name": "foo8", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": { - "List": [ - { - "Text": "/**\n * doc 1\n * doc 2\n */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo9v", - "Name": { - "Name": "foo9", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": { - "List": [ - { - "Text": "/// doc", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsStatic": false, - "Names": [ - { - "Name": "x", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": { - "List": [ - { - "Text": "///\u003c comment", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "y", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": { - "List": [ - { - "Text": "/*!\u003c comment */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "z", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Doc", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": { - "List": [ - { - "Text": "/**\n * static field doc\n */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsStatic": true, - "Names": [ - { - "Name": "x", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": { - "List": [ - { - "Text": "/*!\u003c static field comment */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "Doc": null, - "IsStatic": true, - "Names": [ - { - "Name": "y", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": { - "List": [ - { - "Text": "/**\n * field doc\n */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": { - "List": [ - { - "Text": "///\u003c field comment", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 2, - "Comment": { - "List": [ - { - "Text": "/*!\u003c protected field comment */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "value", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": [ - { - "Doc": { - "List": [ - { - "Text": "/**\n * method doc\n */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/comment/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN3Doc3FooEv", - "Name": { - "Name": "Foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "Doc", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - } - ], - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/comment/temp.h b/_xtool/internal/parser/testdata/comment/temp.h deleted file mode 100644 index d104cfbcf..000000000 --- a/_xtool/internal/parser/testdata/comment/temp.h +++ /dev/null @@ -1,53 +0,0 @@ -// not read doc 1 -void foo1(); -/* not read doc 2 */ -void foo2(); -/// doc -void foo3(); -/** doc */ -void foo4(); -/*! doc */ -void foo5(); -/// doc 1 -/// doc 2 -void foo6(); -/*! doc 1 */ -/*! doc 2 */ -void foo7(); -/** doc 1 */ -/** doc 1 */ -void foo8(); -/** - * doc 1 - * doc 2 - */ -void foo9(); -struct Foo { - /// doc - int x; - int y; ///< comment - /** - * field doc (parse ignore with comment in same cursor) - */ - int z; /*!< comment */ -}; -class Doc { - public: - /** - * static field doc - */ - static int x; - static int y; /*!< static field comment */ - /** - * field doc - */ - int a; - int b; ///< field comment - /** - * method doc - */ - void Foo(); - - protected: - int value; /*!< protected field comment */ -}; \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/enum/expect.json b/_xtool/internal/parser/testdata/enum/expect.json deleted file mode 100755 index 24f63f062..000000000 --- a/_xtool/internal/parser/testdata/enum/expect.json +++ /dev/null @@ -1,220 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/enum/temp.h", - "_Type": "Location" - }, - "Name": null, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "a", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "b", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "c", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "2", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/enum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "Foo1a", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "Foo1b", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "Foo1c", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "2", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/enum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "Foo2a", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "Foo2b", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "2", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "Foo2c", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "4", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/enum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "Foo3a", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "Foo3b", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "2", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "Foo3c", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "3", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/enum/temp.h b/_xtool/internal/parser/testdata/enum/temp.h deleted file mode 100644 index 4a4ba9346..000000000 --- a/_xtool/internal/parser/testdata/enum/temp.h +++ /dev/null @@ -1,21 +0,0 @@ -enum { - a, - b, - c, -}; - -enum Foo1 { - Foo1a, - Foo1b, - Foo1c, -}; -enum Foo2 { - Foo2a = 1, - Foo2b = 2, - Foo2c = 4, -}; -enum Foo3 { - Foo3a = 1, - Foo3b, - Foo3c, -}; diff --git a/_xtool/internal/parser/testdata/forward_vs_empty/expect.json b/_xtool/internal/parser/testdata/forward_vs_empty/expect.json deleted file mode 100755 index 3196f7150..000000000 --- a/_xtool/internal/parser/testdata/forward_vs_empty/expect.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/forward_vs_empty/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "ForwardOnly", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": null, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forward_vs_empty/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "EmptyStruct", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/forward_vs_empty/temp.h b/_xtool/internal/parser/testdata/forward_vs_empty/temp.h deleted file mode 100644 index ffd758424..000000000 --- a/_xtool/internal/parser/testdata/forward_vs_empty/temp.h +++ /dev/null @@ -1,2 +0,0 @@ -struct ForwardOnly; -struct EmptyStruct {}; diff --git a/_xtool/internal/parser/testdata/forwarddecl1/expect.json b/_xtool/internal/parser/testdata/forwarddecl1/expect.json deleted file mode 100755 index 6b7c31f88..000000000 --- a/_xtool/internal/parser/testdata/forwarddecl1/expect.json +++ /dev/null @@ -1,759 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Name": { - "Name": "bar", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "bar", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "sqlite3_pcache_page", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": null, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "sqlite3_pcache_page", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "pExtra", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "sqlite3_pcache", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": null, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "sqlite3_pcache_methods2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": null, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "sqlite3_pcache_methods2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "iVersion", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "xShutdown", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "xCreate", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "X": { - "Name": "sqlite3_pcache", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "FuncType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "sqlite3_file", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": null, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "sqlite3_file", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": { - "List": [ - { - "Text": "/* Methods for an open file */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "pMethods", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Name": "sqlite3_io_methods", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "sqlite3_io_methods", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "xUnfetch", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Name": "sqlite3_file", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "lua_State", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": null, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "lua_Debug", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": null, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z12lua_getstackP9lua_StateiP9lua_Debug", - "Name": { - "Name": "lua_getstack", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "L", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Name": "lua_State", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "level", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "ar", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Name": "lua_Debug", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "lua_Debug", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": { - "List": [ - { - "Text": "// char in ast will got unsigned char \u0026 signed char and they are same in go\n // but in ast,will have different,but with compare test,we need avoid these senario", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "IsStatic": false, - "Names": [ - { - "Name": "short_src", - "_Type": "Ident" - } - ], - "Type": { - "Elt": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "Len": { - "Kind": 0, - "Value": "60", - "_Type": "BasicLit" - }, - "_Type": "ArrayType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": { - "List": [ - { - "Text": "/* active function */", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "i_ci", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Name": { - "Name": "CallInfo", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": [ - { - "Loc": { - "File": "testdata/forwarddecl1/temp.h", - "_Type": "Location" - }, - "Name": "LUA_IDSIZE", - "Tokens": [ - { - "Lit": "LUA_IDSIZE", - "Token": 3, - "_Type": "Token" - }, - { - "Lit": "60", - "Token": 4, - "_Type": "Token" - } - ], - "_Type": "Macro" - } - ] -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/forwarddecl1/temp.h b/_xtool/internal/parser/testdata/forwarddecl1/temp.h deleted file mode 100644 index ead33470e..000000000 --- a/_xtool/internal/parser/testdata/forwarddecl1/temp.h +++ /dev/null @@ -1,47 +0,0 @@ -struct Foo { - struct bar *b; -}; -typedef struct bar bar; -struct bar { - int a; -}; - -typedef struct sqlite3_pcache_page sqlite3_pcache_page; -struct sqlite3_pcache_page { - void *pExtra; -}; - -typedef struct sqlite3_pcache sqlite3_pcache; - -typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2; -struct sqlite3_pcache_methods2 { - int iVersion; - void (*xShutdown)(void *); - sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable); -}; - -typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file { - const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ -}; - -typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods { - int (*xUnfetch)(sqlite3_file *, int iOfst, void *p); -}; - -#define LUA_IDSIZE 60 - -typedef struct lua_State lua_State; - -typedef struct lua_Debug lua_Debug; - -int(lua_getstack)(lua_State *L, int level, lua_Debug *ar); - -struct lua_Debug { - // char in ast will got unsigned char & signed char and they are same in go - // but in ast,will have different,but with compare test,we need avoid these senario - int short_src[LUA_IDSIZE]; - /* private part */ - struct CallInfo *i_ci; /* active function */ -}; \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/forwarddecl2/expect.json b/_xtool/internal/parser/testdata/forwarddecl2/expect.json deleted file mode 100755 index 78789180a..000000000 --- a/_xtool/internal/parser/testdata/forwarddecl2/expect.json +++ /dev/null @@ -1,108 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/forwarddecl2/impl.h", - "_Type": "Location" - }, - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/forwarddecl2/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z1fP3foo", - "Name": { - "Name": "f", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "f", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Name": "foo", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - } - ], - "includes": [ - { - "Path": "testdata/forwarddecl2/impl.h", - "_Type": "Include" - } - ], - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/forwarddecl2/impl.h b/_xtool/internal/parser/testdata/forwarddecl2/impl.h deleted file mode 100644 index 4f665b0d6..000000000 --- a/_xtool/internal/parser/testdata/forwarddecl2/impl.h +++ /dev/null @@ -1,3 +0,0 @@ -struct foo { - int a; -}; \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/forwarddecl2/temp.h b/_xtool/internal/parser/testdata/forwarddecl2/temp.h deleted file mode 100644 index 382c89f3f..000000000 --- a/_xtool/internal/parser/testdata/forwarddecl2/temp.h +++ /dev/null @@ -1,3 +0,0 @@ -#include "impl.h" -typedef struct foo foo; -void f(foo *f); \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/func/expect.json b/_xtool/internal/parser/testdata/func/expect.json deleted file mode 100755 index 4523c263c..000000000 --- a/_xtool/internal/parser/testdata/func/expect.json +++ /dev/null @@ -1,816 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo1v", - "Name": { - "Name": "foo1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo2i", - "Name": { - "Name": "foo2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo3iz", - "Name": { - "Name": "foo3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "_Type": "Variadic" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4foo4id", - "Name": { - "Name": "foo4", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 16, - "Kind": 8, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "X": { - "Flags": 0, - "Kind": 8, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": true, - "IsOverride": false, - "IsStatic": true, - "IsVirtual": false, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZL4foo5ii", - "Name": { - "Name": "foo5", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "fntype1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4bar1v", - "Name": { - "Name": "bar1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "fntype2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 4, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 4, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "fntype3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Name": "fntype2", - "_Type": "Ident" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z4bar2l", - "Name": { - "Name": "bar2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 4, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 4, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "OSSL_CORE_HANDLE", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": null, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "OSSL_DISPATCH", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": null, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "OSSL_provider_init_fn", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Name": "OSSL_CORE_HANDLE", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Name": "OSSL_DISPATCH", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "X": { - "Name": "OSSL_DISPATCH", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z18OSSL_provider_initPK16OSSL_CORE_HANDLEPK13OSSL_DISPATCHPS4_PPv", - "Name": { - "Name": "OSSL_provider_init", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Name": "OSSL_CORE_HANDLE", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Name": "OSSL_DISPATCH", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "X": { - "Name": "OSSL_DISPATCH", - "_Type": "Ident" - }, - "_Type": "PointerType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/func/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z7qsort_bPvU13block_pointerFiPKvS1_E", - "Name": { - "Name": "qsort_b", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "__base", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "__compar", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "BlockPointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/func/temp.h b/_xtool/internal/parser/testdata/func/temp.h deleted file mode 100644 index 94ea26797..000000000 --- a/_xtool/internal/parser/testdata/func/temp.h +++ /dev/null @@ -1,20 +0,0 @@ -void foo1(); -void foo2(int a); -void foo3(int a, ...); -float *foo4(int a, double b); -static inline int foo5(int a, int b); - -typedef void(fntype1)(); -fntype1 bar1; - -typedef long(fntype2)(long a); -typedef fntype2 fntype3; -fntype3 bar2; - -typedef struct OSSL_CORE_HANDLE OSSL_CORE_HANDLE; -typedef struct OSSL_DISPATCH OSSL_DISPATCH; -typedef int(OSSL_provider_init_fn)(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, - void **provctx); -OSSL_provider_init_fn OSSL_provider_init; - -void qsort_b(void *__base, int (^_Nonnull __compar)(const void *, const void *)); diff --git a/_xtool/internal/parser/testdata/include/expect.json b/_xtool/internal/parser/testdata/include/expect.json deleted file mode 100755 index f1997192e..000000000 --- a/_xtool/internal/parser/testdata/include/expect.json +++ /dev/null @@ -1,134 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/include/src/conf.h", - "_Type": "Location" - }, - "Name": { - "Name": "ConfA", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/include/src/core/core.h", - "_Type": "Location" - }, - "Name": { - "Name": "CoreA", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": [ - { - "Path": "testdata/include/src/core/core.h", - "_Type": "Include" - }, - { - "Path": "testdata/include/src/conf.h", - "_Type": "Include" - } - ], - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/include/src/conf.h b/_xtool/internal/parser/testdata/include/src/conf.h deleted file mode 100644 index 8b748cd30..000000000 --- a/_xtool/internal/parser/testdata/include/src/conf.h +++ /dev/null @@ -1,4 +0,0 @@ -typedef struct ConfA { - int a; - int b; -} ConfA; diff --git a/_xtool/internal/parser/testdata/include/src/core/core.h b/_xtool/internal/parser/testdata/include/src/core/core.h deleted file mode 100644 index 3f43b3d20..000000000 --- a/_xtool/internal/parser/testdata/include/src/core/core.h +++ /dev/null @@ -1,5 +0,0 @@ -#include "../conf.h" -typedef struct CoreA { - int a; - int b; -} CoreA; diff --git a/_xtool/internal/parser/testdata/include/temp.h b/_xtool/internal/parser/testdata/include/temp.h deleted file mode 100644 index 1b6fb3721..000000000 --- a/_xtool/internal/parser/testdata/include/temp.h +++ /dev/null @@ -1 +0,0 @@ -#include "src/core/core.h" diff --git a/_xtool/internal/parser/testdata/macro/def.h b/_xtool/internal/parser/testdata/macro/def.h deleted file mode 100644 index 81e2e9052..000000000 --- a/_xtool/internal/parser/testdata/macro/def.h +++ /dev/null @@ -1,4 +0,0 @@ -#define __FSID_T_TYPE \ - struct { \ - int __val[2]; \ - } \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/macro/expect.json b/_xtool/internal/parser/testdata/macro/expect.json deleted file mode 100755 index a18819e5f..000000000 --- a/_xtool/internal/parser/testdata/macro/expect.json +++ /dev/null @@ -1,232 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/macro/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NewType", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "__val", - "_Type": "Ident" - } - ], - "Type": { - "Elt": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "Len": { - "Kind": 0, - "Value": "2", - "_Type": "BasicLit" - }, - "_Type": "ArrayType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": [ - { - "Path": "testdata/macro/def.h", - "_Type": "Include" - } - ], - "macros": [ - { - "Loc": { - "File": "testdata/macro/temp.h", - "_Type": "Location" - }, - "Name": "DEBUG", - "Tokens": [ - { - "Lit": "DEBUG", - "Token": 3, - "_Type": "Token" - } - ], - "_Type": "Macro" - }, - { - "Loc": { - "File": "testdata/macro/temp.h", - "_Type": "Location" - }, - "Name": "OK", - "Tokens": [ - { - "Lit": "OK", - "Token": 3, - "_Type": "Token" - }, - { - "Lit": "1", - "Token": 4, - "_Type": "Token" - } - ], - "_Type": "Macro" - }, - { - "Loc": { - "File": "testdata/macro/temp.h", - "_Type": "Location" - }, - "Name": "SQUARE", - "Tokens": [ - { - "Lit": "SQUARE", - "Token": 3, - "_Type": "Token" - }, - { - "Lit": "(", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "x", - "Token": 3, - "_Type": "Token" - }, - { - "Lit": ")", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "(", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "(", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "x", - "Token": 3, - "_Type": "Token" - }, - { - "Lit": ")", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "*", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "(", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "x", - "Token": 3, - "_Type": "Token" - }, - { - "Lit": ")", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": ")", - "Token": 1, - "_Type": "Token" - } - ], - "_Type": "Macro" - }, - { - "Loc": { - "File": "testdata/macro/def.h", - "_Type": "Location" - }, - "Name": "__FSID_T_TYPE", - "Tokens": [ - { - "Lit": "__FSID_T_TYPE", - "Token": 3, - "_Type": "Token" - }, - { - "Lit": "struct", - "Token": 2, - "_Type": "Token" - }, - { - "Lit": "{", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "int", - "Token": 2, - "_Type": "Token" - }, - { - "Lit": "__val", - "Token": 3, - "_Type": "Token" - }, - { - "Lit": "[", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "2", - "Token": 4, - "_Type": "Token" - }, - { - "Lit": "]", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": ";", - "Token": 1, - "_Type": "Token" - }, - { - "Lit": "}", - "Token": 1, - "_Type": "Token" - } - ], - "_Type": "Macro" - } - ] -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/macro/temp.h b/_xtool/internal/parser/testdata/macro/temp.h deleted file mode 100644 index 199d044c2..000000000 --- a/_xtool/internal/parser/testdata/macro/temp.h +++ /dev/null @@ -1,6 +0,0 @@ -#define DEBUG -#define OK 1 -#define SQUARE(x) ((x) * (x)) - -#include "def.h" -typedef __FSID_T_TYPE NewType; \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/named_nested_struct/expect.json b/_xtool/internal/parser/testdata/named_nested_struct/expect.json deleted file mode 100755 index 3a01cf5fa..000000000 --- a/_xtool/internal/parser/testdata/named_nested_struct/expect.json +++ /dev/null @@ -1,304 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/named_nested_struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "c", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/named_nested_struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "d", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/named_nested_struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "b", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "c_field", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Name": "c", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "d_field", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Name": "d", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/named_nested_struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "f", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/named_nested_struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "e", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "f_field", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Name": "f", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/named_nested_struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "a", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b_field", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Name": "b", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "e_field", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Name": "e", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/named_nested_struct/temp.h b/_xtool/internal/parser/testdata/named_nested_struct/temp.h deleted file mode 100644 index c9fc5fe2a..000000000 --- a/_xtool/internal/parser/testdata/named_nested_struct/temp.h +++ /dev/null @@ -1,15 +0,0 @@ -struct a { - struct b { - struct c { - int a; - } c_field; - struct d { - int b; - } d_field; - } b_field; - struct e { - struct f { - int b; - } f_field; - } e_field; -}; diff --git a/_xtool/internal/parser/testdata/nestedenum/expect.json b/_xtool/internal/parser/testdata/nestedenum/expect.json deleted file mode 100755 index 2fb0b3b49..000000000 --- a/_xtool/internal/parser/testdata/nestedenum/expect.json +++ /dev/null @@ -1,494 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": null, - "Parent": { - "Name": "NestedEnum", - "_Type": "Ident" - }, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA1", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA2", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": null, - "Parent": { - "Name": "a", - "_Type": "Ident" - }, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA_A1", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA_A2", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "a", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "is_metadata1_t", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NestedEnum", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "is_metadata1_t", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a_t", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Name": "a", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": null, - "Parent": { - "Name": "NestedEnum2", - "_Type": "Ident" - }, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA3", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA4", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NestedEnum2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "is_metadata3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA5", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA6", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NestedEnum3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "is_metadata4", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA7", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA8", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NestedEnum4", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "key", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Name": "is_metadata4", - "_Type": "Ident" - }, - "Tag": 2, - "_Type": "TagExpr" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "OuterEnum", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA9", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA10", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Enum", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "k", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Name": "OuterEnum", - "_Type": "Ident" - }, - "Tag": 2, - "_Type": "TagExpr" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/nestedenum/temp.h b/_xtool/internal/parser/testdata/nestedenum/temp.h deleted file mode 100644 index 4fbcce954..000000000 --- a/_xtool/internal/parser/testdata/nestedenum/temp.h +++ /dev/null @@ -1,54 +0,0 @@ -struct NestedEnum -{ - enum - { - APR_BUCKET_DATA1 = 0, - APR_BUCKET_METADATA2 = 1 - } is_metadata1_t; - - struct a - { - enum - { - APR_BUCKET_DATA_A1 = 0, - APR_BUCKET_METADATA_A2 = 1 - } is_metadata1_t; - } a_t; -}; - -struct NestedEnum2 -{ - enum - { - APR_BUCKET_DATA3 = 0, - APR_BUCKET_METADATA4 = 1 - }; -}; - -struct NestedEnum3 -{ - enum is_metadata3 - { - APR_BUCKET_DATA5 = 0, - APR_BUCKET_METADATA6 = 1 - }; -}; - -struct NestedEnum4 -{ - enum is_metadata4 - { - APR_BUCKET_DATA7 = 0, - APR_BUCKET_METADATA8 = 1 - } key; -}; - -enum OuterEnum -{ - APR_BUCKET_DATA9 = 0, - APR_BUCKET_METADATA10 = 1 -}; -struct Enum -{ - enum OuterEnum k; -}; diff --git a/_xtool/internal/parser/testdata/nestedenum_cpp/expect.json b/_xtool/internal/parser/testdata/nestedenum_cpp/expect.json deleted file mode 100755 index 60f865bcc..000000000 --- a/_xtool/internal/parser/testdata/nestedenum_cpp/expect.json +++ /dev/null @@ -1,524 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": null, - "Parent": { - "Name": "NestedEnum", - "_Type": "Ident" - }, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA1", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA2", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": null, - "Parent": { - "Parent": { - "Name": "NestedEnum", - "_Type": "Ident" - }, - "X": { - "Name": "a", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA_A1", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA_A2", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "a", - "_Type": "Ident" - }, - "Parent": { - "Name": "NestedEnum", - "_Type": "Ident" - }, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "is_metadata1_t", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NestedEnum", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "is_metadata1_t", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a_t", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Parent": { - "Name": "NestedEnum", - "_Type": "Ident" - }, - "X": { - "Name": "a", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": null, - "Parent": { - "Name": "NestedEnum2", - "_Type": "Ident" - }, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA3", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA4", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NestedEnum2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "is_metadata3", - "_Type": "Ident" - }, - "Parent": { - "Name": "NestedEnum3", - "_Type": "Ident" - }, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA5", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA6", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NestedEnum3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "is_metadata4", - "_Type": "Ident" - }, - "Parent": { - "Name": "NestedEnum4", - "_Type": "Ident" - }, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA7", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA8", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NestedEnum4", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "key", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Parent": { - "Name": "NestedEnum4", - "_Type": "Ident" - }, - "X": { - "Name": "is_metadata4", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Tag": 2, - "_Type": "TagExpr" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "OuterEnum", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "APR_BUCKET_DATA9", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "APR_BUCKET_METADATA10", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/nestedenum_cpp/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Enum", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "k", - "_Type": "Ident" - } - ], - "Type": { - "Name": { - "Name": "OuterEnum", - "_Type": "Ident" - }, - "Tag": 2, - "_Type": "TagExpr" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/nestedenum_cpp/temp.h b/_xtool/internal/parser/testdata/nestedenum_cpp/temp.h deleted file mode 100644 index 4fbcce954..000000000 --- a/_xtool/internal/parser/testdata/nestedenum_cpp/temp.h +++ /dev/null @@ -1,54 +0,0 @@ -struct NestedEnum -{ - enum - { - APR_BUCKET_DATA1 = 0, - APR_BUCKET_METADATA2 = 1 - } is_metadata1_t; - - struct a - { - enum - { - APR_BUCKET_DATA_A1 = 0, - APR_BUCKET_METADATA_A2 = 1 - } is_metadata1_t; - } a_t; -}; - -struct NestedEnum2 -{ - enum - { - APR_BUCKET_DATA3 = 0, - APR_BUCKET_METADATA4 = 1 - }; -}; - -struct NestedEnum3 -{ - enum is_metadata3 - { - APR_BUCKET_DATA5 = 0, - APR_BUCKET_METADATA6 = 1 - }; -}; - -struct NestedEnum4 -{ - enum is_metadata4 - { - APR_BUCKET_DATA7 = 0, - APR_BUCKET_METADATA8 = 1 - } key; -}; - -enum OuterEnum -{ - APR_BUCKET_DATA9 = 0, - APR_BUCKET_METADATA10 = 1 -}; -struct Enum -{ - enum OuterEnum k; -}; diff --git a/_xtool/internal/parser/testdata/scope/expect.json b/_xtool/internal/parser/testdata/scope/expect.json deleted file mode 100755 index 7da7c09a3..000000000 --- a/_xtool/internal/parser/testdata/scope/expect.json +++ /dev/null @@ -1,252 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/scope/temp.h", - "_Type": "Location" - }, - "MangledName": "_Z3foov", - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/scope/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN1a3fooEv", - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "a", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/scope/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN1a1b3fooEv", - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": { - "Parent": { - "Name": "a", - "_Type": "Ident" - }, - "X": { - "Name": "b", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/scope/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": [ - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/scope/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN3Foo3fooEv", - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "Foo", - "_Type": "Ident" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - } - ], - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/scope/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "a", - "_Type": "Ident" - }, - "Type": { - "Fields": { - "List": null, - "_Type": "FieldList" - }, - "Methods": [ - { - "Doc": null, - "IsConst": false, - "IsConstructor": false, - "IsDestructor": false, - "IsExplicit": false, - "IsInline": false, - "IsOverride": false, - "IsStatic": false, - "IsVirtual": false, - "Loc": { - "File": "testdata/scope/temp.h", - "_Type": "Location" - }, - "MangledName": "_ZN1a3Foo3fooEv", - "Name": { - "Name": "foo", - "_Type": "Ident" - }, - "Parent": { - "Parent": { - "Name": "a", - "_Type": "Ident" - }, - "X": { - "Name": "Foo", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Type": { - "Params": { - "List": null, - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "FuncDecl" - } - ], - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/scope/temp.h b/_xtool/internal/parser/testdata/scope/temp.h deleted file mode 100644 index ec3f6d53f..000000000 --- a/_xtool/internal/parser/testdata/scope/temp.h +++ /dev/null @@ -1,19 +0,0 @@ -void foo(); -namespace a { -void foo(); -} -namespace a { -namespace b { -void foo(); -} -} // namespace a -class Foo { - public: - void foo(); -}; -namespace a { -class Foo { - public: - void foo(); -}; -} // namespace a \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/struct/expect.json b/_xtool/internal/parser/testdata/struct/expect.json deleted file mode 100755 index c121834c0..000000000 --- a/_xtool/internal/parser/testdata/struct/expect.json +++ /dev/null @@ -1,377 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/struct/temp.h", - "_Type": "Location" - }, - "Name": null, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "Foo", - "_Type": "Ident" - } - ], - "Type": { - "X": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/struct/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Person", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "age", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "birthday", - "_Type": "Ident" - } - ], - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "year", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "day", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "month", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/struct/temp.h b/_xtool/internal/parser/testdata/struct/temp.h deleted file mode 100644 index 523025128..000000000 --- a/_xtool/internal/parser/testdata/struct/temp.h +++ /dev/null @@ -1,25 +0,0 @@ -struct { - int a; -}; - -struct Foo1 { - int a; - int b; -}; -struct Foo2 { - int a, b; -}; - -struct Foo3 { - int a; - int (*Foo)(int, int); -}; - -struct Person { - int age; - struct { - int year; - int day; - int month; - } birthday; -}; diff --git a/_xtool/internal/parser/testdata/typedef/expect.json b/_xtool/internal/parser/testdata/typedef/expect.json deleted file mode 100755 index b2466881a..000000000 --- a/_xtool/internal/parser/testdata/typedef/expect.json +++ /dev/null @@ -1,1056 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "INT", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "STANDARD_INT", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Name": "INT", - "_Type": "Ident" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NewINT", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NewIntPtr", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "X": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "NewIntArr", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Elt": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "Len": null, - "_Type": "ArrayType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "X": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "_Type": "Variadic" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "PointerType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "X": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "PointerType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Bar", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "X": { - "Params": { - "List": [ - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - }, - { - "Access": 0, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": null, - "Type": { - "X": { - "Flags": 0, - "Kind": 0, - "_Type": "BuiltinType" - }, - "_Type": "PointerType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Ret": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "FuncType" - }, - "_Type": "PointerType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "Foo", - "_Type": "Ident" - }, - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "Type": { - "Fields": { - "List": [ - { - "Access": 3, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "x", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 3, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyClass", - "_Type": "Ident" - }, - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "Type": { - "Name": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "Foo", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Tag": 3, - "_Type": "TagExpr" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyClassPtr", - "_Type": "Ident" - }, - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "Type": { - "X": { - "Name": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "Foo", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Tag": 3, - "_Type": "TagExpr" - }, - "_Type": "PointerType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyClassArray", - "_Type": "Ident" - }, - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "Type": { - "Elt": { - "Name": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "Foo", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Tag": 3, - "_Type": "TagExpr" - }, - "Len": null, - "_Type": "ArrayType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyStruct1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "x", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyUnion1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "x", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 1, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyEnum1", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "MyEnum1RED", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "MyEnum1GREEN", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "MyEnum1BLUE", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "2", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyStruct2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "x", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyStruct3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Name": { - "Name": "MyStruct2", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "StructPtr", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "X": { - "Name": { - "Name": "MyStruct2", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "PointerType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "StructArr", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Elt": { - "Name": { - "Name": "MyStruct2", - "_Type": "Ident" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "Len": null, - "_Type": "ArrayType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyEnum2", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "MyEnum2RED", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "MyEnum2GREEN", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "MyEnum2BLUE", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "2", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyEnum3", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Name": { - "Name": "MyEnum2", - "_Type": "Ident" - }, - "Tag": 2, - "_Type": "TagExpr" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "EnumPtr", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "X": { - "Name": { - "Name": "MyEnum2", - "_Type": "Ident" - }, - "Tag": 2, - "_Type": "TagExpr" - }, - "_Type": "PointerType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "EnumArr", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Elt": { - "Name": { - "Name": "MyEnum2", - "_Type": "Ident" - }, - "Tag": 2, - "_Type": "TagExpr" - }, - "Len": null, - "_Type": "ArrayType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyStruct", - "_Type": "Ident" - }, - "Parent": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "B", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "x", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "MyStruct2", - "_Type": "Ident" - }, - "Parent": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "B", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Type": { - "Name": { - "Parent": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "B", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "X": { - "Name": "MyStruct", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "StructPtr", - "_Type": "Ident" - }, - "Parent": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "B", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Type": { - "X": { - "Name": { - "Parent": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "B", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "X": { - "Name": "MyStruct", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "_Type": "PointerType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "StructArr", - "_Type": "Ident" - }, - "Parent": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "B", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Type": { - "Elt": { - "Name": { - "Parent": { - "Parent": { - "Name": "A", - "_Type": "Ident" - }, - "X": { - "Name": "B", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "X": { - "Name": "MyStruct", - "_Type": "Ident" - }, - "_Type": "ScopingExpr" - }, - "Tag": 0, - "_Type": "TagExpr" - }, - "Len": null, - "_Type": "ArrayType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "algorithm", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Items": [ - { - "Name": { - "Name": "AlgorithmA", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "0", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - }, - { - "Name": { - "Name": "AlgorithmB", - "_Type": "Ident" - }, - "Value": { - "Kind": 0, - "Value": "1", - "_Type": "BasicLit" - }, - "_Type": "EnumItem" - } - ], - "_Type": "EnumType" - }, - "_Type": "EnumTypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typedef/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "algorithm_t", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Name": { - "Name": "algorithm", - "_Type": "Ident" - }, - "Tag": 2, - "_Type": "TagExpr" - }, - "_Type": "TypedefDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/typedef/temp.h b/_xtool/internal/parser/testdata/typedef/temp.h deleted file mode 100644 index fb7b0320b..000000000 --- a/_xtool/internal/parser/testdata/typedef/temp.h +++ /dev/null @@ -1,41 +0,0 @@ -typedef int INT; - -typedef INT STANDARD_INT; - -typedef int NewINT, *NewIntPtr, NewIntArr[]; - -typedef int (*Foo1)(int, int, ...); - -typedef int (*Foo2)(int, int), (*Bar)(void *, void *); - -namespace A { -typedef class Foo { - int x; -} MyClass, *MyClassPtr, MyClassArray[]; -} // namespace A - -typedef struct { - int x; -} MyStruct1; - -typedef union { - int x; -} MyUnion1; -typedef enum { MyEnum1RED, MyEnum1GREEN, MyEnum1BLUE } MyEnum1; - -typedef struct { - int x; -} MyStruct2, MyStruct3, *StructPtr, StructArr[]; - -typedef enum { MyEnum2RED, MyEnum2GREEN, MyEnum2BLUE } MyEnum2, MyEnum3, *EnumPtr, EnumArr[]; - -namespace A { -namespace B { -typedef struct { - int x; -} MyStruct, MyStruct2, *StructPtr, StructArr[]; -} // namespace B -} // namespace A - -typedef enum algorithm { AlgorithmA, AlgorithmB } algorithm_t; -typedef algorithm_t algorithm; diff --git a/_xtool/internal/parser/testdata/typeof/expect.json b/_xtool/internal/parser/testdata/typeof/expect.json deleted file mode 100755 index 00053e908..000000000 --- a/_xtool/internal/parser/testdata/typeof/expect.json +++ /dev/null @@ -1,155 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": { - "List": [ - { - "Text": "// https://github.com/goplus/llcppg/issues/497", - "_Type": "Comment" - } - ], - "_Type": "CommentGroup" - }, - "Loc": { - "File": "testdata/typeof/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "spi_mem_dev_t", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "x", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "clock", - "_Type": "Ident" - } - ], - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "val", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 4, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 1, - "_Type": "RecordType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 0, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typeof/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "gpspi_flash_ll_clock_reg_t", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Flags": 4, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "TypedefDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/typeof/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "gpspi_flash_ll_dev_t", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "clock", - "_Type": "Ident" - } - ], - "Type": { - "Name": "gpspi_flash_ll_clock_reg_t", - "_Type": "Ident" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 1, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/typeof/temp.h b/_xtool/internal/parser/testdata/typeof/temp.h deleted file mode 100644 index ab5fa1db7..000000000 --- a/_xtool/internal/parser/testdata/typeof/temp.h +++ /dev/null @@ -1,15 +0,0 @@ -// https://github.com/goplus/llcppg/issues/497 -typedef struct { - int x; - union { - long val; - } clock; -} spi_mem_dev_t; - -extern spi_mem_dev_t GPSPI2_t; - -typedef typeof(GPSPI2_t.clock.val) gpspi_flash_ll_clock_reg_t; - -typedef union { - gpspi_flash_ll_clock_reg_t clock; -} gpspi_flash_ll_dev_t; diff --git a/_xtool/internal/parser/testdata/union/expect.json b/_xtool/internal/parser/testdata/union/expect.json deleted file mode 100755 index 6e66026a8..000000000 --- a/_xtool/internal/parser/testdata/union/expect.json +++ /dev/null @@ -1,184 +0,0 @@ -{ - "_Type": "File", - "decls": [ - { - "Doc": null, - "Loc": { - "File": "testdata/union/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "A", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "a", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "b", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 1, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - }, - { - "Doc": null, - "Loc": { - "File": "testdata/union/temp.h", - "_Type": "Location" - }, - "Name": { - "Name": "OuterUnion", - "_Type": "Ident" - }, - "Parent": null, - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "i", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "f", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 8, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "inner", - "_Type": "Ident" - } - ], - "Type": { - "Fields": { - "List": [ - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "c", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 0, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - }, - { - "Access": 1, - "Comment": null, - "Doc": null, - "IsStatic": false, - "Names": [ - { - "Name": "s", - "_Type": "Ident" - } - ], - "Type": { - "Flags": 32, - "Kind": 6, - "_Type": "BuiltinType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 1, - "_Type": "RecordType" - }, - "_Type": "Field" - } - ], - "_Type": "FieldList" - }, - "Methods": null, - "Tag": 1, - "_Type": "RecordType" - }, - "_Type": "TypeDecl" - } - ], - "includes": null, - "macros": null -} \ No newline at end of file diff --git a/_xtool/internal/parser/testdata/union/temp.h b/_xtool/internal/parser/testdata/union/temp.h deleted file mode 100644 index f1874db62..000000000 --- a/_xtool/internal/parser/testdata/union/temp.h +++ /dev/null @@ -1,13 +0,0 @@ -union A { - int a; - int b; -}; - -union OuterUnion { - int i; - float f; - union { - int c; - short s; - } inner; -}; diff --git a/_xtool/internal/symbol/symbol.go b/_xtool/internal/symbol/symbol.go deleted file mode 100644 index 62dbc414e..000000000 --- a/_xtool/internal/symbol/symbol.go +++ /dev/null @@ -1,36 +0,0 @@ -package symbol - -import ( - "fmt" - "os" - "path/filepath" - "runtime" -) - -// FindLibs finds the library file in the given path & the given name. -type Mode int - -const ( - ModeDynamic Mode = iota - ModeStatic -) - -func FindLibFile(path string, name string, mode Mode) (string, error) { - affix := libAffix(mode) - libPath := filepath.Join(path, fmt.Sprintf("lib%s%s", name, affix)) - _, err := os.Stat(libPath) - if err != nil { - return "", err - } - return libPath, nil -} - -func libAffix(mode Mode) (affix string) { - if mode == ModeStatic { - return ".a" - } - if runtime.GOOS == "linux" { - return ".so" - } - return ".dylib" -} diff --git a/_xtool/internal/symbol/symbol_test.go b/_xtool/internal/symbol/symbol_test.go deleted file mode 100644 index 5a31e25ba..000000000 --- a/_xtool/internal/symbol/symbol_test.go +++ /dev/null @@ -1,99 +0,0 @@ -package symbol - -import ( - "os" - "path/filepath" - "runtime" - "testing" -) - -func TestFindLibFile(t *testing.T) { - tempDir, err := os.MkdirTemp("", "symbol_test") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDir) - - tests := []struct { - name string - libName string - mode Mode - shouldCreate bool - expectError bool - }{ - { - name: "find dynamic", - libName: "test", - mode: ModeDynamic, - shouldCreate: true, - expectError: false, - }, - { - name: "find static", - libName: "test", - mode: ModeStatic, - shouldCreate: true, - expectError: false, - }, - { - name: "library not found - dynamic", - libName: "nonexistent", - mode: ModeDynamic, - shouldCreate: false, - expectError: true, - }, - { - name: "library not found - static", - libName: "nonexistent", - mode: ModeStatic, - shouldCreate: false, - expectError: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var expectedFileName string - var expectFilePath string - - // Dynamically create test library file if needed - if tt.shouldCreate { - var extension string - if tt.mode == ModeStatic { - extension = ".a" - } else { - if runtime.GOOS == "linux" { - extension = ".so" - } else { - extension = ".dylib" - } - } - - expectedFileName = "lib" + tt.libName + extension - expectFilePath = filepath.Join(tempDir, expectedFileName) - - file, err := os.Create(expectFilePath) - if err != nil { - t.Fatalf("Failed to create test file %s: %v", expectFilePath, err) - } - file.Close() - defer os.Remove(expectFilePath) - } - - result, err := FindLibFile(tempDir, tt.libName, tt.mode) - - if tt.expectError { - if err == nil { - t.Fatal("expected error, but got nil") - } - } else { - if err != nil { - t.Fatal(err) - } - if result != expectFilePath { - t.Errorf("Expected %s, got %s", expectedFileName, result) - } - } - }) - } -} diff --git a/cl/compile.go b/cl/compile.go new file mode 100644 index 000000000..e359794f4 --- /dev/null +++ b/cl/compile.go @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cl + +import ( + "go/types" + + "github.com/goplus/gogen" + "github.com/goplus/lib/c" + "github.com/goplus/llcppg/clang" +) + +// ----------------------------------------------------------------------------- + +type PkgInfo struct { +} + +// Package represents a generated Go package. +type Package struct { + *gogen.Package + pi *PkgInfo +} + +// Reused specifies to reuse the Package instance between processing multiple C/C++ header files. +type Reused struct { + pkg Package +} + +// ----------------------------------------------------------------------------- + +// Config specifies the configuration for compiling C/C++ header files. +type Config struct { + // An Importer resolves import paths to Packages. + Importer types.Importer + + // Include specifies include searching directories. + Include []string + + // Reused specifies to reuse the Package instance between processing multiple C/C++ header files. + *Reused +} + +// ----------------------------------------------------------------------------- + +// Source represents a C/C++ header to compile. +type Source struct { + clang.TranslationUnit + PresumedFile *c.Char +} + +// ----------------------------------------------------------------------------- + +// NewPackage creates a new Package instance for the specified package path and name, using +// the provided Source and Config. +func NewPackage(pkgPath, pkgName string, src Source, conf *Config) (pkg Package, err error) { + panic("todo") +} + +// ----------------------------------------------------------------------------- diff --git a/go.mod b/go.mod index 50d9fda11..9556fc18f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/goplus/llcppg go 1.27.0 -require github.com/goplus/lib v0.5.2 +require ( + github.com/goplus/gogen v1.23.5 + github.com/goplus/lib v0.5.2 +) diff --git a/go.sum b/go.sum index 8fad59c37..bd55ec317 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/goplus/gogen v1.23.5 h1:76w3zmAHI+ECI7bPr0enUd0du9+t1IYyXmp43CbIpSs= +github.com/goplus/gogen v1.23.5/go.mod h1:Y7ulYW3wonQ3d9er00b0uGFEV/IUZa6okWJZh892ACQ= github.com/goplus/lib v0.5.2 h1:BUd3mUwTajDRBHVxMfS/y/hDJ6n/Pxwf6z7ikrOXvkE= github.com/goplus/lib v0.5.2/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=