Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Commit b859267

Browse files
authored
Cleanup CodeLocation[Extractor] and fill in end line and column (#135)
* Cleanup `CodeLocation[Extractor]` and fill in end line and column. * Address review.
1 parent 9ee0d1f commit b859267

File tree

10 files changed

+49
-44
lines changed

10 files changed

+49
-44
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ organization := "io.shiftleft"
33
scalaVersion := "2.12.10"
44
enablePlugins(GitVersioning)
55

6-
val cpgVersion = "0.10.26"
6+
val cpgVersion = "0.10.78"
77

88
libraryDependencies ++= Seq(
99
"com.github.scopt" %% "scopt" % "3.7.0",

src/main/java/io/shiftleft/fuzzyc2cpg/ast/AstNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class AstNode {
1212

1313
protected List<AstNode> children;
1414
protected int childNumber;
15-
private CodeLocation location = new CodeLocation();
15+
private CodeLocation location = CodeLocation.apply();
1616
private boolean isInCFG = false;
1717

1818
// refers to the parsed source code, used for wiring goto labels

src/main/java/io/shiftleft/fuzzyc2cpg/ast/CodeLocation.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/io/shiftleft/fuzzyc2cpg/parser/CodeLocationExtractor.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/main/scala/io/shiftleft/fuzzyc2cpg/adapter/CpgAdapter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object NodeProperty extends Enumeration {
1010
type NodeProperty = Value
1111
val ORDER, ARGUMENT_INDEX, NAME, FULL_NAME, CODE, EVALUATION_STRATEGY, TYPE_FULL_NAME, TYPE_DECL_FULL_NAME, SIGNATURE,
1212
DISPATCH_TYPE, METHOD_FULL_NAME, METHOD_INST_FULL_NAME, IS_EXTERNAL, PARSER_TYPE_NAME, AST_PARENT_TYPE,
13-
AST_PARENT_FULL_NAME, LINE_NUMBER, COLUMN_NUMBER, ALIAS_TYPE_FULL_NAME = Value
13+
AST_PARENT_FULL_NAME, LINE_NUMBER, COLUMN_NUMBER, LINE_NUMBER_END, COLUMN_NUMBER_END, ALIAS_TYPE_FULL_NAME = Value
1414
}
1515

1616
object NodeKind extends Enumeration {

src/main/scala/io/shiftleft/fuzzyc2cpg/adapter/ProtoCpgAdapter.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class ProtoCpgAdapter(targetCpg: CpgStruct.Builder) extends CpgAdapter[Node.Buil
9595
NodePropertyName.AST_PARENT_FULL_NAME
9696
case NodeProperty.LINE_NUMBER => NodePropertyName.LINE_NUMBER
9797
case NodeProperty.COLUMN_NUMBER => NodePropertyName.COLUMN_NUMBER
98+
case NodeProperty.LINE_NUMBER_END => NodePropertyName.LINE_NUMBER_END
99+
case NodeProperty.COLUMN_NUMBER_END => NodePropertyName.COLUMN_NUMBER_END
98100
case NodeProperty.ALIAS_TYPE_FULL_NAME => NodePropertyName.ALIAS_TYPE_FULL_NAME
99101
}
100102
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.shiftleft.fuzzyc2cpg.ast
2+
3+
object CodeLocation {
4+
def apply: CodeLocation = new CodeLocation()
5+
}
6+
7+
case class CodeLocation(startLine: Option[Int] = None,
8+
startPos: Option[Int] = None,
9+
startIndex: Option[Int] = None,
10+
endIndex: Option[Int] = None,
11+
endLine: Option[Int] = None,
12+
endPos: Option[Int] = None) {
13+
14+
override def toString: String =
15+
String.format("%d:%d:%d:%d:%d:%d", startLine, startPos, startIndex, endIndex, endLine, endPos)
16+
}

src/main/scala/io/shiftleft/fuzzyc2cpg/astnew/AstToCpgConverter.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ class AstToCpgConverter[NodeBuilderType, NodeType, EdgeBuilderType, EdgeType](
6262
adapter.addNodeProperty(nodeBuilder, property, value)
6363
nodeBuilder
6464
}
65+
66+
def addProperty(property: NodeProperty, value: Option[Int]): NodeBuilderType = {
67+
value.foreach(adapter.addNodeProperty(nodeBuilder, property, _))
68+
nodeBuilder
69+
}
70+
6571
def addProperty(property: NodeProperty, value: Int): NodeBuilderType = {
6672
adapter.addNodeProperty(nodeBuilder, property, value)
6773
nodeBuilder
@@ -122,6 +128,8 @@ class AstToCpgConverter[NodeBuilderType, NodeType, EdgeBuilderType, EdgeType](
122128
.addProperty(NodeProperty.FULL_NAME, value = s"${astFunction.getName}")
123129
.addProperty(NodeProperty.LINE_NUMBER, astFunction.getLocation.startLine)
124130
.addProperty(NodeProperty.COLUMN_NUMBER, astFunction.getLocation.startPos)
131+
.addProperty(NodeProperty.LINE_NUMBER_END, astFunction.getLocation.endLine)
132+
.addProperty(NodeProperty.COLUMN_NUMBER_END, astFunction.getLocation.endPos)
125133
.addProperty(NodeProperty.SIGNATURE, signature)
126134
.createNode(astFunction)
127135

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.shiftleft.fuzzyc2cpg.parser
2+
3+
import io.shiftleft.fuzzyc2cpg.ast.CodeLocation
4+
import org.antlr.v4.runtime.ParserRuleContext
5+
6+
object CodeLocationExtractor {
7+
8+
def extractFromContext(ctx: ParserRuleContext): CodeLocation = {
9+
val startLine = Some(ctx.start.getLine)
10+
val startPos = Some(ctx.start.getCharPositionInLine)
11+
val startIndex = Some(ctx.start.getStartIndex)
12+
val endLine = Option(ctx.stop).map(_.getLine)
13+
val endIndex = Option(ctx.stop).map(_.getStopIndex)
14+
val endPos = Option(ctx.stop).map(_.getCharPositionInLine)
15+
CodeLocation(startLine, startPos, startIndex, endIndex, endLine, endPos)
16+
}
17+
18+
}

src/test/scala/io/shiftleft/fuzzyc2cpg/MethodHeaderTests.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class MethodHeaderTests extends WordSpec with Matchers {
1717
methods.head.value2(NodeKeys.SIGNATURE) shouldBe "int(int,int)"
1818
methods.head.value2(NodeKeys.LINE_NUMBER) shouldBe 1
1919
methods.head.value2(NodeKeys.COLUMN_NUMBER) shouldBe 0
20+
methods.head.value2(NodeKeys.LINE_NUMBER_END) shouldBe 3
21+
methods.head.value2(NodeKeys.COLUMN_NUMBER_END) shouldBe 0
2022
}
2123

2224
"have correct METHOD_PARAMETER_IN nodes for method foo" in {

0 commit comments

Comments
 (0)