Skip to content

Commit

Permalink
[swiftsrc2cpg] Fixed handling of extensions from multiple files (joer…
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser authored Mar 8, 2024
1 parent 330eced commit d5f9f02
Show file tree
Hide file tree
Showing 13 changed files with 779 additions and 1,152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package io.joern.swiftsrc2cpg.astcreation

import io.joern.swiftsrc2cpg.Config
import io.joern.swiftsrc2cpg.datastructures.Scope
import io.joern.swiftsrc2cpg.datastructures.SwiftGlobal
import io.joern.swiftsrc2cpg.parser.SwiftJsonParser.ParseResult
import io.joern.swiftsrc2cpg.parser.SwiftNodeSyntax.*
import io.joern.swiftsrc2cpg.passes.Defines
import io.joern.x2cpg.datastructures.Stack.*
import io.joern.x2cpg.utils.NodeBuilders.newMethodReturnNode
import io.joern.x2cpg.{Ast, AstCreatorBase, ValidationMode, AstNodeBuilder as X2CpgAstNodeBuilder}
import io.joern.x2cpg.datastructures.Global
import io.joern.x2cpg.utils.NodeBuilders.newModifierNode
import io.joern.x2cpg.utils.OffsetUtils
import io.shiftleft.semanticcpg.language.types.structure.NamespaceTraversal
Expand All @@ -25,7 +25,7 @@ import overflowdb.BatchedUpdate.DiffGraphBuilder

import scala.collection.mutable

class AstCreator(val config: Config, val global: SwiftGlobal, val parserResult: ParseResult)(implicit
class AstCreator(val config: Config, val global: Global, val parserResult: ParseResult)(implicit
withSchemaValidation: ValidationMode
) extends AstCreatorBase(parserResult.filename)
with AstForSwiftTokenCreator
Expand All @@ -48,11 +48,9 @@ class AstCreator(val config: Config, val global: SwiftGlobal, val parserResult:
protected val typeRefIdStack = new Stack[NewTypeRef]
protected val dynamicInstanceTypeStack = new Stack[String]
protected val localAstParentStack = new Stack[NewBlock]()
protected val typeFullNameToPostfix = mutable.HashMap.empty[String, Int]
protected val functionNodeToNameAndFullName = mutable.HashMap.empty[SwiftNode, (String, String)]
protected val usedVariableNames = mutable.HashMap.empty[String, Int]
protected val seenAliasTypes = mutable.HashSet.empty[NewTypeDecl]
protected val functionFullNames = mutable.HashSet.empty[String]

protected lazy val definedSymbols: Map[String, String] = {
config.defines.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,52 +124,23 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
}

protected def calcTypeNameAndFullName(name: String): (String, String) = {
val fullNamePrefix = s"${parserResult.filename}:${computeScopePath(scope.getScopeHead)}:"
val intendedFullName = s"$fullNamePrefix$name"
val postfix = typeFullNameToPostfix.getOrElse(intendedFullName, 0)
val resultingFullName =
if (postfix == 0) intendedFullName
else s"$intendedFullName$postfix"
typeFullNameToPostfix.put(intendedFullName, postfix + 1)
(name, resultingFullName)
val fullNamePrefix = s"${parserResult.filename}:${computeScopePath(scope.getScopeHead)}:"
val fullName = s"$fullNamePrefix$name"
(name, fullName)
}

protected def calcMethodNameAndFullName(func: SwiftNode): (String, String) = {
// functionNode.getName is not necessarily unique and thus the full name calculated based on the scope
// is not necessarily unique. Specifically we have this problem with lambda functions which are defined
// in the same scope.
functionNodeToNameAndFullName.get(func) match {
case Some(nameAndFullName) => nameAndFullName
case None =>
val intendedName = calcMethodName(func)
val name = calcMethodName(func)
val fullNamePrefix = s"${parserResult.filename}:${computeScopePath(scope.getScopeHead)}:"
var name = intendedName
var fullName = ""
var isUnique = false
var i = 1
while (!isUnique) {
fullName = s"$fullNamePrefix$name"
if (functionFullNames.contains(fullName)) {
name = s"$intendedName$i"
i += 1
} else {
isUnique = true
}
}
functionFullNames.add(fullName)
val fullName = s"$fullNamePrefix$name"
functionNodeToNameAndFullName(func) = (name, fullName)
(name, fullName)
}
}

protected def stripQuotes(str: String): String = str
.stripPrefix("\"")
.stripSuffix("\"")
.stripPrefix("'")
.stripSuffix("'")
.stripPrefix("`")
.stripSuffix("`")

protected def createVariableReferenceLinks(): Unit = {
val resolvedReferenceIt = scope.resolve(createMethodLocalForUnresolvedReference)
val capturedLocals = mutable.HashMap.empty[String, NewNode]
Expand Down
Loading

0 comments on commit d5f9f02

Please sign in to comment.