Skip to content

Commit

Permalink
Merge pull request #63 from Privado-Inc/c2cpgTypeRecovery
Browse files Browse the repository at this point in the history
add support to skip header files
  • Loading branch information
khemrajrathore authored Jul 25, 2024
2 parents 29715a5 + 26e3c7e commit 11d962a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ final case class Config(
includePathsAutoDiscovery: Boolean = false,
skipFunctionBodies: Boolean = false,
noImageLocations: Boolean = false,
withPreprocessedFiles: Boolean = false
withPreprocessedFiles: Boolean = false,
skipHeaderFileContext: Boolean = false
) extends X2CpgConfig[Config] {
def withIncludePaths(includePaths: Set[String]): Config = {
this.copy(includePaths = includePaths).withInheritedFields(this)
Expand Down Expand Up @@ -58,6 +59,10 @@ final case class Config(
def withPreprocessedFiles(value: Boolean): Config = {
this.copy(withPreprocessedFiles = value).withInheritedFields(this)
}

def withSkipHeaderFileContext(value: Boolean): Config = {
this.copy(skipHeaderFileContext = value).withInheritedFields(this)
}
}

private object Frontend {
Expand Down Expand Up @@ -101,6 +106,9 @@ private object Frontend {
opt[Unit]("with-preprocessed-files")
.text("includes *.i files and gives them priority over their unprocessed origin source files.")
.action((_, c) => c.withPreprocessedFiles(true)),
opt[Unit]("skip-header-file-context")
.text("skips copying header files for each .c or .cpp file being processed")
.action((_, c) => c.withSkipHeaderFileContext(true)),
opt[String]("define")
.unbounded()
.text("define a name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ class CdtParser(config: Config) extends ParseProblemsLogger with PreprocessorSta
}

private def parseInternal(code: String, inFile: File): IASTTranslationUnit = {
val fileContent = FileContent.create(inFile.toString, true, code.toCharArray)
val fileContentProvider = new CustomFileContentProvider(headerFileFinder)
val lang = createParseLanguage(inFile.path, code)
val scannerInfo = createScannerInfo(inFile.path)
val translationUnit = lang.getASTTranslationUnit(fileContent, scannerInfo, fileContentProvider, null, opts, log)
val problems = CPPVisitor.getProblems(translationUnit)
val fileContent = FileContent.create(inFile.toString, true, code.toCharArray)
val fileContentProvider =
if config.skipHeaderFileContext then null else new CustomFileContentProvider(headerFileFinder)
val lang = createParseLanguage(inFile.path, code)
val scannerInfo = createScannerInfo(inFile.path)
val translationUnit = lang.getASTTranslationUnit(fileContent, scannerInfo, fileContentProvider, null, opts, log)
val problems = CPPVisitor.getProblems(translationUnit)
if (parserConfig.logProblems) logProblems(problems.toList)
if (parserConfig.logPreprocessor) logPreprocessorStatements(translationUnit)
translationUnit
Expand All @@ -95,8 +96,9 @@ class CdtParser(config: Config) extends ParseProblemsLogger with PreprocessorSta
val realPath = File(file)
if (realPath.isRegularFile) { // handling potentially broken symlinks
try {
val fileContent = readFileAsFileContent(realPath.path)
val fileContentProvider = new CustomFileContentProvider(headerFileFinder)
val fileContent = readFileAsFileContent(realPath.path)
val fileContentProvider =
if config.skipHeaderFileContext then null else new CustomFileContentProvider(headerFileFinder)
val lang = createParseLanguage(realPath.path, fileContent.asInstanceOf[InternalFileContent].toString)
val scannerInfo = createScannerInfo(realPath.path)
val translationUnit = lang.getASTTranslationUnit(fileContent, scannerInfo, fileContentProvider, null, opts, log)
Expand Down

0 comments on commit 11d962a

Please sign in to comment.