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

Kw/backward ast #1628

Open
wants to merge 7 commits into
base: mk/backwards-ast
Choose a base branch
from
Open
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
Expand Up @@ -280,7 +280,7 @@ fun MetadataProvider.newCallExpression(
*/
@JvmOverloads
fun MetadataProvider.newMemberCallExpression(
callee: Expression?,
callee: Expression? = null,
isStatic: Boolean = false,
rawNode: Any? = null
): MemberCallExpression {
Expand Down Expand Up @@ -314,15 +314,15 @@ fun MetadataProvider.newMemberCallExpression(
@JvmOverloads
fun MetadataProvider.newMemberExpression(
name: CharSequence?,
base: Expression,
base: Expression? = null,
memberType: Type = unknownType(),
operatorCode: String? = ".",
rawNode: Any? = null
): MemberExpression {
val node = MemberExpression()
node.applyMetadata(this, name, rawNode, true)

node.base = base
base?.let { node.base = it }
node.operatorCode = operatorCode
node.type = memberType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import de.fraunhofer.aisec.cpg.frontends.TranslationException
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.Annotation
import de.fraunhofer.aisec.cpg.graph.declarations.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression
import de.fraunhofer.aisec.cpg.graph.types.*
import de.fraunhofer.aisec.cpg.helpers.Benchmark
import de.fraunhofer.aisec.cpg.helpers.CommentMatcher
Expand Down Expand Up @@ -385,13 +384,14 @@ open class CXXLanguageFrontend(language: Language<CXXLanguageFrontend>, ctx: Tra
private fun handleAttributes(owner: IASTAttributeOwner): List<Annotation> {
val list: MutableList<Annotation> = ArrayList()
for (attribute in owner.attributes) {
val annotation = newAnnotation(String(attribute.name), rawNode = owner)

// go over the parameters
if (attribute.argumentClause is IASTTokenList) {
val members = handleTokenList(attribute.argumentClause as IASTTokenList)
annotation.members = members
}
val annotation =
newAnnotation(String(attribute.name), rawNode = owner).withChildren {
// go over the parameters
if (attribute.argumentClause is IASTTokenList) {
val members = handleTokenList(attribute.argumentClause as IASTTokenList)
it.members = members
}
}
list.add(annotation)
}
return list
Expand All @@ -411,21 +411,25 @@ open class CXXLanguageFrontend(language: Language<CXXLanguageFrontend>, ctx: Tra

private fun handleToken(token: IASTToken): AnnotationMember {
val code = String(token.tokenCharImage)
val expression: Expression =
when (token.tokenType) {
1 -> // a variable
newReference(code, unknownType(), rawNode = token)
2 -> // an integer
newLiteral(code.toInt(), primitiveType("int"), rawNode = token)
130 -> // a string
newLiteral(
if (code.length >= 2) code.substring(1, code.length - 1) else "",
primitiveType("char").pointer(),
rawNode = token
)
else -> newLiteral(code, primitiveType("char").pointer(), rawNode = token)
val annotationMember =
newAnnotationMember("", rawNode = token).withChildren {
it.value =
when (token.tokenType) {
1 -> // a variable
newReference(code, unknownType(), rawNode = token)
2 -> // an integer
newLiteral(code.toInt(), primitiveType("int"), rawNode = token)
130 -> // a string
newLiteral(
if (code.length >= 2) code.substring(1, code.length - 1) else "",
primitiveType("char").pointer(),
rawNode = token
)
else -> newLiteral(code, primitiveType("char").pointer(), rawNode = token)
}
}
return newAnnotationMember("", expression, rawNode = token)

return annotationMember
}

@Throws(NoSuchFieldException::class)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -293,62 +293,65 @@ class DeclaratorHandler(lang: CXXLanguageFrontend) :
}

// Enter the scope of the function itself
frontend.scopeManager.enterScope(declaration)
declaration.withChildren(true) {

// Create the method receiver (if this is a method)
if (declaration is MethodDeclaration) {
createMethodReceiver(declaration)
}
// Create the method receiver (if this is a method)
if (declaration is MethodDeclaration) {
createMethodReceiver(declaration)
}

var i = 0
for (param in ctx.parameters) {
val arg = frontend.parameterDeclarationHandler.handle(param)

if (arg is ParameterDeclaration) {
// check for void type parameters
if (arg.type is IncompleteType) {
if (arg.name.isNotEmpty()) {
Util.warnWithFileLocation(
declaration,
log,
"Named parameter cannot have void type"
)
} else {
// specifying void as first parameter is ok and means that the function has
// no parameters
if (i == 0) {
continue
} else {
var i = 0
for (param in ctx.parameters) {
val arg = frontend.parameterDeclarationHandler.handle(param)

if (arg is ParameterDeclaration) {
// check for void type parameters
if (arg.type is IncompleteType) {
if (arg.name.isNotEmpty()) {
Util.warnWithFileLocation(
declaration,
log,
"void parameter must be the first and only parameter"
"Named parameter cannot have void type"
)
} else {
// specifying void as first parameter is ok and means that the function
// has
// no parameters
if (i == 0) {
continue
} else {
Util.warnWithFileLocation(
declaration,
log,
"void parameter must be the first and only parameter"
)
}
}
}
}

arg.argumentIndex = i
arg.argumentIndex = i
}
// Note that this .addValueDeclaration call already adds arg to the function's
// parameters.
// This is why the following line has been commented out by @KW
frontend.scopeManager.addDeclaration(arg)
// declaration.getParameters().add(arg);
i++
}
// Note that this .addValueDeclaration call already adds arg to the function's
// parameters.
// This is why the following line has been commented out by @KW
frontend.scopeManager.addDeclaration(arg)
// declaration.getParameters().add(arg);
i++
}

// Check for varargs. Note the difference to Java: here, we don't have a named array
// containing the varargs, but they are rather treated as kind of an invisible arg list that
// is appended to the original ones. For coherent graph behaviour, we introduce an implicit
// declaration that wraps this list
if (ctx.takesVarArgs()) {
val varargs = newParameterDeclaration("va_args", unknownType(), true)
varargs.isImplicit = true
varargs.argumentIndex = i
frontend.scopeManager.addDeclaration(varargs)
// Check for varargs. Note the difference to Java: here, we don't have a named array
// containing the varargs, but they are rather treated as kind of an invisible arg list
// that
// is appended to the original ones. For coherent graph behaviour, we introduce an
// implicit
// declaration that wraps this list
if (ctx.takesVarArgs()) {
val varargs = newParameterDeclaration("va_args", unknownType(), true)
varargs.isImplicit = true
varargs.argumentIndex = i
frontend.scopeManager.addDeclaration(varargs)
}
}
frontend.scopeManager.leaveScope(declaration)

// if we know our record declaration, but are outside the actual record, we
// need to leave the record scope again afterwards
Expand Down Expand Up @@ -461,29 +464,28 @@ class DeclaratorHandler(lang: CXXLanguageFrontend) :

frontend.scopeManager.addDeclaration(recordDeclaration)

frontend.scopeManager.enterScope(recordDeclaration)

processMembers(ctx)
recordDeclaration.withChildren(true) {
processMembers(ctx)

if (recordDeclaration.constructors.isEmpty()) {
// create an implicit constructor declaration with the same name as the record
val constructorDeclaration =
newConstructorDeclaration(
recordDeclaration.name.localName,
recordDeclaration,
)
.implicit(code = recordDeclaration.name.localName)
if (recordDeclaration.constructors.isEmpty()) {
// create an implicit constructor declaration with the same name as the record
val constructorDeclaration =
newConstructorDeclaration(
recordDeclaration.name.localName,
recordDeclaration,
)
.implicit(code = recordDeclaration.name.localName)

createMethodReceiver(constructorDeclaration)
createMethodReceiver(constructorDeclaration)

// and set the type, constructors always have implicitly the return type of their class
constructorDeclaration.type = FunctionType.computeType(constructorDeclaration)
recordDeclaration.addConstructor(constructorDeclaration)
frontend.scopeManager.addDeclaration(constructorDeclaration)
// and set the type, constructors always have implicitly the return type of their
// class
constructorDeclaration.type = FunctionType.computeType(constructorDeclaration)
recordDeclaration.addConstructor(constructorDeclaration)
frontend.scopeManager.addDeclaration(constructorDeclaration)
}
}

frontend.scopeManager.leaveScope(recordDeclaration)

return recordDeclaration
}

Expand Down
Loading
Loading