Skip to content

Commit 8e7067d

Browse files
[kotlin2cpg] Replace expr.getText with code(expr) (#5851)
* [kotlin2cpg] Replace `expr.getText` with `code(expr)` * Remove accidental overzelous case
1 parent 4654561 commit 8e7067d

File tree

6 files changed

+150
-157
lines changed

6 files changed

+150
-157
lines changed

joern-cli/frontends/kotlin2cpg/src/main/scala/io/joern/kotlin2cpg/ast/AstCreator.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ class AstCreator(
447447
argNameMaybe: Option[String],
448448
annotations: Seq[KtAnnotationEntry] = Seq()
449449
): Ast = {
450-
val node = unknownNode(expr, Option(expr).map(_.getText).getOrElse(Constants.CodePropUndefinedValue))
450+
val node = unknownNode(expr, Option(expr).map(code).getOrElse(Constants.CodePropUndefinedValue))
451451
Ast(withArgumentIndex(node, argIdx).argumentName(argNameMaybe))
452452
.withChildren(annotations.map(astForAnnotationEntry))
453453
}
@@ -491,7 +491,7 @@ class AstCreator(
491491
callAst(componentNCallNode, Seq(), Option(rhsBaseAst))
492492

493493
val assignmentCallNode =
494-
operatorCallNode(entry, s"${entry.getText} = $componentNCallCode", Operators.assignment, None)
494+
operatorCallNode(entry, shortenCode(s"${entry.getText} = $componentNCallCode"), Operators.assignment, None)
495495
callAst(assignmentCallNode, List(assignmentLHSAst, componentNAst))
496496
}
497497

joern-cli/frontends/kotlin2cpg/src/main/scala/io/joern/kotlin2cpg/ast/AstForDeclarationsCreator.scala

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
150150

151151
val assignmentNode = operatorCallNode(
152152
decl,
153-
s"${fieldAccessCall.code} = ${decl.getInitializer.getText}",
153+
shortenCode(s"${fieldAccessCall.code} = ${decl.getInitializer.getText}"),
154154
Operators.assignment,
155155
None
156156
)
@@ -401,18 +401,18 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
401401
case _ => false
402402
}
403403
val tmpName = s"${Constants.TmpLocalPrefix}${tmpKeyPool.next}"
404-
val localForTmpNode = localNode(expr, tmpName, tmpName, callRhsTypeFullName)
404+
val localForTmpNode = localNode(expr, tmpName, shortenCode(tmpName), callRhsTypeFullName)
405405
scope.addToScope(localForTmpNode.name, localForTmpNode)
406406
val localForTmpAst = Ast(localForTmpNode)
407407

408-
val assignmentLhsNode = identifierNode(expr, tmpName, tmpName, localForTmpNode.typeFullName)
408+
val assignmentLhsNode = identifierNode(expr, tmpName, shortenCode(tmpName), localForTmpNode.typeFullName)
409409
val assignmentLhsAst = Ast(assignmentLhsNode).withRefEdge(assignmentLhsNode, localForTmpNode)
410410
val tmpAssignmentAst =
411411
if (isCtor) {
412412
val assignmentRhsNode =
413413
operatorCallNode(expr, Constants.Alloc, Operators.alloc, Option(localForTmpNode.typeFullName))
414414
val assignmentNode =
415-
operatorCallNode(expr, s"$tmpName = ${Constants.Alloc}", Operators.assignment, None)
415+
operatorCallNode(expr, shortenCode(s"$tmpName = ${Constants.Alloc}"), Operators.assignment, None)
416416
callAst(assignmentNode, List(assignmentLhsAst, Ast(assignmentRhsNode)))
417417
} else {
418418
expr.getInitializer match {
@@ -426,7 +426,12 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
426426
astForIfAsExpression(expression, None, None)
427427
case _ =>
428428
val assignmentNode =
429-
operatorCallNode(expr.getInitializer, s"$tmpName = ${rhsCall.getText}", Operators.assignment, None)
429+
operatorCallNode(
430+
expr.getInitializer,
431+
shortenCode(s"$tmpName = ${rhsCall.getText}"),
432+
Operators.assignment,
433+
None
434+
)
430435
val assignmentRhsAst =
431436
astsForExpression(rhsCall, None).headOption.getOrElse(Ast(unknownNode(rhsCall, Constants.Empty)))
432437
callAst(assignmentNode, List(assignmentLhsAst, assignmentRhsAst))
@@ -462,7 +467,7 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
462467
val rhsBaseAst =
463468
astWithRefEdgeMaybe(
464469
localForTmpNode.name,
465-
identifierNode(entry, localForTmpNode.name, localForTmpNode.name, localForTmpNode.typeFullName)
470+
identifierNode(entry, localForTmpNode.name, shortenCode(localForTmpNode.name), localForTmpNode.typeFullName)
466471
.argumentIndex(0)
467472
)
468473
assignmentAstForDestructuringEntry(entry, rhsBaseAst, idx + 1)
@@ -508,8 +513,8 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
508513
.flatMap(desc => nameRenderer.typeFullName(desc.getType))
509514
.getOrElse(TypeConstants.Any)
510515
)
511-
val entryName = entry.getText
512-
val node = localNode(entry, entryName, entryName, entryTypeFullName)
516+
val entryName = code(entry)
517+
val node = localNode(entry, entryName, shortenCode(entryName), entryTypeFullName)
513518
scope.addToScope(entryName, node)
514519
Ast(node)
515520
}
@@ -554,7 +559,7 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
554559
identifierNode(valueParam, Constants.ThisName, Constants.ThisName, typeDecl.fullName, Seq(typeDecl.fullName))
555560
val thisAst = Ast(thisIdentifier).withRefEdge(thisIdentifier, thisParam)
556561

557-
val fieldIdentifier = fieldIdentifierNode(valueParam, valueParam.getName, valueParam.getName)
562+
val fieldIdentifier = fieldIdentifierNode(valueParam, valueParam.getName, shortenCode(valueParam.getName))
558563
val fieldAccessCall = operatorCallNode(
559564
valueParam,
560565
s"${Constants.ThisName}.${valueParam.getName}",
@@ -660,16 +665,17 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
660665
val typeDeclAst = astForClassOrObject(expr.getObjectDeclaration, Some(ctx))
661666
val typeDeclFullName = typeDeclAst.root.get.asInstanceOf[NewTypeDecl].fullName
662667

663-
val localForTmp = localNode(expr, tmpName, tmpName, typeDeclFullName)
668+
val localForTmp = localNode(expr, tmpName, shortenCode(tmpName), typeDeclFullName)
664669
scope.addToScope(tmpName, localForTmp)
665670
val localAst = Ast(localForTmp)
666671

667672
val rhsAst = Ast(operatorCallNode(expr, Operators.alloc, Operators.alloc, None))
668673

669-
val identifier = identifierNode(expr, tmpName, tmpName, localForTmp.typeFullName)
674+
val identifier = identifierNode(expr, tmpName, shortenCode(tmpName), localForTmp.typeFullName)
670675
val identifierAst = astWithRefEdgeMaybe(identifier.name, identifier)
671676

672-
val assignmentNode = operatorCallNode(expr, s"${identifier.name} = <alloc>", Operators.assignment, None)
677+
val assignmentNode =
678+
operatorCallNode(expr, shortenCode(s"${identifier.name} = <alloc>"), Operators.assignment, None)
673679
val assignmentCallAst = callAst(assignmentNode, List(identifierAst) ++ List(rhsAst))
674680
val initSignature = s"${TypeConstants.Void}()"
675681
val initFullName = s"$typeDeclFullName.${Defines.ConstructorMethodName}:$initSignature"
@@ -684,16 +690,16 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
684690
Some(TypeConstants.Void)
685691
)
686692

687-
val initReceiverNode = identifierNode(expr, identifier.name, identifier.name, identifier.typeFullName)
693+
val initReceiverNode = identifierNode(expr, identifier.name, shortenCode(identifier.name), identifier.typeFullName)
688694
val initReceiverAst =
689695
Ast(initReceiverNode).withRefEdge(initReceiverNode, localForTmp)
690696
val initAst = callAst(initCallNode, Seq(), Option(initReceiverAst))
691697

692-
val refTmpNode = identifierNode(expr, tmpName, tmpName, localForTmp.typeFullName)
698+
val refTmpNode = identifierNode(expr, tmpName, shortenCode(tmpName), localForTmp.typeFullName)
693699
val refTmpAst = astWithRefEdgeMaybe(refTmpNode.name, refTmpNode)
694700

695701
val blockNode_ =
696-
withArgumentIndex(blockNode(expr, expr.getText, TypeConstants.Any), argIdxMaybe)
702+
withArgumentIndex(blockNode(expr, code(expr), TypeConstants.Any), argIdxMaybe)
697703
.argumentName(argNameMaybe)
698704
blockAst(blockNode_, Seq(typeDeclAst, localAst, assignmentCallAst, initAst, refTmpAst).toList)
699705
.withChildren(annotations.map(astForAnnotationEntry))
@@ -730,7 +736,7 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
730736
.orElse(fullNameByImportPath(expr.getTypeReference, expr.getContainingKtFile))
731737
.getOrElse(explicitTypeName)
732738
registerType(localTypeFullName)
733-
val local = localNode(expr, expr.getName, expr.getName, localTypeFullName)
739+
val local = localNode(expr, expr.getName, code(expr), localTypeFullName)
734740
scope.addToScope(expr.getName, local)
735741
val localAst = Ast(local)
736742

@@ -739,11 +745,11 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
739745
)
740746
val rhsAst = Ast(operatorCallNode(expr, Operators.alloc, Operators.alloc, Option(typeFullName)))
741747

742-
val identifier = identifierNode(elem, elem.getText, elem.getText, local.typeFullName)
748+
val identifier = identifierNode(elem, elem.getText, code(elem), local.typeFullName)
743749
val identifierAst = astWithRefEdgeMaybe(identifier.name, identifier)
744750

745751
val assignmentNode =
746-
operatorCallNode(expr, expr.getText, Operators.assignment, None)
752+
operatorCallNode(expr, code(expr), Operators.assignment, None)
747753
val assignmentCallAst = callAst(assignmentNode, List(identifierAst) ++ List(rhsAst))
748754

749755
val (fullName, signature) =
@@ -754,16 +760,17 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
754760
)
755761
val initCallNode = callNode(
756762
callExpr,
757-
callExpr.getText,
763+
code(callExpr),
758764
Defines.ConstructorMethodName,
759765
fullName,
760766
DispatchTypes.STATIC_DISPATCH,
761767
Some(signature),
762768
Some(TypeConstants.Void)
763769
)
764-
val initReceiverNode = identifierNode(expr, identifier.name, identifier.name, identifier.typeFullName)
765-
val initReceiverAst = Ast(initReceiverNode).withRefEdge(initReceiverNode, local)
766-
val argAsts = astsForKtCallExpressionArguments(callExpr)
770+
val initReceiverNode =
771+
identifierNode(expr, identifier.name, shortenCode(identifier.name), identifier.typeFullName)
772+
val initReceiverAst = Ast(initReceiverNode).withRefEdge(initReceiverNode, local)
773+
val argAsts = astsForKtCallExpressionArguments(callExpr)
767774

768775
val initAst =
769776
callAst(initCallNode, argAsts, Option(initReceiverAst))
@@ -781,7 +788,7 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
781788
val typeDeclAst = astForClassOrObject(typedExpr.getObjectDeclaration, Some(ctx))
782789
val typeDeclFullName = typeDeclAst.root.get.asInstanceOf[NewTypeDecl].fullName
783790

784-
val node = localNode(expr, expr.getName, expr.getName, typeDeclFullName)
791+
val node = localNode(expr, expr.getName, shortenCode(expr.getName), typeDeclFullName)
785792
scope.addToScope(expr.getName, node)
786793
val localAst = Ast(node)
787794

@@ -790,10 +797,10 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
790797
)
791798
val rhsAst = Ast(operatorCallNode(expr, Operators.alloc, Operators.alloc, None))
792799

793-
val identifier = identifierNode(elem, elem.getText, elem.getText, node.typeFullName)
800+
val identifier = identifierNode(elem, elem.getText, code(elem), node.typeFullName)
794801
val identifierAst = astWithRefEdgeMaybe(identifier.name, identifier)
795802

796-
val assignmentNode = operatorCallNode(expr, expr.getText, Operators.assignment, None)
803+
val assignmentNode = operatorCallNode(expr, code(expr), Operators.assignment, None)
797804
val assignmentCallAst = callAst(assignmentNode, List(identifierAst) ++ List(rhsAst))
798805
val initSignature = s"${TypeConstants.Void}()"
799806
val initFullName = s"$typeFullName${Defines.ConstructorMethodName}:$initSignature"
@@ -820,15 +827,15 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
820827
.orElse(fullNameByImportPath(expr.getTypeReference, expr.getContainingKtFile))
821828
.getOrElse(explicitTypeName)
822829
registerType(typeFullName)
823-
val node = localNode(expr, expr.getName, expr.getName, typeFullName)
830+
val node = localNode(expr, expr.getName, shortenCode(expr.getName), typeFullName)
824831
scope.addToScope(expr.getName, node)
825832
val localAst = Ast(node)
826833

827834
if (expr.getDelegateExpressionOrInitializer != null) {
828835
val rhsAsts = astsForExpression(expr.getDelegateExpressionOrInitializer, Some(2))
829-
val identifier = identifierNode(elem, elem.getText, elem.getText, typeFullName)
836+
val identifier = identifierNode(elem, code(elem), code(elem), typeFullName)
830837
val identifierAst = astWithRefEdgeMaybe(identifier.name, identifier)
831-
val assignmentNode = operatorCallNode(expr, expr.getText, Operators.assignment, None)
838+
val assignmentNode = operatorCallNode(expr, code(expr), Operators.assignment, None)
832839
val call =
833840
callAst(assignmentNode, List(identifierAst) ++ rhsAsts)
834841
.withChildren(annotations.map(astForAnnotationEntry))
@@ -856,7 +863,7 @@ trait AstForDeclarationsCreator(implicit withSchemaValidation: ValidationMode) {
856863
}
857864
registerType(typeFullName)
858865

859-
val node = memberNode(decl, name, name, typeFullName)
866+
val node = memberNode(decl, name, shortenCode(name), typeFullName)
860867
scope.addToScope(name, node)
861868
Ast(node)
862869
}

0 commit comments

Comments
 (0)