Skip to content

Commit

Permalink
[c2cpg] Restore pointer operator code representation (#4894)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser authored Sep 2, 2024
1 parent a997dc3 commit 72bc423
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
}
if (pointers.isEmpty) { s"$tpe$arr" }
else {
val refs = "*" * (pointers.length - pointers.count(_.isInstanceOf[ICPPASTReferenceOperator]))
val refs = pointers.map(_.getRawSignature).mkString("")
s"$tpe$arr$refs".strip()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
val name = Operators.alloc
val tpe = registerType(typeFor(d))
val codeString = code(d)
val allocCallNode = callNode(d, code(d), name, name, DispatchTypes.STATIC_DISPATCH, None, Some(tpe))
val allocCallNode = callNode(d, codeString, name, name, DispatchTypes.STATIC_DISPATCH, None, Some(tpe))
val allocCallAst = callAst(allocCallNode, d.getArrayModifiers.toIndexedSeq.map(astForNode))
val operatorName = Operators.assignment
val assignmentCallNode =
callNode(d, code(d), operatorName, operatorName, DispatchTypes.STATIC_DISPATCH, None, Some(tpe))
callNode(d, codeString, operatorName, operatorName, DispatchTypes.STATIC_DISPATCH, None, Some(tpe))
val left = astForNode(d.getName)
callAst(assignmentCallNode, List(left, allocCallAst))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,20 @@ class AstCreationPassTests extends AstC2CpgSuite {
}
}

"be correct for decl assignment with references" in {
val cpg = code(
"""
|int addrOfLocalRef(struct x **foo) {
| struct x &bar = **foo;
| *foo = &bar;
|}""".stripMargin,
"foo.cc"
)
val List(barLocal) = cpg.method.nameExact("addrOfLocalRef").local.l
barLocal.name shouldBe "bar"
barLocal.code shouldBe "struct x& bar"
}

"be correct for decl assignment of multiple locals" in {
val cpg = code("""
|void method(int x, int y) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class MethodTests extends C2CpgSuite {
data.index shouldBe 1
data.name shouldBe "data"
data.code shouldBe "int &data"
data.typeFullName shouldBe "int"
data.typeFullName shouldBe "int&"
data.isVariadic shouldBe false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ class ClassTypeTests extends C2CpgSuite(FileDefaults.CPP_EXT) {
| ): Bar::Foo(a, b) {}
|}""".stripMargin)
val List(constructor) = cpg.typeDecl.nameExact("FooT").method.isConstructor.l
constructor.signature shouldBe "Bar.Foo(std.string,Bar.SomeClass)"
constructor.signature shouldBe "Bar.Foo(std.string&,Bar.SomeClass&)"
val List(thisP, p1, p2) = constructor.parameter.l
thisP.name shouldBe "this"
thisP.typeFullName shouldBe "FooT"
thisP.index shouldBe 0
p1.typ.fullName shouldBe "std.string"
p1.typ.fullName shouldBe "std.string&"
p1.index shouldBe 1
p2.typ.fullName shouldBe "Bar.SomeClass"
p2.typ.fullName shouldBe "Bar.SomeClass&"
p2.index shouldBe 2
}
}
Expand Down

0 comments on commit 72bc423

Please sign in to comment.