Skip to content
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

Using node builder functions for Go language frontend #968

Merged
merged 5 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ logs
*.so
*.log

gradle.properties
Empty file modified configure_frontends.sh
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ class ExpressionHandler(lang: CXXLanguageFrontend) :
}

return newMemberExpression(
ctx.fieldName.toString(),
base,
UnknownType.getUnknownType(language),
ctx.fieldName.toString(),
if (ctx.isPointerDereference) "->" else ".",
ctx.rawSignature
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ public RecordDeclaration handleClassOrInterfaceDeclaration(
null,
null,
null,
null,
false);
field.setImplicit(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@ private DeclarationStatement handleVariableDeclarationExpr(Expression expr) {
MemberExpression memberExpression =
newMemberExpression(
this,
fieldAccessExpr.getName().getIdentifier(),
base,
fieldType,
fieldAccessExpr.getName().getIdentifier(),
".", // there is only "." in java
fieldAccessExpr.toString());
"." // there is only "." in java
);
memberExpression.setStaticAccess(true);
return memberExpression;
}
Expand All @@ -395,12 +395,7 @@ private DeclarationStatement handleVariableDeclarationExpr(Expression expr) {
}

return newMemberExpression(
this,
base,
fieldType,
fieldAccessExpr.getName().getIdentifier(),
".",
fieldAccessExpr.toString());
this, fieldAccessExpr.getName().getIdentifier(), base, fieldType, ".");
}

private Literal handleLiteralExpression(Expression expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import de.fraunhofer.aisec.cpg.graph.declarations.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.ArrayCreationExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression
import de.fraunhofer.aisec.cpg.graph.types.Type
import de.fraunhofer.aisec.cpg.graph.types.UnknownType
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation

/**
Expand Down Expand Up @@ -87,8 +88,8 @@ fun MetadataProvider.newFunctionDeclaration(
fun MetadataProvider.newMethodDeclaration(
name: String?,
code: String? = null,
isStatic: Boolean,
recordDeclaration: RecordDeclaration?,
isStatic: Boolean = false,
recordDeclaration: RecordDeclaration? = null,
rawNode: Any? = null
): MethodDeclaration {
val node = MethodDeclaration()
Expand Down Expand Up @@ -134,8 +135,8 @@ fun MetadataProvider.newConstructorDeclaration(
@JvmOverloads
fun MetadataProvider.newParamVariableDeclaration(
name: String?,
type: Type?,
variadic: Boolean,
type: Type? = UnknownType.getUnknownType(),
variadic: Boolean = false,
code: String? = null,
rawNode: Any? = null
): ParamVariableDeclaration {
Expand All @@ -159,9 +160,9 @@ fun MetadataProvider.newParamVariableDeclaration(
@JvmOverloads
fun MetadataProvider.newVariableDeclaration(
name: String?,
type: Type?,
type: Type? = UnknownType.getUnknownType(),
code: String? = null,
implicitInitializerAllowed: Boolean,
implicitInitializerAllowed: Boolean = false,
rawNode: Any? = null
): VariableDeclaration {
val node = VariableDeclaration()
Expand Down Expand Up @@ -340,12 +341,12 @@ fun MetadataProvider.newEnumConstantDeclaration(
@JvmOverloads
fun MetadataProvider.newFieldDeclaration(
name: String?,
type: Type?,
modifiers: List<String?>?,
type: Type? = UnknownType.getUnknownType(),
modifiers: List<String?>? = listOf(),
code: String? = null,
location: PhysicalLocation?,
initializer: Expression?,
implicitInitializerAllowed: Boolean,
location: PhysicalLocation? = null,
initializer: Expression? = null,
implicitInitializerAllowed: Boolean = false,
rawNode: Any? = null
): FieldDeclaration {
val node = FieldDeclaration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import de.fraunhofer.aisec.cpg.graph.types.UnknownType
@JvmOverloads
fun <T> MetadataProvider.newLiteral(
value: T,
type: Type?,
type: Type? = UnknownType.getUnknownType(),
code: String? = null,
rawNode: Any? = null,
): Literal<T> {
Expand Down Expand Up @@ -114,7 +114,7 @@ fun MetadataProvider.newUnaryOperator(
@JvmOverloads
fun MetadataProvider.newNewExpression(
code: String? = null,
type: Type?,
type: Type? = UnknownType.getUnknownType(),
rawNode: Any? = null
): NewExpression {
val node = NewExpression()
Expand Down Expand Up @@ -179,8 +179,8 @@ fun MetadataProvider.newConditionalExpression(
*/
@JvmOverloads
fun MetadataProvider.newKeyValueExpression(
key: Expression?,
value: Expression?,
key: Expression? = null,
value: Expression? = null,
code: String? = null,
rawNode: Any? = null
): KeyValueExpression {
Expand Down Expand Up @@ -238,10 +238,10 @@ fun MetadataProvider.newCompoundStatementExpression(
*/
@JvmOverloads
fun MetadataProvider.newCallExpression(
callee: Expression?,
fqn: String?,
callee: Expression? = null,
fqn: String? = null,
code: String? = null,
template: Boolean,
template: Boolean = false,
rawNode: Any? = null
): CallExpression {
val node = CallExpression()
Expand Down Expand Up @@ -288,10 +288,10 @@ fun MetadataProvider.newMemberCallExpression(
fqn: String?,
base: Expression?,
member: Node?,
operatorCode: String?,
operatorCode: String? = ".",
code: String? = null,
rawNode: Any? = null
): CallExpression {
): MemberCallExpression {
val node = MemberCallExpression()
node.applyMetadata(this, rawNode, code)

Expand All @@ -313,10 +313,10 @@ fun MetadataProvider.newMemberCallExpression(
*/
@JvmOverloads
fun MetadataProvider.newMemberExpression(
base: Expression,
memberType: Type?,
name: String?,
operatorCode: String?,
base: Expression,
memberType: Type? = UnknownType.getUnknownType(),
operatorCode: String? = ".",
code: String? = null,
rawNode: Any? = null
): MemberExpression {
Expand Down
12 changes: 10 additions & 2 deletions cpg-language-go/src/main/golang/basic_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,21 @@ func NewString(s string) *jnigi.ObjectRef {
return o
}

func NewBoolean(b bool) *jnigi.ObjectRef {
// TODO: Use Boolean.valueOf
o, err := env.NewObject("java/lang/Boolean", b)
if err != nil {
log.Fatal(err)
}

return o
}

func NewInteger(i int) *jnigi.ObjectRef {
// TODO: Use Integer.valueOf
o, err := env.NewObject("java/lang/Integer", i)
if err != nil {
log.Fatal(err)

}

return o
Expand All @@ -57,7 +66,6 @@ func NewDouble(d float64) *jnigi.ObjectRef {
o, err := env.NewObject("java/lang/Double", d)
if err != nil {
log.Fatal(err)

}

return o
Expand Down
24 changes: 15 additions & 9 deletions cpg-language-go/src/main/golang/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"tekao.net/jnigi"
)

type Declaration jnigi.ObjectRef
type Declaration Node
type IncludeDeclaration jnigi.ObjectRef
type TranslationUnitDeclaration Declaration
type FunctionDeclaration Declaration
Expand All @@ -43,6 +43,14 @@ type VariableDeclaration Declaration
type ParamVariableDeclaration Declaration
type NamespaceDeclaration Declaration

const DeclarationsPackage = GraphPackage + "/declarations"
const DeclarationClass = DeclarationsPackage + "/Declaration"
const RecordDeclarationClass = DeclarationsPackage + "/RecordDeclaration"
const FunctionDeclarationClass = DeclarationsPackage + "/FunctionDeclaration"
const VariableDeclarationClass = DeclarationsPackage + "/VariableDeclaration"
const IncludeDeclarationClass = DeclarationsPackage + "/IncludeDeclaration"
const TranslationUnitDeclarationClass = DeclarationsPackage + "/TranslationUnitDeclaration"

func (n *NamespaceDeclaration) SetName(s string) error {
return (*Node)(n).SetName(s)
}
Expand Down Expand Up @@ -71,9 +79,7 @@ func (f *FunctionDeclaration) SetReturnTypes(types []*Type) (err error) {
return err
}

// Stupid workaround, since casting does not work. See
// https://github.com/timob/jnigi/issues/60
var funcDecl = jnigi.WrapJObject(uintptr((*jnigi.ObjectRef)(f).JObject()), "de/fraunhofer/aisec/cpg/graph/declarations/FunctionDeclaration", false)
var funcDecl = (*jnigi.ObjectRef)(f).Cast(FunctionDeclarationClass)

err = (*jnigi.ObjectRef)(funcDecl).CallMethod(env, "setReturnTypes", nil, list.Cast("java/util/List"))

Expand All @@ -85,7 +91,7 @@ func (f *FunctionDeclaration) AddParameter(p *ParamVariableDeclaration) {
}

func (f *FunctionDeclaration) SetBody(s *Statement) (err error) {
err = (*jnigi.ObjectRef)(f).CallMethod(env, "setBody", nil, (*jnigi.ObjectRef)(s).Cast("de/fraunhofer/aisec/cpg/graph/statements/Statement"))
err = (*jnigi.ObjectRef)(f).CallMethod(env, "setBody", nil, (*jnigi.ObjectRef)(s).Cast(StatementClass))

return
}
Expand All @@ -103,7 +109,7 @@ func (m *MethodDeclaration) SetReceiver(v *VariableDeclaration) error {
}

func (m *MethodDeclaration) GetReceiver() *VariableDeclaration {
o := jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/declarations/VariableDeclaration")
o := jnigi.NewObjectRef(VariableDeclarationClass)
err := (*jnigi.ObjectRef)(m).GetField(env, "receiver", o)

if err != nil {
Expand Down Expand Up @@ -143,7 +149,7 @@ func (v *VariableDeclaration) IsNil() bool {
}

func (v *VariableDeclaration) SetInitializer(e *Expression) (err error) {
err = (*jnigi.ObjectRef)(v).CallMethod(env, "setInitializer", nil, (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression"))
err = (*jnigi.ObjectRef)(v).CallMethod(env, "setInitializer", nil, (*jnigi.ObjectRef)(e).Cast(ExpressionClass))

return
}
Expand All @@ -153,7 +159,7 @@ func (v *VariableDeclaration) Declaration() *Declaration {
}

func (t *TranslationUnitDeclaration) GetIncludeByName(s string) *IncludeDeclaration {
var i = jnigi.NewObjectRef("de/fraunhofer/aisec/cpg/graph/declarations/IncludeDeclaration")
var i = jnigi.NewObjectRef(IncludeDeclarationClass)
err := (*jnigi.ObjectRef)(t).CallMethod(env, "getIncludeByName", i, NewString(s))
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -196,5 +202,5 @@ func (r *CompoundStatement) IsNil() bool {
}

func (c *CaseStatement) SetCaseExpression(e *Expression) error {
return (*jnigi.ObjectRef)(c).SetField(env, "caseExpression", (*jnigi.ObjectRef)(e).Cast("de/fraunhofer/aisec/cpg/graph/statements/expressions/Expression"))
return (*jnigi.ObjectRef)(c).SetField(env, "caseExpression", (*jnigi.ObjectRef)(e).Cast(ExpressionClass))
}