Skip to content

Commit

Permalink
[c#] finish refactoring astForSimpleMemberAccess (#5198)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierpinho authored Jan 5, 2025
1 parent 56463fd commit ec2b517
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,40 +311,18 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
Seq(_callAst)
}

protected def astForSimpleMemberAccessExpression(accessExpr: DotNetNodeInfo): Seq[Ast] = {
private def astForSimpleMemberAccessExpression(accessExpr: DotNetNodeInfo): Seq[Ast] = {
val fieldIdentifierName = nameFromNode(accessExpr)

val (identifierName, typeFullName) = {
createDotNetNodeInfo(accessExpr.json(ParserKeys.Expression)).node match
case SuppressNullableWarningExpression =>
val baseNode = createDotNetNodeInfo(accessExpr.json(ParserKeys.Expression)(ParserKeys.Operand))
val baseAst = astForNode(baseNode)
val baseTypeFullName = getTypeFullNameFromAstNode(baseAst)

val fieldInScope = scope.tryResolveFieldAccess(fieldIdentifierName, typeFullName = Option(baseTypeFullName))

(
nameFromNode(baseNode),
fieldInScope
.map(_.typeName)
.getOrElse(Defines.Any)
)
case _ => {
val fieldInScope = scope.findFieldInScope(fieldIdentifierName)
val _identifierName =
if (fieldInScope.nonEmpty && fieldInScope.exists(_.isStatic))
scope.surroundingTypeDeclFullName.getOrElse(Defines.Any)
else Constants.This
val _typeFullName = fieldInScope.map(_.typeFullName).getOrElse(Defines.Any)
(_identifierName, _typeFullName)
}
}

val identifier = newIdentifierNode(identifierName, typeFullName)
val baseAst = astForNode(createDotNetNodeInfo(accessExpr.json(ParserKeys.Expression))).head
val baseTypeFullName = getTypeFullNameFromAstNode(baseAst)
val typeFullName = scope
.tryResolveFieldAccess(fieldIdentifierName, Some(baseTypeFullName))
.map(_.typeName)
.getOrElse(Defines.Any)

fieldAccessAst(
Ast(identifier),
s"$identifierName.$fieldIdentifierName",
baseAst,
accessExpr.code,
line(accessExpr),
column(accessExpr),
fieldIdentifierName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,22 @@ trait AstSummaryVisitor(implicit withSchemaValidation: ValidationMode) { this: A
CSharpField(f.name, f.typeFullName)
}

val mapping = mutable.Map
.from(cpg.namespaceBlock.map { namespace =>
namespace.fullName -> mutable.Set.from(namespace.typeDecl.map { typ =>
CSharpType(typ.fullName, typ.method.map(toMethod).l, typ.member.map(toField).l)
})
})
.asInstanceOf[NamespaceToTypeMap]
def toType(t: TypeDecl): CSharpType = {
CSharpType(t.fullName, t.method.map(toMethod).l, t.member.map(toField).l)
}

val mapping = {
// TypeDecls found inside explicit namespace blocks
val withExplicitNamespace = cpg.namespaceBlock.map { namespace =>
namespace.fullName -> mutable.Set.from(namespace.typeDecl.map(toType))
}

// TypeDecls found outside explicit namespace blocks
val withoutExplicitNamespace = Set("" -> mutable.Set.from(cpg.typeDecl.whereNot(_.namespaceBlock).map(toType)))

mutable.Map.from(withExplicitNamespace ++ withoutExplicitNamespace)
}

CSharpProgramSummary(mapping, imports, globalImports)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class MemberTests extends CSharpCode2CpgFixture {
| int b;
|
| static Car() { // static constructor
| this.nonInitMaxSpeed = 2000;
| nonInitMaxSpeed = 2000;
| }
|
| public Car() {
Expand All @@ -347,7 +347,7 @@ class MemberTests extends CSharpCode2CpgFixture {
inside(m.body.astChildren.isCall.l) {
case staticImplicit :: staticExplicit :: Nil =>
staticExplicit.methodFullName shouldBe Operators.assignment
staticExplicit.code shouldBe "this.nonInitMaxSpeed = 2000"
staticExplicit.code shouldBe "nonInitMaxSpeed = 2000"

inside(staticExplicit.argument.fieldAccess.l) {
case fieldAccess :: Nil =>
Expand Down

0 comments on commit ec2b517

Please sign in to comment.