Skip to content
Draft
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 @@ -553,8 +553,19 @@ open class SymbolResolver(ctx: TranslationContext) : EOGStarterPass(ctx) {
)
for (missingParam in missingNewParams) {
if (missingParam != null) {
// duplicate node to prevent changes through the reference
val node: AstNode =
when (missingParam) {
is Literal<*> -> {
missingParam.duplicate(true)
}
is TypeExpression -> {
missingParam.duplicate(true)
}
else -> missingParam
}
constructExpression.addTemplateParameter(
missingParam,
node,
Template.TemplateInitialization.DEFAULT,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ package de.fraunhofer.aisec.cpg.passes
import de.fraunhofer.aisec.cpg.frontends.Language
import de.fraunhofer.aisec.cpg.graph.AstNode
import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.builder.div
import de.fraunhofer.aisec.cpg.graph.declarations.*
import de.fraunhofer.aisec.cpg.graph.duplicate
import de.fraunhofer.aisec.cpg.graph.expressions.Construction
import de.fraunhofer.aisec.cpg.graph.expressions.Literal
import de.fraunhofer.aisec.cpg.graph.expressions.Reference
import de.fraunhofer.aisec.cpg.graph.expressions.TypeExpression
import de.fraunhofer.aisec.cpg.graph.objectType
Expand Down Expand Up @@ -142,8 +145,19 @@ fun SymbolResolver.applyMissingParams(
// If default is a previously defined template argument that has been explicitly
// passed
templateParametersExplicitInitialization[missingParam]?.let {
// duplicate node to prevent changes through the reference
val node: AstNode =
when (it) {
is Literal<*> -> {
it.duplicate(true)
}
is TypeExpression -> {
it.duplicate(true)
}
else -> it
}
constructExpression.addTemplateParameter(
it,
node,
Template.TemplateInitialization.DEFAULT,
)
}
Expand All @@ -161,8 +175,19 @@ fun SymbolResolver.applyMissingParams(
} else if (missingParam in templateParameterRealDefaultInitialization) {
// Add default of template parameter to construct declaration
templateParameterRealDefaultInitialization[missingParam]?.let {
// duplicate node to prevent changes through the reference
val node: AstNode =
when (it) {
is Literal<*> -> {
it.duplicate(true)
}
is TypeExpression -> {
it.duplicate(true)
}
else -> it
}
constructExpression.addTemplateParameter(
it,
node,
Template.TemplateInitialization.DEFAULT,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import de.fraunhofer.aisec.cpg.graph.declarations.Variable
import de.fraunhofer.aisec.cpg.graph.expressions.BinaryOperator
import de.fraunhofer.aisec.cpg.graph.expressions.Call
import de.fraunhofer.aisec.cpg.graph.expressions.Construction
import de.fraunhofer.aisec.cpg.graph.expressions.Literal
import de.fraunhofer.aisec.cpg.graph.expressions.Reference
import de.fraunhofer.aisec.cpg.graph.expressions.TypeExpression
import de.fraunhofer.aisec.cpg.graph.expressions.UnaryOperator
import de.fraunhofer.aisec.cpg.graph.scopes.GlobalScope
import de.fraunhofer.aisec.cpg.graph.types.recordDeclaration
Expand Down Expand Up @@ -244,7 +246,14 @@ class CXXExtraPass(ctx: TranslationContext) : TranslationUnitPass(ctx) {
// understand
for (i in declaration.parameters.indices) {
if (candidate.parameters[i].default != null) {
declaration.parameters[i].default = candidate.parameters[i].default
// duplicate node to prevent changes through the reference
val default =
when (val candidateDefault = candidate.parameters[i].default) {
is Literal<*> -> candidateDefault.duplicate(true)
is TypeExpression -> candidateDefault.duplicate(true)
else -> candidateDefault
}
declaration.parameters[i].default = default
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2026, 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.cxx

import de.fraunhofer.aisec.cpg.helpers.SubgraphWalker
import de.fraunhofer.aisec.cpg.processing.strategy.Strategy
import de.fraunhofer.aisec.cpg.test.analyze
import java.nio.file.Path
import java.util.stream.Stream
import kotlin.io.path.Path
import kotlin.io.path.walk
import kotlin.streams.asStream
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource

class AstSanityTest {

/** Tests whether a node's AST parent is the same as the parent from AST traversal. */
@ParameterizedTest
@MethodSource("getTestFilePaths")
fun testAstParentSanity(path: Path) {
val file = path.toFile()
val result =
analyze(listOf(file), file.parentFile.toPath(), true) {
it.registerLanguage<CLanguage>()
it.registerLanguage<CPPLanguage>()
}
assertNotNull(result)

val walker = SubgraphWalker.IterativeGraphWalker(Strategy::AST_FORWARD)
walker.registerOnNodeVisit { node, parent ->
if (parent != null) {
assertTrue("$parent <> ${node.astParent}") { parent === node.astParent }
}
}
for (c in result.components) {
walker.iterate(c)
}
}

companion object {
@JvmStatic
fun getTestFilePaths(): Stream<Path> {
return Path("src/test/resources/")
.walk()
.filter { it.toString().endsWith("c") || it.toString().endsWith(".cpp") }
.asStream()
}
}
}
Loading