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] Implemented cached header file loading #4997

Merged
merged 2 commits into from
Oct 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 @@ -30,9 +30,13 @@ object CdtParser {
failure: Option[Throwable] = None
)

def readFileAsFileContent(path: Path): FileContent = {
def loadLinesAsFileContent(path: Path, lines: Array[Char]): InternalFileContent = {
FileContent.create(path.toString, true, lines).asInstanceOf[InternalFileContent]
}

def readFileAsFileContent(path: Path): InternalFileContent = {
val lines = IOUtils.readLinesInFile(path).mkString("\n").toArray
FileContent.create(path.toString, true, lines)
loadLinesAsFileContent(path, lines)
}

}
Expand Down Expand Up @@ -97,8 +101,8 @@ class CdtParser(config: Config) extends ParseProblemsLogger with PreprocessorSta
try {
val fileContent = readFileAsFileContent(realPath.path)
val fileContentProvider = new CustomFileContentProvider(headerFileFinder)
val lang = createParseLanguage(realPath.path, fileContent.asInstanceOf[InternalFileContent].toString)
val scannerInfo = createScannerInfo(realPath.path)
val lang = createParseLanguage(realPath.path, fileContent.toString)
val scannerInfo = createScannerInfo(realPath.path)
val translationUnit = lang.getASTTranslationUnit(fileContent, scannerInfo, fileContentProvider, null, opts, log)
val problems = CPPVisitor.getProblems(translationUnit)
if (parserConfig.logProblems) logProblems(problems.toList)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package io.joern.c2cpg.parser

import io.joern.c2cpg.parser.CustomFileContentProvider.missingHeaderFiles
import io.shiftleft.utils.IOUtils
import org.eclipse.cdt.core.index.IIndexFileLocation
import org.eclipse.cdt.internal.core.parser.IMacroDictionary
import org.eclipse.cdt.internal.core.parser.scanner.{InternalFileContent, InternalFileContentProvider}
import org.slf4j.LoggerFactory

import java.nio.file.Paths
import java.util.concurrent.ConcurrentHashMap

object CustomFileContentProvider {
private val headerFileToLines: ConcurrentHashMap[String, Array[Char]] = new ConcurrentHashMap()
private val missingHeaderFiles: ConcurrentHashMap[String, Boolean] = new ConcurrentHashMap()
}

class CustomFileContentProvider(headerFileFinder: HeaderFileFinder) extends InternalFileContentProvider {

import io.joern.c2cpg.parser.CustomFileContentProvider.headerFileToLines

private val logger = LoggerFactory.getLogger(classOf[CustomFileContentProvider])

private def loadContent(path: String): InternalFileContent = {
Expand All @@ -19,11 +29,24 @@ class CustomFileContentProvider(headerFileFinder: HeaderFileFinder) extends Inte
}
maybeFullPath
.map { foundPath =>
logger.debug(s"Loading header file '$foundPath'")
CdtParser.readFileAsFileContent(Paths.get(foundPath)).asInstanceOf[InternalFileContent]
val p = Paths.get(foundPath)
val content = headerFileToLines.computeIfAbsent(
foundPath,
_ => {
logger.debug(s"Loading header file '$foundPath'")
IOUtils.readLinesInFile(p).mkString("\n").toArray
}
)
CdtParser.loadLinesAsFileContent(p, content)
}
.getOrElse {
logger.debug(s"Cannot find header file for '$path'")
missingHeaderFiles.computeIfAbsent(
path,
_ => {
logger.debug(s"Cannot find header file for '$path'")
true
}
)
null
}

Expand Down
Loading