From 70f3a7abc6f107e8fcb39ebf86cb92f403f6dde1 Mon Sep 17 00:00:00 2001 From: Prabhu Subramanian Date: Wed, 1 Nov 2023 16:03:15 +0000 Subject: [PATCH 1/3] Improve performance of structural analysis by sacrificing some precision Signed-off-by: Prabhu Subramanian --- build.sbt | 2 +- .../c2cpg/astcreation/AstCreatorHelper.scala | 44 +++++++++------ .../AstForExpressionsCreator.scala | 55 ++++++++++++------- .../io/appthreat/c2cpg/parser/CdtParser.scala | 32 +++++++---- .../c2cpg/passes/AstCreationPass.scala | 1 + pyproject.toml | 2 +- 6 files changed, 85 insertions(+), 51 deletions(-) diff --git a/build.sbt b/build.sbt index 1b5b653..8858c92 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ name := "chen" ThisBuild / organization := "io.appthreat" -ThisBuild / version := "0.5.4" +ThisBuild / version := "0.6.0" ThisBuild / scalaVersion := "3.3.1" val cpgVersion = "1.4.22" diff --git a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala index 686496b..f274b58 100644 --- a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala +++ b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala @@ -485,23 +485,33 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As } protected def astForNode(node: IASTNode): Ast = { - node match { - case expr: IASTExpression => astForExpression(expr) - case name: IASTName => astForIdentifier(name) - case decl: IASTDeclSpecifier => astForIdentifier(decl) - case l: IASTInitializerList => astForInitializerList(l) - case c: ICPPASTConstructorInitializer => astForCPPASTConstructorInitializer(c) - case d: ICASTDesignatedInitializer => astForCASTDesignatedInitializer(d) - case d: ICPPASTDesignatedInitializer => astForCPPASTDesignatedInitializer(d) - case d: CASTArrayRangeDesignator => astForCASTArrayRangeDesignator(d) - case d: CPPASTArrayRangeDesignator => astForCPPASTArrayRangeDesignator(d) - case d: ICASTArrayDesignator => nullSafeAst(d.getSubscriptExpression) - case d: ICPPASTArrayDesignator => nullSafeAst(d.getSubscriptExpression) - case d: ICPPASTFieldDesignator => astForNode(d.getName) - case d: ICASTFieldDesignator => astForNode(d.getName) - case decl: ICPPASTDecltypeSpecifier => astForDecltypeSpecifier(decl) - case arrMod: IASTArrayModifier => astForArrayModifier(arrMod) - case _ => notHandledYet(node) + if (config.includeFunctionBodies) { + node match { + case expr: IASTExpression => astForExpression(expr) + case name: IASTName => astForIdentifier(name) + case decl: IASTDeclSpecifier => astForIdentifier(decl) + case l: IASTInitializerList => astForInitializerList(l) + case c: ICPPASTConstructorInitializer => astForCPPASTConstructorInitializer(c) + case d: ICASTDesignatedInitializer => astForCASTDesignatedInitializer(d) + case d: ICPPASTDesignatedInitializer => astForCPPASTDesignatedInitializer(d) + case d: CASTArrayRangeDesignator => astForCASTArrayRangeDesignator(d) + case d: CPPASTArrayRangeDesignator => astForCPPASTArrayRangeDesignator(d) + case d: ICASTArrayDesignator => nullSafeAst(d.getSubscriptExpression) + case d: ICPPASTArrayDesignator => nullSafeAst(d.getSubscriptExpression) + case d: ICPPASTFieldDesignator => astForNode(d.getName) + case d: ICASTFieldDesignator => astForNode(d.getName) + case decl: ICPPASTDecltypeSpecifier => astForDecltypeSpecifier(decl) + case arrMod: IASTArrayModifier => astForArrayModifier(arrMod) + case _ => notHandledYet(node) + } + } else { + node match { + case expr: IASTExpression => astForExpression(expr) + case name: IASTName => astForIdentifier(name) + case decl: IASTDeclSpecifier => astForIdentifier(decl) + case decl: ICPPASTDecltypeSpecifier => astForDecltypeSpecifier(decl) + case _ => notHandledYet(node) + } } } diff --git a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstForExpressionsCreator.scala b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstForExpressionsCreator.scala index d9bb380..dc65f2b 100644 --- a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstForExpressionsCreator.scala +++ b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstForExpressionsCreator.scala @@ -271,28 +271,41 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) { astForExpression(packExpansionExpression.getPattern) protected def astForExpression(expression: IASTExpression): Ast = { - val r = expression match { - case lit: IASTLiteralExpression => astForLiteral(lit) - case un: IASTUnaryExpression => astForUnaryExpression(un) - case bin: IASTBinaryExpression => astForBinaryExpression(bin) - case exprList: IASTExpressionList => astForExpressionList(exprList) - case idExpr: IASTIdExpression => astForIdExpression(idExpr) - case call: IASTFunctionCallExpression => astForCallExpression(call) - case typeId: IASTTypeIdExpression => astForTypeIdExpression(typeId) - case fieldRef: IASTFieldReference => astForFieldReference(fieldRef) - case expr: IASTConditionalExpression => astForConditionalExpression(expr) - case arr: IASTArraySubscriptExpression => astForArrayIndexExpression(arr) - case castExpression: IASTCastExpression => astForCastExpression(castExpression) - case newExpression: ICPPASTNewExpression => astForNewExpression(newExpression) - case delExpression: ICPPASTDeleteExpression => astForDeleteExpression(delExpression) - case typeIdInit: IASTTypeIdInitializerExpression => astForTypeIdInitExpression(typeIdInit) - case c: ICPPASTSimpleTypeConstructorExpression => astForConstructorExpression(c) - case lambdaExpression: ICPPASTLambdaExpression => astForMethodRefForLambda(lambdaExpression) - case cExpr: IGNUASTCompoundStatementExpression => astForCompoundStatementExpression(cExpr) - case pExpr: ICPPASTPackExpansionExpression => astForPackExpansionExpression(pExpr) - case _ => notHandledYet(expression) + if (config.includeFunctionBodies) { + val r = expression match { + case lit: IASTLiteralExpression => astForLiteral(lit) + case un: IASTUnaryExpression => astForUnaryExpression(un) + case bin: IASTBinaryExpression => astForBinaryExpression(bin) + case exprList: IASTExpressionList => astForExpressionList(exprList) + case idExpr: IASTIdExpression => astForIdExpression(idExpr) + case call: IASTFunctionCallExpression => astForCallExpression(call) + case typeId: IASTTypeIdExpression => astForTypeIdExpression(typeId) + case fieldRef: IASTFieldReference => astForFieldReference(fieldRef) + case expr: IASTConditionalExpression => astForConditionalExpression(expr) + case arr: IASTArraySubscriptExpression => astForArrayIndexExpression(arr) + case castExpression: IASTCastExpression => astForCastExpression(castExpression) + case newExpression: ICPPASTNewExpression => astForNewExpression(newExpression) + case delExpression: ICPPASTDeleteExpression => astForDeleteExpression(delExpression) + case typeIdInit: IASTTypeIdInitializerExpression => astForTypeIdInitExpression(typeIdInit) + case c: ICPPASTSimpleTypeConstructorExpression => astForConstructorExpression(c) + case lambdaExpression: ICPPASTLambdaExpression => astForMethodRefForLambda(lambdaExpression) + case cExpr: IGNUASTCompoundStatementExpression => astForCompoundStatementExpression(cExpr) + case pExpr: ICPPASTPackExpansionExpression => astForPackExpansionExpression(pExpr) + case _ => notHandledYet(expression) + } + asChildOfMacroCall(expression, r) + } else { + val r = expression match { + case call: IASTFunctionCallExpression => astForCallExpression(call) + case typeId: IASTTypeIdExpression => astForTypeIdExpression(typeId) + case fieldRef: IASTFieldReference => astForFieldReference(fieldRef) + case newExpression: ICPPASTNewExpression => astForNewExpression(newExpression) + case typeIdInit: IASTTypeIdInitializerExpression => astForTypeIdInitExpression(typeIdInit) + case c: ICPPASTSimpleTypeConstructorExpression => astForConstructorExpression(c) + case _ => notHandledYet(expression) + } + asChildOfMacroCall(expression, r) } - asChildOfMacroCall(expression, r) } private def astForIdExpression(idExpression: IASTIdExpression): Ast = idExpression.getName match { diff --git a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/parser/CdtParser.scala b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/parser/CdtParser.scala index 8cc3ffa..5fe3f86 100644 --- a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/parser/CdtParser.scala +++ b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/parser/CdtParser.scala @@ -45,6 +45,22 @@ class CdtParser(config: Config) extends ParseProblemsLogger with PreprocessorSta private val includePaths = parserConfig.userIncludePaths private val log = new DefaultLogService + private var stayCpp: Boolean = false; + + private val cScannerInfo: ExtendedScannerInfo = new ExtendedScannerInfo( + definedSymbols, + (includePaths ++ parserConfig.systemIncludePathsC).map(_.toString).toArray, + parserConfig.macroFiles.map(_.toString).toArray, + parserConfig.includeFiles.map(_.toString).toArray + ) + + private val cppScannerInfo: ExtendedScannerInfo = new ExtendedScannerInfo( + definedSymbols, + (includePaths ++ parserConfig.systemIncludePathsCPP).map(_.toString).toArray, + parserConfig.macroFiles.map(_.toString).toArray, + parserConfig.includeFiles.map(_.toString).toArray + ) + // Setup indexing var index: Option[IIndex] = Option(EmptyCIndex.INSTANCE) if (config.useProjectIndex) { @@ -71,17 +87,11 @@ class CdtParser(config: Config) extends ParseProblemsLogger with PreprocessorSta } } - private def createScannerInfo(file: Path): ExtendedScannerInfo = { - val additionalIncludes = - if (FileDefaults.isCPPFile(file.toString)) parserConfig.systemIncludePathsCPP - else parserConfig.systemIncludePathsC - new ExtendedScannerInfo( - definedSymbols, - (includePaths ++ additionalIncludes).map(_.toString).toArray, - parserConfig.macroFiles.map(_.toString).toArray, - parserConfig.includeFiles.map(_.toString).toArray - ) - } + private def createScannerInfo(file: Path): ExtendedScannerInfo = + if (stayCpp || FileDefaults.isCPPFile(file.toString)) { + stayCpp = true + cppScannerInfo + } else cScannerInfo private def parseInternal(file: Path): ParseResult = { val realPath = File(file) diff --git a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/passes/AstCreationPass.scala b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/passes/AstCreationPass.scala index 295bdd4..0dd31da 100644 --- a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/passes/AstCreationPass.scala +++ b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/passes/AstCreationPass.scala @@ -33,6 +33,7 @@ class AstCreationPass(cpg: Cpg, config: Config, report: Report = new Report()) FileDefaults.SOURCE_FILE_EXTENSIONS ++ FileDefaults.HEADER_FILE_EXTENSIONS, config.withDefaultIgnoredFilesRegex(DefaultIgnoredFolders) ) + .sortWith(_.compareToIgnoreCase(_) > 0) .toArray override def runOnPart(diffGraph: DiffGraphBuilder, filename: String): Unit = { diff --git a/pyproject.toml b/pyproject.toml index 3e6f54f..38be402 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "appthreat-chen" -version = "0.5.4" +version = "0.6.0" description = "Code Hierarchy Exploration Net (chen)" authors = ["Team AppThreat "] license = "Apache-2.0" From 4c9510b5e072be46b8c2747ad6b07fe1042e41d9 Mon Sep 17 00:00:00 2001 From: Prabhu Subramanian Date: Wed, 1 Nov 2023 16:19:52 +0000 Subject: [PATCH 2/3] Test fix Signed-off-by: Prabhu Subramanian --- .../c2cpg/astcreation/AstCreatorHelper.scala | 58 ++++++++------- .../AstForExpressionsCreator.scala | 74 ++++++++++--------- .../ast/HeaderAstCreationPassTests.scala | 4 +- .../passes/ast/ProgramStructureTests.scala | 2 +- 4 files changed, 74 insertions(+), 64 deletions(-) diff --git a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala index f274b58..879dce1 100644 --- a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala +++ b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala @@ -485,33 +485,37 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As } protected def astForNode(node: IASTNode): Ast = { - if (config.includeFunctionBodies) { - node match { - case expr: IASTExpression => astForExpression(expr) - case name: IASTName => astForIdentifier(name) - case decl: IASTDeclSpecifier => astForIdentifier(decl) - case l: IASTInitializerList => astForInitializerList(l) - case c: ICPPASTConstructorInitializer => astForCPPASTConstructorInitializer(c) - case d: ICASTDesignatedInitializer => astForCASTDesignatedInitializer(d) - case d: ICPPASTDesignatedInitializer => astForCPPASTDesignatedInitializer(d) - case d: CASTArrayRangeDesignator => astForCASTArrayRangeDesignator(d) - case d: CPPASTArrayRangeDesignator => astForCPPASTArrayRangeDesignator(d) - case d: ICASTArrayDesignator => nullSafeAst(d.getSubscriptExpression) - case d: ICPPASTArrayDesignator => nullSafeAst(d.getSubscriptExpression) - case d: ICPPASTFieldDesignator => astForNode(d.getName) - case d: ICASTFieldDesignator => astForNode(d.getName) - case decl: ICPPASTDecltypeSpecifier => astForDecltypeSpecifier(decl) - case arrMod: IASTArrayModifier => astForArrayModifier(arrMod) - case _ => notHandledYet(node) - } - } else { - node match { - case expr: IASTExpression => astForExpression(expr) - case name: IASTName => astForIdentifier(name) - case decl: IASTDeclSpecifier => astForIdentifier(decl) - case decl: ICPPASTDecltypeSpecifier => astForDecltypeSpecifier(decl) - case _ => notHandledYet(node) - } + if (config.includeFunctionBodies) astForNodeFull(node) else astForNodePartial(node) + } + + protected def astForNodeFull(node: IASTNode): Ast = { + node match { + case expr: IASTExpression => astForExpression(expr) + case name: IASTName => astForIdentifier(name) + case decl: IASTDeclSpecifier => astForIdentifier(decl) + case l: IASTInitializerList => astForInitializerList(l) + case c: ICPPASTConstructorInitializer => astForCPPASTConstructorInitializer(c) + case d: ICASTDesignatedInitializer => astForCASTDesignatedInitializer(d) + case d: ICPPASTDesignatedInitializer => astForCPPASTDesignatedInitializer(d) + case d: CASTArrayRangeDesignator => astForCASTArrayRangeDesignator(d) + case d: CPPASTArrayRangeDesignator => astForCPPASTArrayRangeDesignator(d) + case d: ICASTArrayDesignator => nullSafeAst(d.getSubscriptExpression) + case d: ICPPASTArrayDesignator => nullSafeAst(d.getSubscriptExpression) + case d: ICPPASTFieldDesignator => astForNode(d.getName) + case d: ICASTFieldDesignator => astForNode(d.getName) + case decl: ICPPASTDecltypeSpecifier => astForDecltypeSpecifier(decl) + case arrMod: IASTArrayModifier => astForArrayModifier(arrMod) + case _ => notHandledYet(node) + } + } + + protected def astForNodePartial(node: IASTNode): Ast = { + node match { + case expr: IASTExpression => astForExpression(expr) + case name: IASTName => astForIdentifier(name) + case decl: IASTDeclSpecifier => astForIdentifier(decl) + case decl: ICPPASTDecltypeSpecifier => astForDecltypeSpecifier(decl) + case _ => notHandledYet(node) } } diff --git a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstForExpressionsCreator.scala b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstForExpressionsCreator.scala index dc65f2b..d38593f 100644 --- a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstForExpressionsCreator.scala +++ b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstForExpressionsCreator.scala @@ -271,41 +271,47 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) { astForExpression(packExpansionExpression.getPattern) protected def astForExpression(expression: IASTExpression): Ast = { - if (config.includeFunctionBodies) { - val r = expression match { - case lit: IASTLiteralExpression => astForLiteral(lit) - case un: IASTUnaryExpression => astForUnaryExpression(un) - case bin: IASTBinaryExpression => astForBinaryExpression(bin) - case exprList: IASTExpressionList => astForExpressionList(exprList) - case idExpr: IASTIdExpression => astForIdExpression(idExpr) - case call: IASTFunctionCallExpression => astForCallExpression(call) - case typeId: IASTTypeIdExpression => astForTypeIdExpression(typeId) - case fieldRef: IASTFieldReference => astForFieldReference(fieldRef) - case expr: IASTConditionalExpression => astForConditionalExpression(expr) - case arr: IASTArraySubscriptExpression => astForArrayIndexExpression(arr) - case castExpression: IASTCastExpression => astForCastExpression(castExpression) - case newExpression: ICPPASTNewExpression => astForNewExpression(newExpression) - case delExpression: ICPPASTDeleteExpression => astForDeleteExpression(delExpression) - case typeIdInit: IASTTypeIdInitializerExpression => astForTypeIdInitExpression(typeIdInit) - case c: ICPPASTSimpleTypeConstructorExpression => astForConstructorExpression(c) - case lambdaExpression: ICPPASTLambdaExpression => astForMethodRefForLambda(lambdaExpression) - case cExpr: IGNUASTCompoundStatementExpression => astForCompoundStatementExpression(cExpr) - case pExpr: ICPPASTPackExpansionExpression => astForPackExpansionExpression(pExpr) - case _ => notHandledYet(expression) - } - asChildOfMacroCall(expression, r) - } else { - val r = expression match { - case call: IASTFunctionCallExpression => astForCallExpression(call) - case typeId: IASTTypeIdExpression => astForTypeIdExpression(typeId) - case fieldRef: IASTFieldReference => astForFieldReference(fieldRef) - case newExpression: ICPPASTNewExpression => astForNewExpression(newExpression) - case typeIdInit: IASTTypeIdInitializerExpression => astForTypeIdInitExpression(typeIdInit) - case c: ICPPASTSimpleTypeConstructorExpression => astForConstructorExpression(c) - case _ => notHandledYet(expression) - } - asChildOfMacroCall(expression, r) + if (config.includeFunctionBodies) + astForExpressionFull(expression) + else astForExpressionPartial(expression) + } + + protected def astForExpressionFull(expression: IASTExpression): Ast = { + val r = expression match { + case lit: IASTLiteralExpression => astForLiteral(lit) + case un: IASTUnaryExpression => astForUnaryExpression(un) + case bin: IASTBinaryExpression => astForBinaryExpression(bin) + case exprList: IASTExpressionList => astForExpressionList(exprList) + case idExpr: IASTIdExpression => astForIdExpression(idExpr) + case call: IASTFunctionCallExpression => astForCallExpression(call) + case typeId: IASTTypeIdExpression => astForTypeIdExpression(typeId) + case fieldRef: IASTFieldReference => astForFieldReference(fieldRef) + case expr: IASTConditionalExpression => astForConditionalExpression(expr) + case arr: IASTArraySubscriptExpression => astForArrayIndexExpression(arr) + case castExpression: IASTCastExpression => astForCastExpression(castExpression) + case newExpression: ICPPASTNewExpression => astForNewExpression(newExpression) + case delExpression: ICPPASTDeleteExpression => astForDeleteExpression(delExpression) + case typeIdInit: IASTTypeIdInitializerExpression => astForTypeIdInitExpression(typeIdInit) + case c: ICPPASTSimpleTypeConstructorExpression => astForConstructorExpression(c) + case lambdaExpression: ICPPASTLambdaExpression => astForMethodRefForLambda(lambdaExpression) + case cExpr: IGNUASTCompoundStatementExpression => astForCompoundStatementExpression(cExpr) + case pExpr: ICPPASTPackExpansionExpression => astForPackExpansionExpression(pExpr) + case _ => notHandledYet(expression) + } + asChildOfMacroCall(expression, r) + } + + protected def astForExpressionPartial(expression: IASTExpression): Ast = { + val r = expression match { + case call: IASTFunctionCallExpression => astForCallExpression(call) + case typeId: IASTTypeIdExpression => astForTypeIdExpression(typeId) + case fieldRef: IASTFieldReference => astForFieldReference(fieldRef) + case newExpression: ICPPASTNewExpression => astForNewExpression(newExpression) + case typeIdInit: IASTTypeIdInitializerExpression => astForTypeIdInitExpression(typeIdInit) + case c: ICPPASTSimpleTypeConstructorExpression => astForConstructorExpression(c) + case _ => notHandledYet(expression) } + asChildOfMacroCall(expression, r) } private def astForIdExpression(idExpression: IASTIdExpression): Ast = idExpression.getName match { diff --git a/platform/frontends/c2cpg/src/test/scala/io/appthreat/c2cpg/passes/ast/HeaderAstCreationPassTests.scala b/platform/frontends/c2cpg/src/test/scala/io/appthreat/c2cpg/passes/ast/HeaderAstCreationPassTests.scala index c5a7d75..2a5cd92 100644 --- a/platform/frontends/c2cpg/src/test/scala/io/appthreat/c2cpg/passes/ast/HeaderAstCreationPassTests.scala +++ b/platform/frontends/c2cpg/src/test/scala/io/appthreat/c2cpg/passes/ast/HeaderAstCreationPassTests.scala @@ -48,9 +48,9 @@ class HeaderAstCreationPassTests extends CCodeToCpgSuite { // second time for the actual implementation in the source file // We do not de-duplicate this as line/column numbers differ m1.fullName shouldBe "main" - m1.filename shouldBe "main.c" + m1.filename shouldBe "main.h" m2.fullName shouldBe "main" - m2.filename shouldBe "main.h" + m2.filename shouldBe "main.c" printf.fullName shouldBe "printf" } } diff --git a/platform/frontends/c2cpg/src/test/scala/io/appthreat/c2cpg/passes/ast/ProgramStructureTests.scala b/platform/frontends/c2cpg/src/test/scala/io/appthreat/c2cpg/passes/ast/ProgramStructureTests.scala index 0e41c6b..76b0470 100644 --- a/platform/frontends/c2cpg/src/test/scala/io/appthreat/c2cpg/passes/ast/ProgramStructureTests.scala +++ b/platform/frontends/c2cpg/src/test/scala/io/appthreat/c2cpg/passes/ast/ProgramStructureTests.scala @@ -24,7 +24,7 @@ class ProgramStructureTests extends CCodeToCpgSuite { "create one NamespaceBlock per file" in { val cpg = code("", "foo.c").moreCode("", "woo.c") - val expectedFilenames = Seq("foo.c", "woo.c") + val expectedFilenames = Seq("woo.c", "foo.c") val expectedNamespaceFullNames = expectedFilenames.map(f => s"$f:${NamespaceTraversal.globalNamespaceName}") val allNamespaceBlockFullNames = cpg.namespaceBlock.fullNameNot(NamespaceTraversal.globalNamespaceName).fullName.l allNamespaceBlockFullNames.zip(expectedNamespaceFullNames).foreach { case (actual, expected) => From e7c4960c635649e6f8b3d86808aafe93d56c8126 Mon Sep 17 00:00:00 2001 From: Prabhu Subramanian Date: Wed, 1 Nov 2023 19:26:50 +0000 Subject: [PATCH 3/3] Microsoft specific default defines Signed-off-by: Prabhu Subramanian --- .../c2cpg/astcreation/AstCreatorHelper.scala | 6 +++- .../c2cpg/parser/DefaultDefines.scala | 32 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala index 879dce1..cef20ed 100644 --- a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala +++ b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/astcreation/AstCreatorHelper.scala @@ -144,7 +144,11 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As "enum", "struct", "interface", - "class" + "class", + "naked", + "export", + "module", + "import" ) protected def cleanType(rawType: String, stripKeywords: Boolean = true): String = { diff --git a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/parser/DefaultDefines.scala b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/parser/DefaultDefines.scala index c8c698e..593900f 100644 --- a/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/parser/DefaultDefines.scala +++ b/platform/frontends/c2cpg/src/main/scala/io/appthreat/c2cpg/parser/DefaultDefines.scala @@ -2,8 +2,34 @@ package io.appthreat.c2cpg.parser object DefaultDefines { val DEFAULT_CALL_CONVENTIONS: Map[String, String] = Map( - "__fastcall" -> "__attribute((fastcall))", - "__cdecl" -> "__attribute((cdecl))", - "__pascal" -> "__attribute((pascal))" + "__fastcall" -> "__attribute((fastcall))", + "__cdecl" -> "__attribute((cdecl))", + "__pascal" -> "__attribute((pascal))", + "__vectorcall" -> "__attribute((vectorcall))", + "__clrcall" -> "__attribute((clrcall))", + "__stdcall" -> "__attribute((stdcall))", + "__thiscall" -> "__attribute((thiscall))", + "__declspec" -> "__attribute((declspec))", + "__restrict" -> "__attribute((restrict))", + "__sptr" -> "__attribute((sptr))", + "__uptr" -> "__attribute((uptr))", + "__syscall" -> "__attribute((syscall))", + "__oldcall" -> "__attribute((oldcall))", + "__unaligned" -> "__attribute((unaligned))", + "__w64" -> "__attribute((w64))", + "__asm" -> "__attribute((asm))", + "__based" -> "__attribute((based))", + "__interface" -> "__attribute((interface))", + "__event" -> "__attribute((event))", + "__hook" -> "__attribute((hook))", + "__unhook" -> "__attribute((unhook))", + "__raise" -> "__attribute((raise))", + "__try" -> "__attribute((try))", + "__except" -> "__attribute((except))", + "__finally" -> "__attribute((finally))", + "__m128" -> "__attribute((m128))", + "__m128d" -> "__attribute((m128d))", + "__m128i" -> "__attribute((m128i))", + "__m64" -> "__attribute((m64))" ) }