generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: complete c parser #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7e80be5
fix: fix hardcode in export.go
Hoblovski ddca707
feat: cxx/c parser skeleton with clangd-18
Hoblovski 5ba1764
feat: initial support for cxx collect
Hoblovski 83847e7
fix: get json.Types and build graph
Hoblovski 0ef54ac
fix: fix comments
Hoblovski fe8fb67
test: duplicate c functions
Hoblovski 57af595
refactor: clang-format c tests
Hoblovski e44bd7c
fix: pkgpath -> file. skip build.
Hoblovski 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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2025 CloudWeGo Authors | ||
// | ||
// 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 | ||
// | ||
// https://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 cxx | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cloudwego/abcoder/lang/uniast" | ||
"github.com/cloudwego/abcoder/lang/utils" | ||
) | ||
|
||
const MaxWaitDuration = 5 * time.Minute | ||
|
||
func GetDefaultLSP() (lang uniast.Language, name string) { | ||
return uniast.Cxx, "clangd-18" | ||
} | ||
|
||
func CheckRepo(repo string) (string, time.Duration) { | ||
openfile := "" | ||
// TODO: check if the project compiles. | ||
|
||
// NOTICE: wait for Rust projects based on code files | ||
_, size := utils.CountFiles(repo, ".c", "build/") | ||
wait := 2*time.Second + time.Second*time.Duration(size/1024) | ||
if wait > MaxWaitDuration { | ||
wait = MaxWaitDuration | ||
} | ||
return openfile, wait | ||
} |
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 |
---|---|---|
@@ -0,0 +1,199 @@ | ||
// Copyright 2025 CloudWeGo Authors | ||
// | ||
// 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 | ||
// | ||
// https://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 cxx | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
lsp "github.com/cloudwego/abcoder/lang/lsp" | ||
"github.com/cloudwego/abcoder/lang/utils" | ||
) | ||
|
||
type CxxSpec struct { | ||
repo string | ||
} | ||
|
||
func NewCxxSpec() *CxxSpec { | ||
return &CxxSpec{} | ||
} | ||
|
||
// XXX: maybe multi module support for C++? | ||
func (c *CxxSpec) WorkSpace(root string) (map[string]string, error) { | ||
c.repo = root | ||
rets := map[string]string{} | ||
absPath, err := filepath.Abs(root) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get absolute path: %w", err) | ||
} | ||
rets["current"] = absPath | ||
return rets, nil | ||
} | ||
|
||
// returns: modname, pathpath, error | ||
// Multiple symbols with the same name could occur (for example in the Linux kernel). | ||
// The identify is mod::pkg::name. So we use the pkg (the file name) to distinguish them. | ||
func (c *CxxSpec) NameSpace(path string) (string, string, error) { | ||
AsterDY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// external lib: only standard library (system headers), in /usr/ | ||
if !strings.HasPrefix(path, c.repo) { | ||
if strings.HasPrefix(path, "/usr") { | ||
// assume it is c system library | ||
return "cstdlib", "cstdlib", nil | ||
} | ||
panic(fmt.Sprintf("external lib: %s\n", path)) | ||
} | ||
|
||
relpath, _ := filepath.Rel(c.repo, path) | ||
return "current", relpath, nil | ||
} | ||
|
||
func (c *CxxSpec) ShouldSkip(path string) bool { | ||
if strings.HasSuffix(path, ".c") || strings.HasSuffix(path, ".h") { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (c *CxxSpec) IsDocToken(tok lsp.Token) bool { | ||
return tok.Type == "comment" | ||
} | ||
|
||
func (c *CxxSpec) DeclareTokenOfSymbol(sym lsp.DocumentSymbol) int { | ||
for i, t := range sym.Tokens { | ||
if c.IsDocToken(t) { | ||
continue | ||
} | ||
for _, m := range t.Modifiers { | ||
if m == "declaration" { | ||
return i | ||
} | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func (c *CxxSpec) IsEntityToken(tok lsp.Token) bool { | ||
return tok.Type == "class" || tok.Type == "function" || tok.Type == "variable" | ||
} | ||
|
||
func (c *CxxSpec) IsStdToken(tok lsp.Token) bool { | ||
panic("TODO") | ||
} | ||
|
||
func (c *CxxSpec) TokenKind(tok lsp.Token) lsp.SymbolKind { | ||
switch tok.Type { | ||
case "class": | ||
return lsp.SKStruct | ||
case "enum": | ||
return lsp.SKEnum | ||
case "enumMember": | ||
return lsp.SKEnumMember | ||
case "function", "macro": | ||
return lsp.SKFunction | ||
// rust spec does not treat parameter as a variable | ||
case "parameter": | ||
return lsp.SKVariable | ||
case "typeParameter": | ||
return lsp.SKTypeParameter | ||
// type: TODO | ||
case "interface", "concept", "method", "modifier", "namespace", "type": | ||
panic(fmt.Sprintf("Unsupported token type: %s at %+v\n", tok.Type, tok.Location)) | ||
case "bracket", "comment", "label", "operator", "property", "unknown": | ||
return lsp.SKUnknown | ||
} | ||
panic(fmt.Sprintf("Weird token type: %s at %+v\n", tok.Type, tok.Location)) | ||
} | ||
|
||
func (c *CxxSpec) IsMainFunction(sym lsp.DocumentSymbol) bool { | ||
return sym.Kind == lsp.SKFunction && sym.Name == "main" | ||
} | ||
|
||
func (c *CxxSpec) IsEntitySymbol(sym lsp.DocumentSymbol) bool { | ||
typ := sym.Kind | ||
return typ == lsp.SKFunction || typ == lsp.SKVariable || typ == lsp.SKClass | ||
|
||
} | ||
|
||
func (c *CxxSpec) IsPublicSymbol(sym lsp.DocumentSymbol) bool { | ||
id := c.DeclareTokenOfSymbol(sym) | ||
if id == -1 { | ||
return false | ||
} | ||
for _, m := range sym.Tokens[id].Modifiers { | ||
if m == "globalScope" { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// TODO(cpp): support C++ OOP | ||
func (c *CxxSpec) HasImplSymbol() bool { | ||
return false | ||
} | ||
|
||
func (c *CxxSpec) ImplSymbol(sym lsp.DocumentSymbol) (int, int, int) { | ||
panic("TODO") | ||
} | ||
|
||
func (c *CxxSpec) FunctionSymbol(sym lsp.DocumentSymbol) (int, []int, []int, []int) { | ||
// No receiver and no type params for C | ||
if sym.Kind != lsp.SKFunction { | ||
return -1, nil, nil, nil | ||
} | ||
receiver := -1 | ||
typeParams := []int{} | ||
inputParams := []int{} | ||
outputs := []int{} | ||
|
||
// general format: RETURNVALUE NAME "(" PARAMS ")" BODY | ||
// -------- | ||
// fnNameText | ||
// state machine phase 0 phase 1 phase 2: break | ||
// TODO: attributes may contain parens. also inline structs. | ||
|
||
endRelOffset := 0 | ||
lines := utils.CountLinesCached(sym.Text) | ||
phase := 0 | ||
for i, tok := range sym.Tokens { | ||
switch phase { | ||
case 0: | ||
if tok.Type == "function" { | ||
offset := lsp.RelativePostionWithLines(*lines, sym.Location.Range.Start, tok.Location.Range.Start) | ||
endRelOffset = offset + strings.Index(sym.Text[offset:], ")") | ||
phase = 1 | ||
continue | ||
} | ||
if c.IsEntityToken(tok) { | ||
outputs = append(outputs, i) | ||
} | ||
case 1: | ||
offset := lsp.RelativePostionWithLines(*lines, sym.Location.Range.Start, tok.Location.Range.Start) | ||
if offset > endRelOffset { | ||
phase = 2 | ||
continue | ||
} | ||
if c.IsEntityToken(tok) { | ||
inputParams = append(inputParams, i) | ||
} | ||
} | ||
} | ||
return receiver, typeParams, inputParams, outputs | ||
} | ||
|
||
func (c *CxxSpec) GetUnloadedSymbol(from lsp.Token, define lsp.Location) (string, error) { | ||
panic("TODO") | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.