Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jul 14, 2023
1 parent 5ec2218 commit 407036c
Show file tree
Hide file tree
Showing 26 changed files with 191 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private constructor(
addIncludesToGraph: Boolean
) {
/** This list contains all languages which we want to translate. */
val languages: List<Language<out LanguageFrontend<*, *>>>
val languages: List<Language<*>>

/**
* Switch off cleaning up TypeManager memory after analysis.
Expand Down Expand Up @@ -414,7 +414,7 @@ private constructor(
}

/** Registers an additional [Language]. */
inline fun <reified T : Language<out LanguageFrontend<*, *>>> registerLanguage(): Builder {
inline fun <reified T : Language<*>> registerLanguage(): Builder {
T::class.primaryConstructor?.call()?.let { registerLanguage(it) }
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ abstract class LanguageFrontend<in AstNode, TypeNode>(
* or array types and then either recursively call this function or call other helper functions
* similar to this one. Ideally, they should share the [typeOf] name, but have different method
* signatures.
*
* TODO(oxisto): It would be nice to make LanguageFrontend generic and introduce type parameters
* for ast and type node types. But this is a breaking change in the public API and should be
* reserved for v8
*/
abstract fun typeOf(type: TypeNode): Type

Expand All @@ -119,7 +115,7 @@ abstract class LanguageFrontend<in AstNode, TypeNode>(
* @param astNode the ast node
* @return the source code </T>
*/
abstract fun getCodeFromRawNode(astNode: AstNode?): String?
abstract fun codeOf(astNode: AstNode): String?

/**
* Returns the [Region] of the code with line and column, index starting at 1, generic for java
Expand All @@ -129,20 +125,20 @@ abstract class LanguageFrontend<in AstNode, TypeNode>(
* @param astNode the ast node
* @return the location </T>
*/
abstract fun getLocationFromRawNode(astNode: AstNode): PhysicalLocation?
abstract fun locationOf(astNode: AstNode): PhysicalLocation?

override fun setCodeAndLocation(cpgNode: Node?, astNode: Any?) {
if (cpgNode != null && astNode != null) {
if (config.codeInNodes) {
// only set code, if it's not already set or empty
val code = getCodeFromRawNode(astNode as AstNode)
val code = codeOf(astNode as AstNode)
if (code != null) {
cpgNode.code = code
} else {
log.warn("Unexpected: No code for node {}", astNode)
}
}
cpgNode.location = getLocationFromRawNode(astNode as AstNode)
cpgNode.location = locationOf(astNode as AstNode)
}
}

Expand Down Expand Up @@ -242,7 +238,7 @@ abstract class LanguageFrontend<in AstNode, TypeNode>(
clearProcessed()
}

abstract fun <S, T> setComment(s: S, ctx: T)
abstract fun setComment(node: Node, astNode: AstNode)

companion object {
// Allow non-Java frontends to access the logger (i.e. jep)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Name(
constructor(
localName: String,
parent: Name? = null,
language: Language<out LanguageFrontend<*, *>>?
language: Language<*>?
) : this(localName, parent, language?.namespaceDelimiter ?: ".")

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import de.fraunhofer.aisec.cpg.PopulatedByPass
import de.fraunhofer.aisec.cpg.TranslationContext
import de.fraunhofer.aisec.cpg.frontends.Handler
import de.fraunhofer.aisec.cpg.frontends.Language
import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend
import de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
Expand Down Expand Up @@ -87,7 +86,7 @@ open class Node : IVisitable<Node>, Persistable, LanguageProvider, ScopeProvider
*/
@Relationship(value = "LANGUAGE", direction = Relationship.Direction.OUTGOING)
@JsonBackReference
override var language: Language<out LanguageFrontend<*, *>>? = null
override var language: Language<*>? = null

/**
* The scope this node "lives" in / in which it is defined. This property is set in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface MetadataProvider
* each [Node], but also transformation steps, such as [Handler].
*/
interface LanguageProvider : MetadataProvider {
val language: Language<out LanguageFrontend<*, *>>?
val language: Language<*>?
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ object Util {
log.warn(
String.format(
"%s: %s",
PhysicalLocation.locationLink(lang.getLocationFromRawNode(astNode)),
PhysicalLocation.locationLink(lang.locationOf(astNode)),
format
),
*arguments
Expand All @@ -205,7 +205,7 @@ object Util {
log.error(
String.format(
"%s: %s",
PhysicalLocation.locationLink(lang.getLocationFromRawNode(astNode)),
PhysicalLocation.locationLink(lang.locationOf(astNode)),
format
),
*arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ open class TestLanguageFrontend(
return newUnknownType()
}

override fun getCodeFromRawNode(astNode: Any?): String? {
override fun codeOf(astNode: Any): String? {
TODO("Not yet implemented")
}

override fun getLocationFromRawNode(astNode: Any): PhysicalLocation? {
override fun locationOf(astNode: Any): PhysicalLocation? {
TODO("Not yet implemented")
}

override fun <S : Any?, T : Any?> setComment(s: S, ctx: T) {
override fun setComment(node: Node, astNode: Any) {
TODO("Not yet implemented")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ class CXXLanguageFrontend(language: Language<CXXLanguageFrontend>, ctx: Translat
}
}

override fun getCodeFromRawNode(astNode: IASTNode?): String? {
override fun codeOf(astNode: IASTNode): String? {
val node = astNode as ASTNode
return node.rawSignature
}

override fun getLocationFromRawNode(astNode: IASTNode): PhysicalLocation? {
override fun locationOf(astNode: IASTNode): PhysicalLocation? {
val fLocation = astNode.fileLocation ?: return null
val lineBreaks: IntArray =
try {
Expand Down Expand Up @@ -438,23 +438,20 @@ class CXXLanguageFrontend(language: Language<CXXLanguageFrontend>, ctx: Translat
}
}

override fun <S, T> setComment(s: S, ctx: T) {
if (ctx is ASTNode && s is Node) {
val cpgNode = s as Node
val location = cpgNode.location ?: return
override fun setComment(node: Node, astNode: IASTNode) {
val location = node.location ?: return

// No location, no comment
// No location, no comment

val loc: Pair<String, Int> =
Pair(location.artifactLocation.uri.path, location.region.endLine)
comments[loc]?.let {
// only exact match for now}
cpgNode.comment = it
}
// TODO: handle orphanComments? i.e. comments which do not correspond to one line
// todo: what to do with comments which are in a line which contains multiple
// statements?
val loc: Pair<String, Int> =
Pair(location.artifactLocation.uri.path, location.region.endLine)
comments[loc]?.let {
// only exact match for now}
node.comment = it
}
// TODO: handle orphanComments? i.e. comments which do not correspond to one line
// TODO: what to do with comments which are in a line which contains multiple
// statements?
}

/** Returns the [Type] that is represented by an [IASTTypeId]. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ class DeclarationHandler(lang: CXXLanguageFrontend) :
* into a [NamespaceDeclaration].
*/
private fun handleNamespace(ctx: CPPASTNamespaceDefinition): NamespaceDeclaration {
val declaration =
newNamespaceDeclaration(ctx.name.toString(), frontend.getCodeFromRawNode(ctx))
val declaration = newNamespaceDeclaration(ctx.name.toString(), frontend.codeOf(ctx))

frontend.scopeManager.addDeclaration(declaration)

Expand Down Expand Up @@ -270,12 +269,12 @@ class DeclarationHandler(lang: CXXLanguageFrontend) :

val templateDeclaration: TemplateDeclaration =
if (ctx.declaration is CPPASTFunctionDefinition) {
newFunctionTemplateDeclaration(name, frontend.getCodeFromRawNode(ctx))
newFunctionTemplateDeclaration(name, frontend.codeOf(ctx))
} else {
newClassTemplateDeclaration(name, frontend.getCodeFromRawNode(ctx))
newClassTemplateDeclaration(name, frontend.codeOf(ctx))
}

templateDeclaration.location = frontend.getLocationFromRawNode(ctx)
templateDeclaration.location = frontend.locationOf(ctx)
frontend.scopeManager.addDeclaration(templateDeclaration)
frontend.scopeManager.enterScope(templateDeclaration)
addTemplateParameters(ctx, templateDeclaration)
Expand Down Expand Up @@ -543,7 +542,7 @@ class DeclarationHandler(lang: CXXLanguageFrontend) :
val declaration =
frontend.typeManager.createTypeAlias(
frontend,
frontend.getCodeFromRawNode(ctx),
frontend.codeOf(ctx),
type,
nameDecl.name.toString()
)
Expand All @@ -562,16 +561,16 @@ class DeclarationHandler(lang: CXXLanguageFrontend) :
val enum =
newEnumDeclaration(
name = declSpecifier.name.toString(),
location = frontend.getLocationFromRawNode(ctx),
location = frontend.locationOf(ctx),
)

// Loop through its members
for (enumerator in declSpecifier.enumerators) {
val enumConst =
newEnumConstantDeclaration(
enumerator.name.toString(),
frontend.getCodeFromRawNode(enumerator),
frontend.getLocationFromRawNode(enumerator),
frontend.codeOf(enumerator),
frontend.locationOf(enumerator),
)

// In C/C++, default enums are of type int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class DeclaratorHandler(lang: CXXLanguageFrontend) :
newUnknownType(),
emptyList(),
ctx.rawSignature,
frontend.getLocationFromRawNode(ctx),
frontend.locationOf(ctx),
initializer,
true
)
Expand All @@ -165,7 +165,7 @@ class DeclaratorHandler(lang: CXXLanguageFrontend) :
newUnknownType(),
emptyList(),
ctx.rawSignature,
frontend.getLocationFromRawNode(ctx),
frontend.locationOf(ctx),
initializer,
true
)
Expand Down Expand Up @@ -434,13 +434,13 @@ class DeclaratorHandler(lang: CXXLanguageFrontend) :
newUnknownType(),
emptyList(),
code,
frontend.getLocationFromRawNode(ctx),
frontend.locationOf(ctx),
initializer,
true
)
}

result.location = frontend.getLocationFromRawNode(ctx)
result.location = frontend.locationOf(ctx)
frontend.scopeManager.addDeclaration(result)
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ExpressionHandler(lang: CXXLanguageFrontend) :
}

private fun handleLambdaExpression(node: CPPASTLambdaExpression): Expression {
val lambda = newLambdaExpression(frontend.getCodeFromRawNode(node))
val lambda = newLambdaExpression(frontend.codeOf(node))

// Variables passed by reference are mutable. If we have initializers, we have to model the
// variable explicitly.
Expand Down Expand Up @@ -356,10 +356,10 @@ class ExpressionHandler(lang: CXXLanguageFrontend) :
// this can either be just a meaningless bracket or it can be a cast expression
val typeName = (ctx.operand as IASTIdExpression).name.toString()
if (frontend.typeManager.typeExists(typeName)) {
val cast = newCastExpression(frontend.getCodeFromRawNode(ctx))
val cast = newCastExpression(frontend.codeOf(ctx))
cast.castType = parseType(typeName)
cast.expression = input ?: newProblemExpression("could not parse input")
cast.location = frontend.getLocationFromRawNode(ctx)
cast.location = frontend.locationOf(ctx)
return cast
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PerformanceRegressionTest {
* in reasonable time. We had issues with literals and their hashcode when they were inserted
* into a set.
* * Second, we want to make that list essentially a one-liner because we had issues when
* populating the [Node.location] property using [CXXLanguageFrontend.getLocationFromRawNode].
* populating the [Node.location] property using [CXXLanguageFrontend.locationOf].
*/
@Test
fun testParseLargeList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import de.fraunhofer.aisec.cpg.frontends.Language
import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend
import de.fraunhofer.aisec.cpg.frontends.SupportsParallelParsing
import de.fraunhofer.aisec.cpg.frontends.TranslationException
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
import de.fraunhofer.aisec.cpg.graph.newUnknownType
import de.fraunhofer.aisec.cpg.graph.types.Type
Expand Down Expand Up @@ -95,17 +96,17 @@ class GoLanguageFrontend(language: Language<GoLanguageFrontend>, ctx: Translatio
return newUnknownType()
}

override fun getCodeFromRawNode(astNode: Any?): String? {
override fun codeOf(astNode: Any): String? {
// this is handled by native code
return null
}

override fun getLocationFromRawNode(astNode: Any): PhysicalLocation? {
override fun locationOf(astNode: Any): PhysicalLocation? {
// this is handled by native code
return null
}

override fun <S, T> setComment(s: S, ctx: T) {}
override fun setComment(node: Node, astNode: Any) {}

private external fun parseInternal(
s: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ open class DeclarationHandler(lang: JavaLanguageFrontend) :
val variable = fieldDecl.getVariable(0)
val modifiers = fieldDecl.modifiers.map { modifier -> modifier.keyword.asString() }
val joinedModifiers = java.lang.String.join(" ", modifiers) + " "
val location = frontend.getLocationFromRawNode(fieldDecl)
val location = frontend.locationOf(fieldDecl)
val initializer =
variable.initializer
.map { ctx: Expression -> frontend.expressionHandler.handle(ctx) }
Expand Down Expand Up @@ -374,7 +374,7 @@ open class DeclarationHandler(lang: JavaLanguageFrontend) :
enumDecl: com.github.javaparser.ast.body.EnumDeclaration
): EnumDeclaration {
val name = enumDecl.nameAsString
val location = frontend.getLocationFromRawNode(enumDecl)
val location = frontend.locationOf(enumDecl)
val enumDeclaration = this.newEnumDeclaration(name, enumDecl.toString(), location)
val entries = enumDecl.entries.mapNotNull { handle(it) as EnumConstantDeclaration? }

Expand All @@ -392,7 +392,7 @@ open class DeclarationHandler(lang: JavaLanguageFrontend) :
return this.newEnumConstantDeclaration(
enumConstDecl.nameAsString,
enumConstDecl.toString(),
frontend.getLocationFromRawNode(enumConstDecl)
frontend.locationOf(enumConstDecl)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class ExpressionHandler(lang: JavaLanguageFrontend) :

private fun handleLambdaExpr(expr: Expression): Statement {
val lambdaExpr = expr.asLambdaExpr()
val lambda = newLambdaExpression(frontend.getCodeFromRawNode(lambdaExpr))
val anonymousFunction = newFunctionDeclaration("", frontend.getCodeFromRawNode(lambdaExpr))
val lambda = newLambdaExpression(frontend.codeOf(lambdaExpr))
val anonymousFunction = newFunctionDeclaration("", frontend.codeOf(lambdaExpr))
frontend.scopeManager.enterScope(anonymousFunction)
for (parameter in lambdaExpr.parameters) {
val resolvedType = frontend.getTypeAsGoodAsPossible(parameter.type)
Expand Down Expand Up @@ -418,7 +418,7 @@ class ExpressionHandler(lang: JavaLanguageFrontend) :
return memberExpression
}
if (base.location == null) {
base.location = frontend.getLocationFromRawNode(fieldAccessExpr)
base.location = frontend.locationOf(fieldAccessExpr)
}
return this.newMemberExpression(fieldAccessExpr.name.identifier, base, fieldType, ".")
}
Expand Down Expand Up @@ -833,7 +833,7 @@ class ExpressionHandler(lang: JavaLanguageFrontend) :
if (objectCreationExpr.anonymousClassBody.isPresent) {
// We have an anonymous class and will create a RecordDeclaration for it and add all the
// implemented methods.
val locationHash = frontend.getLocationFromRawNode(objectCreationExpr)?.hashCode()
val locationHash = frontend.locationOf(objectCreationExpr)?.hashCode()

// We use the hash of the location to distinguish multiple instances of the anonymous
// class' superclass
Expand Down
Loading

0 comments on commit 407036c

Please sign in to comment.