-
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 File
- Loading branch information
1 parent
e0e493d
commit 511d4d2
Showing
10 changed files
with
309 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
79 changes: 79 additions & 0 deletions
79
.../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,79 @@ | ||
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) | ||
|
||
/** {{{ | ||
* 1) -D: Matches the -D flag, which is the key prefix for defining macros. | ||
* 2) ([A-Za-z_][A-Za-z0-9_]+): Matches a valid macro name (which must start with a letter or underscore and can be followed by letters, numbers, or underscores). | ||
* 3) (=(\\*".*"))?: Optionally matches = followed by either: | ||
* a) A quoted string: Allows for strings in quotes. | ||
* b) Any char sequence (.*") closed with a quote. | ||
* }}} | ||
*/ | ||
private val defineInCommandPattern = """-D([A-Za-z_][A-Za-z0-9_]+)(=(\\*".*"))?""".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 s = define.stripPrefix("-D") | ||
if (s.contains("=")) { | ||
val splitted = s.split("=") | ||
(splitted.head, splitted(1)) | ||
} else { | ||
(s, "1") | ||
} | ||
} | ||
|
||
def defines(): List[(String, String)] = { | ||
val definesFromArguments = arguments.filter(a => a.startsWith("-D")).map(nameValuePairFromDefine) | ||
val definesFromCommand = command.flatMap { c => | ||
val defines = defineInCommandPattern.findAllIn(c).toList | ||
defines.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.