Skip to content

Commit

Permalink
[gosrc2cpg] Fixed an issue with skipping the statement before the if …
Browse files Browse the repository at this point in the history
…condition (#5046)

* [gosrc2cpg]handle the init code before if condition.

* [gosrc2cpg]handle the init code before if condition, fix the test error.

* [gosrc2cpg]after formatting.
  • Loading branch information
ch0a3 authored Nov 4, 2024
1 parent b00c030 commit eed2101
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
case DeclStmt => astForNode(statement.json(ParserKeys.Decl))
case ExprStmt => astsForExpression(createParserNodeInfo(statement.json(ParserKeys.X)))
case ForStmt => Seq(astForForStatement(statement))
case IfStmt => Seq(astForIfStatement(statement))
case IfStmt => astForIfStatement(statement)
case IncDecStmt => Seq(astForIncDecStatement(statement))
case RangeStmt => Seq(astForRangeStatement(statement))
case SwitchStmt => Seq(astForSwitchStatement(statement))
Expand Down Expand Up @@ -133,7 +133,13 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
ast
}

private def astForIfStatement(ifStmt: ParserNodeInfo): Ast = {
private def astForIfStatement(ifStmt: ParserNodeInfo): Seq[Ast] = {
// handle init code before condition in if;
val initParserNode = nullSafeCreateParserNodeInfo(ifStmt.json.obj.get(ParserKeys.Init))
val initAstBlock = blockNode(ifStmt, Defines.empty, Defines.voidTypeName)
scope.pushNewScope(initAstBlock)
val initAst = blockAst(initAstBlock, astsForStatement(initParserNode, 1).toList)
scope.popScope()

val conditionParserNode = createParserNodeInfo(ifStmt.json(ParserKeys.Cond))
val conditionAst = astForConditionExpression(conditionParserNode)
Expand All @@ -159,7 +165,7 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
Ast(elseNode).withChild(blockAst(elseBlock, a.toList))
case _ => Ast()
}
controlStructureAst(ifNode, Some(conditionAst), Seq(thenAst, elseAst))
Seq(initAst, controlStructureAst(ifNode, Some(conditionAst), Seq(thenAst, elseAst)))
}

private def astForSwitchStatement(switchStmt: ParserNodeInfo): Ast = {
Expand Down

0 comments on commit eed2101

Please sign in to comment.