Skip to content

Commit

Permalink
Accurate Definition .code Property (#231)
Browse files Browse the repository at this point in the history
* Code property on assignments is now generated from child AST nodes

* Updated tests

* Updated changelog
  • Loading branch information
DavidBakerEffendi authored Feb 16, 2022
1 parent 1b2d8d0 commit 49cde00
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- Generate `<operator>.assignment` call node's `code` property from child argument `code`.

## [1.0.11] - 2022-02-15

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,18 @@ class PlumeAstCreator(filename: String, global: Global) {
case x: FieldRef => Seq(astForFieldRef(x, 1, assignStmt))
case x => astsForValue(x, 1, assignStmt)
}
val initAsts = astsForValue(initializer, 2, assignStmt)
val assignmentRhsCode = initAsts
.flatMap(_.root)
.map(_.properties.getOrElse(PropertyNames.CODE, ""))
.mkString(", ")
val assignment = NewCall()
.name(Operators.assignment)
.code(s"$name = ${initializer.toString()}")
.code(s"$name = $assignmentRhsCode")
.dispatchType(DispatchTypes.STATIC_DISPATCH)
.order(order)
.argumentIndex(order)
.typeFullName(registerType(assignStmt.getLeftOp.getType.toQuotedString))

val initAsts = astsForValue(initializer, 2, assignStmt)
val initializerAst = Seq(callAst(assignment, identifier ++ initAsts))
Seq(
Ast(
Expand Down
14 changes: 7 additions & 7 deletions src/test/scala/com/github/plume/oss/querying/CfgTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CfgTests extends Jimple2CpgFixture {
| int foo(int x, int y) {
| if (y < 10)
| return -1;
| if (x < 10) {
| if (x < 5) {
| sink(x);
| }
| System.out.println("foo");
Expand All @@ -28,7 +28,7 @@ class CfgTests extends Jimple2CpgFixture {
"should find that sink is control dependent on condition" in {
val controllers = cpg.call("sink").controlledBy.isCall.toSet
controllers.map(_.code) should contain("y >= 10")
controllers.map(_.code) should contain("x >= 10")
controllers.map(_.code) should contain("x >= 5")
}

// No control structure node on flat ast
Expand All @@ -40,13 +40,13 @@ class CfgTests extends Jimple2CpgFixture {
// cpg.call("sink").dominates.l.size shouldBe 0
// }

"should find sink(x) is dominated by `x<10` and `y < 10`" in {
"should find sink(x) is dominated by `x < 5` and `y < 10`" in {
cpg.call("sink").dominatedBy.isCall.code.toSet shouldBe Set(
"y = @parameter1: int",
"x >= 10",
"x >= 5",
"this = this",
"x = @parameter0",
"y >= 10",
"this = @this: Foo",
"x = @parameter0: int"
"y = @parameter1"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class StaticCallGraphTests extends Jimple2CpgFixture {
Set(
"println($stack3)",
"add(3, 3)",
"$stack2 = <java.lang.System: java.io.PrintStream out>",
"argc = @parameter0: int",
"$stack3 = staticinvoke <Foo: int add(int,int)>(3, 3)",
"argv = @parameter1: char",
"argc = @parameter0",
"$stack2 = java.lang.System.out",
"argv = @parameter1",
"$stack3 = add(3, 3)",
"java.lang.System.out"
)
}
Expand Down

0 comments on commit 49cde00

Please sign in to comment.