Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace TODO() with ProblemDeclaration nodes #1663

Merged
merged 16 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
Expand Up @@ -403,6 +403,27 @@ fun MetadataProvider.newProblemDeclaration(
return node
}

/**
* Creates a new [Problem]. The [MetadataProvider] receiver will be used to fill different meta-data
* using [Node.applyMetadata]. Calling this extension function outside of Kotlin requires an
* appropriate [MetadataProvider], such as a [LanguageFrontend] as an additional prepended argument.
*/
@JvmOverloads
fun MetadataProvider.newProblem(
problem: String = "",
problemType: ProblemNode.ProblemType = ProblemNode.ProblemType.PARSING,
rawNode: Any? = null
): Problem {
val node = Problem()
node.applyMetadata(this, EMPTY_NAME, rawNode, true)

node.problem = problem
node.problemType = problemType

log(node)
return node
}

/**
* Creates a new [IncludeDeclaration]. The [MetadataProvider] receiver will be used to fill
* different meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ abstract class Node :
@Relationship("ANNOTATIONS") var annotationEdges = astEdgesOf<Annotation>()
var annotations by unwrapping(Node::annotationEdges)

/**
* Additional problem nodes. These nodes represent problems which occurred during processing of
* a node (i.e. only partially processed).
*/
val additionalProblems: MutableSet<ProblemNode> = mutableSetOf()
oxisto marked this conversation as resolved.
Show resolved Hide resolved

/**
* If a node should be removed from the graph, just removing it from the AST is not enough (see
* issue #60). It will most probably be referenced somewhere via DFG or EOG edges. Thus, if it
Expand Down
32 changes: 32 additions & 0 deletions cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Problem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $$$$$$\ $$$$$$$\ $$$$$$\
* $$ __$$\ $$ __$$\ $$ __$$\
* $$ / \__|$$ | $$ |$$ / \__|
* $$ | $$$$$$$ |$$ |$$$$\
* $$ | $$ ____/ $$ |\_$$ |
* $$ | $$\ $$ | $$ | $$ |
* \$$$$$ |$$ | \$$$$$ |
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.graph

/** A problem node documenting that something failed. */
data class Problem(
maximiliankaul marked this conversation as resolved.
Show resolved Hide resolved
override var problem: String = "",
override var problemType: ProblemNode.ProblemType = ProblemNode.ProblemType.TRANSLATION
) : ProblemNode, MetadataProvider, Node()
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
// Wildcards luckily do not have aliases
val decl =
if (imp.name == "*") {
// In the wildcard case, our "import" is the module name and we set "wildcard"
// In the wildcard case, our "import" is the module name, and we set "wildcard"
// to true
newImportDeclaration(module, true, rawNode = imp)
} else {
Expand All @@ -139,7 +139,13 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
val ret = newWhileStatement(rawNode = node)
ret.condition = frontend.expressionHandler.handle(node.test)
ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node)
node.orelse.firstOrNull()?.let { TODO("Not supported") }
if (node.orelse.isNotEmpty()) {
ret.additionalProblems +=
newProblem(
problem = "Cannot handle \"orelse\" in while loops.",
rawNode = node.orelse
)
}
return ret
}

Expand All @@ -148,7 +154,13 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
ret.iterable = frontend.expressionHandler.handle(node.iter)
ret.variable = frontend.expressionHandler.handle(node.target)
ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node)
node.orelse.firstOrNull()?.let { TODO("Not supported") }
if (node.orelse.isNotEmpty()) {
ret.additionalProblems +=
newProblem(
problem = "Cannot handle \"orelse\" in for loops.",
rawNode = node.orelse
)
}
return ret
}

Expand All @@ -157,7 +169,13 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
ret.iterable = frontend.expressionHandler.handle(node.iter)
ret.variable = frontend.expressionHandler.handle(node.target)
ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node)
node.orelse.firstOrNull()?.let { TODO("Not supported") }
if (node.orelse.isNotEmpty()) {
ret.additionalProblems +=
newProblem(
problem = "Cannot handle \"orelse\" in async for loops.",
rawNode = node.orelse
)
}
return ret
}

Expand Down Expand Up @@ -241,9 +259,8 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
frontend.scopeManager.enterScope(cls)

stmt.keywords.forEach {
frontend.currentTU?.addDeclaration(
newProblemDeclaration("could not parse keyword $it in class")
)
cls.additionalProblems +=
newProblem(problem = "could not parse keyword $it in class", rawNode = it)
}

for (s in stmt.body) {
Expand Down Expand Up @@ -391,20 +408,19 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
) {
// We can merge posonlyargs and args because both are positional arguments. We do not
// enforce that posonlyargs can ONLY be used in a positional style, whereas args can be used
// both in positional as well as keyword style.
// both in positional and keyword style.
var positionalArguments = args.posonlyargs + args.args
// Handle arguments
if (recordDeclaration != null) {
// first argument is the `receiver`
val recvPythonNode = positionalArguments.firstOrNull()
if (recvPythonNode == null) {
val problem =
newProblemDeclaration(
result.additionalProblems +=
newProblem(
"Expected a receiver",
problemType = ProblemNode.ProblemType.TRANSLATION,
rawNode = args
)
frontend.scopeManager.addDeclaration(problem)
} else {
val tpe = recordDeclaration.toType()
val recvNode =
Expand All @@ -418,15 +434,24 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
when (result) {
is ConstructorDeclaration -> result.receiver = recvNode
is MethodDeclaration -> result.receiver = recvNode
else -> TODO()
else ->
result.additionalProblems +=
newProblem(
problem =
"Expected a constructor or method declaration. Got something else.",
rawNode = result
)
}
}
}

if (recordDeclaration != null) {
// first argument is the receiver
for (arg in positionalArguments.subList(1, positionalArguments.size)) {
handleArgument(arg, arg in args.posonlyargs)
if (
positionalArguments.size > 1 // more arguments than only a receiver
) { // first argument is the receiver
for (arg in positionalArguments.subList(1, positionalArguments.size)) {
handleArgument(arg, arg in args.posonlyargs)
}
}
} else {
for (arg in positionalArguments) {
Expand All @@ -437,43 +462,39 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
args.vararg?.let { handleArgument(it, isPosOnly = false, isVariadic = true) }

if (args.kwonlyargs.isNotEmpty()) {
val problem =
result.additionalProblems +=
newProblemDeclaration(
"`kwonlyargs` are not yet supported",
problemType = ProblemNode.ProblemType.TRANSLATION,
rawNode = args
)
frontend.scopeManager.addDeclaration(problem)
}

if (args.kw_defaults.isNotEmpty()) {
val problem =
result.additionalProblems +=
newProblemDeclaration(
"`kw_defaults` are not yet supported",
problemType = ProblemNode.ProblemType.TRANSLATION,
rawNode = args
)
frontend.scopeManager.addDeclaration(problem)
}

args.kwarg?.let {
val problem =
result.additionalProblems +=
newProblemDeclaration(
"`kwarg` is not yet supported",
problemType = ProblemNode.ProblemType.TRANSLATION,
rawNode = it
)
frontend.scopeManager.addDeclaration(problem)
}

if (args.defaults.isNotEmpty()) {
val problem =
result.additionalProblems +=
newProblemDeclaration(
"`defaults` are not yet supported",
problemType = ProblemNode.ProblemType.TRANSLATION,
rawNode = args
)
frontend.scopeManager.addDeclaration(problem)
}
}

Expand Down
Loading