-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:ChrisTimperley/Kaskara
- Loading branch information
Showing
12 changed files
with
517 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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 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,46 @@ | ||
# -*- coding: utf-8 -*- | ||
__all__ = ('SpoonStatement',) | ||
|
||
from typing import Any, FrozenSet, Mapping, Optional | ||
|
||
import attr | ||
|
||
from ..core import FileLocationRange | ||
from ..functions import Function | ||
from ..statements import Statement | ||
|
||
|
||
@attr.s(frozen=True, slots=True, auto_attribs=True) | ||
class SpoonFunction(Function): | ||
name: str | ||
location: FileLocationRange | ||
body_location: FileLocationRange | ||
return_type: str | ||
|
||
@staticmethod | ||
def from_dict(dict_: Mapping[str, Any]) -> 'SpoonFunction': | ||
name: str = dict_['name'] | ||
location = FileLocationRange.from_string(dict_['location']) | ||
body_location = FileLocationRange.from_string(dict_['body']) | ||
return_type = dict_['return-type'] | ||
return SpoonFunction(name, location, body_location, return_type) | ||
|
||
|
||
@attr.s(frozen=True, auto_attribs=True, slots=True) | ||
class SpoonStatement(Statement): | ||
kind: str | ||
content: str | ||
canonical: str | ||
location: FileLocationRange | ||
|
||
@staticmethod | ||
def from_dict(dict_: Mapping[str, Any]) -> 'SpoonStatement': | ||
kind: str = dict_['kind'] | ||
content: str = dict_['source'] | ||
canonical: str = dict_['canonical'] | ||
location = FileLocationRange.from_string(dict_['location']) | ||
return SpoonStatement(kind, content, canonical, location) | ||
|
||
@property | ||
def visible(self) -> Optional[FrozenSet[str]]: | ||
return None |
59 changes: 59 additions & 0 deletions
59
lib/kaskara/spoon/backend/src/main/java/christimperley/kaskara/Function.java
This file contains 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,59 @@ | ||
package christimperley.kaskara; | ||
|
||
import com.fasterxml.jackson.annotation.JsonGetter; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import spoon.reflect.cu.SourcePosition; | ||
import spoon.reflect.declaration.CtMethod; | ||
import spoon.reflect.reference.CtTypeReference; | ||
|
||
/** | ||
* Describes a function within a given project. | ||
*/ | ||
public class Function { | ||
@JsonProperty("name") | ||
private final String name; | ||
@JsonSerialize(converter = SourcePositionSerializer.class) | ||
@JsonProperty("location") | ||
private final SourcePosition location; | ||
@JsonSerialize(converter = SourcePositionSerializer.class) | ||
@JsonProperty("body") | ||
private final SourcePosition bodyLocation; | ||
private final CtTypeReference<?> returnType; | ||
|
||
/** | ||
* Constructs a function description for a given Clang AST method element. | ||
* @param element The AST element for the method. | ||
* @return A description of the given AST element. | ||
*/ | ||
public static Function forSpoonMethod(CtMethod<?> element) { | ||
var name = element.getSimpleName(); | ||
var location = element.getPosition(); | ||
var body = element.getBody(); | ||
var bodyLocation = body.getPosition(); | ||
var returnType = element.getType(); | ||
return new Function(name, location, bodyLocation, returnType); | ||
} | ||
|
||
/** | ||
* Constructs a function description. | ||
* @param name The name of the function. | ||
* @param location The location of the function definition. | ||
* @param bodyLocation The location of the body of the function definition. | ||
* @param returnType The return type of the function. | ||
*/ | ||
public Function(String name, | ||
SourcePosition location, | ||
SourcePosition bodyLocation, | ||
CtTypeReference<?> returnType) { | ||
this.name = name; | ||
this.location = location; | ||
this.bodyLocation = bodyLocation; | ||
this.returnType = returnType; | ||
} | ||
|
||
@JsonGetter("return-type") | ||
public String getReturnType() { | ||
return this.returnType.getQualifiedName(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
lib/kaskara/spoon/backend/src/main/java/christimperley/kaskara/FunctionFinder.java
This file contains 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,46 @@ | ||
package christimperley.kaskara; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import spoon.reflect.declaration.CtMethod; | ||
import spoon.reflect.visitor.filter.AbstractFilter; | ||
|
||
/** | ||
* Provides an interface for finding all function definitions within a given project. | ||
*/ | ||
public class FunctionFinder { | ||
private final Project project; | ||
|
||
public static FunctionFinder forProject(Project project) { | ||
return new FunctionFinder(project); | ||
} | ||
|
||
protected FunctionFinder(Project project) { | ||
this.project = project; | ||
} | ||
|
||
/** | ||
* Finds all function declarations within the associated project. | ||
* @return A list of all functions within the associated project. | ||
*/ | ||
public List<Function> find() { | ||
var elements = this.project.getModel().getElements(new AbstractFilter<CtMethod>() { | ||
@Override | ||
public boolean matches(CtMethod element) { | ||
// function must have body | ||
if (element.getBody() == null) { | ||
return false; | ||
} | ||
// function must appear in file | ||
return element.getPosition().isValidPosition(); | ||
} | ||
}); | ||
|
||
List<Function> functions = new ArrayList<>(); | ||
for (var element : elements) { | ||
var function = Function.forSpoonMethod(element); | ||
functions.add(function); | ||
} | ||
return functions; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
lib/kaskara/spoon/backend/src/main/java/christimperley/kaskara/Loop.java
This file contains 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,29 @@ | ||
package christimperley.kaskara; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import spoon.reflect.code.CtLoop; | ||
import spoon.reflect.cu.SourcePosition; | ||
|
||
/** | ||
* Describes a control-flow loop within a given project. | ||
*/ | ||
public class Loop { | ||
@JsonSerialize(converter = SourcePositionSerializer.class) | ||
@JsonProperty("body") | ||
private final SourcePosition bodyLocation; | ||
|
||
/** | ||
* Constructs a description for a given Clang AST loop element. | ||
* @param element The AST element for the loop. | ||
* @return A description of the given AST element. | ||
*/ | ||
public static Loop forSpoonLoop(CtLoop element) { | ||
var bodyLocation = element.getBody().getPosition(); | ||
return new Loop(bodyLocation); | ||
} | ||
|
||
protected Loop(SourcePosition bodyLocation) { | ||
this.bodyLocation = bodyLocation; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
lib/kaskara/spoon/backend/src/main/java/christimperley/kaskara/LoopFinder.java
This file contains 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,45 @@ | ||
package christimperley.kaskara; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import spoon.reflect.code.CtLoop; | ||
import spoon.reflect.visitor.filter.AbstractFilter; | ||
|
||
/** | ||
* Provides an interface for finding all loops within a given project. | ||
*/ | ||
public class LoopFinder { | ||
private final Project project; | ||
|
||
public static LoopFinder forProject(Project project) { | ||
return new LoopFinder(project); | ||
} | ||
|
||
protected LoopFinder(Project project) { | ||
this.project = project; | ||
} | ||
|
||
/** | ||
* Finds all loops within the associated project. | ||
* @return A list of all loops within the associated project. | ||
*/ | ||
public List<Loop> find() { | ||
var elements = this.project.getModel().getElements(new AbstractFilter<CtLoop>() { | ||
@Override | ||
public boolean matches(CtLoop element) { | ||
// loop must have body | ||
if (element.getBody() == null) { | ||
return false; | ||
} | ||
// loop must appear in file | ||
return element.getPosition().isValidPosition(); | ||
} | ||
}); | ||
|
||
List<Loop> loops = new ArrayList<>(); | ||
for (var element : elements) { | ||
loops.add(Loop.forSpoonLoop(element)); | ||
} | ||
return loops; | ||
} | ||
} |
Oops, something went wrong.