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

[c2cpg] Restore line/column numbers during IASTProblemStatement handling #4039

Merged
merged 1 commit into from
Jan 11, 2024
Merged
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 @@ -3,6 +3,7 @@ package io.joern.c2cpg.astcreation
import io.joern.c2cpg.parser.CdtParser
import io.shiftleft.codepropertygraph.generated.ControlStructureTypes
import io.joern.x2cpg.{Ast, ValidationMode}
import io.shiftleft.codepropertygraph.generated.nodes.AstNodeNew
import io.shiftleft.codepropertygraph.generated.nodes.ExpressionNew
import org.eclipse.cdt.core.dom.ast.*
import org.eclipse.cdt.core.dom.ast.cpp.*
Expand Down Expand Up @@ -159,15 +160,25 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

private def astsForProblemStatement(statement: IASTProblemStatement): Seq[Ast] = {
val lineNumber = line(statement)
val columnNumber = column(statement)
// We only handle un-parsable macros here for now
val isFromMacroExpansion = statement.getProblem.getNodeLocations.exists(_.isInstanceOf[IASTMacroExpansionLocation])
if (isFromMacroExpansion) {
val asts = if (isFromMacroExpansion) {
new CdtParser(config).parse(statement.getRawSignature, Paths.get(statement.getContainingFilename)) match
case Some(node) => node.getDeclarations.toIndexedSeq.flatMap(astsForDeclaration)
case None => Seq.empty
} else {
Seq.empty
}
// Restore the line/column numbers relative to the statements position
asts.flatMap(_.nodes).foreach {
case astNodeNew: AstNodeNew =>
astNodeNew.lineNumber = (lineNumber ++ astNodeNew.lineNumber).reduceOption { case (a, b) => a + (b - 1) }
astNodeNew.columnNumber = (columnNumber ++ astNodeNew.columnNumber).reduceOption(_ + _)
case _ => // do nothing
}
asts
}

private def astForConditionExpression(expr: IASTExpression, explicitArgumentIndex: Option[Int] = None): Ast = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,18 @@ class MacroHandlingTests extends CCodeToCpgSuite {
val List(localZ) = cpg.local.nameExact("z").l
localZ.code shouldBe "int z"
localZ.typeFullName shouldBe "int"
localZ.lineNumber shouldBe Some(12)
localZ.columnNumber shouldBe Some(7)
val List(zAssignmentCall) = cpg.call.codeExact("z = type_num(x)").l
zAssignmentCall.argument(1).code shouldBe "z"
val zIdentifier = zAssignmentCall.argument(1).asInstanceOf[Identifier]
zIdentifier.code shouldBe "z"
zIdentifier.lineNumber shouldBe Some(12)
zIdentifier.columnNumber shouldBe Some(7)
val typeNumCall = zAssignmentCall.argument(2).asInstanceOf[Call]
typeNumCall.code shouldBe "type_num(x)"
typeNumCall.name shouldBe "type_num"
typeNumCall.lineNumber shouldBe Some(12)
typeNumCall.columnNumber shouldBe Some(11)
}
}
}
Expand Down
Loading