-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
276 additions
and
58 deletions.
There are no files selected for viewing
72 changes: 68 additions & 4 deletions
72
...anguage-ruby/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/ruby/DeclarationHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,91 @@ | ||
/* | ||
* Copyright (c) 2023, 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.frontends.ruby | ||
|
||
import de.fraunhofer.aisec.cpg.frontends.Handler | ||
import de.fraunhofer.aisec.cpg.graph.declarations.Declaration | ||
import de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.declarations.ProblemDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.newFunctionDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.newParameterDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.newReturnStatement | ||
import de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement | ||
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block | ||
import org.jruby.ast.ArgumentNode | ||
import org.jruby.ast.MethodDefNode | ||
import org.jruby.ast.Node | ||
|
||
class DeclarationHandler(lang: RubyLanguageFrontend) : | ||
Handler<Declaration, Node, RubyLanguageFrontend>({ ProblemDeclaration() }, lang) { | ||
|
||
init { | ||
map.put(ArgumentNode::class.java, ::handleArgumentNode) | ||
map.put(MethodDefNode::class.java, ::handleMethodDefNode) | ||
} | ||
|
||
private fun handleArgumentNode(node: Node?): Declaration? { | ||
if (node !is ArgumentNode) { | ||
return null | ||
} | ||
|
||
return newParameterDeclaration( | ||
node.name.idString(), | ||
variadic = false | ||
) | ||
return newParameterDeclaration(node.name.idString(), variadic = false) | ||
} | ||
|
||
private fun handleMethodDefNode(node: Node): FunctionDeclaration? { | ||
if (node !is MethodDefNode) { | ||
return null | ||
} | ||
|
||
val func = newFunctionDeclaration(node.name.idString()) | ||
|
||
frontend.scopeManager.enterScope(func) | ||
|
||
for (arg in node.argsNode.args) { | ||
val param = this.handle(arg) | ||
param?.let { frontend.scopeManager.addDeclaration(it) } | ||
} | ||
|
||
val body = frontend.statementHandler.handle(node.bodyNode) | ||
if (body is Block) { | ||
// get the last statement | ||
val lastStatement = body.statements.lastOrNull() | ||
|
||
// add an implicit return statement, if there is no return statement | ||
if (lastStatement !is ReturnStatement) { | ||
val returnStatement = newReturnStatement("return") | ||
returnStatement.isImplicit = true | ||
body += returnStatement | ||
|
||
// TODO: Ruby returns the last expression, if there is no explicit return | ||
} | ||
} | ||
func.body = body | ||
|
||
frontend.scopeManager.leaveScope(func) | ||
|
||
return func | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 25 additions & 1 deletion
26
cpg-language-ruby/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/ruby/RubyLanguage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.