-
Notifications
You must be signed in to change notification settings - Fork 11
feat(internal/config): reduce calling libclang mutiple times #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MeteorsLiu
wants to merge
44
commits into
goplus:main
Choose a base branch
from
MeteorsLiu:config/optimize-pkghfile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+901
−34
Open
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
253ae7c
feat: reduce calling libclang mutiple time
MeteorsLiu 19846fb
test: add commondir test case 3
MeteorsLiu bcc3e2e
test: remove test case 3
MeteorsLiu ec3dfe8
fix: use MM to retrieve interface
MeteorsLiu 1a3a4f4
fix: mm output parse
MeteorsLiu 4c9ccfa
fix: mm output parse
MeteorsLiu 277bc64
add test
MeteorsLiu f22a90d
add test
MeteorsLiu 2f4085b
add test
MeteorsLiu 8b8b9eb
add test
MeteorsLiu 39ee6de
add test
MeteorsLiu 0494289
add test
MeteorsLiu aa853a5
add test
MeteorsLiu 2658889
add test
MeteorsLiu 8ae3077
add test
MeteorsLiu fbcdd58
add test
MeteorsLiu 726df0c
add test
MeteorsLiu 1100394
add test
MeteorsLiu 7844d98
add test
MeteorsLiu ba356a3
feat: implement trie for longest common prefix
MeteorsLiu 9cac662
test: add more abs tests
MeteorsLiu e25406b
chore: remove println
MeteorsLiu 1c1c16f
test: fix test
MeteorsLiu 3cf470f
Merge branch 'main' of https://github.com/goplus/llcppg into trie
MeteorsLiu bc22861
merge
MeteorsLiu c8484de
chore: fix namespace
MeteorsLiu 1522a10
merge
MeteorsLiu 3d303d1
Merge branch 'trie' into config/optimize-pkghfile
MeteorsLiu e6de029
fix: change contains logic
MeteorsLiu f571c18
Merge branch 'trie' into config/optimize-pkghfile
MeteorsLiu cc75254
fix: use trie to filter out non-interface header files
MeteorsLiu 3776c37
fix: contains logic
MeteorsLiu 690b3f8
test: add more tests
MeteorsLiu 8f81a2b
chore: rename Contains
MeteorsLiu 1102c98
test: remove duplicated test
MeteorsLiu d429ac9
Merge branch 'trie' into config/optimize-pkghfile
MeteorsLiu a766abb
feat: use trie to replace commonDir
MeteorsLiu 1315fe9
add test
MeteorsLiu 7a11f7c
add test
MeteorsLiu 3780339
feat: use DFS to scan the longest common prefix
MeteorsLiu bad7b23
Merge branch 'trie' into config/optimize-pkghfile
MeteorsLiu c0263ed
chore: rename IsSubset
MeteorsLiu 2ad3a37
Merge branch 'trie' into config/optimize-pkghfile
MeteorsLiu 8c25072
fix: trie name
MeteorsLiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,16 @@ package header_test | |
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/goplus/lib/c/clang" | ||
| clangutils "github.com/goplus/llcppg/_xtool/internal/clang" | ||
| "github.com/goplus/llcppg/_xtool/internal/clangtool" | ||
| "github.com/goplus/llcppg/_xtool/internal/header" | ||
| llconfig "github.com/goplus/llcppg/config" | ||
| ) | ||
|
|
@@ -73,3 +78,173 @@ func TestPkgHfileInfo(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestLongestPrefix(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| strs []string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "empty string 1", | ||
| strs: []string{}, | ||
| want: "", | ||
| }, | ||
| { | ||
| name: "empty string 2", | ||
| strs: []string{"", ""}, | ||
| want: ".", | ||
| }, | ||
| { | ||
| name: "one empty string(b)", | ||
| strs: []string{"/a", ""}, | ||
| want: "", | ||
| }, | ||
| { | ||
| name: "one empty string(a)", | ||
| strs: []string{"", "/a"}, | ||
|
|
||
| want: "", | ||
| }, | ||
| // FIXME: substring bug | ||
| // { | ||
| // name: "b is substring of a", | ||
| // strs: []string{"/usr/a/b", "/usr/a"}, | ||
| // want: "/usr/a", | ||
| // }, | ||
| // { | ||
| // name: "a is substring of b", | ||
| // strs: []string{"/usr/c", "/usr/c/b"}, | ||
| // want: "/usr/c", | ||
| // }, | ||
| { | ||
| name: "normal case 1", | ||
| strs: []string{"testdata/hfile/temp1.h", "testdata/thirdhfile/third.h"}, | ||
| want: "testdata", | ||
| }, | ||
| { | ||
| name: "normal case 2", | ||
| strs: []string{"testdata/hfile/temp1.h", "testdata/hfile/third.h"}, | ||
|
|
||
| want: "testdata/hfile", | ||
| }, | ||
| // FIXME: absolute path | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A Note is recommended to use "MARKER(uid): note body" format. Detailslint 解释这个lint结果提示在注释中使用“MARKER(uid): note body”格式是一个推荐的做法。这意味着在编写注释时,应该遵循这种特定的格式来提高代码的可读性和一致性。 错误用法以下是一个错误的示例,展示了不正确的注释格式: // 这是一个错误的注释格式正确用法以下是一个正确的示例,展示了符合推荐格式的注释: // MARKER(12345): 这是一个正确的注释格式
|
||
| // { | ||
| // name: "normal case 3", | ||
| // strs: []string{"/opt/homebrew/Cellar/cjson/1.7.18/include/cJSON/cJSON.h", "/opt/homebrew/Cellar/cjson/1.7.18/include/cJSON.h", "/opt/homebrew/Cellar/cjson/1.7.18/include/zlib/zlib.h"}, | ||
| // want: "/opt/homebrew/Cellar/cjson/1.7.18/include", | ||
| // }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if got := header.CommonParentDir(tc.strs); got != tc.want { | ||
| t.Fatalf("unexpected longest prefix: want %s got %s", tc.want, got) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func benchmarkFn(fn func()) time.Duration { | ||
| now := time.Now() | ||
|
|
||
| fn() | ||
|
|
||
| return time.Since(now) | ||
| } | ||
|
|
||
| func TestBenchmarkPkgHfileInfo(t *testing.T) { | ||
| include := []string{"temp1.h", "temp2.h"} | ||
| cflags := []string{"-I./testdata/hfile", "-I./testdata/thirdhfile"} | ||
| t1 := benchmarkFn(func() { | ||
| for i := 0; i < 100; i++ { | ||
| pkgHfileInfo(include, cflags, false) | ||
| } | ||
| }) | ||
|
|
||
| t2 := benchmarkFn(func() { | ||
| for i := 0; i < 100; i++ { | ||
| header.PkgHfileInfo(include, cflags, false) | ||
| } | ||
| }) | ||
|
|
||
| fmt.Println("old PkgHfileInfo elapsed: ", t1, "new PkgHfileInfo elasped: ", t2) | ||
| } | ||
|
|
||
| func pkgHfileInfo(includes []string, args []string, mix bool) *header.PkgHfilesInfo { | ||
| info := &header.PkgHfilesInfo{ | ||
| Inters: []string{}, | ||
| Impls: []string{}, | ||
| Thirds: []string{}, | ||
| } | ||
| outfile, err := os.CreateTemp("", "compose_*.h") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer os.Remove(outfile.Name()) | ||
|
|
||
| inters := make(map[string]struct{}) | ||
| others := []string{} // impl & third | ||
| for _, f := range includes { | ||
| content := "#include <" + f + ">" | ||
| index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{ | ||
| File: content, | ||
| Temp: true, | ||
| Args: args, | ||
| }) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| clangutils.GetInclusions(unit, func(inced clang.File, incins []clang.SourceLocation) { | ||
| if len(incins) == 1 { | ||
| filename := filepath.Clean(clang.GoString(inced.FileName())) | ||
| info.Inters = append(info.Inters, filename) | ||
| inters[filename] = struct{}{} | ||
| } | ||
| }) | ||
| unit.Dispose() | ||
| index.Dispose() | ||
| } | ||
|
|
||
| clangtool.ComposeIncludes(includes, outfile.Name()) | ||
| index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{ | ||
| File: outfile.Name(), | ||
| Temp: false, | ||
| Args: args, | ||
| }) | ||
| defer unit.Dispose() | ||
| defer index.Dispose() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| clangutils.GetInclusions(unit, func(inced clang.File, incins []clang.SourceLocation) { | ||
| // not in the first level include maybe impl or third hfile | ||
| filename := filepath.Clean(clang.GoString(inced.FileName())) | ||
| _, inter := inters[filename] | ||
| if len(incins) > 1 && !inter { | ||
| others = append(others, filename) | ||
| } | ||
| }) | ||
|
|
||
| if mix { | ||
| info.Thirds = others | ||
| return info | ||
| } | ||
|
|
||
| root, err := filepath.Abs(header.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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| #include "tempimpl.h" | ||
| #include "temp2.h" | ||
| #include <third.h> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A Note is recommended to use "MARKER(uid): note body" format.
Details
lint 解释
这个lint结果提示在注释中使用“MARKER(uid): note body”格式是一个建议。这意味着在编写注释时,应该遵循这种特定的格式来提高代码的可读性和一致性。
错误用法
以下是一个错误的示例,展示了不正确的注释格式:
// 这是一个错误的注释格式正确用法
以下是一个正确的示例,展示了符合建议的注释格式:
// MARKER(12345): 这是一个正确的注释格式