forked from joernio/joern
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[c#] Ast for
foreach
statements (joernio#4072)
This PR includes, 1. Upgrading to new `DotNetAstGen` version. 2. AST Creation for `foreach` statements. 3. Tests for `foreach` statements. 4. Type Mapping for `PredefinedType` node. Resolves joernio#3986
- Loading branch information
1 parent
cc1db59
commit 52a1016
Showing
5 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
joern-cli/frontends/csharpsrc2cpg/src/main/resources/application.conf
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
csharpsrc2cpg { | ||
dotnetastgen_version: "0.14.0" | ||
dotnetastgen_version: "0.15.0" | ||
} |
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
43 changes: 43 additions & 0 deletions
43
...ontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/LoopsTests.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,43 @@ | ||
package io.joern.csharpsrc2cpg.querying.ast | ||
|
||
import io.joern.csharpsrc2cpg.testfixtures.CSharpCode2CpgFixture | ||
import io.shiftleft.codepropertygraph.generated.ControlStructureTypes | ||
import io.shiftleft.semanticcpg.language.* | ||
|
||
class LoopsTests extends CSharpCode2CpgFixture { | ||
"AST Creation for loops" should { | ||
"be correct for foreach statement" in { | ||
val cpg = code(basicBoilerplate(""" | ||
|List<int> fibNumbers = [0, 1, 1, 2, 3, 5, 8, 13]; | ||
|foreach (int element in fibNumbers) | ||
|{ | ||
| Console.Write($"{element} "); | ||
|} | ||
|""".stripMargin)) | ||
|
||
inside(cpg.method("Main").controlStructure.l) { | ||
case forEachNode :: Nil => | ||
forEachNode.controlStructureType shouldBe ControlStructureTypes.FOR | ||
|
||
inside(forEachNode.astChildren.isIdentifier.l) { | ||
case iteratorNode :: iterableNode :: Nil => | ||
iteratorNode.code shouldBe "element" | ||
iteratorNode.typeFullName shouldBe "System.Int32" | ||
|
||
iterableNode.code shouldBe "fibNumbers" | ||
iterableNode.typeFullName shouldBe "List<int>" | ||
case _ => fail("No node for iterable found in `foreach` statement") | ||
} | ||
|
||
inside(forEachNode.astChildren.isBlock.l) { | ||
case blockNode :: Nil => | ||
val List(writeCall) = cpg.call.nameExact("Write").l | ||
writeCall.astParent shouldBe blockNode | ||
case _ => fail("Correct blockNode as child not found for `foreach` statement") | ||
} | ||
|
||
case _ => fail("No control structure node found for `foreach`.") | ||
} | ||
} | ||
} | ||
} |