Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package io.joern.c2cpg.astcreation

import io.joern.c2cpg.passes.FunctionDeclNodePass
import io.joern.x2cpg.Ast
import io.joern.x2cpg.AstNodeBuilder
import io.joern.x2cpg.AstNodeBuilder.dependencyNode
import io.joern.x2cpg.SourceFiles
import io.shiftleft.codepropertygraph.generated.nodes.{AstNodeNew, ExpressionNew, NewCall, NewNode}
import io.joern.x2cpg.{Ast, AstNodeBuilder, SourceFiles}
import io.shiftleft.codepropertygraph.generated.EdgeTypes
import io.shiftleft.codepropertygraph.generated.nodes.{ExpressionNew, NewCall, NewNode}
import org.eclipse.cdt.core.dom.ast.*
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayDesignator
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer
import org.eclipse.cdt.core.dom.ast.c.ICASTFieldDesignator
import org.eclipse.cdt.core.dom.ast.c.{ICASTArrayDesignator, ICASTDesignatedInitializer, ICASTFieldDesignator}
import org.eclipse.cdt.core.dom.ast.cpp.*
import org.eclipse.cdt.internal.core.dom.parser.c.CASTArrayRangeDesignator
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTArrayRangeDesignator
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation
import org.eclipse.cdt.internal.core.dom.parser.cpp.{CPPASTArrayRangeDesignator, ICPPEvaluation}

import scala.collection.mutable
import scala.util.Success
import scala.util.Try
import scala.util.{Success, Try}

trait AstCreatorHelper { this: AstCreator =>

Expand Down Expand Up @@ -72,6 +66,22 @@ trait AstCreatorHelper { this: AstCreator =>
s"$name$idx"
}

protected def scopeLocalUniqueNamespaceFullName(fullName: String): String = {
val newFullName = fullName match {
case "" => "<anonymous>"
case s"$p." => s"$p.<anonymous>"
case other => other
}
scopeLocalUniqueNames.get(newFullName) match {
case None =>
scopeLocalUniqueNames.update(newFullName, 0)
newFullName
case Some(index) =>
val suffix = s"${Defines.NamespaceExtension}$index"
s"$newFullName$suffix"
}
}

protected def scopeLocalUniqueName(name: String, fullName: String, targetName: String): (String, String) = {
if (name.isEmpty && (fullName.isEmpty || fullName.endsWith("."))) {
val newName = scopeLocalUniqueName(targetName, fullName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ trait AstForTypesCreator { this: AstCreator =>
}

protected def astForNamespaceAlias(namespaceAlias: ICPPASTNamespaceAlias): Ast = {
val TypeFullNameInfo(name, fullName) = typeFullNameInfo(namespaceAlias)
val codeString = code(namespaceAlias)
val filename = fileName(namespaceAlias)
Ast(namespaceBlockNode(namespaceAlias, name, s"$fullName<alias>", filename).code(codeString))
// Namespace alias does not create any new AST nodes, so we return an empty AST here.
// Ideally, we would create a namespace block node with an alias property, but that's not in the CPG schema.
// Anyway, namespace aliases do not affect the AST structure. When used, CDT resolves them to the original namespace.
Ast()
}

private def typeForIASTDeclarator(
Expand Down Expand Up @@ -335,8 +335,9 @@ trait AstForTypesCreator { this: AstCreator =>
val TypeFullNameInfo(name, fullName) = typeFullNameInfo(namespaceDefinition)
val codeString = code(namespaceDefinition)
val filename = fileName(namespaceDefinition)
val namespaceBlockNode_ = namespaceBlockNode(namespaceDefinition, name, fullName, filename).code(codeString)
val blockNode_ = blockNode(namespaceDefinition)
val namespaceBlockNode_ =
namespaceBlockNode(namespaceDefinition, name, s"$filename:$fullName", filename).code(codeString)
val blockNode_ = blockNode(namespaceDefinition)
methodAstParentStack.push(blockNode_)
scope.pushNewMethodScope(fullName, name, namespaceBlockNode_, None)
scope.pushNewBlockScope(blockNode_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ object Defines {
val OperatorCall: String = "<operator>()"
val OperatorNew: String = "<operator>.new"
val DuplicateSuffix = "<duplicate>"
val NamespaceExtension = "<extension>"
val ConstSuffix = "<const>"
val GlobalTag = "<global>"
val UnknownTag = "<unknown>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ object FullNameProvider {

private val TagsToKeepInFullName = List(
"<anonymous>",
"<const>",
"<duplicate>",
"<enum>",
"<extension>",
"<global>",
"<iterator>",
"<lambda>",
"<global>",
"<param>",
"<const>",
"<alias>",
"<type>",
"<enum>",
"<tmp>"
"<tmp>",
"<type>"
)

/** Removes template type parameters from qualified names while preserving special tags.
Expand Down Expand Up @@ -108,7 +109,6 @@ trait FullNameProvider { this: AstCreator =>
case d: CPPASTIdExpression => shortNameForCPPASTIdExpression(d)
case u: IASTUnaryExpression => shortName(u.getOperand)
case c: IASTFunctionCallExpression => shortName(c.getFunctionNameExpression)
case a: ICPPASTNamespaceAlias => shortName(a.getAlias)
case d: IASTIdExpression => shortName(d.getName)
case m: IASTPreprocessorMacroDefinition => shortName(m.getName)
case n: ICPPASTNamespaceDefinition => shortName(n.getName)
Expand All @@ -131,7 +131,6 @@ trait FullNameProvider { this: AstCreator =>
case None =>
val qualifiedName = node match {
case _: IASTTranslationUnit => ""
case alias: ICPPASTNamespaceAlias => fullNameForICPPASTNamespaceAlias(alias)
case aliasDecl: ICPPASTAliasDeclaration => fullNameForICPPASTAliasDeclaration(aliasDecl)
case namespace: ICPPASTNamespaceDefinition => fullNameForICPPASTNamespaceDefinition(namespace)
case compType: IASTCompositeTypeSpecifier => fullNameForIASTCompositeTypeSpecifier(compType)
Expand Down Expand Up @@ -184,10 +183,6 @@ trait FullNameProvider { this: AstCreator =>
Try(ASTStringUtil.getQualifiedName(qfn)).getOrElse(nextClosureName())
}

private def fullNameForICPPASTNamespaceAlias(alias: ICPPASTNamespaceAlias): String = {
ASTStringUtil.getQualifiedName(alias.getMappingName)
}

private def fullNameForICPPASTAliasDeclaration(alias: ICPPASTAliasDeclaration): String = {
ASTStringUtil.getQualifiedName(alias.getAlias)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import scala.util.Try

object TypeNameProvider {

private type TypeLike = IASTEnumerationSpecifier | ICPPASTNamespaceDefinition | ICPPASTNamespaceAlias |
IASTCompositeTypeSpecifier | IASTElaboratedTypeSpecifier
private type TypeLike = IASTEnumerationSpecifier | ICPPASTNamespaceDefinition | IASTCompositeTypeSpecifier |
IASTElaboratedTypeSpecifier

}

Expand All @@ -26,19 +26,19 @@ trait TypeNameProvider { this: AstCreator =>
// Sadly, there is no predefined List / Enum of this within Eclipse CDT:
private val ReservedKeywordsAtTypes: List[String] =
List(
"auto",
"class",
"const",
"static",
"restrict",
"extern",
"typedef",
"inline",
"constexpr",
"auto",
"virtual",
"enum",
"struct",
"extern",
"inline",
"interface",
"class"
"restrict",
"static",
"struct",
"typedef",
"virtual"
)

private val KeywordsAtTypesToKeep: List[String] = List("unsigned", "volatile")
Expand Down Expand Up @@ -174,19 +174,17 @@ trait TypeNameProvider { this: AstCreator =>
val fullName_ = registerType(cleanType(fullName(typeLike)))
TypeFullNameInfo(name_, fullName_)
case e: IASTEnumerationSpecifier =>
val name_ = shortName(e)
val fullName_ = fullName(e)
val (uniqueName_, uniqueNameFullName_) = scopeLocalUniqueName(name_, fullName_, "enum")
TypeFullNameInfo(uniqueName_, uniqueNameFullName_)
val name_ = shortName(e)
val fullName_ = fullName(e)
val (uniqueName, uniqueNameFullName) = scopeLocalUniqueName(name_, fullName_, "enum")
TypeFullNameInfo(uniqueName, uniqueNameFullName)
case n: ICPPASTNamespaceDefinition =>
val name_ = shortName(n)
val fullName_ = fullName(n)
val (uniqueName_, uniqueNameFullName_) = scopeLocalUniqueName(name_, fullName_, "namespace")
TypeFullNameInfo(uniqueName_, uniqueNameFullName_)
case a: ICPPASTNamespaceAlias =>
val name_ = shortName(a)
val fullName_ = fullName(a)
TypeFullNameInfo(name_, fullName_)
val name = shortName(n) match {
case "" => "<anonymous>"
case other => other
}
val fullName_ = fullName(n)
TypeFullNameInfo(name, scopeLocalUniqueNamespaceFullName(fullName_))
case s: IASTCompositeTypeSpecifier =>
val fullName_ = registerType(cleanType(fullName(s)))
val name_ = shortName(s) match {
Expand Down Expand Up @@ -255,11 +253,15 @@ trait TypeNameProvider { this: AstCreator =>
}

private def typeForIASTName(name: IASTName): String = {
val x = safeGetNodeType(name)
safeGetBinding(name) match {
case Some(v: IVariable) =>
v.getType match {
case f: IFunctionType => f.getReturnType.toString
case other => other.toString
case f: IFunctionType =>
f.getReturnType.toString
case c: ICPPBinding =>
c.getQualifiedName.mkString(".")
case other => other.toString
}
case _ => safeGetNodeType(name)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package io.joern.c2cpg.passes

import io.joern.c2cpg.astcreation.Defines
import io.shiftleft.codepropertygraph.generated.nodes.{Method, NamespaceBlock, TypeDecl}
import io.shiftleft.codepropertygraph.generated.{Cpg, PropertyNames}
import io.shiftleft.codepropertygraph.generated.nodes.Binding
import io.shiftleft.codepropertygraph.generated.nodes.Call
import io.shiftleft.codepropertygraph.generated.nodes.Method
import io.shiftleft.codepropertygraph.generated.nodes.NamespaceBlock
import io.shiftleft.codepropertygraph.generated.nodes.TypeDecl
import io.shiftleft.passes.CpgPass
import io.shiftleft.semanticcpg.language.*
import io.shiftleft.semanticcpg.language.types.structure.NamespaceTraversal
Expand Down Expand Up @@ -40,7 +36,6 @@ class FullNameUniquenessPass(cpg: Cpg) extends CpgPass(cpg) {
override def run(dstGraph: DiffGraphBuilder): Unit = {
handleMethods(dstGraph)
handleTypeDecls(dstGraph)
handleNamespaceBlocks(dstGraph)
}

private def handleMethods(dstGraph: DiffGraphBuilder): Unit = {
Expand Down Expand Up @@ -112,14 +107,6 @@ class FullNameUniquenessPass(cpg: Cpg) extends CpgPass(cpg) {
)
}

private def handleNamespaceBlocks(dstGraph: DiffGraphBuilder): Unit = {
handleDuplicateFullNames(
cpg.namespaceBlock.nameNot(NamespaceTraversal.globalNamespaceName),
PropertyNames.FullName,
dstGraph
)
}

private def handleDuplicateFullNames[T <: AffectedNodeType](
nodes: Iterator[T],
fullNameProperty: String,
Expand Down
Loading
Loading