diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala index 294e8f7839f2..a1f4f703bb81 100644 --- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala +++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstCreatorHelper.scala @@ -142,18 +142,14 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As "class" ) - private val KeywordsAtTypesToKeep: List[String] = List("unsigned", "volatile", "static", "const") + private val KeywordsAtTypesToKeep: List[String] = List("unsigned", "volatile") - protected def cleanType(rawType: String, stripKeywords: Boolean = true): String = { + protected def cleanType(rawType: String): String = { if (rawType == Defines.Any) return rawType - val tpe = if (stripKeywords) { - ReservedKeywordsAtTypes.foldLeft(rawType) { (cur, repl) => - if (cur.startsWith(s"$repl ") || cur.contains(s" $repl ")) { - cur.replace(s" $repl ", " ").replace(s"$repl ", "") - } else cur - } - } else { - rawType + val tpe = ReservedKeywordsAtTypes.foldLeft(rawType) { (cur, repl) => + if (cur.startsWith(s"$repl ") || cur.contains(s" $repl ")) { + cur.replace(s" $repl ", " ").stripPrefix(s"$repl ") + } else cur } val normalizedTpe = StringUtils.normalizeSpace(tpe.stripSuffix(" ()")) replaceWhitespaceAfterKeyword(normalizedTpe) match { @@ -166,16 +162,14 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As case t if t.contains("::{") || t.contains("}::") => Defines.Any case t if t.contains("{") || t.contains("}") => Defines.Any case t if t.contains("org.eclipse.cdt.internal.core.dom.parser.ProblemType") => Defines.Any - case t if t.contains("( ") => fixQualifiedName(t.substring(0, t.indexOf("( "))) - case t if t.contains(Defines.QualifiedNameSeparator) => fixQualifiedName(t) - case someType => someType + case t if t.contains("( ") => fixQualifiedName(t.substring(0, t.indexOf("( "))) + case someType => fixQualifiedName(someType) } } private def replaceWhitespaceAfterKeyword(tpe: String): String = { - val keywordsToKeep = ReservedKeywordsAtTypes ++ KeywordsAtTypesToKeep - if (keywordsToKeep.exists(k => tpe.startsWith(s"$k ") || tpe.contains(s" $k "))) { - keywordsToKeep.foldLeft(tpe) { (cur, repl) => + if (KeywordsAtTypesToKeep.exists(k => tpe.startsWith(s"$k ") || tpe.contains(s" $k "))) { + KeywordsAtTypesToKeep.foldLeft(tpe) { (cur, repl) => val prefixStartsWith = s"$repl " val prefixContains = s" $repl " if (cur.startsWith(prefixStartsWith)) { @@ -215,23 +209,23 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As Try(ASTTypeUtil.getNodeType(node)).getOrElse(Defines.Any) } - private def typeForCPPASTFieldReference(f: CPPASTFieldReference, stripKeywords: Boolean = true): String = { + private def typeForCPPASTFieldReference(f: CPPASTFieldReference): String = { safeGetEvaluation(f.getFieldOwner) match { - case Some(evaluation: EvalBinding) => cleanType(evaluation.getType.toString, stripKeywords) - case _ => cleanType(safeGetType(f.getFieldOwner.getExpressionType), stripKeywords) + case Some(evaluation: EvalBinding) => cleanType(evaluation.getType.toString) + case _ => cleanType(safeGetType(f.getFieldOwner.getExpressionType)) } } - private def typeForCPPASTFoldExpression(f: CPPASTFoldExpression, stripKeywords: Boolean = true): String = { + private def typeForCPPASTFoldExpression(f: CPPASTFoldExpression): String = { safeGetEvaluation(f) match { case Some(evaluation: EvalFoldExpression) => Try(evaluation.getValue.getEvaluation).toOption match { case Some(value: EvalBinary) => val s = value.toString - cleanType(s.substring(0, s.indexOf(": ")), stripKeywords) + cleanType(s.substring(0, s.indexOf(": "))) case Some(value: EvalBinding) if value.getType.isInstanceOf[ICPPParameterPackType] => val s = value.getType.asInstanceOf[ICPPParameterPackType].getType.toString - cleanType(s, stripKeywords) + cleanType(s) case _ => Defines.Any } case _ => Defines.Any @@ -239,7 +233,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As } @nowarn - private def typeForIASTArrayDeclarator(a: IASTArrayDeclarator, stripKeywords: Boolean = true): String = { + private def typeForIASTArrayDeclarator(a: IASTArrayDeclarator): String = { import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil.getNodeSignature if (safeGetNodeType(a).startsWith("? ")) { val tpe = getNodeSignature(a).replace("[]", "").strip() @@ -258,82 +252,76 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As }.mkString s"$tpe$arr" } else { - cleanType(safeGetNodeType(a), stripKeywords) + cleanType(safeGetNodeType(a)) } } - private def typeForCPPASTIdExpression(s: CPPASTIdExpression, stripKeywords: Boolean = true): String = { + private def typeForCPPASTIdExpression(s: CPPASTIdExpression): String = { safeGetEvaluation(s) match { case Some(evaluation: EvalMemberAccess) => val deref = if (evaluation.isPointerDeref) "*" else "" - cleanType(evaluation.getOwnerType.toString + deref, stripKeywords) + cleanType(evaluation.getOwnerType.toString + deref) case Some(evalBinding: EvalBinding) => evalBinding.getBinding match { - case m: CPPMethod => cleanType(safeGetNodeType(m.getPrimaryDeclaration), stripKeywords) - case f: CPPFunction => cleanType(safeGetNodeType(f.getDefinition), stripKeywords) + case m: CPPMethod => cleanType(safeGetNodeType(m.getPrimaryDeclaration)) + case f: CPPFunction => cleanType(safeGetNodeType(f.getDefinition)) case v: CPPVariable => cleanType(v.getType.toString) - case _ => cleanType(safeGetNodeType(s), stripKeywords) + case _ => cleanType(safeGetNodeType(s)) } - case _ => cleanType(safeGetNodeType(s), stripKeywords) + case _ => cleanType(safeGetNodeType(s)) } } @nowarn - private def typeForICPPASTConstructorInitializer( - c: ICPPASTConstructorInitializer, - stripKeywords: Boolean = true - ): String = { + private def typeForICPPASTConstructorInitializer(c: ICPPASTConstructorInitializer): String = { import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil.getNodeSignature c.getParent match { case initializer: ICPPASTConstructorChainInitializer => - val fullName_ = fullName(initializer.getMemberInitializerId) - cleanType(fullName_, stripKeywords) + val initIdFullName = fullName(initializer.getMemberInitializerId) + cleanType(initIdFullName) case _ => - cleanType(getNodeSignature(c), stripKeywords) + cleanType(getNodeSignature(c)) } } - private def typeForCPPASTEqualsInitializer(c: CPPASTEqualsInitializer, stripKeywords: Boolean = true): String = { + private def typeForCPPASTEqualsInitializer(c: CPPASTEqualsInitializer): String = { import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil.getNodeSignature c.getInitializerClause match { case initializer: ICPPASTFunctionCallExpression if initializer.getFunctionNameExpression.isInstanceOf[CPPASTIdExpression] => val name = initializer.getFunctionNameExpression.asInstanceOf[CPPASTIdExpression] - typeForCPPASTIdExpression(name, stripKeywords) + typeForCPPASTIdExpression(name) case _ => - cleanType(getNodeSignature(c), stripKeywords) + cleanType(getNodeSignature(c)) } } @nowarn - protected def typeFor(node: IASTNode, stripKeywords: Boolean = true): String = { + protected def typeFor(node: IASTNode): String = { import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil.getNodeSignature node match { - case f: CPPASTFoldExpression => typeForCPPASTFoldExpression(f, stripKeywords) - case f: CPPASTFieldReference => typeForCPPASTFieldReference(f, stripKeywords) - case s: CPPASTIdExpression => typeForCPPASTIdExpression(s, stripKeywords) - case s: ICPPASTNamedTypeSpecifier => typeForCPPAstNamedTypeSpecifier(s, stripKeywords) - case a: IASTArrayDeclarator => typeForIASTArrayDeclarator(a, stripKeywords) - case c: ICPPASTConstructorInitializer => typeForICPPASTConstructorInitializer(c, stripKeywords) - case c: CPPASTEqualsInitializer => typeForCPPASTEqualsInitializer(c, stripKeywords) - case _: IASTIdExpression | _: IASTName | _: IASTDeclarator => cleanType(safeGetNodeType(node), stripKeywords) - case f: IASTFieldReference => cleanType(safeGetType(f.getFieldOwner.getExpressionType), stripKeywords) - case s: IASTNamedTypeSpecifier => cleanType(ASTStringUtil.getReturnTypeString(s, null), stripKeywords) - case s: IASTCompositeTypeSpecifier => cleanType(ASTStringUtil.getReturnTypeString(s, null), stripKeywords) - case s: IASTEnumerationSpecifier => cleanType(ASTStringUtil.getReturnTypeString(s, null), stripKeywords) - case s: IASTElaboratedTypeSpecifier => cleanType(ASTStringUtil.getReturnTypeString(s, null), stripKeywords) - case l: IASTLiteralExpression => cleanType(safeGetType(l.getExpressionType), stripKeywords) - case e: IASTExpression => cleanType(safeGetNodeType(e), stripKeywords) - case _ => cleanType(getNodeSignature(node), stripKeywords) + case f: CPPASTFoldExpression => typeForCPPASTFoldExpression(f) + case f: CPPASTFieldReference => typeForCPPASTFieldReference(f) + case s: CPPASTIdExpression => typeForCPPASTIdExpression(s) + case s: ICPPASTNamedTypeSpecifier => typeForCPPAstNamedTypeSpecifier(s) + case a: IASTArrayDeclarator => typeForIASTArrayDeclarator(a) + case c: ICPPASTConstructorInitializer => typeForICPPASTConstructorInitializer(c) + case c: CPPASTEqualsInitializer => typeForCPPASTEqualsInitializer(c) + case _: IASTIdExpression | _: IASTName | _: IASTDeclarator => cleanType(safeGetNodeType(node)) + case f: IASTFieldReference => cleanType(safeGetType(f.getFieldOwner.getExpressionType)) + case s: IASTNamedTypeSpecifier => cleanType(ASTStringUtil.getReturnTypeString(s, null)) + case s: IASTCompositeTypeSpecifier => cleanType(ASTStringUtil.getReturnTypeString(s, null)) + case s: IASTEnumerationSpecifier => cleanType(ASTStringUtil.getReturnTypeString(s, null)) + case s: IASTElaboratedTypeSpecifier => cleanType(ASTStringUtil.getReturnTypeString(s, null)) + case l: IASTLiteralExpression => cleanType(safeGetType(l.getExpressionType)) + case e: IASTExpression => cleanType(safeGetNodeType(e)) + case _ => cleanType(getNodeSignature(node)) } } - private def typeForCPPAstNamedTypeSpecifier(s: ICPPASTNamedTypeSpecifier, stripKeywords: Boolean): String = { - val tpe = safeGetBinding(s) match { - case Some(binding) if stripKeywords => binding.toString - case _ => s.getRawSignature - } - cleanType(tpe, stripKeywords) + private def typeForCPPAstNamedTypeSpecifier(s: ICPPASTNamedTypeSpecifier): String = { + val tpe = safeGetBinding(s).map(_.toString).getOrElse(s.getRawSignature) + cleanType(tpe) } private def notHandledText(node: IASTNode): String = @@ -384,9 +372,9 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As StringUtils.normalizeSpace(s"$returnType(${parameterTypes.mkString(",")})") } - private def pointersAsString(spec: IASTDeclSpecifier, parentDecl: IASTDeclarator, stripKeywords: Boolean): String = { - val tpe = typeFor(spec, stripKeywords) match { - case Defines.Auto => typeFor(parentDecl, stripKeywords) + private def pointersAsString(spec: IASTDeclSpecifier, parentDecl: IASTDeclarator): String = { + val tpe = typeFor(spec) match { + case Defines.Auto => typeFor(parentDecl) case t => t } val pointers = parentDecl.getPointerOperators @@ -505,44 +493,44 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As } } - protected def typeForDeclSpecifier(spec: IASTNode, stripKeywords: Boolean = true, index: Int = 0): String = { + protected def typeForDeclSpecifier(spec: IASTNode, index: Int = 0): String = { val tpe = spec match { case s: IASTSimpleDeclSpecifier if s.getParent.isInstanceOf[IASTParameterDeclaration] => val parentDecl = s.getParent.asInstanceOf[IASTParameterDeclaration].getDeclarator - pointersAsString(s, parentDecl, stripKeywords) + pointersAsString(s, parentDecl) case s: IASTSimpleDeclSpecifier if s.getParent.isInstanceOf[IASTFunctionDefinition] => val parentDecl = s.getParent.asInstanceOf[IASTFunctionDefinition].getDeclarator ASTStringUtil.getReturnTypeString(s, parentDecl) case s: IASTSimpleDeclaration if s.getParent.isInstanceOf[ICASTKnRFunctionDeclarator] => val decl = s.getDeclarators.toList(index) - pointersAsString(s.getDeclSpecifier, decl, stripKeywords) + pointersAsString(s.getDeclSpecifier, decl) case s: IASTSimpleDeclSpecifier if s.getParent.isInstanceOf[IASTSimpleDeclaration] => val parentDecl = s.getParent.asInstanceOf[IASTSimpleDeclaration].getDeclarators.toList(index) - pointersAsString(s, parentDecl, stripKeywords) + pointersAsString(s, parentDecl) case s: IASTSimpleDeclSpecifier => ASTStringUtil.getReturnTypeString(s, null) case s: IASTNamedTypeSpecifier if s.getParent.isInstanceOf[IASTParameterDeclaration] => val parentDecl = s.getParent.asInstanceOf[IASTParameterDeclaration].getDeclarator - pointersAsString(s, parentDecl, stripKeywords) + pointersAsString(s, parentDecl) case s: IASTNamedTypeSpecifier if s.getParent.isInstanceOf[IASTSimpleDeclaration] => val parentDecl = s.getParent.asInstanceOf[IASTSimpleDeclaration].getDeclarators.toList(index) - pointersAsString(s, parentDecl, stripKeywords) + pointersAsString(s, parentDecl) case s: IASTNamedTypeSpecifier => ASTStringUtil.getSimpleName(s.getName) case s: IASTCompositeTypeSpecifier if s.getParent.isInstanceOf[IASTSimpleDeclaration] => val parentDecl = s.getParent.asInstanceOf[IASTSimpleDeclaration].getDeclarators.toList(index) - pointersAsString(s, parentDecl, stripKeywords) + pointersAsString(s, parentDecl) case s: IASTCompositeTypeSpecifier => ASTStringUtil.getSimpleName(s.getName) case s: IASTEnumerationSpecifier if s.getParent.isInstanceOf[IASTSimpleDeclaration] => val parentDecl = s.getParent.asInstanceOf[IASTSimpleDeclaration].getDeclarators.toList(index) - pointersAsString(s, parentDecl, stripKeywords) + pointersAsString(s, parentDecl) case s: IASTEnumerationSpecifier => ASTStringUtil.getSimpleName(s.getName) case s: IASTElaboratedTypeSpecifier if s.getParent.isInstanceOf[IASTParameterDeclaration] => val parentDecl = s.getParent.asInstanceOf[IASTParameterDeclaration].getDeclarator - pointersAsString(s, parentDecl, stripKeywords) + pointersAsString(s, parentDecl) case s: IASTElaboratedTypeSpecifier if s.getParent.isInstanceOf[IASTSimpleDeclaration] => val parentDecl = s.getParent.asInstanceOf[IASTSimpleDeclaration].getDeclarators.toList(index) - pointersAsString(s, parentDecl, stripKeywords) + pointersAsString(s, parentDecl) case s: IASTElaboratedTypeSpecifier => ASTStringUtil.getSignatureString(s, null) // TODO: handle other types of IASTDeclSpecifier case _ => Defines.Any diff --git a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala index 77e318181701..6be1c982c293 100644 --- a/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala +++ b/joern-cli/frontends/c2cpg/src/main/scala/io/joern/c2cpg/astcreation/AstForTypesCreator.scala @@ -9,6 +9,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.* import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTAliasDeclaration import org.eclipse.cdt.internal.core.model.ASTStringUtil import io.joern.x2cpg.datastructures.Stack.* +import org.apache.commons.lang3.StringUtils import scala.util.Try @@ -19,8 +20,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this: case _ => false } - private def isTypeDef(decl: IASTSimpleDeclaration): Boolean = - code(decl).startsWith("typedef") + private def isTypeDef(decl: IASTSimpleDeclaration): Boolean = decl.getRawSignature.startsWith("typedef") private def templateParameters(e: IASTNode): Option[String] = { val templateDeclaration = e match { @@ -90,27 +90,35 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this: ) case d if parentIsClassDef(d) => val tpe = declarator match { - case _: IASTArrayDeclarator => registerType(typeFor(declarator)) - case _ => registerType(typeForDeclSpecifier(declaration.getDeclSpecifier)) + case _: IASTArrayDeclarator => registerType(cleanType(typeFor(declarator))) + case _ => registerType(cleanType(typeForDeclSpecifier(declaration.getDeclSpecifier, index = index))) } Ast(memberNode(declarator, name, code(declarator), tpe)) case d if isAssignmentFromBrokenMacro(d, declarator) && scope.lookupVariable(name).nonEmpty => Ast() - case _ if declarator.isInstanceOf[IASTArrayDeclarator] => - val tpe = registerType(typeFor(declarator)) - val codeTpe = typeFor(declarator, stripKeywords = false) - val node = localNode(declarator, name, s"$codeTpe $name", tpe) - scope.addToScope(name, (node, tpe)) - Ast(node) case _ => - val tpe = registerType(cleanType(typeForDeclSpecifier(declaration.getDeclSpecifier, index = index))) - val codeTpe = typeForDeclSpecifier(declaration.getDeclSpecifier, stripKeywords = false, index = index) - val node = localNode(declarator, name, s"$codeTpe $name", tpe) + val tpe = declarator match { + case arrayDecl: IASTArrayDeclarator => registerType(cleanType(typeFor(arrayDecl))) + case _ => registerType(cleanType(typeForDeclSpecifier(declaration.getDeclSpecifier, index = index))) + } + val code = codeForDeclarator(declaration, declarator) + val node = localNode(declarator, name, code, tpe) scope.addToScope(name, (node, tpe)) Ast(node) } } + private def codeForDeclarator(declaration: IASTSimpleDeclaration, declarator: IASTDeclarator): String = { + val specCode = declaration.getDeclSpecifier.getRawSignature + val declCodeRaw = declarator.getRawSignature + val declCode = declarator.getInitializer match { + case null => declCodeRaw + case _ => declCodeRaw.replace(declarator.getInitializer.getRawSignature, "") + } + val normalizedCode = StringUtils.normalizeSpace(s"$specCode $declCode") + normalizedCode.strip() + } + protected def astForInitializer(declarator: IASTDeclarator, init: IASTInitializer): Ast = init match { case i: IASTEqualsInitializer => val operatorName = Operators.assignment diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/AstCreationPassTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/AstCreationPassTests.scala index 792342570f62..f1b69d42e80c 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/AstCreationPassTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/ast/AstCreationPassTests.scala @@ -511,7 +511,7 @@ class AstCreationPassTests extends AstC2CpgSuite { ) val List(barLocal) = cpg.method.nameExact("addrOfLocalRef").local.l barLocal.name shouldBe "bar" - barLocal.code shouldBe "struct x& bar" + barLocal.code shouldBe "struct x &bar" } "be correct for decl assignment of multiple locals" in { @@ -1094,7 +1094,7 @@ class AstCreationPassTests extends AstC2CpgSuite { inside(cpg.local.l) { case List(localA, localB) => localA.name shouldBe "a" localA.typeFullName shouldBe "A" - localA.code shouldBe "struct A a" + localA.code shouldBe "struct A { int x; } a" localB.name shouldBe "b" localB.typeFullName shouldBe "B" localB.code shouldBe "struct B b" @@ -1564,11 +1564,11 @@ class AstCreationPassTests extends AstC2CpgSuite { inside(cpg.local.l) { case List(bufA: Local, bufB: Local) => bufA.typeFullName shouldBe "char[256]" bufA.name shouldBe "bufA" - bufA.code shouldBe "char[256] bufA" + bufA.code shouldBe "char bufA[256]" bufB.typeFullName shouldBe "char[1+2]" bufB.name shouldBe "bufB" - bufB.code shouldBe "char[1+2] bufB" + bufB.code shouldBe "char bufB[1+2]" } } @@ -2249,7 +2249,7 @@ class AstCreationPassTests extends AstC2CpgSuite { |""".stripMargin) val List(bufLocal) = cpg.local.nameExact("buf").l bufLocal.typeFullName shouldBe "char[0x111111111111111]" - bufLocal.code shouldBe "char[0x111111111111111] buf" + bufLocal.code shouldBe "char buf[BUFSIZE]" val List(bufAllocCall) = cpg.call.nameExact(Operators.alloc).l bufAllocCall.code shouldBe "buf[BUFSIZE]" bufAllocCall.argument.ast.isLiteral.code.l shouldBe List("0x111111111111111") diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/TypeNodePassTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/TypeNodePassTests.scala index 9e48c9ffdc65..6f88cd7e97a4 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/TypeNodePassTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/passes/types/TypeNodePassTests.scala @@ -147,7 +147,7 @@ class TypeNodePassTests extends C2CpgSuite { inside(cpg.local.l) { case List(ptr) => ptr.name shouldBe "ptr" ptr.typeFullName shouldBe "test*" - ptr.code shouldBe "struct test* ptr" + ptr.code shouldBe "struct test *ptr" } inside(cpg.local.typ.referencedTypeDecl.l) { case List(tpe) => tpe.name shouldBe "test*" @@ -200,7 +200,7 @@ class TypeNodePassTests extends C2CpgSuite { inside(cpg.method("test_func").ast.isLocal.name(badChar.name).code(".*\\*.*").l) { case List(ptr) => ptr.name shouldBe "badChar" ptr.typeFullName shouldBe "char*" - ptr.code shouldBe "char* badChar" + ptr.code shouldBe "char * badChar" } } } diff --git a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/querying/LocalQueryTests.scala b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/querying/LocalQueryTests.scala index 831d2ab558c6..e03416bdd4ae 100644 --- a/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/querying/LocalQueryTests.scala +++ b/joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/querying/LocalQueryTests.scala @@ -8,7 +8,7 @@ import io.shiftleft.semanticcpg.language.* class LocalQueryTests extends C2CpgSuite { "local query example 1" should { - "allow to query for the local" in { + "allow to query for the locals" in { val cpg = code( """ |void foo() { @@ -20,11 +20,11 @@ class LocalQueryTests extends C2CpgSuite { ) val List(barLocal) = cpg.method.name("foo").local.nameExact("bar").l barLocal.typeFullName shouldBe "Foo.Bar" - barLocal.code shouldBe "static const Foo.Bar bar" + barLocal.code shouldBe "static const Foo::Bar bar" val List(vecLocal) = cpg.method.name("foo").local.nameExact("vec").l vecLocal.typeFullName shouldBe "std.vector" - vecLocal.code shouldBe "static extern std.vector vec" + vecLocal.code shouldBe "static extern std::vector vec" } } @@ -73,7 +73,7 @@ class LocalQueryTests extends C2CpgSuite { | } | | void test() { - | static int a, b, c; + | static int a, *b, c[1]; | wchar_t *foo; | int d[10], e = 1; | } @@ -87,10 +87,10 @@ class LocalQueryTests extends C2CpgSuite { inside(cpg.method.name("free_list").local.l) { case List(q, p) => q.name shouldBe "q" q.typeFullName shouldBe "node*" - q.code shouldBe "struct node* q" + q.code shouldBe "struct node *q" p.name shouldBe "p" p.typeFullName shouldBe "node*" - p.code shouldBe "struct node* p" + p.code shouldBe "struct node *p" } } @@ -100,17 +100,17 @@ class LocalQueryTests extends C2CpgSuite { a.typeFullName shouldBe "int" a.code shouldBe "static int a" b.name shouldBe "b" - b.typeFullName shouldBe "int" - b.code shouldBe "static int b" + b.typeFullName shouldBe "int*" + b.code shouldBe "static int *b" c.name shouldBe "c" - c.typeFullName shouldBe "int" - c.code shouldBe "static int c" + c.typeFullName shouldBe "int[1]" + c.code shouldBe "static int c[1]" foo.name shouldBe "foo" foo.typeFullName shouldBe "wchar_t*" - foo.code shouldBe "wchar_t* foo" + foo.code shouldBe "wchar_t *foo" d.name shouldBe "d" d.typeFullName shouldBe "int[10]" - d.code shouldBe "int[10] d" + d.code shouldBe "int d[10]" e.name shouldBe "e" e.typeFullName shouldBe "int" e.code shouldBe "int e"