Skip to content

Commit

Permalink
Create FreshNameGenerator class
Browse files Browse the repository at this point in the history
  • Loading branch information
badly-drawn-wizards committed Mar 19, 2024
1 parent 864126b commit bc8af93
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class AstCreator(
with AstForExpressionsCreator
with AstForFunctionsCreator
with AstForTypesCreator
with FreshVariableCreator
with AstSummaryVisitor
with AstNodeBuilder[RubyNode, AstCreator] {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import io.joern.x2cpg.{Ast, Defines as XDefines, ValidationMode}
import io.joern.rubysrc2cpg.passes.Defines
import io.shiftleft.codepropertygraph.generated.nodes.*
import io.shiftleft.codepropertygraph.generated.{ControlStructureTypes, DispatchTypes, Operators, PropertyNames}
import io.joern.rubysrc2cpg.utils.FreshNameGenerator

trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) { this: AstCreator =>

val tmpGen = FreshNameGenerator(i => s"<tmp-$i>")

protected def astForExpression(node: RubyNode): Ast = node match
case node: StaticLiteral => astForStaticLiteral(node)
case node: HereDocNode => astForHereDoc(node)
Expand Down Expand Up @@ -176,7 +179,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
val block = blockNode(node)
scope.pushNewScope(BlockScope(block))

val tmp = SimpleIdentifier(Option(className))(node.span.spanStart(freshVariableName()))
val tmp = SimpleIdentifier(Option(className))(node.span.spanStart(tmpGen.fresh))
def tmpIdentifier = {
val tmpAst = astForSimpleIdentifier(tmp)
tmpAst.root.collect { case x: NewIdentifier => x.typeFullName(receiverTypeFullName) }
Expand Down Expand Up @@ -393,7 +396,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
val block = blockNode(node)
scope.pushNewScope(BlockScope(block))

val tmp = freshVariableName()
val tmp = tmpGen.fresh

val argumentAsts = node.elements.flatMap(elem =>
elem match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import io.joern.x2cpg.utils.NodeBuilders.{newClosureBindingNode, newLocalNode, n
import io.joern.x2cpg.{Ast, AstEdge, ValidationMode, Defines as XDefines}
import io.shiftleft.codepropertygraph.generated.nodes.*
import io.shiftleft.codepropertygraph.generated.{EdgeTypes, EvaluationStrategies, ModifierTypes, NodeTypes}
import io.joern.rubysrc2cpg.utils.FreshNameGenerator

trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) { this: AstCreator =>

val procParamGen = FreshNameGenerator(i => Left(s"<proc-param-$i>"))

/** Creates method declaration related structures.
* @param node
* the node to create the AST structure from.
Expand Down Expand Up @@ -40,7 +43,7 @@ trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) { th
)

if (methodName == XDefines.ConstructorMethodName) scope.pushNewScope(ConstructorScope(fullName))
else scope.pushNewScope(MethodScope(fullName, Left(freshVariableName(i => s"<proc-param-$i>"))))
else scope.pushNewScope(MethodScope(fullName, procParamGen.fresh))

val parameterAsts = astForParameters(node.parameters)

Expand Down Expand Up @@ -271,7 +274,7 @@ trait AstForFunctionsCreator(implicit withSchemaValidation: ValidationMode) { th
astParentFullName = scope.surroundingScopeFullName
)

scope.pushNewScope(MethodScope(fullName, Left(freshVariableName(i => s"<proc-param-$i>"))))
scope.pushNewScope(MethodScope(fullName, procParamGen.fresh))

val thisParameterAst = Ast(
newThisParameterNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}
def generatedNode: StatementList = node.expression
.map { e =>
val tmp = SimpleIdentifier(None)(e.span.spanStart(freshVariableName()))
val tmp = SimpleIdentifier(None)(e.span.spanStart(tmpGen.fresh))
StatementList(
List(SingleAssignment(tmp, "=", e)(e.span)) ++
goCase(Some(tmp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
astParentType = scope.surroundingAstLabel,
astParentFullName = scope.surroundingScopeFullName
)
scope.pushNewScope(MethodScope(fullName, Left("")))
scope.pushNewScope(MethodScope(fullName, procParamGen.fresh))
val block_ = blockNode(node)
scope.pushNewScope(BlockScope(block_))
// TODO: Should it be `return this.@abc`?
Expand Down Expand Up @@ -155,7 +155,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
astParentType = scope.surroundingAstLabel,
astParentFullName = scope.surroundingScopeFullName
)
scope.pushNewScope(MethodScope(fullName, Left("")))
scope.pushNewScope(MethodScope(fullName, procParamGen.fresh))
val parameter = parameterInNode(node, "x", "x", 1, false, EvaluationStrategies.BY_REFERENCE)
val methodBody = {
val block_ = blockNode(node)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ import org.antlr.v4.runtime.tree.{ParseTree, RuleNode}
import io.joern.x2cpg.Defines as XDefines;

import scala.jdk.CollectionConverters.*
import io.joern.rubysrc2cpg.utils.FreshNameGenerator

/** Converts an ANTLR Ruby Parse Tree into the intermediate Ruby AST.
*/
class RubyNodeCreator extends RubyParserBaseVisitor[RubyNode] {

private var classCounter: Int = 0

private def tmpClassTemplate(id: Int): String = s"<anon-class-$id>"

private val classNameGen = FreshNameGenerator(id => s"<anon-class-$id>")
protected def freshClassName(span: TextSpan): SimpleIdentifier = {
val name = tmpClassTemplate(classCounter)
classCounter += 1
SimpleIdentifier(None)(span.spanStart(name))
SimpleIdentifier(None)(span.spanStart(classNameGen.fresh))
}

private def defaultTextSpan(code: String = ""): TextSpan = TextSpan(None, None, None, None, code)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.joern.rubysrc2cpg.utils

class FreshNameGenerator[T](template: Int => T) {
private var counter: Int = 0
def fresh: T = {
val name = template(counter)
counter += 1
name
}
}

0 comments on commit bc8af93

Please sign in to comment.