-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[c2cpg] Implemented support for JSON Compilation Database Format Spec…
…ification
- Loading branch information
1 parent
e0e493d
commit f21a010
Showing
10 changed files
with
296 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
.../frontends/c2cpg/src/main/scala/io/joern/c2cpg/parser/JSONCompilationDatabaseParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.joern.c2cpg.parser | ||
|
||
import io.joern.x2cpg.SourceFiles | ||
import io.shiftleft.utils.IOUtils | ||
import org.slf4j.LoggerFactory | ||
import ujson.Value | ||
|
||
import java.nio.file.Paths | ||
import scala.util.Try | ||
|
||
object JSONCompilationDatabaseParser { | ||
|
||
private val logger = LoggerFactory.getLogger(getClass) | ||
|
||
private val defineInCommandPattern = "-D(\\S+)=\"(.+)\"".r | ||
|
||
case class CommandObject(directory: String, arguments: List[String], command: List[String], file: String) { | ||
|
||
/** @return | ||
* the file path (guaranteed to be absolute) | ||
*/ | ||
def compiledFile(): String = SourceFiles.toAbsolutePath(file, directory) | ||
|
||
private def nameValuePairFromDefine(define: String): (String, String) = { | ||
val defineName = define.substring(2, define.indexOf("=")) | ||
val defineValue = define.substring(define.indexOf("=") + 1) | ||
(defineName, defineValue) | ||
} | ||
|
||
def defines(): List[(String, String)] = { | ||
val definesFromArguments = | ||
arguments.filter(a => a.startsWith("-D") && a.contains("=")).map(nameValuePairFromDefine) | ||
val definesFromCommand = command.flatMap { c => | ||
defineInCommandPattern.findAllIn(c).toList.map(nameValuePairFromDefine) | ||
} | ||
definesFromArguments ++ definesFromCommand | ||
} | ||
} | ||
|
||
private def hasKey(node: Value, key: String): Boolean = Try(node(key)).isSuccess | ||
|
||
private def safeArguments(obj: Value): List[String] = { | ||
if (hasKey(obj, "arguments")) obj("arguments").arrOpt.map(_.toList.map(_.str)).getOrElse(List.empty) | ||
else List.empty | ||
} | ||
|
||
private def safeCommand(obj: Value): List[String] = { | ||
if (hasKey(obj, "command")) List(obj("command").str) | ||
else List.empty | ||
} | ||
|
||
def parse(compileCommandsJson: String): List[CommandObject] = { | ||
try { | ||
val jsonContent = IOUtils.readEntireFile(Paths.get(compileCommandsJson)) | ||
val json = ujson.read(jsonContent) | ||
val allCommandObjects = json.arr.toList | ||
allCommandObjects.map { obj => | ||
CommandObject(obj("directory").str, safeArguments(obj), safeCommand(obj), obj("file").str) | ||
} | ||
} catch { | ||
case t: Throwable => | ||
logger.warn(s"Could not parse '$compileCommandsJson'", t) | ||
List.empty | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.