Skip to content

Commit

Permalink
Access Routine Feature (#25)
Browse files Browse the repository at this point in the history
* Added has_access function

* Fix false positve with local scope

* Fix lint
  • Loading branch information
shivasurya authored May 18, 2024
1 parent e7f4fca commit 6129d26
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
47 changes: 36 additions & 11 deletions sourcecode-parser/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil" //nolint:all
"log"
"os"
"path/filepath"
"strings"

sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/java"
//nolint:all
)

type GraphNode struct {
Expand All @@ -34,6 +34,8 @@ type GraphNode struct {
DataType string
Scope string
VariableValue string
hasAccess bool
File string
}

type GraphEdge struct {
Expand Down Expand Up @@ -87,7 +89,27 @@ func extractVisibilityModifier(modifiers string) string {
return "" // return an empty string if no visibility modifier is found
}

func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, currentContext *GraphNode) {
func hasAccess(node *sitter.Node, variableName string, sourceCode []byte) bool {
if node == nil {
return false
}
if node.Type() == "identifier" && node.Content(sourceCode) == variableName {
return true
}

// Recursively check all children of the current node
for i := 0; i < int(node.ChildCount()); i++ {
childNode := node.Child(i)
if hasAccess(childNode, variableName, sourceCode) {
return true
}
}

// Continue checking in the next sibling
return hasAccess(node.NextSibling(), variableName, sourceCode)
}

func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, currentContext *GraphNode, file string) {
switch node.Type() {
case "method_declaration":
methodName, methodID := extractMethodName(node, sourceCode)
Expand Down Expand Up @@ -204,12 +226,8 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
variableType := ""
variableModifier := ""
variableValue := ""
hasAccessValue := false
var scope string
if node.Type() == "local_variable_declaration" {
scope = "local"
} else {
scope = "field"
}
for i := 0; i < int(node.ChildCount()); i++ {
child := node.Child(i)
switch child.Type() {
Expand Down Expand Up @@ -239,6 +257,12 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
variableType = child.Content(sourceCode)
}
}
if node.Type() == "local_variable_declaration" {
scope = "local"
hasAccessValue = hasAccess(node.NextSibling(), variableName, sourceCode)
} else {
scope = "field"
}
// Create a new node for the variable
variableNode := &GraphNode{
ID: generateMethodID(variableName, []string{}),
Expand All @@ -250,14 +274,16 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
DataType: variableType,
Scope: scope,
VariableValue: variableValue,
hasAccess: hasAccessValue,
File: file,
}
graph.AddNode(variableNode)
}

// Recursively process child nodes
for i := 0; i < int(node.ChildCount()); i++ {
child := node.Child(i)
buildGraphFromAST(child, sourceCode, graph, currentContext)
buildGraphFromAST(child, sourceCode, graph, currentContext, file)
}
}

Expand Down Expand Up @@ -341,7 +367,7 @@ func getFiles(directory string) ([]string, error) {
}

func readFile(path string) ([]byte, error) {
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -381,14 +407,13 @@ func Initialize(directory string) *CodeGraph {

rootNode := tree.RootNode()

buildGraphFromAST(rootNode, sourceCode, codeGraph, nil)
buildGraphFromAST(rootNode, sourceCode, codeGraph, nil, file)
}
//nolint:all
// log.Println("Graph built successfully:", codeGraph)
log.Println("Graph built successfully")
//nolint:all
// go StartServer(codeGraph)

// select {}
return codeGraph
}
3 changes: 3 additions & 0 deletions sourcecode-parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func processQuery(input string, graph *CodeGraph, output string) (string, error)
var result strings.Builder
result.WriteString("------Query Results------\n")
for _, entity := range entities {
result.WriteString("-------------------\n")
result.WriteString(entity.CodeSnippet + "\n")
result.WriteString(entity.File + "\n")
result.WriteString("-------------------\n")
}
result.WriteString("-------------------\n")
return result.String(), nil
Expand Down
19 changes: 5 additions & 14 deletions sourcecode-parser/source_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ import (
"queryparser"
)

type SourceSinkPath struct {
Source *GraphNode
Sink *GraphNode
}

var MethodAttribute = map[string]int{
"name": 0,
"visibility": 1,
"returntype": 2,
"argumentype": 3,
"argumentname": 4,
// Add more attributes as needed
}

type Result struct {
IsConnected bool `json:"is_connected"`
SourceMethod string `json:"source_method"`
Expand Down Expand Up @@ -124,6 +110,11 @@ func (gnc *GraphNodeContext) GetValue(key, val string) string {
return gnc.Node.VariableValue
case "variabledatatype":
return gnc.Node.DataType
case "has_access":
if gnc.Node.hasAccess {
return "true"
}
return "false"
default:
fmt.Printf("Unsupported attribute key: %s\n", key)
return ""
Expand Down

0 comments on commit 6129d26

Please sign in to comment.