From d5020a5e5fcf8d00b8bd1c6c263a04872bb95eda Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 02:05:49 +0800 Subject: [PATCH 01/11] import dot ast --- .../dotgenerator/DotSerializer.scala | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala index 88029763adc2..b1b190077000 100644 --- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala +++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala @@ -4,10 +4,11 @@ import io.shiftleft.codepropertygraph.generated.PropertyNames import io.shiftleft.codepropertygraph.generated.nodes._ import io.shiftleft.semanticcpg.language._ import io.shiftleft.semanticcpg.utils.MemberAccess +import org.apache.commons.lang.StringUtils -import java.util.Optional import scala.collection.immutable.HashMap import scala.collection.mutable +import scala.jdk.OptionConverters.RichOptional import scala.language.postfixOps object DotSerializer { @@ -36,6 +37,7 @@ object DotSerializer { case Some(r) => namedGraphBegin(r) case None => defaultGraphBegin() } + sb.append("node [shape=\"rect\"];\n") val nodeStrings = graph.vertices.map(nodeToDot) val edgeStrings = graph.edges.map(e => edgeToDot(e, withEdgeTypes)) val subgraphStrings = graph.subgraph.zipWithIndex.map { case ((subgraph, nodes), idx) => @@ -61,23 +63,30 @@ object DotSerializer { } private def stringRepr(vertex: StoredNode): String = { - val maybeLineNo: Optional[AnyRef] = vertex.propertyOption(PropertyNames.LINE_NUMBER) - escape(vertex match { - case call: Call => (call.name, call.code).toString - case expr: Expression => (expr.label, expr.code, toCfgNode(expr).code).toString - case method: Method => (method.label, method.name).toString - case ret: MethodReturn => (ret.label, ret.typeFullName).toString - case param: MethodParameterIn => ("PARAM", param.code).toString - case local: Local => (local.label, s"${local.code}: ${local.typeFullName}").toString - case target: JumpTarget => (target.label, target.name).toString - case modifier: Modifier => (modifier.label, modifier.modifierType).toString() - case annoAssign: AnnotationParameterAssign => (annoAssign.label, annoAssign.code).toString() - case annoParam: AnnotationParameter => (annoParam.label, annoParam.code).toString() - case typ: Type => (typ.label, typ.name).toString() - case typeDecl: TypeDecl => (typeDecl.label, typeDecl.name).toString() - case member: Member => (member.label, member.name).toString() - case _ => "" - }) + (if (maybeLineNo.isPresent) s"${maybeLineNo.get()}" else "") + val lineOpt = vertex.propertyOption(PropertyNames.LINE_NUMBER).toScala.map(_.toString) + val list = (vertex match { + case call: Call => List(call.label, call.name, call.code) + case expr: Expression => List(expr.label, expr.code, toCfgNode(expr).code) + case method: Method => List(method.label, method.name) + case ret: MethodReturn => List(ret.label, ret.typeFullName) + case param: MethodParameterIn => List("PARAM", param.code) + case local: Local => List(local.label, s"${local.code}: ${local.typeFullName}") + case target: JumpTarget => List(target.label, target.name) + case modifier: Modifier => List(modifier.label, modifier.modifierType) + case annoAssign: AnnotationParameterAssign => List(annoAssign.label, annoAssign.code) + case annoParam: AnnotationParameter => List(annoParam.label, annoParam.code) + case typ: Type => List(typ.label, typ.name) + case typeDecl: TypeDecl => List(typeDecl.label, typeDecl.name) + case member: Member => List(member.label, member.name) + case others: AstNode => List(others.label, others.code) + }).map(StringUtils.abbreviate(_, 50)) + .map(StringUtils.normalizeSpace) + .map(escape) + (lineOpt match { + case Some(line) => s"${list.head}, $line" :: list.tail + case None => list + }).distinct // expr.code, toCfgNode(expr).code could be the same + .mkString("
") } private def toCfgNode(node: StoredNode): CfgNode = { @@ -96,7 +105,7 @@ object DotSerializer { } private def nodeToDot(node: StoredNode): String = { - s""""${node.id}" [label = <${stringRepr(node)}> ]""".stripMargin + s""""${node.id}" [label = <${stringRepr(node)}>]""".stripMargin } private def edgeToDot(edge: Edge, withEdgeTypes: Boolean): String = { From a67820678d600ce8d7af0477d58a79399d1ee5ea Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 02:05:49 +0800 Subject: [PATCH 02/11] import dot ast --- .../dotgenerator/DotSerializer.scala | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala index 88029763adc2..d06badd93710 100644 --- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala +++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala @@ -4,10 +4,11 @@ import io.shiftleft.codepropertygraph.generated.PropertyNames import io.shiftleft.codepropertygraph.generated.nodes._ import io.shiftleft.semanticcpg.language._ import io.shiftleft.semanticcpg.utils.MemberAccess +import org.apache.commons.lang.StringUtils -import java.util.Optional import scala.collection.immutable.HashMap import scala.collection.mutable +import scala.jdk.OptionConverters.RichOptional import scala.language.postfixOps object DotSerializer { @@ -36,6 +37,7 @@ object DotSerializer { case Some(r) => namedGraphBegin(r) case None => defaultGraphBegin() } + sb.append("node [shape=\"rect\"];\n") val nodeStrings = graph.vertices.map(nodeToDot) val edgeStrings = graph.edges.map(e => edgeToDot(e, withEdgeTypes)) val subgraphStrings = graph.subgraph.zipWithIndex.map { case ((subgraph, nodes), idx) => @@ -61,23 +63,30 @@ object DotSerializer { } private def stringRepr(vertex: StoredNode): String = { - val maybeLineNo: Optional[AnyRef] = vertex.propertyOption(PropertyNames.LINE_NUMBER) - escape(vertex match { - case call: Call => (call.name, call.code).toString - case expr: Expression => (expr.label, expr.code, toCfgNode(expr).code).toString - case method: Method => (method.label, method.name).toString - case ret: MethodReturn => (ret.label, ret.typeFullName).toString - case param: MethodParameterIn => ("PARAM", param.code).toString - case local: Local => (local.label, s"${local.code}: ${local.typeFullName}").toString - case target: JumpTarget => (target.label, target.name).toString - case modifier: Modifier => (modifier.label, modifier.modifierType).toString() - case annoAssign: AnnotationParameterAssign => (annoAssign.label, annoAssign.code).toString() - case annoParam: AnnotationParameter => (annoParam.label, annoParam.code).toString() - case typ: Type => (typ.label, typ.name).toString() - case typeDecl: TypeDecl => (typeDecl.label, typeDecl.name).toString() - case member: Member => (member.label, member.name).toString() - case _ => "" - }) + (if (maybeLineNo.isPresent) s"${maybeLineNo.get()}" else "") + val lineOpt = vertex.propertyOption(PropertyNames.LINE_NUMBER).toScala.map(_.toString) + val list = (vertex match { + case call: Call => List(call.label, call.name, call.code) + case expr: Expression => List(expr.label, expr.code, toCfgNode(expr).code) + case method: Method => List(method.label, method.name) + case ret: MethodReturn => List(ret.label, ret.typeFullName) + case param: MethodParameterIn => List("PARAM", param.code) + case local: Local => List(local.label, s"${local.code}: ${local.typeFullName}") + case target: JumpTarget => List(target.label, target.name) + case modifier: Modifier => List(modifier.label, modifier.modifierType) + case annoAssign: AnnotationParameterAssign => List(annoAssign.label, annoAssign.code) + case annoParam: AnnotationParameter => List(annoParam.label, annoParam.code) + case typ: Type => List(typ.label, typ.name) + case typeDecl: TypeDecl => List(typeDecl.label, typeDecl.name) + case member: Member => List(member.label, member.name) + case others: AstNode => List(others.label, others.code) + }).map(StringUtils.abbreviate(_, 50)) + .map(StringUtils.normalizeSpace) + .map(escape) + (lineOpt match { + case Some(line) => s"${list.head}, $line" :: list.tail + case None => list + }).distinct // expr.code, toCfgNode(expr).code could be the same + .mkString("
") } private def toCfgNode(node: StoredNode): CfgNode = { From 25cd880ccf277b1c493ade5d8c3d636ff5bd021a Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 19:40:57 +0800 Subject: [PATCH 03/11] update tests --- .../c2cpg/io/dotgenerator/DotAstGeneratorTests.scala | 8 ++++---- .../c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala | 12 ++++++------ .../c2cpg/io/dotgenerator/DotCfgGeneratorTests.scala | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala index e504e951950e..f9bce8749366 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala @@ -32,7 +32,7 @@ class DotAstGeneratorTests extends CCodeToCpgSuite { inside(cpg.method.name("my_func").dotAst.l) { case List(x) => x should ( startWith("digraph \"my_func\"") and - include("""[label = <(CONTROL_STRUCTURE,if (y > 42),if (y > 42))5> ]""") and + include("""[label = <operator>.greaterThan
y > 42>]""") and endWith("}\n") ) } @@ -52,7 +52,7 @@ class DotAstGeneratorTests extends CCodeToCpgSuite { "allow plotting sub trees of methods" in { inside(cpg.method.ast.isControlStructure.code(".*y > 42.*").dotAst.l) { case List(x, _) => - x should (include("y > 42") and include("IDENTIFIER,y") and not include "x * 2") + x should (include("y > 42") and include("IDENTIFIER, 5
y
y ") and not include "x * 2") } } @@ -60,9 +60,9 @@ class DotAstGeneratorTests extends CCodeToCpgSuite { inside(cpg.method.name("lemon").dotAst.l) { case List(x) => x should ( startWith("digraph \"lemon\"") and - include("""[label = <(goog,goog("\"yes\""))18> ]""") and + include("""[label = goog
goog("\"yes\"")>]""") and include( - """[label = <(LITERAL,"\"yes\"",goog("\"yes\""))18> ]""" + """[label = "\"yes\""
goog("\"yes\"")>]""" ) and endWith("}\n") ) diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala index 08778bd8ec6e..7e459688188c 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala @@ -19,9 +19,9 @@ class DotCdgGeneratorTests extends DataFlowCodeToCpgSuite { inside(cpg.method.name("foo").dotCdg.l) { case List(x) => x should ( startWith("digraph \"foo\"") and - include("""[label = <(<operator>.greaterThan,x > 8)3> ]""") and - include("""[label = <(<operator>.assignment,z = a(x))4> ]""") and - include("""[label = <(a,a(x))4> ]""") and + include("""[label = <operator>.greaterThan
x > 8>]""") and + include("""[label = <operator>.assignment
z = a(x)>]""") and + include("""[label = a
a(x)>]""") and endWith("}\n") ) val lines = x.split("\n") @@ -46,9 +46,9 @@ class DotCdgGeneratorTests extends DataFlowCodeToCpgSuite { inside(cpg.method.name("foo").dotCdg.l) { case List(x) => x should ( startWith("digraph \"foo\"") and - include("""[label = <(<operator>.greaterThan,x > 8)3> ]""") and - include("""[label = <(<operator>.assignment,z = a(x))4> ]""") and - include("""[label = <(a,a(x))4> ]""") and + include("""[label = <operator>.greaterThan
x > 8>]""") and + include("""[label = <operator>.assignment
z = a(x)>]""") and + include("""[label = a
a(x)>]""") and endWith("}\n") ) val lines = x.split("\n") diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCfgGeneratorTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCfgGeneratorTests.scala index 237944ea4d64..59b06936439c 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCfgGeneratorTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCfgGeneratorTests.scala @@ -21,7 +21,7 @@ class DotCfgGeneratorTests extends CCodeToCpgSuite { inside(cpg.method.name("main").dotCfg.l) { case List(dotStr) => dotStr should ( startWith("digraph \"main\" {") and - include("(<operator>.assignment,i = 0)") and + include("<operator>.assignment
i = 0") and endWith("}\n") ) } From a2a5ae8a830ffe3d890a16b652a64a36869947fb Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 19:44:47 +0800 Subject: [PATCH 04/11] update tests --- .../io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala index f9bce8749366..77fa35b6b61e 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala @@ -32,7 +32,7 @@ class DotAstGeneratorTests extends CCodeToCpgSuite { inside(cpg.method.name("my_func").dotAst.l) { case List(x) => x should ( startWith("digraph \"my_func\"") and - include("""[label = <operator>.greaterThan
y > 42>]""") and + include("""[label = if (y > 42)>]""") and endWith("}\n") ) } From a8efef3c998aec61901089bf5e2307c1d30932b3 Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 19:47:49 +0800 Subject: [PATCH 05/11] update tests --- .../io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala index 77fa35b6b61e..11ccdc02eb7e 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala @@ -52,7 +52,7 @@ class DotAstGeneratorTests extends CCodeToCpgSuite { "allow plotting sub trees of methods" in { inside(cpg.method.ast.isControlStructure.code(".*y > 42.*").dotAst.l) { case List(x, _) => - x should (include("y > 42") and include("IDENTIFIER, 5
y
y ") and not include "x * 2") + x should (include("y > 42") and include("IDENTIFIER, 5
y") and not include "x * 2") } } From 7c09f7cc4e3c637bedec9e16fc2cbde328af042e Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 19:58:46 +0800 Subject: [PATCH 06/11] update tests --- .../c2cpg/io/dotgenerator/DotAstGeneratorTests.scala | 6 +++--- .../c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala | 12 ++++++------ .../semanticcpg/dotgenerator/DotSerializer.scala | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala index 11ccdc02eb7e..66dce1402277 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala @@ -32,7 +32,7 @@ class DotAstGeneratorTests extends CCodeToCpgSuite { inside(cpg.method.name("my_func").dotAst.l) { case List(x) => x should ( startWith("digraph \"my_func\"") and - include("""[label = if (y > 42)>]""") and + include("""[label = if (y > 42)> ]""") and endWith("}\n") ) } @@ -60,9 +60,9 @@ class DotAstGeneratorTests extends CCodeToCpgSuite { inside(cpg.method.name("lemon").dotAst.l) { case List(x) => x should ( startWith("digraph \"lemon\"") and - include("""[label = goog
goog("\"yes\"")>]""") and + include("""[label = goog
goog("\"yes\"")> ]""") and include( - """[label = "\"yes\""
goog("\"yes\"")>]""" + """[label = "\"yes\""
goog("\"yes\"")> ]""" ) and endWith("}\n") ) diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala index 7e459688188c..954e7035e676 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotCdgGeneratorTests.scala @@ -19,9 +19,9 @@ class DotCdgGeneratorTests extends DataFlowCodeToCpgSuite { inside(cpg.method.name("foo").dotCdg.l) { case List(x) => x should ( startWith("digraph \"foo\"") and - include("""[label = <operator>.greaterThan
x > 8>]""") and - include("""[label = <operator>.assignment
z = a(x)>]""") and - include("""[label = a
a(x)>]""") and + include("""[label = <operator>.greaterThan
x > 8> ]""") and + include("""[label = <operator>.assignment
z = a(x)> ]""") and + include("""[label = a
a(x)> ]""") and endWith("}\n") ) val lines = x.split("\n") @@ -46,9 +46,9 @@ class DotCdgGeneratorTests extends DataFlowCodeToCpgSuite { inside(cpg.method.name("foo").dotCdg.l) { case List(x) => x should ( startWith("digraph \"foo\"") and - include("""[label = <operator>.greaterThan
x > 8>]""") and - include("""[label = <operator>.assignment
z = a(x)>]""") and - include("""[label = a
a(x)>]""") and + include("""[label = <operator>.greaterThan
x > 8> ]""") and + include("""[label = <operator>.assignment
z = a(x)> ]""") and + include("""[label = a
a(x)> ]""") and endWith("}\n") ) val lines = x.split("\n") diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala index b1b190077000..d06badd93710 100644 --- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala +++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala @@ -105,7 +105,7 @@ object DotSerializer { } private def nodeToDot(node: StoredNode): String = { - s""""${node.id}" [label = <${stringRepr(node)}>]""".stripMargin + s""""${node.id}" [label = <${stringRepr(node)}> ]""".stripMargin } private def edgeToDot(edge: Edge, withEdgeTypes: Boolean): String = { From 9d1e0a79134e98de2e60a6132564e0c0942a86bd Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 21:07:18 +0800 Subject: [PATCH 07/11] simplify --- .../dotgenerator/DotSerializer.scala | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala index d06badd93710..4912589edf4f 100644 --- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala +++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala @@ -65,20 +65,16 @@ object DotSerializer { private def stringRepr(vertex: StoredNode): String = { val lineOpt = vertex.propertyOption(PropertyNames.LINE_NUMBER).toScala.map(_.toString) val list = (vertex match { - case call: Call => List(call.label, call.name, call.code) - case expr: Expression => List(expr.label, expr.code, toCfgNode(expr).code) - case method: Method => List(method.label, method.name) - case ret: MethodReturn => List(ret.label, ret.typeFullName) - case param: MethodParameterIn => List("PARAM", param.code) - case local: Local => List(local.label, s"${local.code}: ${local.typeFullName}") - case target: JumpTarget => List(target.label, target.name) - case modifier: Modifier => List(modifier.label, modifier.modifierType) - case annoAssign: AnnotationParameterAssign => List(annoAssign.label, annoAssign.code) - case annoParam: AnnotationParameter => List(annoParam.label, annoParam.code) - case typ: Type => List(typ.label, typ.name) - case typeDecl: TypeDecl => List(typeDecl.label, typeDecl.name) - case member: Member => List(member.label, member.name) - case others: AstNode => List(others.label, others.code) + case call: Call => List(call.label, call.name, call.code) + case expr: Expression => List(expr.label, expr.code) + case ret: MethodReturn => List(ret.label, ret.code, ret.typeFullName) + case param: MethodParameterIn => List("PARAMETER", param.name, param.typeFullName) + case local: Local => List(local.label, local.name, local.typeFullName) + case target: JumpTarget => List(target.label, target.name) + case modifier: Modifier => List(modifier.label, modifier.modifierType) + case typ: Type => List(typ.label, typ.name) + case dec: DeclarationBase => List(dec.label, dec.name) + case others: AstNode => List(others.label, others.code) }).map(StringUtils.abbreviate(_, 50)) .map(StringUtils.normalizeSpace) .map(escape) From c0a420d4ffc2e38deb6a82a2e34a91917f8049fe Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 21:24:39 +0800 Subject: [PATCH 08/11] fix test --- .../io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala index 4912589edf4f..faae545c341d 100644 --- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala +++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala @@ -66,7 +66,7 @@ object DotSerializer { val lineOpt = vertex.propertyOption(PropertyNames.LINE_NUMBER).toScala.map(_.toString) val list = (vertex match { case call: Call => List(call.label, call.name, call.code) - case expr: Expression => List(expr.label, expr.code) + case expr: Expression => List(expr.label, expr.code, toCfgNode(expr).code) case ret: MethodReturn => List(ret.label, ret.code, ret.typeFullName) case param: MethodParameterIn => List("PARAMETER", param.name, param.typeFullName) case local: Local => List(local.label, local.name, local.typeFullName) From ab6cb2c178a68bbe66007d368be1fe532fddccdb Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 22:23:51 +0800 Subject: [PATCH 09/11] small adjustment --- .../semanticcpg/dotgenerator/DotSerializer.scala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala index faae545c341d..5ecd7df0a0f8 100644 --- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala +++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala @@ -66,10 +66,12 @@ object DotSerializer { val lineOpt = vertex.propertyOption(PropertyNames.LINE_NUMBER).toScala.map(_.toString) val list = (vertex match { case call: Call => List(call.label, call.name, call.code) - case expr: Expression => List(expr.label, expr.code, toCfgNode(expr).code) - case ret: MethodReturn => List(ret.label, ret.code, ret.typeFullName) - case param: MethodParameterIn => List("PARAMETER", param.name, param.typeFullName) + case identifier: Identifier => List(identifier.label, identifier.name, identifier.typeFullName) case local: Local => List(local.label, local.name, local.typeFullName) + case method: Method => List(method.label, method.name, method.fullName) + case expr: Expression => List(expr.label, expr.code, toCfgNode(expr).code) + case ret: MethodReturn => List(ret.label) + case param: MethodParameterIn => List("PARAM", param.name, param.typeFullName) case target: JumpTarget => List(target.label, target.name) case modifier: Modifier => List(modifier.label, modifier.modifierType) case typ: Type => List(typ.label, typ.name) From 346c9ec8e7ffff985e392a4b5597702715608332 Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sun, 20 Nov 2022 22:53:39 +0800 Subject: [PATCH 10/11] adjust ast --- .../c2cpg/io/dotgenerator/DotAstGeneratorTests.scala | 4 +--- .../semanticcpg/dotgenerator/DotSerializer.scala | 8 +++++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala index 66dce1402277..15399533a725 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/dotgenerator/DotAstGeneratorTests.scala @@ -61,9 +61,7 @@ class DotAstGeneratorTests extends CCodeToCpgSuite { x should ( startWith("digraph \"lemon\"") and include("""[label = goog
goog("\"yes\"")> ]""") and - include( - """[label = "\"yes\""
goog("\"yes\"")> ]""" - ) and + include("""[label = "\"yes\""> ]""") and endWith("}\n") ) } diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala index 5ecd7df0a0f8..e3d97eec09a4 100644 --- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala +++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala @@ -66,12 +66,14 @@ object DotSerializer { val lineOpt = vertex.propertyOption(PropertyNames.LINE_NUMBER).toScala.map(_.toString) val list = (vertex match { case call: Call => List(call.label, call.name, call.code) - case identifier: Identifier => List(identifier.label, identifier.name, identifier.typeFullName) + case identifier: Identifier => List(identifier.label, identifier.name) + case literal: Literal => List(literal.label, literal.code) case local: Local => List(local.label, local.name, local.typeFullName) - case method: Method => List(method.label, method.name, method.fullName) + case method: Method => List(method.label, method.name) case expr: Expression => List(expr.label, expr.code, toCfgNode(expr).code) case ret: MethodReturn => List(ret.label) - case param: MethodParameterIn => List("PARAM", param.name, param.typeFullName) + case typeDecl: TypeDecl => List(typeDecl.label, typeDecl.name) + case param: MethodParameterIn => List("PARAM", param.name) case target: JumpTarget => List(target.label, target.name) case modifier: Modifier => List(modifier.label, modifier.modifierType) case typ: Type => List(typ.label, typ.name) From ee5ac4e44b80e71ca16c1dff4dec626cfdaa7500 Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Fri, 17 Mar 2023 22:03:10 +0800 Subject: [PATCH 11/11] merge current master remove StringUtils.abbreviate --- .../io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala index e3d97eec09a4..ad5a14041616 100644 --- a/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala +++ b/semanticcpg/src/main/scala/io/shiftleft/semanticcpg/dotgenerator/DotSerializer.scala @@ -79,8 +79,7 @@ object DotSerializer { case typ: Type => List(typ.label, typ.name) case dec: DeclarationBase => List(dec.label, dec.name) case others: AstNode => List(others.label, others.code) - }).map(StringUtils.abbreviate(_, 50)) - .map(StringUtils.normalizeSpace) + }).map(StringUtils.normalizeSpace) .map(escape) (lineOpt match { case Some(line) => s"${list.head}, $line" :: list.tail