From 912bb17c23f829c3b4833ca734c656b515241c90 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Mon, 16 Oct 2023 20:06:23 +0200 Subject: [PATCH] More module stuff --- .../frontends/golang/GoLanguageFrontend.kt | 2 - .../cpg/frontends/golang/GoStandardLibrary.kt | 501 ++++-------------- .../aisec/cpg/frontends/golang/GoUtils.kt | 11 +- .../src/test/resources/golang/literal.go | 1 + 4 files changed, 123 insertions(+), 392 deletions(-) diff --git a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoLanguageFrontend.kt b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoLanguageFrontend.kt index 855bf41a0d3..b65fc149b9d 100644 --- a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoLanguageFrontend.kt +++ b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoLanguageFrontend.kt @@ -28,7 +28,6 @@ package de.fraunhofer.aisec.cpg.frontends.golang import de.fraunhofer.aisec.cpg.TranslationContext import de.fraunhofer.aisec.cpg.frontends.Language import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend -import de.fraunhofer.aisec.cpg.frontends.SupportsParallelParsing import de.fraunhofer.aisec.cpg.frontends.TranslationException import de.fraunhofer.aisec.cpg.frontends.golang.GoStandardLibrary.Modfile import de.fraunhofer.aisec.cpg.frontends.golang.GoStandardLibrary.Parser @@ -57,7 +56,6 @@ import java.net.URI * We make use of JNA to call a dynamic library which exports C function wrappers around the Go API. * This is needed because we cannot directly export Go structs and pointers to C. */ -@SupportsParallelParsing(false) @RegisterExtraPass(GoExtraPass::class) @ReplacePass( lang = GoLanguage::class, diff --git a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoStandardLibrary.kt b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoStandardLibrary.kt index 0fc6480d579..672cb2a729a 100644 --- a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoStandardLibrary.kt +++ b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoStandardLibrary.kt @@ -59,17 +59,11 @@ interface GoStandardLibrary : Library { object Modfile { class File(p: Pointer? = Pointer.NULL) : GoObject(p) { - val module: Module - get() { - return INSTANCE.modfileGetFileModule(this) - } + val module: Module by lazy { INSTANCE.modfileGetFileModule(this) } } class Module(p: Pointer? = Pointer.NULL) : GoObject(p) { - val mod: GoStandardLibrary.Module.Version - get() { - return INSTANCE.modfileGetModuleMod(this) - } + val mod: GoStandardLibrary.Module.Version by lazy { INSTANCE.modfileGetModuleMod(this) } } fun parse(file: String, bytes: String): File { @@ -80,10 +74,7 @@ interface GoStandardLibrary : Library { object Module { class Version(p: Pointer? = Pointer.NULL) : GoObject(p) { - val path: String - get() { - return INSTANCE.moduleGetVersionPath(this) - } + val path: String by lazy { INSTANCE.moduleGetVersionPath(this) } } } @@ -101,22 +92,15 @@ interface GoStandardLibrary : Library { */ interface Ast { open class Node(p: Pointer? = Pointer.NULL) : GoObject(p) { - val pos: Int - get() { - return INSTANCE.GetNodePos(this) - } + val pos: Int by lazy { INSTANCE.GetNodePos(this) } - val end: Int - get() { - return INSTANCE.GetNodeEnd(this) - } + val end: Int by lazy { INSTANCE.GetNodeEnd(this) } } class FieldList(p: Pointer? = Pointer.NULL) : Node(p) { - val list: List - get() { - return list(INSTANCE::GetNumFieldListList, INSTANCE::GetFieldListList) - } + val list: List by lazy { + list(INSTANCE::GetNumFieldListList, INSTANCE::GetFieldListList) + } } class Field(p: Pointer? = Pointer.NULL) : Node(p) { @@ -149,34 +133,21 @@ interface GoStandardLibrary : Library { } class GenDecl(p: Pointer? = Pointer.NULL) : Decl(p) { - val specs: List - get() { - return list(INSTANCE::GetNumGenDeclSpecs, INSTANCE::GetGenDeclSpec) - } + val specs: List by lazy { + list(INSTANCE::GetNumGenDeclSpecs, INSTANCE::GetGenDeclSpec) + } - val tok: Int - get() { - return INSTANCE.GetGenDeclTok(this) - } + val tok: Int by lazy { INSTANCE.GetGenDeclTok(this) } } class FuncDecl(p: Pointer? = Pointer.NULL) : Decl(p) { - val recv: FieldList? - get() { - return INSTANCE.GetFuncDeclRecv(this) - } + val recv: FieldList? by lazy { INSTANCE.GetFuncDeclRecv(this) } val type: FuncType by lazy { INSTANCE.GetFuncDeclType(this) } - val name: Ident - get() { - return INSTANCE.GetFuncDeclName(this) - } + val name: Ident by lazy { INSTANCE.GetFuncDeclName(this) } - val body: BlockStmt? - get() { - return INSTANCE.GetFuncDeclBody(this) - } + val body: BlockStmt? by lazy { INSTANCE.GetFuncDeclBody(this) } } open class Spec(p: Pointer? = Pointer.NULL) : Node(p) { @@ -195,32 +166,17 @@ interface GoStandardLibrary : Library { } class TypeSpec(p: Pointer? = Pointer.NULL) : Spec(p) { - val name: Ident - get() { - return INSTANCE.GetTypeSpecName(this) - } + val name: Ident by lazy { INSTANCE.GetTypeSpecName(this) } - val assign: Int - get() { - return INSTANCE.GetTypeSpecAssign(this) - } + val assign: Int by lazy { INSTANCE.GetTypeSpecAssign(this) } - val type: Expr - get() { - return INSTANCE.GetTypeSpecType(this) - } + val type: Expr by lazy { INSTANCE.GetTypeSpecType(this) } } class ImportSpec(p: Pointer? = Pointer.NULL) : Spec(p) { - val name: Ident? - get() { - return INSTANCE.GetImportSpecName(this) - } + val name: Ident? by lazy { INSTANCE.GetImportSpecName(this) } - val path: BasicLit - get() { - return INSTANCE.GetImportSpecPath(this) - } + val path: BasicLit by lazy { INSTANCE.GetImportSpecPath(this) } } class ValueSpec(p: Pointer? = Pointer.NULL) : Spec(p) { @@ -228,10 +184,7 @@ interface GoStandardLibrary : Library { list(INSTANCE::GetNumValueSpecNames, INSTANCE::GetValueSpecName) } - val type: Expr? - get() { - return INSTANCE.GetValueSpecType(this) - } + val type: Expr? by lazy { INSTANCE.GetValueSpecType(this) } val values: List by lazy { list(INSTANCE::GetNumValueSpecValues, INSTANCE::GetValueSpecValue) @@ -283,32 +236,17 @@ interface GoStandardLibrary : Library { STRING(9) } - val value: String - get() { - return INSTANCE.GetBasicLitValue(this) - } + val value: String by lazy { INSTANCE.GetBasicLitValue(this) } - val kind: Kind - get() { - return Kind.entries.first { it.i == INSTANCE.GetBasicLitKind(this) } - } + val kind: Kind by lazy { Kind.entries.first { it.i == INSTANCE.GetBasicLitKind(this) } } } class BinaryExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val x: Expr - get() { - return INSTANCE.GetBinaryExprX(this) - } + val x: Expr by lazy { INSTANCE.GetBinaryExprX(this) } - val opString: String - get() { - return INSTANCE.GetBinaryExprOpString(this) - } + val opString: String by lazy { INSTANCE.GetBinaryExprOpString(this) } - val y: Expr - get() { - return INSTANCE.GetBinaryExprY(this) - } + val y: Expr by lazy { INSTANCE.GetBinaryExprY(this) } } class CallExpr(p: Pointer? = Pointer.NULL) : Expr(p) { @@ -316,41 +254,25 @@ interface GoStandardLibrary : Library { list(INSTANCE::GetNumCallExprArgs, INSTANCE::GetCallExprArg) } - val `fun`: Expr - get() { - return INSTANCE.GetCallExprFun(this) - } + val `fun`: Expr by lazy { INSTANCE.GetCallExprFun(this) } } class CompositeLit(p: Pointer? = Pointer.NULL) : Expr(p) { - val type: Expr? - get() { - return INSTANCE.GetCompositeLitType(this) - } + val type: Expr? by lazy { INSTANCE.GetCompositeLitType(this) } - val elts: List - get() { - return list(INSTANCE::GetNumCompositeLitElts, INSTANCE::GetCompositeLitElt) - } + val elts: List by lazy { + list(INSTANCE::GetNumCompositeLitElts, INSTANCE::GetCompositeLitElt) + } } class KeyValueExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val key: Expr - get() { - return INSTANCE.GetKeyValueExprKey(this) - } + val key: Expr by lazy { INSTANCE.GetKeyValueExprKey(this) } - val value: Expr - get() { - return INSTANCE.GetKeyValueExprValue(this) - } + val value: Expr by lazy { INSTANCE.GetKeyValueExprValue(this) } } class ParenExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val x: Expr - get() { - return INSTANCE.GetParenExprX(this) - } + val x: Expr by lazy { INSTANCE.GetParenExprX(this) } } class FuncLit(p: Pointer? = Pointer.NULL) : Expr(p) { @@ -364,15 +286,9 @@ interface GoStandardLibrary : Library { } class Ident(p: Pointer? = Pointer.NULL) : Expr(p) { - val isUnexported: Boolean - get() { - return name.isNotEmpty() && name[0].isLowerCase() - } + val isUnexported: Boolean by lazy { name.isNotEmpty() && name[0].isLowerCase() } - val name: String - get() { - return INSTANCE.GetIdentName(this) - } + val name: String by lazy { INSTANCE.GetIdentName(this) } override fun toString(): String { return name @@ -380,159 +296,83 @@ interface GoStandardLibrary : Library { } class IndexExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val x: Expr - get() { - return INSTANCE.GetIndexExprX(this) - } + val x: Expr by lazy { INSTANCE.GetIndexExprX(this) } - val index: Expr - get() { - return INSTANCE.GetIndexExprIndex(this) - } + val index: Expr by lazy { INSTANCE.GetIndexExprIndex(this) } } class IndexListExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val x: Expr - get() { - return INSTANCE.GetIndexListExprX(this) - } + val x: Expr by lazy { INSTANCE.GetIndexListExprX(this) } - val indices: List - get() { - return list( - INSTANCE::GetNumIndexListExprIndices, - INSTANCE::GetIndexListExprIndex - ) - } + val indices: List by lazy { + list(INSTANCE::GetNumIndexListExprIndices, INSTANCE::GetIndexListExprIndex) + } } class SelectorExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val x: Expr - get() { - return INSTANCE.GetSelectorExprX(this) - } + val x: Expr by lazy { INSTANCE.GetSelectorExprX(this) } - val sel: Ident - get() { - return INSTANCE.GetSelectorExprSel(this) - } + val sel: Ident by lazy { INSTANCE.GetSelectorExprSel(this) } } class StarExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val x: Expr - get() { - return INSTANCE.GetStarExprX(this) - } + val x: Expr by lazy { INSTANCE.GetStarExprX(this) } } class SliceExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val x: Expr - get() { - return INSTANCE.GetSliceExprX(this) - } + val x: Expr by lazy { INSTANCE.GetSliceExprX(this) } - val low: Expr? - get() { - return INSTANCE.GetSliceExprLow(this) - } + val low: Expr? by lazy { INSTANCE.GetSliceExprLow(this) } - val high: Expr? - get() { - return INSTANCE.GetSliceExprHigh(this) - } + val high: Expr? by lazy { INSTANCE.GetSliceExprHigh(this) } - val max: Expr? - get() { - return INSTANCE.GetSliceExprMax(this) - } + val max: Expr? by lazy { INSTANCE.GetSliceExprMax(this) } } class TypeAssertExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val x: Expr - get() { - return INSTANCE.GetTypeAssertExprX(this) - } + val x: Expr by lazy { INSTANCE.GetTypeAssertExprX(this) } - val type: Expr? - get() { - return INSTANCE.GetTypeAssertExprType(this) - } + val type: Expr? by lazy { INSTANCE.GetTypeAssertExprType(this) } } class UnaryExpr(p: Pointer? = Pointer.NULL) : Expr(p) { - val opString: String - get() { - return INSTANCE.GetUnaryExprOpString(this) - } + val opString: String by lazy { INSTANCE.GetUnaryExprOpString(this) } - val x: Expr - get() { - return INSTANCE.GetUnaryExprX(this) - } + val x: Expr by lazy { INSTANCE.GetUnaryExprX(this) } } class ArrayType(p: Pointer? = Pointer.NULL) : Expr(p) { - val elt: Expr - get() { - return INSTANCE.GetArrayTypeElt(this) - } + val elt: Expr by lazy { INSTANCE.GetArrayTypeElt(this) } } class ChanType(p: Pointer? = Pointer.NULL) : Expr(p) { - val value: Expr - get() { - return INSTANCE.GetChanTypeValue(this) - } + val value: Expr by lazy { INSTANCE.GetChanTypeValue(this) } } class InterfaceType(p: Pointer? = Pointer.NULL) : Expr(p) { - val methods: FieldList - get() { - return INSTANCE.GetInterfaceTypeMethods(this) - } + val methods: FieldList by lazy { INSTANCE.GetInterfaceTypeMethods(this) } - val incomplete: Boolean - get() { - return INSTANCE.GetInterfaceTypeIncomplete(this) - } + val incomplete: Boolean by lazy { INSTANCE.GetInterfaceTypeIncomplete(this) } } class FuncType(p: Pointer? = Pointer.NULL) : Expr(p) { - val typeParams: FieldList? - get() { - return INSTANCE.GetFuncTypeTypeParams(this) - } + val typeParams: FieldList? by lazy { INSTANCE.GetFuncTypeTypeParams(this) } - val params: FieldList - get() { - return INSTANCE.GetFuncTypeParams(this) - } + val params: FieldList by lazy { INSTANCE.GetFuncTypeParams(this) } val results: FieldList? by lazy { INSTANCE.GetFuncTypeResults(this) } } class MapType(p: Pointer? = Pointer.NULL) : Expr(p) { - val key: Expr - get() { - return INSTANCE.GetMapTypeKey(this) - } + val key: Expr by lazy { INSTANCE.GetMapTypeKey(this) } - val value: Expr - get() { - return INSTANCE.GetMapTypeValue(this) - } + val value: Expr by lazy { INSTANCE.GetMapTypeValue(this) } } class StructType(p: Pointer? = Pointer.NULL) : Expr(p) { - val fields: FieldList - get() { - return INSTANCE.GetStructTypeFields(this) - } + val fields: FieldList by lazy { INSTANCE.GetStructTypeFields(this) } - val incomplete: Boolean - get() { - return INSTANCE.GetStructTypeIncomplete(this) - } + val incomplete: Boolean by lazy { INSTANCE.GetStructTypeIncomplete(this) } } open class Stmt(p: Pointer? = Pointer.NULL) : Node(p) { @@ -565,33 +405,22 @@ interface GoStandardLibrary : Library { } class AssignStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val lhs: List - get() { - return this.list(INSTANCE::GetNumAssignStmtLhs, INSTANCE::GetAssignStmtLhs) - } + val lhs: List by lazy { + this.list(INSTANCE::GetNumAssignStmtLhs, INSTANCE::GetAssignStmtLhs) + } - val tok: Int - get() { - return INSTANCE.GetAssignStmtTok(this) - } + val tok: Int by lazy { INSTANCE.GetAssignStmtTok(this) } - val rhs: List - get() { - return this.list(INSTANCE::GetNumAssignStmtRhs, INSTANCE::GetAssignStmtRhs) - } + val rhs: List by lazy { + this.list(INSTANCE::GetNumAssignStmtRhs, INSTANCE::GetAssignStmtRhs) + } } class BranchStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val tokString: String - get() { - return INSTANCE.GetBranchStmtTokString(this) - } + val tokString: String by lazy { INSTANCE.GetBranchStmtTokString(this) } - val label: Ident? - get() { - return INSTANCE.GetBranchStmtLabel(this) - } + val label: Ident? by lazy { INSTANCE.GetBranchStmtLabel(this) } } class BlockStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { @@ -611,192 +440,98 @@ interface GoStandardLibrary : Library { } class DeclStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val decl: Decl - get() { - return INSTANCE.GetDeclStmtDecl(this) - } + val decl: Decl by lazy { INSTANCE.GetDeclStmtDecl(this) } } class DeferStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val call: Expr - get() { - return INSTANCE.GetDeferStmtCall(this) - } + val call: Expr by lazy { INSTANCE.GetDeferStmtCall(this) } } class ExprStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val x: Expr - get() { - return INSTANCE.GetExprStmtX(this) - } + val x: Expr by lazy { INSTANCE.GetExprStmtX(this) } } class IfStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val init: Stmt? - get() { - return INSTANCE.GetIfStmtInit(this) - } + val init: Stmt? by lazy { INSTANCE.GetIfStmtInit(this) } - val cond: Expr - get() { - return INSTANCE.GetIfStmtCond(this) - } + val cond: Expr by lazy { INSTANCE.GetIfStmtCond(this) } - val body: BlockStmt - get() { - return INSTANCE.GetIfStmtBody(this) - } + val body: BlockStmt by lazy { INSTANCE.GetIfStmtBody(this) } - val `else`: Stmt? - get() { - return INSTANCE.GetIfStmtElse(this) - } + val `else`: Stmt? by lazy { INSTANCE.GetIfStmtElse(this) } } class ForStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val init: Stmt? - get() { - return INSTANCE.GetForStmtInit(this) - } + val init: Stmt? by lazy { INSTANCE.GetForStmtInit(this) } - val cond: Expr? - get() { - return INSTANCE.GetForStmtCond(this) - } + val cond: Expr? by lazy { INSTANCE.GetForStmtCond(this) } - val post: Stmt? - get() { - return INSTANCE.GetForStmtPost(this) - } + val post: Stmt? by lazy { INSTANCE.GetForStmtPost(this) } - val body: BlockStmt? - get() { - return INSTANCE.GetForStmtBody(this) - } + val body: BlockStmt? by lazy { INSTANCE.GetForStmtBody(this) } } class GoStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val call: Expr - get() { - return INSTANCE.GetGoStmtCall(this) - } + val call: Expr by lazy { INSTANCE.GetGoStmtCall(this) } } class IncDecStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val tokString: String - get() { - return INSTANCE.GetIncDecStmtTokString(this) - } + val tokString: String by lazy { INSTANCE.GetIncDecStmtTokString(this) } - val x: Expr - get() { - return INSTANCE.GetIncDecStmtX(this) - } + val x: Expr by lazy { INSTANCE.GetIncDecStmtX(this) } } class LabeledStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val label: Ident - get() { - return INSTANCE.GetLabeledStmtLabel(this) - } + val label: Ident by lazy { INSTANCE.GetLabeledStmtLabel(this) } - val stmt: Stmt - get() { - return INSTANCE.GetLabeledStmtStmt(this) - } + val stmt: Stmt by lazy { INSTANCE.GetLabeledStmtStmt(this) } } class RangeStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val tokString: String - get() { - return INSTANCE.GetRangeStmtTokString(this) - } + val tokString: String by lazy { INSTANCE.GetRangeStmtTokString(this) } - val key: Expr? - get() { - return INSTANCE.GetRangeStmtKey(this) - } + val key: Expr? by lazy { INSTANCE.GetRangeStmtKey(this) } - val value: Expr? - get() { - return INSTANCE.GetRangeStmtValue(this) - } + val value: Expr? by lazy { INSTANCE.GetRangeStmtValue(this) } - val x: Expr - get() { - return INSTANCE.GetRangeStmtX(this) - } + val x: Expr by lazy { INSTANCE.GetRangeStmtX(this) } - val body: BlockStmt - get() { - return INSTANCE.GetRangeStmtBody(this) - } + val body: BlockStmt by lazy { INSTANCE.GetRangeStmtBody(this) } } class ReturnStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val results: List - get() { - return list(INSTANCE::GetNumReturnStmtResults, INSTANCE::GetReturnStmtResult) - } + val results: List by lazy { + list(INSTANCE::GetNumReturnStmtResults, INSTANCE::GetReturnStmtResult) + } } class SendStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val chan: Expr - get() { - return INSTANCE.GetSendStmtChan(this) - } + val chan: Expr by lazy { INSTANCE.GetSendStmtChan(this) } - val value: Expr - get() { - return INSTANCE.GetSendStmtValue(this) - } + val value: Expr by lazy { INSTANCE.GetSendStmtValue(this) } } class SwitchStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val init: Stmt? - get() { - return INSTANCE.GetSwitchStmtInit(this) - } + val init: Stmt? by lazy { INSTANCE.GetSwitchStmtInit(this) } - val tag: Expr? - get() { - return INSTANCE.GetSwitchStmtTag(this) - } + val tag: Expr? by lazy { INSTANCE.GetSwitchStmtTag(this) } - val body: BlockStmt - get() { - return INSTANCE.GetSwitchStmtBody(this) - } + val body: BlockStmt by lazy { INSTANCE.GetSwitchStmtBody(this) } } class TypeSwitchStmt(p: Pointer? = Pointer.NULL) : Stmt(p) { - val init: Stmt? - get() { - return INSTANCE.GetTypeSwitchStmtInit(this) - } + val init: Stmt? by lazy { INSTANCE.GetTypeSwitchStmtInit(this) } - val assign: Stmt - get() { - return INSTANCE.GetTypeSwitchStmtAssign(this) - } + val assign: Stmt by lazy { INSTANCE.GetTypeSwitchStmtAssign(this) } - val body: BlockStmt - get() { - return INSTANCE.GetTypeSwitchStmtBody(this) - } + val body: BlockStmt by lazy { INSTANCE.GetTypeSwitchStmtBody(this) } } class Position(p: Pointer? = Pointer.NULL) : GoObject(p) { - val line: Int - get() { - return INSTANCE.GetPositionLine(this) - } + val line: Int by lazy { INSTANCE.GetPositionLine(this) } - val column: Int - get() { - return INSTANCE.GetPositionColumn(this) - } + val column: Int by lazy { INSTANCE.GetPositionColumn(this) } } class FileSet(p: Pointer? = Pointer.NULL) : GoObject(p) { @@ -820,25 +555,17 @@ interface GoStandardLibrary : Library { } class File(p: Pointer? = Pointer.NULL) : Node(p) { - val comments: Pointer - get() { - return INSTANCE.GetFileComments(this) - } + val comments: Pointer by lazy { INSTANCE.GetFileComments(this) } - val imports: List - get() { - return list(INSTANCE::GetNumFileImports, INSTANCE::GetFileImport) - } + val imports: List by lazy { + list(INSTANCE::GetNumFileImports, INSTANCE::GetFileImport) + } - val decls: List - get() { - return this.list(INSTANCE::GetNumFileDecls, INSTANCE::GetFileDecl) - } + val decls: List by lazy { + this.list(INSTANCE::GetNumFileDecls, INSTANCE::GetFileDecl) + } - val name: Ident - get() { - return INSTANCE.GetFileName(this) - } + val name: Ident by lazy { INSTANCE.GetFileName(this) } } } diff --git a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoUtils.kt b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoUtils.kt index 77179dad1e9..9ac7296430d 100644 --- a/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoUtils.kt +++ b/cpg-language-go/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/golang/GoUtils.kt @@ -171,7 +171,6 @@ internal class Project { */ internal fun buildProject( modulePath: String, - module: String, goos: String? = null, goarch: String? = null, goVersion: Int? = null, @@ -183,6 +182,10 @@ internal class Project { val topLevel = File(modulePath) + val goModFile = topLevel.resolve("go.mod") + var module = + GoStandardLibrary.Modfile.parse(goModFile.absolutePath, goModFile.readText()) + val pb = ProcessBuilder("go", "list", "-deps", tags.joinToString(",", "-tags="), "all") .directory(topLevel) @@ -226,8 +229,8 @@ internal class Project { // if it already starts with vendor, it is a vendored stdlib package, we // don't really include these for now since they blow up the stdlib null - } else if (it.startsWith(module)) { - topLevel.resolve(it.substringAfter(module)) + } else if (it.startsWith(module.module.mod.path)) { + topLevel.resolve(it.substringAfter(module.module.mod.path)) } else { // for all other dependencies, we try whether they are vendored topLevel.resolve("vendor").resolve(it) @@ -235,6 +238,8 @@ internal class Project { } files += dirs.flatMap { gatherGoFiles(it, false) } + // add cmd folder + files += gatherGoFiles(topLevel.resolve("cmd")) goos?.let { symbols["GOOS"] = it } goarch?.let { symbols["GOARCH"] = it } diff --git a/cpg-language-go/src/test/resources/golang/literal.go b/cpg-language-go/src/test/resources/golang/literal.go index b0ae560a448..ebc1659a073 100644 --- a/cpg-language-go/src/test/resources/golang/literal.go +++ b/cpg-language-go/src/test/resources/golang/literal.go @@ -58,6 +58,7 @@ type pairNameValue struct { var structKey = map[pairNameValue]uint64{ {name: "this", value: "that"}: 1, + {name: "that", value: "this"}: 2, } var structValue = map[uint64]pairNameValue{