Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix comments and formatting. * Added missing comments. * Introduce AstNode. * scalafmt. * Move EnhancedBlock into its own file. * Export implicit + comments. * Cleanup. * More cleanup. * Add AST_NODE base type to (hopefully) all correct child types. * Cleanup + test. * Completed AST traversals. * scalafmt. * Address review. * Rename. * Make constants static.
- Loading branch information
Showing
24 changed files
with
392 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
cpgqueryingtests/src/test/scala/io/shiftleft/cpgqueryingtests/steps/CAstTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.shiftleft.cpgqueryingtests.steps | ||
|
||
import io.shiftleft.cpgqueryingtests.codepropertygraph.{CpgFactory, LanguageFrontend} | ||
|
||
class CAstTests extends CpgDataFlowTests { | ||
val cpgFactory = new CpgFactory(LanguageFrontend.Fuzzyc, "cpgqueryingtests/src/test/resources/default.semantics") | ||
|
||
val code = | ||
""" | ||
| int foo(int y) { | ||
| int x = 10; | ||
| if (x > 10) { | ||
| return bar(x + 10); | ||
| } else { | ||
| if (y > x) { | ||
| printf("reached"); | ||
| } | ||
| } | ||
| } | ||
""".stripMargin | ||
|
||
"should identify four blocks" in { | ||
cpgFactory.buildCpg(code) { cpg => | ||
cpg.method.name("foo").ast.isBlock.l.size shouldBe 4 | ||
} | ||
} | ||
|
||
"should identify three control structures" in { | ||
cpgFactory.buildCpg(code) { cpg => | ||
cpg.method | ||
.name("foo") | ||
.ast | ||
.isControlStructure | ||
.parserTypeName("IfStatement") | ||
.l | ||
.size shouldBe 2 | ||
|
||
cpg.method | ||
.name("foo") | ||
.ast | ||
.isControlStructure | ||
.parserTypeName("ElseStatement") | ||
.l | ||
.size shouldBe 1 | ||
} | ||
} | ||
|
||
"should identify conditions" in { | ||
cpgFactory.buildCpg(code) { cpg => | ||
cpg.method.name("foo").ast.isControlStructure.condition.code.l shouldBe List("x > 10", "", "y > x") | ||
} | ||
} | ||
|
||
"should allow filtering on conditions" in { | ||
cpgFactory.buildCpg(code) { cpg => | ||
cpg.method | ||
.name("foo") | ||
.condition(".*x > 10.*") | ||
.l | ||
.size shouldBe 1 | ||
|
||
cpg.method | ||
.name("foo") | ||
.condition(".*x > 10.*") | ||
.whenTrue | ||
.ast | ||
.isReturnNode | ||
.code | ||
.l shouldBe List("return bar(x + 10);") | ||
|
||
cpg.method | ||
.name("foo") | ||
.condition(".*x > 10.*") | ||
.whenFalse | ||
.ast | ||
.isCall | ||
.code(".*printf.*") | ||
.code | ||
.l shouldBe List("printf(\"reached\")") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
query-primitives/src/main/scala/io/shiftleft/queryprimitives/steps/AstNodeMethods.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.shiftleft.queryprimitives.steps | ||
|
||
import io.shiftleft.codepropertygraph.generated.nodes | ||
import io.shiftleft.queryprimitives.steps.types.expressions.generalizations.AstNode | ||
import shapeless.HNil | ||
|
||
class AstNodeMethods(val node: nodes.AstNode) extends AnyVal { | ||
|
||
// Shorthands to allow ast/children to be executed | ||
// on single nodes. | ||
|
||
def ast: AstNode[HNil] = node.start.ast | ||
|
||
def children: AstNode[HNil] = node.start.astMinusRoot | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.